﻿/* MISC UTILITIES */
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 hideDiv(el)
{
    document.getElementById(el).style.display = "none";
}

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 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;
}
/* END MISC UTILITIES */

/* AJAX SEARCH */
var relativePath = "";
var lastSearchQuery;
function okp_query(rel,target,src,e)
{
    relativePath = rel;
    
    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 + "/Search.aspx?query=" + val;
}
function search(el,query)
{
    lastSearchQuery = query;
    ajax_call(el, relativePath + "/Ajax/Search.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;
    el.innerHTML = "";
    
    if (categories.length > 0)
    {
        var newDiv = document.createElement('div');
        newDiv.className = "header";
        newDiv.innerHTML = "Categories (<a href='" + relativePath + "/Search.aspx?query=" + lastSearchQuery + "'>More...</a>)";
        el.appendChild(newDiv);
    
        for(var i = 0; i < categories.length; i++)
        {
            var newDiv = document.createElement('div');
            newDiv.className = "item";
            newDiv.innerHTML = "<a href='" + relativePath + "/Browse.aspx/" + categories[i].childNodes[1].firstChild.nodeValue + "/" + categories[i].childNodes[0].firstChild.nodeValue + "'>" + categories[i].childNodes[0].firstChild.nodeValue + "</a>";
            el.appendChild(newDiv);
        }
    }

    if (products.length > 0)
    {
        var newDiv = document.createElement('div');
        newDiv.className = "header";
        newDiv.innerHTML = "Products (<a href='" + relativePath + "/Search.aspx?query=" + lastSearchQuery + "'>More...</a>)";
        el.appendChild(newDiv);
        
        for(var i = 0; i < products.length; i++)
        {
            var newDiv = document.createElement('div');
            newDiv.className = "item";
            newDiv.innerHTML = "<img src='" + relativePath + "/LiveImage.ashx?preset=icon&img=ProductPhotos/" + products[i].childNodes[3].firstChild.nodeValue + "' border='0' align='absmiddle' /><a href='" + relativePath + "/View.aspx/" + products[i].childNodes[2].firstChild.nodeValue + "/" + products[i].childNodes[0].firstChild.nodeValue + "'><div class='sku'>" + products[i].childNodes[1].firstChild.nodeValue + "</div>" + products[i].childNodes[0].firstChild.nodeValue + "</a>";
            el.appendChild(newDiv);
        }
    }
}
/* END AJAX SEARCH */

/* AJAX PRODUCT DETAIL POPUPS */
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 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; }

/* END PRODUCT PAGE POPUPS */

/*
function removeReturnSubmit_setEvents()
{
    var inputElements = document.getElementsByTagName('input');
    for(var i = 0; i < inputElements.length; i++)
    {
        if (inputElements[i].type == "text")
        {
            inputElements[i].onkeydown = removeReturnSubmit;
        }
    }
}
function removeReturnSubmit(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)
	    return false;
	else
	    return true;
}
*/