﻿/*** HOMEPAGE SEARCH BOX ***/
function SearchBox(TargetTextBoxID)
{
    var self = this;
    
    // wire up keypress event
    this.KeyPress = function(e)
    {
	    var code;
	    if (!e) var e = window.event;
	    if (e.keyCode) code = e.keyCode;
	    else if (e.which) code = e.which;

        if (code == 13)
        {
	        e.cancelBubble = true;
	        if (e.stopPropagation) e.stopPropagation();
	        self.Search();
	        return false;
        }
        else
            return true;
    }
    
    this.Search = function()
    {
        var TargetTextBox = document.getElementById(TargetTextBoxID);
        if (TargetTextBox.value.length > 0)
        {
            window.location.href = "/Browse.aspx?Search=" + TargetTextBox.value;
        }
    }
}

/** LEGACY CODE **/
function viewLarge(productid)
{
    window.open("EnlargePhoto.aspx?ProductID=" + productid, null, "height=700,width=955,location=no,menubar=no,statusbar=no,scrollbars=no,resizable=no,left=50,top=50");
}

function ajax_call(el,url, xmlparsercallback)
{
    var xmlHttp;
    try { xmlHttp=new XMLHttpRequest(); }
    catch (e)
    {
        try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
        catch (e)
        { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
    }
  
    xmlHttp.onreadystatechange=function()
    {
        if(xmlHttp.readyState==4)
        {
            xmlparsercallback(el,xmlHttp.responseXML);
        }
    }
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

var relativePath = "";

function showProductPagePopup(source, evt, SelectorID)
{
    var ie=(window.ActiveXObject ? 1 : 0); 
    
	if (!evt)
	{  
		var evt = window.event;
		ie = false;
	}

	evt.cancelBubble = true;
	if (evt.stopPropagation) evt.stopPropagation();		
	
	var popupDiv = document.getElementById("popupDiv");
	if (!popupDiv)
	{
		popupDiv = document.createElement('div');
		popupDiv.id = "popupDiv";
		document.body.appendChild(popupDiv);
		
		// add the close link
		var closeLink = document.createElement('a');
		closeLink.id = "popupDiv_closeLink";
		closeLink.href = "#";
		closeLink.onclick = hideProductPagePopup;
		popupDiv.appendChild(closeLink);
		
		var closeImg = document.createElement('img');
		closeImg.src = "/images/closepopup.gif";
		closeImg.border = 0;
		closeLink.appendChild(closeImg);
		
		// add the container that the image will go in
		var imgContainer = document.createElement('div');
		imgContainer.id = "popupDiv_imgContainer";
		popupDiv.appendChild(imgContainer);
		
		// add the image into the container
		var imgPhoto = document.createElement('img');
		imgPhoto.id = "popupDiv_img";
		imgContainer.appendChild(imgPhoto);
		
		// add the enlarge link
		var enlargeLink = document.createElement('a');
		enlargeLink.id = "popupDiv_enlarge";
		enlargeLink.innerHTML = "Enlarge";
		enlargeLink.target = "_blank";
		popupDiv.appendChild(enlargeLink);
		
		// add the name div
		var nameDiv = document.createElement('div');
		nameDiv.id = "popupDiv_name";
		popupDiv.appendChild(nameDiv);
		
		// add the text div
		var textDiv = document.createElement('div');
		textDiv.id = "popupDiv_text";
		popupDiv.appendChild(textDiv);
	}

    // set the top appropriately
	var windowHeight = (ie ? document.body.offsetHeight : window.innerHeight);
    var sourceTop = getGlobalY(source) + 15;
    if (sourceTop + 250 > windowHeight)
        sourceTop -= 267;
	popupDiv.style.top = sourceTop + "px";

    // set the left appropriately
	var sourceLeft = getGlobalX(source);
	popupDiv.style.left = sourceLeft + "px";
	
	ajax_call(popupDiv, "/ProductPopup.ashx?SelectorID=" + SelectorID, showProductPagePopup_callback);
	
    return false;
}

function getGlobalY(source)
{
    var ret = 0;
    while( source != null )
    {
        ret += source.offsetTop;
        source = source.offsetParent;
    }
    return ret;
}

function getGlobalX(source)
{
    var ret = 0;
    while( source != null )
    {
        ret += source.offsetLeft;
        source = source.offsetParent;
    }
    return ret;
}

function showProductPagePopup_callback(el, responseXML)
{
    var photosetid = responseXML.getElementsByTagName("photosetid")[0].firstChild.nodeValue;
    var photo = responseXML.getElementsByTagName("photo")[0].firstChild.nodeValue;
    var name = responseXML.getElementsByTagName("name")[0].firstChild.nodeValue;
    var description = responseXML.getElementsByTagName("description")[0].firstChild.nodeValue;    
    
    el.childNodes[1].firstChild.src = "/ProductPhotos/Small/" + photo;
    el.childNodes[2].href = "/EnlargePhoto.aspx?PhotoSetID=" + photosetid;
    el.childNodes[3].innerHTML = name;
    el.childNodes[4].innerHTML = description;
        
    // make the div visible
    el.style.display = "block";
}


function hideProductPagePopup()
{ document.getElementById("popupDiv").style.display = "none"; return false; }

function VerifyQuantity(target,flag,showAlert)
{
    var ctlValue = parseInt(target.value);
    if (isNaN(ctlValue))
    {
        if (showAlert)
            alert('Please enter a valid quantity.');
        return false;
    }
    else
    {
        var quantization = parseInt(target.getAttribute('quantization'));
        if (quantization != null)
        {
            if (ctlValue % quantization > 0)
            {
                ctlValue = (Math.floor(ctlValue/quantization)+1) * quantization;
                target.value = ctlValue;
                if (showAlert)
                {
                    if (flag)
                        alert('One or more of your items must be purchased in quantites of ' + quantization + '.  The relevant quantity boxes have been rounded up to the next acceptable quantity.');
                    else
                        alert('The item you have selected must be purchased in quantites of ' + quantization + '.  The relevant quantity box has been rounded up to the next acceptable quantity.');
                }
                return false;
            }            
        }
        return true;
    }		
}

function VerifyListQuantity(e)
{
	var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;

    var QuantityBox = targ.parentNode.previousSibling.firstChild;
	
    return VerifyQuantity(QuantityBox, false, true);
}

function VerifyQuickOrderQuantity(e)
{
    var isValid = true;
    var inputElements = document.getElementsByTagName('input');
    for(var i = 0; i < inputElements.length; i++)
    {
        var quantization = inputElements[i].getAttribute('quantization');
        if (quantization != null)
        {
            if (isValid)
                isValid = VerifyQuantity(inputElements[i], true, true);
            else
                VerifyQuantity(inputElements[i], true, false);
        }
    }
    return isValid;
}

function VerifyShoppingCartQuantity()
{
    var isValid = true;
    var inputElements = document.getElementsByTagName('input');
    for(var i = 0; i < inputElements.length; i++)
    {
        var quantization = inputElements[i].getAttribute('quantization');
        if (quantization != null)
        {
            if (isValid)
                isValid = VerifyQuantity(inputElements[i], true, true);
            else
                VerifyQuantity(inputElements[i], true, false);
        }
    }
    return isValid;
}

function oneClick(ctl)
{
    var hasClicked = ctl.getAttribute("hasClicked");
    if (hasClicked != null && hasClicked != "")
    {
        alert("Your submission is currently being transmitted to the website.  If nothing happens for 60 seconds, click your browser's Back button and try again.");
        return false;
    }
    else
    {
        ctl.setAttribute("hasClicked", "true");
        return true;
    }
}