﻿//openwindowlink
//Opens a new window 
//<param>page - the window links that's being opened</param>
function openwindowlink(page){
	newwin = window.open(page,"Window1","width=625,height=775,scrollbars=0,resizable=1,toolbar=0,location=0,statusbar=0,menubar=0");
}	


//openwindowlink
//Opens a new window 
//<param>page - the window links that's being opened</param>
function openwindow(page){
    newwin = window.open(page,"AlliedElectronics","scrollbars=1,resizable=1,toolbar=0,location=0,statusbar=0,menubar=0");
}	

//openwindowlink
//Opens a new window 
//<param>page - the window links that's being opened</param>
function openscrollablewindowlink(page){
	newwin = window.open(page,"Window1","width=625,height=775,scrollable=1,scrollbars=1,resizable=1,toolbar=0,location=0,statusbar=0,menubar=0");
}	

// pre/re populate fieldObject
function PrePopulate(fieldObject) {
	fieldObject.value = "";
}

// get height
function getheight(element){
	document.element.height;
}

//OpenNewWindow
//Opens a new window 
//<param>url - the window links that's being opened</param>
//<param>width - the widtho of the window</param>
//<param>height - the height of the window</param>
function OpenNewWindow(url, width, height)
{
	var randomnumber=Math.floor(Math.random()*5001);
	window.open(url, randomnumber,"top=10,left=10,menubar=0,resizable=1,scrollable=0,scrollbars=0,width=" + width + ",height=" + height);
}

 //Google Analytics
function getAnalyticsAccountNumber( environment){
    switch( environment){
        case "devtest1.alliedelec.com":	//dev test 1
	        return "UA-1833888-4";
	        break;
        case "redesign.alliedelec.com":	//redesign
	        return "UA-1833888-5";
	        break;
        case "dev.alliedelec.com": //development
	        return "UA-1833888-2";
	        break;
        case "estaging.alliedelec.com": //estaging
	        return "UA-1833888-3";
	        break;
        case "www.alliedelec.com":	//prod
	        return "UA-1833888-1";
	        break;
        default:	//prod
	        return "UA-1833888-1";	
	        break;
    }
}
//Make a gallery window
function makeGalleryWindow(url) 
{
	var newWindow;
	newWindow = window.open(url,"VMFGallery","height=450,width=420,left=0,top=0,resizable=no");
	newWindow.focus();
}

//Tabs 
//It is necessary to create the arrayOfTabs and arrayOfDivs on the actual page.
//Product detail will dynamically generate the list so the tabs are in the
//correct place and there aren't missing tabs.  May be used in multiple
//places if necessary.  To see a list of tabs that are generated & see the format
//view the source of the product details.
//<param>tabToShow - Which tab is to be displayed</param>
function changeTabs(tabToShow){

	var prefix = "ctl00_contentMain_";
	
    for(var i = 0; i < arrayOfTabs.length; i++)
    {
        var stringToReplace = document.getElementById(prefix + arrayOfTabs[i]).innerHTML;
        var replacement = stringToReplace.substring(stringToReplace.indexOf(">")+1, stringToReplace.lastIndexOf("<"));
        var link = "<A class=tabs href=\"javascript:changeTabs('" + arrayOfTabs[i].replace('Tab','').replace(" ", "") + "')\">" + stringToReplace + "</A>";
      
        if( arrayOfDivs[i] == tabToShow){
        
            try{//added try catch for Ariba situations - this will not allow the user to navigate directly to a tabbed page, but in ariba they cannot do that anyways
                //appends the tab name to the hash
                parent.location.hash = "tab=" + arrayOfTabs[i].replace('Tab','').replace(" ", "");
            }catch(error){
            }
            
            //set up the tabs
            document.getElementById(prefix + arrayOfTabs[i]).className = 'activeTab';
            
            if(replacement != ""){
                document.getElementById(prefix + arrayOfTabs[i]).innerHTML = document.getElementById(prefix + arrayOfTabs[i]).innerHTML.replace(stringToReplace , replacement);
            }
            
            if(tabToShow == "SimilarProducts")
            {
                document.getElementById(prefix + arrayOfDivs[i]).className = 'shownSimilarDiv';
            }
            else
            {
                document.getElementById(prefix + arrayOfDivs[i]).className = 'shownDiv';
            }
        }else if (document.getElementById(prefix + arrayOfTabs[i]) != null && document.getElementById(prefix + arrayOfTabs[i]).className != 'noTab'){
           
            if(document.getElementById(prefix + arrayOfTabs[i]).innerHTML.indexOf("<A") == -1){
                document.getElementById(prefix + arrayOfTabs[i]).innerHTML = link;
            }
           
            document.getElementById(prefix + arrayOfTabs[i]).className = 'inactiveTab';
            document.getElementById(prefix + arrayOfDivs[i]).className = 'hiddenDiv';
        }
    }
    
}

//ShowLarger
//Displas an image hover
function showLarger(local, obj, out){

    if(window.event == null){//FF
        var sku = local;
        sku = sku.replace("/Images/Products/Small/","");
        sku = sku.replace(".jpg","");

        var e = document.getElementById('img_' + sku);

        clickX= (e.x + 50) + "px";
        clickY = (e.y + 50) + "px";
        
        dropTop = (e.x -138) + "px";
        dropLeft = (e.y +244) + "px";
        
    }else{//IE
        clickX = window.event.clientX;
        clickY = window.event.clientY;
        
        dropTop = clickY+3;
        dropLeft = clickX+3;
    }
    if(out == null && local != null){
        document.getElementById('showLarger').style.display = 'block';
        document.getElementById('showLarger').style.position = 'absolute';
        document.getElementById('showLarger').style.top = clickY;
        document.getElementById('showLarger').style.left = clickX;
        document.getElementById('showLarger').innerHTML = "<img src=\"" + local + "\" />";
        
        document.getElementById('dropShadow').style.display = 'block';
        document.getElementById('dropShadow').style.position = 'absolute';
        document.getElementById('dropShadow').style.top = dropTop;
        document.getElementById('dropShadow').style.left = dropLeft;
        

    }else{
        document.getElementById('showLarger').style.display = 'none';
        document.getElementById('dropShadow').style.display = 'none';
    }
}

///Changes
function changeCursor(over){
    if(over != null){
        document.body.style.cursor = 'default';
    }else{
        document.body.style.cursor = 'all-scroll';
    }
}

///Changes
function changeCursorHand(over){
    if(over != null){
        document.body.style.cursor = 'hand';
    }else{
        document.body.style.cursor = 'default';
    }
}


//handles the "KeyPress" for textareas that do not allow the "enter"
//key to be pressed.
function keyPress(obj){
    if(obj.addEventListener){
        obj.parentNode.addEventListener("keypress",
            function(e){
                if(e && e.keyCode && e.keyCode == 13){
                    e.stopPropagation();
                }
            }, false);
    }
}


//isnumeric
function IsNumeric(textValue){
    var ValidChars = "0123456789."
    var IsNumber = true;
    var Char;
    
    for(var i = 0; i < textValue.length && IsNumber; i++){
        Char = textValue.charAt(i);
        if(ValidChars.indexOf(Char) == -1){
            IsNumber= false;
            break;
        }
    }
    
    return IsNumber;
}



function DisplayValues(DivId, valueName, valueDescription, idName, idDescription){

    var prefix = 'ctl00_contentMain_';
    document.getElementById(prefix+ DivId).style.display = '';
    document.getElementById(prefix+ idName).value = valueName;
    document.getElementById(prefix+ idDescription).value = valueDescription;
}

function HideObject(id){
    document.getElementById(id).style.display = 'none';
}



var arrayOfUserIds = null;
function dragAndDropSetup(){

    if(arrayOfUserIds != null){
      
        //arrayOfUserIds
        var position = GetPosition('ctl00_contentMain_groupListing_pnlGroup');
        var left = position.Left + 20;
        
        for(var i = 0; i < arrayOfUserIds.length; i++){
            var userLeft = parseInt(document.getElementById(arrayOfUserIds[i]).style.left.replace("px",""));
            
            if(userLeft >= left){
                document.getElementById(arrayOfUserIds[i]).style.left = left + "px";
            }
        }
    }
}



String.prototype.trim = function(){
	return this.replace(/^\s+|\s+$/g,"");
}

String.prototype.contains = function(what){
	if(this.indexOf(what) == -1){
		return false;
	}else{
		return true;
	}
}

//set up the ID Prefixes for the role & group
var groupPrefix = "ctl00_contentMain_groupListing_lbGroup";
var rolePrefix = "ctl00_contentMain_availableUsers_lbRole";
var textbox;

/**
 ** Move
 ** Handles to movement from one drop down list to another.
 ** Pass in direction (either "right" or "left")
 ** Pass in moveAll (either true or false - if we are to move all the items or just the selected ones)
 ** Pass in role (the role of the users being moved
 ** <remarks> 2008.08.12 - Initial</remarks>
 **/
function Move(direction, moveAll, role)
{
    //get the textbox of users
    textbox = document.getElementById("ctl00_contentMain_groupListing_txtAdd_2");
    
    //check if we are moving all the users
    if(moveAll)
    {
        if(direction == "right")//check the direction (right is move from role to group)
        {
            //select all the users
            SelectAll(role, rolePrefix);
        }else //(left is move from group to role)
        { 
            //select all the users
            SelectAll(role, groupPrefix);
        }
    }
    
    //check the direction
    if(direction == "right")
    {
        MoveRight(role);
    }else{
        MoveLeft(role);
    }
}

/**
 ** SelectAll
 ** Selects all the options in the list.
 ** Pass in role (the role of the users being moved
 ** Pass in the prefix (either the groups or the roles)
 ** <remarks> 2008.08.12 - Initial </remarks>
 **/
function SelectAll(role, prefix)
{
    //get the select box
    var selectlist = document.getElementById(prefix + role);
    
    //iterate over the options
    for(var i = 0; i < selectlist.options.length; i++)
    {
        //select each option
        selectlist.options[i].selected = true;
    }
}

/**
 ** MoveLeft
 ** Moves the selected options left
 ** Pass in role (the role of the users being moved
 ** <remarks> 2008.08.12 - Initial </remarks>
 **/
function MoveLeft(role)
{
    //Get the role list box & the group list box
    var roleLB = document.getElementById(rolePrefix + role);
    var groupLB = document.getElementById(groupPrefix + role);
    
    //get the amount of options in the role list box
    var index = roleLB.options.length;
 
    //iterate over the options in the group list box
    for (var i= groupLB.options.length-1; i>=0; i--)
    {
        //if they are selected
        if(groupLB.options[i].selected)
        {
            //create a new option with the element values
            var newOption = document.createElement('option');
            newOption.text = groupLB.options[i].text;
            newOption.value = groupLB.options[i].value;
            
            //get the reg id
            var id = groupLB.options[i].value.split(":")[0];
           
            while(textbox.value.indexOf(id) != -1)
            {
                //add to the textbox the reg ids
                textbox.value = textbox.value.replace(id + ";","");
            }
            
            //add the new option to the role list box
            roleLB.options[index] = newOption;
            
            //remove the old option from the group
            groupLB.remove(i);
            
            //increment the index
            index++;
        }
    }
    
}

/**
 ** MoveRight
 ** Moves the selected options right
 ** Pass in role (the role of the users being moved
 ** <remarks> 2008.08.12 - Initial </remarks>
 **/
function MoveRight(role)
{
    //Get the role list box & the group list box
    var roleLB = document.getElementById(rolePrefix + role);
    var groupLB = document.getElementById(groupPrefix + role);
    
    //get the amount of options in the group list box
    var index = groupLB.options.length;
 
    //iterate over the options in the role list box
    for (var i= roleLB.options.length-1; i>=0; i--)
    {
        //if they are selected
        if(roleLB.options[i].selected)
        {
            //create a new option with the element values
            var newOption = document.createElement('option');
            newOption.text = roleLB.options[i].text;
            newOption.value = roleLB.options[i].value;
            
            //get the reg id
            var id = roleLB.options[i].value.split(":")[0];
            
            //add to the textbox the reg ids
            textbox.value += id + ";";
    
            //add the new option to the group list box
            groupLB.options[index] = newOption;
            
            //remove the old option from the role
            roleLB.remove(i);
            
            //increment the index
            index++;
        }
    }
}



function RemoveLeftSelected()
{
    unSelect(document.getElementById("ctl00_contentMain_availableUsers_lbRoleRequisitioner"));
    unSelect(document.getElementById("ctl00_contentMain_availableUsers_lbRoleApprover"));
    unSelect(document.getElementById("ctl00_contentMain_availableUsers_lbRolePurchaser"));
    unSelect(document.getElementById("ctl00_contentMain_availableUsers_lbRoleApproverPurchase"));
    unSelect(document.getElementById("ctl00_contentMain_availableUsers_lbRoleAdministrator"));
    
    unSelect(document.getElementById("ctl00_contentMain_groupListing_lbGroupRequisitioner"));
    unSelect(document.getElementById("ctl00_contentMain_groupListing_lbGroupApprover"));
    unSelect(document.getElementById("ctl00_contentMain_groupListing_lbGroupPurchaser"));
    unSelect(document.getElementById("ctl00_contentMain_groupListing_lbGroupApproverPurchase"));
    unSelect(document.getElementById("ctl00_contentMain_groupListing_lbGroupAdministrator"));
}


function unSelect(object)
{
    for(var i = 0; i < object.options.length; i++)
    {
        object.options[i].selected = false;
    }
}


/*** COOKIE TESTING ****/
    
//alert(navigator.cookieEnabled); -- works for only IE(4+) & FF (2+)

function CreateACookie(name, value, expireDate)
{
    var exDate = new Date();
    exDate.setDate(exDate.getDate() + expireDate);
    
    document.cookie = name + '=' + value + ';expires=' + exDate.toUTCString();
}

function GetCookie(name)
{

    var cookies = document.cookie.toString();
    var cookieSplit = cookies.split(';');
    
    for(var i = 0; i < cookieSplit.length; i++)
    {
        if(cookieSplit[i].indexOf(name + '=') != -1)
        {
            return cookieSplit[i];
        }
    }
    
    return '';
}

function DeleteCookie(name)
{
    CreateACookie(name, '', -1);
}



/** Payment method display for Checkout **/

/**
 ** ShowPaymentPanel
 ** Shows the add payment method panel & checks the radio button.
 ** 2008.09.24 - Initial 
 **/
function ShowPaymentPanel()
{
    var newPaymentMethod_Radio = document.getElementById('ctl00_contentMain_radPaymentMethodEdit');
    var securityDiv = document.getElementById('divSecurityCode');
    
    document.getElementById('divCreditCardEdit').style.display= '';
    newPaymentMethod_Radio.checked = true;
    securityDiv.style.display = 'block';
}


/**
 ** HidePaymentPanel
 ** Hides the add payment method panel & un-checks the radio button.
 ** 2008.09.24 - Initial 
 ** 2008.10.17 - F282: Check saved payment method radio when hiding add
 **/
function HidePaymentPanel()
{
    var newPaymentMethod_Radio = document.getElementById('ctl00_contentMain_radPaymentMethodEdit');
    var savedPaymentMethod_Radio = document.getElementById('ctl00_contentMain_radSavedPaymentMethods');
    var securityDiv = document.getElementById('divSecurityCode');
    
    document.getElementById('divCreditCardEdit').style.display= 'none';
    savedPaymentMethod_Radio.checked = true;
    
    ShowSecurity(document.getElementById('ctl00_contentMain_uctlCreditCardList_dropSavedPaymentMethods'));
}

/** Freight Account display for Checkout **/

/**
 **ShowFreightAccountPanel()
 **Shows the add new freight account
 **2008.10.16 - Initial Version
 **/
 function ShowFreightAccountPanel()
 {
    var newFreightAccount_Radio = document.getElementById('ctl00_contentMain_rbAddFreightAccount');
    var oldFreightAccount_Radio = document.getElementById('ctl00_contentMain_rbYourSavedFreightAccounts');
    document.getElementById('divFreightAccountAdd').style.display='';
    oldFreightAccount_Radio.checked = false;
    newFreightAccount_Radio.checked = true;
 }
 
/**
 **HideFreightAccountPanel()
 **Hides the freight account panel and unchecks the radio button
 **2008.10.17 - Initial Version
 **/
function HideFreightAccountPanel()
{
    var newFreightAccount_Radio = document.getElementById('ctl00_contentMain_rbAddFreightAccount');
    var oldFreightAccount_Radio = document.getElementById('ctl00_contentMain_rbYourSavedFreightAccounts');

    document.getElementById('divFreightAccountAdd').style.display='none';
    newFreightAccount_Radio.checked = false;
    oldFreightAccount_Radio.checked = true;
}

/** Shipping Address display for Checkout **/

/**
 **ShowNewShippingAddressPanel()
 **Shows the add new shipping address panel
 **2009.01.16 - Initial Version
 **/
 function ShowNewShippingAddressPanel()
 {
    var newShippingAddress_Radio = document.getElementById('ctl00_contentMain_radAddAddress');
    var oldShippingAddress_Radio = document.getElementById('ctl00_contentMain_radSavedAddresses');
    var txtShippingAddress1 = document.getElementById('ctl00_contentMain_uctlShippingEdit_txtEditShipAddress1');
    var tdAliasCheckbox = document.getElementByID('ctl00_contentMain_uctlShippingEdit_tdCheckSaveAddress');
    
    txtShippingAddress1.focus();
    tdAliasCheckbox.style.display = 'block';
    oldShippingAddress_Radio.text = 'Changed to not be checked';
    newShippingAddress_Radio.text = '  New Shipping Address';
    oldShippingAddress_Radio.checked = false;
    newShippingAddress_Radio.checked = true;
 }
 
/**
 **HideNewShippingAddressPanel()
 **Hides the new shipping address panel and unchecks the radio button
 **2008.10.17 - Initial Version
 **/
function HideNewShippingAddressPanel()
{
    var newShippingAddress_Radio = document.getElementById('ctl00_contentMain_radAddAddress');
    var oldShippingAddress_Radio = document.getElementById('ctl00_contentMain_radSavedAddresses');
    var lnkNewShipAddress = document.getElementById('ctl00_contentMain_lnkNewAddress');
    var divNewAddresses = document.getElementById('divNewAddresses');
    
    //document.getElementById('divNewAddresses').style.display='none';
    divNewAddresses.style.display = 'none';
    lnkNewShipAddress.style.display = 'block';
        oldShippingAddress_Radio.text = 'Changed to be checked';
    newShippingAddress_Radio.checked = false;
    oldShippingAddress_Radio.checked = true;
}

/**
 ** ShowSecurity
 ** Shows the security textbox for the payment methods that aren't accounts.
 ** 2008.09.26 - Initial
 **/
function ShowSecurity(e)
{
    var securityDiv = document.getElementById('divSecurityCode');
    
    for(var i = 0; i < e.options.length; i++)
    {
        if(e.options[i].selected)
        {
            if(e.options[i].text.indexOf("Allied Account") == -1)
            {
                securityDiv.style.display = 'block';
            }else{
                securityDiv.style.display = 'none';
            }
        }
    }
}