﻿
/////////////////////////////////////////////////////////////////////
//////// Utility Functions used by JS in this section: START  ///////
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////
// trim white space on both end of string
//For EBTM Product Page
var imageArray = new Array();
var inventoryArray = new Array();
var StockMessage = new String();

function GetEmailInStockURL()
{
    var objSize = document.getElementById('Size');
    var objColor = document.getElementById('Color');
    var objStockInAnchor = document.getElementById('EmailStockInAnchor');
    
    var sSize = "";
    var sColor = "";
    var sURL = "";
    
    if (null != objSize)
        sSize = objSize[objSize.selectedIndex].text;
        
    if (null != objColor)
        sColor = objColor[objColor.selectedIndex].text;
    if (null != objStockInAnchor)
    {
        objStockInAnchor.href = objStockInAnchor.href + "&Size=" + escape(sSize) + "&Color=" + escape(sColor);
    }    
        
    return true;
}

function SetStockMessage(sMessage)
{
    StockMessage = sMessage;
}


function setCookie(c_name,value,expiredays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie=c_name+ "=" +escape(value)+ ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function setInventory(bNoOffset)
{
    
    var nColorIndex = -1;
    var nSizeIndex = -1;
    var objDiv = null;
    
    
    objDiv = document.getElementById('ProductInventory');
    if (null == objDiv)
        return;
    
    //This takes care of auto select size option 
    if (bNoOffset)
    {
        nColorOffset = 0;
        nSizeOffset = 1;
    }
    else
    {
        nColorOffset = 1;
        nSizeOffset = 0;
    }
    
    if (document.getElementById('Color'))
    {
        nColorIndex = document.getElementById('Color').selectedIndex;
        var nSelectedColor = document.getElementById('Color').options[nColorIndex].innerHTML;
        objLastClicked = document.getElementById('lastColor');
        if (objLastClicked)
            objLastClicked.Value = nSelectedColor;
    }    
    if (document.getElementById('Size'))
    {
        nSizeIndex = document.getElementById('Size').selectedIndex;
        var nSelectedSize = document.getElementById('Size').options[nSizeIndex].innerHTML;
        objLastClicked = document.getElementById('lastSize');
        if (objLastClicked)
            objLastClicked.Value = nSelectedSize;
    }
    else if (nColorIndex == -1)
        return;
    
    if ((0 == nSizeIndex || 0 == nColorIndex) && bNoOffset)
    {
        objDiv.innerHTML = "";
        return;
    }
    
    var sText = new String();
    
    if (nColorIndex == -1)
        sText = inventoryArray[0][nSizeIndex - nSizeOffset];
    else if (nSizeIndex == -1)
        sText.innerHTML = inventoryArray[nColorIndex + nColorOffset][0];
    else
        sText = inventoryArray[nColorIndex + nColorOffset][nSizeIndex - nSizeOffset];    
        
    objDiv.innerHTML = sText.replace(/%27/g,"'");
    
    //Now make the email in stock visible if the item is not to be displayed
    
    var objStockDiv = document.getElementById('ProductEmailInStock');
    
    if (objStockDiv != null)
    {
        if (StockMessage == objDiv.innerHTML)
            document.getElementById('ProductEmailInStock').style.display = 'none';    
        else
            document.getElementById('ProductEmailInStock').style.display = 'block';    
    }       
    
    return;    
        
        
}

function SetMediumImageSrc(objID, bNoOffset)
{
    var nID = 0;
    var nOffset = 0;
    
    //This takes care of auto select size option 
    if (bNoOffset)
        nOffset = 0;
    else
        nOffset = 1;
    
    if (objID)
        nID = objID.name;
        
    var nSelectedIndex = 0;
    
    if (document.getElementById('Color'))
        nSelectedIndex = document.getElementById('Color').selectedIndex;
    else
        nSelectedIndex = -1;
        
    var objLastClicked = document.getElementById('lastClicked');
    if (objLastClicked)
    {
        objLastClicked.value = nID;  
    }
    
    if (nSelectedIndex > -1)
    {
        document.getElementById('ProductMainImage').src = imageArray[1][nID][nSelectedIndex + nOffset];
    }
    else
    {
        document.getElementById('ProductMainImage').src = imageArray[1][nID][0];
    }
    
    return;     
}

//For Setting(s)
//use 0 - to make Dest = Source
//Use 1 - to make same size as largest
//Use 2 - to make same size as smallest

function matchColumns(idSource, idDest, nSettings)
{

    var divSource = document.getElementById(idSource);
    var divDest = document.getElementById(idDest);
    
    var nSourceHeight = 0;
    var nDestHeight = 0;
        
    if (!divSource || !divDest)
        return;
    
    if(divSource.offsetHeight)
    {
        nSourceHeight = divSource.offsetHeight;
        nDestHeight = divDest.offsetHeight;
    }
    else if(divSource.style.pixelHeight)
    {
        nSourceHeight = divSource.style.pixelHeight;
        nDestHeight = divDest.style.pixelHeight;
    }
    
    switch(nSettings)
    {
        case 0: 
            divDest.style.height = nSourceHeight + "px";
            break;
        case 1:
            if (nSourceHeight > nDestHeight)
            {
                divDest.style.height = nSourceHeight + "px";
                divSource.style.height = nSourceHeight + "px";
            }
            else
                divSource.style.height = nDestHeight + "px";
                divDest.style.height = nDestHeight + "px";
            break;
        case 2:   
            if (nSourceHeight > nDestHeight)
            {
                divSource.style.height = nDestHeight + "px";
                divDest.style.height = nDestHeight + "px";
            }
            else
            {
                divDest.style.height = nSourceHeight + "px";
                divSource.style.height = nSourceHeight + "px";
            }
            break;
    }
    
    return;
}


function createImageGallery(bNoOffset)
{
    var ITEMS_PER_ROW = 3;
    var NO_PICTURE = "nopicture";
    var objNextImage;
    var sGallery = "";
    var nSelectedIndex = 0;
    var objUL = null;
    var objLI = null;
    var objAnchor = null;
    var objImage = null; 
    
   if (document.getElementById('Color'))
        nSelectedIndex = document.getElementById('Color').selectedIndex;
   else
        nSelectedIndex = -1;
        
    var iconGallery = document.getElementById('ProductIconGalleryDiv');
    
    //This takes care of auto select size option 
    if (bNoOffset && -1 != nSelectedIndex)
        nOffset = 0;
    else
        nOffset = 1;
    
    iconGallery.innerHTML = "";
    
    var length = imageArray[0].length;
    
    //We need to centre the last row of the gallery so we will calculate 
    //how many are onj the last row and then space accordingly
    var nNumberOfItemsOnLastRow = length % ITEMS_PER_ROW;
    var sID = "";
   
    objUL = document.createElement("ul");
    objUL.setAttribute("id", "ProductIconGallery");
    objUL.setAttribute("title", "List of possible option for the current size and color");
   
    sGallery = "<ul id=\"ProductIconGallery\" title=\"List of possible option for the current size and color\">";    
    
    var nCount = 0;
    var sMediumImg = "";
    var sIconImg = ""; 
    
    //Count the images in the as we dont need a gallery when there is only 1 image
    for (i = 0; i < length; i++)
    {
        sMediumImg = new String(imageArray[1][i][nSelectedIndex + nOffset]);
        if (sMediumImg.indexOf(NO_PICTURE) == -1 )
            nCount++;
    }
    //Don't draw a gallery if there is only 1 - pointless
    if (nCount < 2)
        return;
        
    for (i = 0; i < length; i++)
    {   
       
        objLI = document.createElement("li");
        objUL.appendChild(objLI);
        
        objAnchor = document.createElement("a");
            
        objAnchor.setAttribute("id", "GalleryAnchor" + i );
        objAnchor.setAttribute("title", "Click to show the medium size image" );
        objAnchor.setAttribute("href", "#" );
        objAnchor.setAttribute("name", i);
        if (document.attachEvent){
            if (bNoOffset)
                objAnchor['onclick'] = function(){SetMediumImageSrc(this,true)};
            else
                objAnchor['onclick'] = function(){SetMediumImageSrc(this,false)};
        }
        else if (document.addEventListener) 
        {
            if (bNoOffset)
                objAnchor.addEventListener('click', function(){ SetMediumImageSrc(this,true);}, false);
            else
                objAnchor.addEventListener('click', function(){ SetMediumImageSrc(this,false);}, false);
        }
 
        sMediumImg = new String(imageArray[1][i][nSelectedIndex + nOffset]);
        sIconImg = new String(imageArray[0][i][nSelectedIndex + nOffset]);
        if ((typeof(sIconImg) == "undefined" || sIconImg.indexOf(NO_PICTURE) != -1) && sMediumImg.indexOf(NO_PICTURE) == -1 )
        {
            objAnchor.appendChild(document.createTextNode("[" + (i + 1) + "]"));       
        }
        else if (typeof(imageArray[0][i][nSelectedIndex + nOffset]) != "undefined" && sMediumImg.indexOf(NO_PICTURE) == -1 )
        {
            objImage = document.createElement("img");
            objImage.setAttribute("id", "GalleryImage");
            objImage.setAttribute("src", imageArray[0][i][nSelectedIndex + nOffset]);
            objAnchor.appendChild(objImage);
        } 
        
        objLI.appendChild(objAnchor);                  
        
    }
    
    iconGallery.appendChild(objUL);
    return;
    
}


function ShowLargeImage(nOffset)
{ 
    var LARGEIMAGEINDEX = 2;
    var LARGEIMAGEINDEX = 2;
    var objLastClicked = document.getElementById('lastClicked');
    var objColor = document.getElementById('Color');
    var nSelectedIndex = -1;
    
    if (objLastClicked)
    {
        if (objColor)
            nSelectedIndex = document.getElementById('Color').selectedIndex;
        
        popupimg(imageArray[LARGEIMAGEINDEX][objLastClicked.value][nSelectedIndex + nOffset]);
        
        
    }
    
    return;    
}



function popupimg(url)
{
    var NOPICTURE = "nopicture";
    
    
    if (typeof(url) == "undefined" || url.indexOf(NOPICTURE) != -1)     
    {
        alert("There is currently no matching large image for this image");
        return;       
    }
    
    window.open('popup.aspx?src=' + url,'','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=449,height=449,left=0,top=0');
    return;
}

function popupTopic(sTopic)
{
   window.open('popup.aspx?Topic=' + sTopic,'','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=800,height=600,left=0,top=0');
            
    
}

function trim(varString)
{
    var strBuffer = "";
    var strTrimmed = "";
    
    if(varString==null || varString=="undefined")
        return "";
    
    if(varString.length==0)
        return "";

     for(var i=0;i<varString.length;i++)
    {
        strBuffer = varString.substring(i,i+1);
        
        if(i==0 && strBuffer == " ")
            continue;
        else if(i==varString.length-1 && strBuffer == " ")
            continue;
        else
            strTrimmed += strBuffer;
    }        
    
    return strTrimmed;   
}

function removeSpace(varString)
{
    var strBuffer = "";
    var strTrimmed = "";
    
    if(varString==null || varString=="undefined")
        return "";
    
    if(varString.length==0)
        return "";

     for(var i=0;i<varString.length;i++)
    {
        strBuffer = varString.substring(i,i+1);
        if(strBuffer != " ")
            strTrimmed += strBuffer;
    }        
    
    return strTrimmed;   
}
/////////////////////////////////////////////////////////////////////
//////// Utility Functions used by JS in this section: END    ///////
/////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////
// QAS Integration: Quick Address
// ----------------------------------------------
function popUpQuickAddress(arrInputIDs,path){
	var url = path+"?inputs="+arrInputIDs;

	var popUp = window.open(url,"QuickAddress","width=700,height=360,scrollbars=no;resizable");
	popUp.focus(); 
	return false;
}


/////////////////////////////////////////////////
// functions used by Search By Brand/Band/Keyword
// ----------------------------------------------

function mag_searchby(optItem, frm)
{
    var nID;
    var strID="";
    var strBuffer="";
    var strError="";
    var theOptionSelected;
    var strOptionText="";
    var strSEPrefix="";
    var strAction="";

    if(optItem==null ||optItem=="undefined")            
    {
        strError = "Unknown error for Band/Brand search, please contact support.";
        alert(strError);
        return false;
    }
    
    if(optItem.selectedIndex==0)
    {
        strError = "Please select from the dropdown list.";
        alert(strError);
        optItem.selectedIndex=0;
        optItem.focus();
        return false;
    }
       
    theOptionSelected = optItem.options[optItem.selectedIndex];
    
    if(optItem.selectedIndex==0)
    {
        strError = "Please select from the dropdown list.";
        alert(strError);
        optItem.selectedIndex=0;
        optItem.focus();
        return false;
    }    
		    
//    nID = theOptionSelected.value;
//    
//    // trim the value in case empty value encountered
//    strID = removeSpace(nID);
//    
//    if(strID.length == 0)
//    {   
//        strError = "Unknown error for '" + theOptionSelected.text + "'.";
//        strError += "\n\nPlease choose another one or contact support.";
//        alert(strError);
//        optItem.focus();
//        return false;
//    }
//    
//    // trim the value in case empty value encountered
//    strOptionText = removeSpace(theOptionSelected.text);

//    if(strOptionText.length == 0)
//    {   
//        strError = "Unknown error for selection [IndexID=" + optItem.selectedIndex + "].";
//        strError += "\n\nPlease choose another one or contact support.";
//        alert(strError);
//        optItem.focus();
//        return false;
//    }
//    else
//    {
//        /* 
//        Two replace() called can be done in one go, but white space would be   
//        hardly visible in pattern, so do it explicitly in second replace call
//        */
//        //do global & case insensitive replace for invalid chars - excluding white space
//        strOptionText = strOptionText.replace(/[^0-9a-z_]/gi, '-');
//        //do global & case insensitive replace - for white space only
//        strOptionText = strOptionText.replace(/ /gi, '-'); 
//    }
//    
//    if(frm==document.forms[0])
//        strSEPrefix = "g";
//    else if(frm==document.forms[1])
//        strSEPrefix = "m";
//    else
//    {
//        strError = "Unknown form error for '" + frm.name + "'.";
//        strError += "\n\nPlease contact support.";
//        optItem.focus();
//        return false;
//    }
//    
//    strAction = strSEPrefix + "-" + strID + "-" + strOptionText + ".aspx"

		strAction = theOptionSelected.value;
    window.top.location = 
    frm.action = strAction;
    frm.submit();    
    
}

function mag_focussearchkeyword(obj)
{
    if(obj.value.toLowerCase()=="by keyword")
        obj.value = "";
    
}

function mag_searchkeyword(obj)
{
    var strBuffer="";
    var strError="";
    if(obj==null ||obj=="undefined")
    {
        strError = "Unknown error for Keyword search, please contact support.";
        alert(strError);
        return;    
    }
    strBuffer = obj.value;
    strBuffer = strBuffer.toLowerCase();
    if(strBuffer=="by keyword")
        obj.value = "";
    
}

function mag_submitOnEnter(e,form,buttonToClick)
{
    var keynum = 0;
    if(e.keyCode) // IE
    {
    keynum = e.keyCode
    }
    else if(e.which) // Netscape/Firefox/Opera
    {
    keynum = e.which
    }
    if(keynum == 13){
    document.forms[form][buttonToClick].click();
    }
}



/////////////////////////////////////////////////
// functions used by Nisbets' Submit Orders to RB site
// ----------------------------------------------

function mag_popUp(url, name, width, height){
	var popUp = window.open(url,name,"width="+width+",height="+height+",scrollbars=yes,resizable=yes,status=yes");
	popUp.focus(); 
	return false;
}
function mag_showSendOrder(orderNumber){
	var url = "SendOrder.aspx?OrderNumber="+orderNumber;
	return mag_popUp(url,"SendOrder",800,600);
}
function mag_showOrderResponse(orderNumber){
	var url = "SendOrderResponse.aspx?OrderNumber="+orderNumber;
	return mag_popUp(url,"SentOrder",800,600);
}

