
/**********************
=GLOBAL VARS
**********************/
var siteRoot = "";

/* *************************************************************************

=LOAD SCRIPT

 Includes all common js files for the application

 Use for all files on page load

************************************************************************* */

function loadScript(url)

{

   document.write('<script src="', url, '" type="text/JavaScript"><\/script>');

}

/*Add js file below*/

//loadScript(siteRoot + "/js/prototype.js");
//loadScript(siteRoot + "/js/jquery-1.2.1.js");
//loadScript(siteRoot + "/js/init.js");


/***********************************************************

=FIND POSITION : function finPos
Finds the top/left offset of a specific object
attaches a hidden object to the first object

***********************************************************/		



// FUNCTION UPDATED 9.13.07

// Function modified to allow for extraWidth argument
var popupBox;

function findPos(obj, hiddenDiv, extraWidth, extraHeight) {

    var objId = obj.id;
    popupBox = hiddenDiv;

    if(extraHeight){
	    var objHeight = parseInt(extraHeight);
    }
    else{var objHeight = 0;}

    if(extraWidth){
	    var objWidth = parseInt(extraWidth);
    }
    else
    {
	    var objWidth = 0;
    }

    var curleft = curtop = curright = 0;

    if (obj.offsetParent) {
	    curleft = obj.offsetLeft
	    curtop = obj.offsetTop

	    while (obj = obj.offsetParent) {
		    curleft += obj.offsetLeft
		    curtop += obj.offsetTop
	    }
    }

    var popupDiv;
    
    var widthHeight = new Array()
    widthHeight = getContentPos();
    
    
    
    var hidObjLeft;
    var hidObjTop;
    
    hidObjTop = curtop - widthHeight[1];
    hidObjLeft = curleft - widthHeight[0];
    
    if (hiddenDiv)
    {
        popupDiv = document.getElementById(hiddenDiv);
        
         if(popupDiv){
            popupDiv.style.display = "block";
            /*popupDiv.style.top = hidObjTop + objHeight + "px";
            popupDiv.style.left = hidObjLeft + objWidth + "px";*/
            Event.observe(document, "mousemove", positionCalPopup, false);
         }
    }
    
    //alert(hidObjLeft + "," + hidObjTop);
    return [curleft,curtop];
}

function positionCalPopup(e)
{
    $(popupBox).style.left = (Event.pointerX(e)+ 15) + "px";
    $(popupBox).style.top = (Event.pointerY(e)+ -28)+ "px";
}

function getContentPos()
{
    var page = $$('.page-content');
    
    var obj = page[0];
    
    var objleft = objtop = objright = 0;

    if (obj.offsetParent) {
	    objleft = obj.offsetLeft
	    objtop = obj.offsetTop

	    while (obj = obj.offsetParent) {
		    objleft += obj.offsetLeft
		    objtop += obj.offsetTop
	    }
    }
    
    //alert(objleft + "," + objtop);

    return [objleft,objtop];
}

/***********HIDE FUNCTION**********/
function hideThis(objID)
{
    //$(objID).hide();
    document.getElementById(objID).style.display = "none";
}

/******Submit Zipcode Search*****/

function searchZip()
{
    var searchInput = document.getElementsByTagName("input");
    for (var i = 0; i < searchInput.length; i++)
    {
        if (searchInput[i].name == "zip_code")
        {
            var searchBox = searchInput[i];
            var searchText = searchInput[i].value;
            break;
        }
    }

    if ( searchText != "")
    {
        window.location = siteRoot + "/Axis-Representative-Search-Results.aspx?zip=" + searchText;
    }
    else
    {
        alert("Please enter a zipcode");
    }
}

/******Submit Product Search*****/

function searchProduct()
{
    var searchInput = document.getElementsByTagName("input");
    for (var e = 0; e < searchInput.length; e++)
    {
        if (searchInput[e].name == "SKUNumber")
        {
            var searchBox = searchInput[e];
            var searchText = searchInput[e].value;
            break;
        }
    }

    if ( searchText != "")
    {
        window.location = siteRoot + "SearchResults.aspx?SKUNumber=" + searchText;
    }
    else
    {
        alert("Please enter a product");
    }
}


function mapRollover(mapID)
{
    var imgSrc;
    var mapImg = document.getElementById("usa_interactive_map");
    var canadaImg = document.getElementById("canada_interactive_map");
    
    switch(mapID)
    {
        case "ne_usa":
            imgSrc = siteRoot + "/images/map/usa_ne_over.gif";
            break;
        case "se_usa":
            imgSrc = siteRoot + "/images/map/usa_se_over.gif";
            break;
        case "west_usa":
            imgSrc = siteRoot + "/images/map/usa_west_over.gif";
            break;
        case "central_usa":
            imgSrc = siteRoot  + "/images/map/usa_central_over.gif";
            break;
        case "canada_all":
            imgSrc = siteRoot  + "/images/map/canada_all.gif";
            break;
        default:
            imgSrc = mapImg.src;
            break;
    }
   
   if (mapID != 'canada_all')
   {
    mapImg.src = imgSrc;
   }
   else
   {
    canadaImg.src = imgSrc;
   }
}

function mapRolloff()
{
    var usaMapImg = $("usa_interactive_map");
    var canMapImg = $("canada_interactive_map");
    
    if ($('usa_interactive_map'))
    {
        usaMapImg.src = siteRoot + "/images/map/usa.gif";
    }
    
    if ($('canada_interactive_map'))
    {
        canMapImg.src = siteRoot + "/images/map/canada.gif";
    }
}

var popupMap;

function mapPopup(popupID)
{
    var imgSrc;
    var popup = document.getElementById(popupID);
    //popupMap = popupID;
    
    hideMapPopups();
    popup.style.visibility = "visible";
    //popup.style.left = (Event.pointerX(document)) + "px";
    //Event.observe(document, "mousemove", positionMapPopup, false);
}

function positionMapPopup(e)
{
    $(popupMap).style.left = (Event.pointerX(e)+ 15) + "px";
    $(popupMap).style.top = (Event.pointerY(e)+ -28)+ "px";
}

function hideMapPopups()
{
    parentDiv = document.getElementById("account-managers");
    
    var usa_parts = [ 'usa_ne', 'usa_se', 'usa_central', 'usa_west' ];
    var canada_parts = ['canada_all'];
    
    var popupObj;
    var i;
    
    for (i=0;i<usa_parts.length;i++)
    {
        popupObj = document.getElementById(usa_parts[i]);
        popupObj.style.visibility = "hidden";
    }
    
    for (i=0;i<canada_parts.length;i++)
    {
        popupObj = document.getElementById(canada_parts[i]);
        popupObj.style.visibility = "hidden";
    }
}

function getPopup(id)
{
    var hidWindow;
    
    if (document.getElementById('inline-popup'))
    {
        hidWindow = document.getElementById('inline-popup');
        hidWindow.style.display = "block";
    }
    
    var arrPage = $$('.page-content');
    var page = arrPage[0];
    
    var arrForm = $$('.requestForm');
    
    if( arrForm[0] != null)
    {
        var arrInput = page.getElementsByTagName("select");
        var i;
        for (i=0;i<arrInput.length;i++)
        {
            arrInput[i].style.visibility = "hidden";
        }
    }
}

function hidePopup(id)
{
    var hidWindow;
    
    if (document.getElementById('inline-popup'))
    {
        hidWindow = document.getElementById('inline-popup');
        hidWindow.style.display = "none";
    }  
    
    var arrInput = document.getElementsByTagName("select"); 
    
    for (i=0;i<arrInput.length;i++)
    {
        arrInput[i].style.visibility = "visible";
    }
}

function changeRequest(i)
{
    arrDD = $$('.institutionDD')
    
    arrDD[0].selectedIndex = i;
    
}

function addCancel()
{
    //alert('not found');
    
    var arrForm = $$('.requestForm');
    
    
        if( arrForm[0] != null)
        {
            var popupDiv = $('inline-popup');
            var arrInput = popupDiv.getElementsByTagName('input');
            
            //alert(arrInput);
            var submitBtn = arrInput[arrInput.length-1];
            
            //submitBtn.insert({after: "<p class='right cancelBtn'><a class='left' href='#' onclick='hidePopup()'>cancel</a><span class='left orange'>&nbsp;|&nbsp;</span></p>" })
            //new Insertion.after(submitBtn, :"<p class='right cancelBtn'><a class='left' href='#' onclick='hidePopup()'>cancel</a><span class='left orange'>&nbsp;|&nbsp;</span></p>");
            
            var pTag;
            var nbs = document.createTextNode('\u00A0');
            pTag = document.createElement("p");
            pTag.innerHTML = "<a class='left' href='#' onclick='hidePopup()'>cancel</a><span class='left orange'>&nbsp;|&nbsp;</span>";
            pTag.className = "right cancelBtn";
            submitBtn.parentNode.appendChild(pTag);
            
            var arrError = $$('.ErrorLabel');
            
            if( arrError[0] != "" && arrError[0] != null)
            {
                getPopup();
            }
            
        }
        
       
}

function setupCalPopup()
{
    if ($('popupHolder'))
    {
        var popupTxt = $('popupHolder').innerHTML;
        //alert(popupTxt);
        
        var arrPage = $('wrapper');
        
        arrPage.insert({before: popupTxt});
        
        $('popupHolder').innerHTML = "";
    }
}

function submitEnter(myField,e)
{
    var keycode;

    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;

    var strText = myField;
    
    if (keycode == 13)
    {
       if (strText != "")
       {
        window.location = siteRoot + "SearchResults.aspx?SKUNumber=" + strText;
        return false;
       }
       else
       {
        return false;
       }
    }
   else
   {
        return true;
   }

}


function submitEnter(myField,e)
{
    var keycode;

    if (window.event) keycode = window.event.keyCode;
    else if (s) keycode = s.which;

    var strText = myField;
    
    if (keycode == 13)
    {
       if (strText != "")
       {
        window.location = siteRoot + "/Search-Results.aspx?zip=" + strText;
        return false;
       }
       else
       {
        return false;
       }
    }
   else
   {
        return true;
   }

}


function distributorLogin()
{
    var loginPopup = $('login-popup');
    
    loginPopup.style.display = "block";
}

function checkRequest()
{
    var url = window.location.href;
    
    if (url.indexOf('requestMoreInfo') != -1)
    {
        getPopup();
    }
}

addCancel();
setupCalPopup();
checkRequest();
