﻿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);
}

function okp_query(target,src,e)
{
    var ele = document.getElementById(target);
    if (src.value.length > 2)
        search(ele,src.value);
    else
        ele.style.display = "none";
        
    document.body.onclick = function(){ ele.style.display = "none"; }
    ele.onclick = function (e) { if (!e) var e = window.event; e.cancelBubble = true; if (e.stopPropagation) e.stopPropagation(); }
}

function hardSearch(rel,val)
{
    window.location.href = rel + "/Browse.aspx?Search=" + val;
}

var lastSearchQuery;
function search(el,query)
{
    lastSearchQuery = query;
    ajax_call(el,"Ajax.ashx?action=search&query=" + query, search_callback);
}

function search_callback(parent,xmlDoc)
{   
    var categories = xmlDoc.getElementsByTagName("category");
    var products = xmlDoc.getElementsByTagName("product");

    if (products.length == 0 && categories.length == 0)
        parent.style.display = "none";
    else
        parent.style.display = "block";

    var el = parent.childNodes[1];
    el.innerHTML = "";
        
    for(var i = 0; i < categories.length; i++)
    {
        var newDiv = document.createElement('div');
        newDiv.setAttribute("class", "item");
        newDiv.innerHTML = "<a href='Browse.aspx?CategoryID=" + categories[i].childNodes[1].firstChild.nodeValue + "'>" + categories[i].childNodes[0].firstChild.nodeValue + "</a>";
        el.appendChild(newDiv);
    }
    
    if (categories.length > 0)
    {
        var newDiv = document.createElement('div');
        newDiv.setAttribute("class", "item moreLink");
        newDiv.innerHTML = "<a href='Browse.aspx?Search=" + lastSearchQuery + ">More...</a>";
        el.appendChild(newDiv);    
    }

    for(var i = 0; i < products.length; i++)
    {
        var newDiv = document.createElement('div');
        newDiv.setAttribute("class", "item");
        newDiv.innerHTML = "<img src='ProductPhotos/Tiny/" + products[i].childNodes[2].firstChild.nodeValue + "' border='0' align='absmiddle' /><a href='View.aspx?SelectorID=" + products[i].childNodes[1].firstChild.nodeValue + "'>" + products[i].childNodes[0].firstChild.nodeValue + "</a>";
        el.appendChild(newDiv);
    }

    if (products.length > 0)
    {
        var newDiv = document.createElement('div');
        newDiv.setAttribute("class", "item moreLink");
        newDiv.innerHTML = "<a href='Browse.aspx?Search=" + lastSearchQuery + ">More...</a>";
        el.appendChild(newDiv);
    }
}

function hideDiv(el)
{
    document.getElementById(el).style.display = "none";
}

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 image
		var imgPhoto = document.createElement('img');
		imgPhoto.id = "popupDiv_img";
		popupDiv.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].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 setQuantizers()
{
    var inputElements = document.getElementsByTagName('input');
    for(var i = 0; i < inputElements.length; i++)
    {
        var quantization = inputElements[i].getAttribute('quantization');
        if (quantization != null)
        {
            inputElements[i].onblur = quantizationListener;
        }
    }
}

var evx;

function quantizationListener(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 quantization = parseInt(targ.getAttribute('quantization'));
    if (quantization != null)
    {
        var ctlValue = parseInt(targ.value);
        if (isNaN(ctlValue))
            alert('Please enter a valid quantity.');
        else
        {
            if (ctlValue % quantization > 0)
            {
                alert('This item must be purchased in quantites of ' + quantization + '.  Your quantity has been updated to reflect this requirement.');
                ctlValue = (Math.floor(ctlValue/quantization)+1) * quantization;
                targ.value = ctlValue;
            }
        }
    }
}

/* for the navigation roll-overs */
function setSwapImage()
{
    var imgs = document.getElementsByTagName("img");
    for(var i = 0; i < imgs.length; i++)
    {
        var swapImg = imgs[i].getAttribute("swapImg");
        if (swapImg != null)
        {
            imgs[i].onmouseover = toggleImg;
            imgs[i].onmouseout = toggleImg;
        }
    }
}

function toggleImg(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 oldImg = targ.src;
    var swapImg = targ.getAttribute("swapImg");
    targ.src = swapImg;
    targ.setAttribute("swapImg", oldImg);
}