﻿function GoToProduct(sel) 
{
    if (sel == null) return;
    var url = sel.options[sel.selectedIndex].value;
    if (url != "") top.location.href = url;
}

function GetRef(id) 
{
    return (document.all) ? document.all[id] : (document.getElementById) ? document.getElementById(id) : null;
}

function ShowHide(id, show) 
{
    var e = GetRef(id);
    if (e == null) return;
    var display = (show) ? "block" : "none";
    e.style.display = display;
    //alert(id +" ~ "+ e.id +" ~ "+ e.style.display);
}

function ShowHideWithStyle(id, show, style) 
{
    var e = GetRef(id);
    if (e == null) return;
    var display = (show) ? style : "none";
    e.style.display = display;
}

function RetrieveDataItem(id) 
{
    var item = new Object();
    var ws_item;
    var static_item;
       
    try 
    {
       // retrieve data item from web service
       for (var p in products.productlist)
       {
            var pid = products.productlist[p].id;
            //alert("retrievedataitem : "+ id +" ~ "+ pid);
            //if (pid == id) return products.productlist[p];
            if (pid == id) 
            {
                //alert("found");
                ws_item = products.productlist[p];
                break;
            }
       }
       
       // add to main product item object
       for (var i in ws_item)
       {
            item[i] = ws_item[i];
       }
    }
    catch(e) 
    {
        //alert("prod list: "+ e);
        //return null;
    }

    try 
    {
       // retrieve data from static product list
       for (var p in staticProductInfo.productlist)
       {
            var pid = staticProductInfo.productlist[p].id;
            if (pid == id) 
            {
                static_item = staticProductInfo.productlist[p];
                break;
            }
       }
       
       // add to main product item object
        for (var i in static_item) 
       {
            if (i != "id")
            {
                item[i] = static_item[i];
            }
       }
    }
    catch(e) 
    {
        //alert("static list: "+ e);
        return item;
    }

    return item;

}


function GetContainerID(id) 
{
    var precede = "prodInfo_";
    if (id == "imageurl") return precede +"image";
    if (id == "nutritionalurl") return precede +"nutritional";
    return precede + id;
}

function PopulateDataItem(containerID, dataID, data)
{
    var oItemContainer = GetRef(containerID);
    if (oItemContainer == null) return "";
    
    if (dataID == "" || data == "") return "";
    
    var template = oItemContainer.innerHTML;
    var search = (template.indexOf("%7E") > -1) ? "%7E"+ dataID +"%7E" : "~"+ dataID +"~";
    var result = template.replace(search, data);
    
    return result;
}

function view_product(product_id) 
{
    if (isNullOrEmpty(product_id)) return;
	//alert("got product id: "+ product_id);
	
	if (isNullOrEmpty(products)) return;
	//alert("got products : "+ products.productlist.length);
	
	var productItem = RetrieveDataItem(product_id);
	if (isNullOrEmpty(productItem.name)) return;
	//alert("got product item : "+ productItem.name +" ~ "+ typeof(productItem.name));
	
	var oTarget = GetRef("popupInfoText");
	if (oTarget == null) return;
	//alert("got target");
	
	var oSource = GetRef("popupInfoText_template");
    if (oSource == null) return "";
    //alert("got source");
    
	var sRet = oSource.innerHTML;
	for (var i in productItem) 
	{
	    var containerID = GetContainerID(i);
	    var data = productItem[i];
	    
	    if (data == "")
	    {
	        sRet = sRet.replace(containerID +"\"", containerID +"\" style=\"display:none;\"");
	        sRet = sRet.replace(containerID +">", containerID +" style=\"display:none;\">"); // replace without quotes because IE chooses to strip out our quotes
	    }
	    else
	    {
	        //sRet += PopulateDataItem(containerID, i, data);
	        var search = (sRet.indexOf("%7E"+ i +"%7E") > -1) ? "%7E"+ i +"%7E" : "~"+ i +"~";
            sRet = sRet.replace(search, data);
        }
	}
	
	oTarget.innerHTML = sRet;
	
	ShowHide("popupInfoText", true);
	ShowHide("newwaysForm", true);
}

function isNullOrEmpty(val) 
{
    if (val == null) return true;
    if (typeof(val) == "undefined") return true;
    if (val == "") return true;
    return false;
}

function closeForm() {
	document.getElementById("newwaysForm").style.display = 'none';
}

function RetrieveQueryStringVars()
{
    try {
		var namevals = new Array();
		
		var qstring = location.search.substring(1);
		//alert(qstring);
		
		var arrparams = qstring.split("&");
		for (var i=0; i<arrparams.length; i++) {
			//alert(arrparams[i]);
			var arrpairs = arrparams[i].split("=");
			namevals[arrpairs[0]] = arrpairs[1];				
		}
		
		//alert(namevals["pid"]);
		return namevals;
	}
	catch(e) 
	{
	    //alert(e);
	    return null;
	}
}

function opennutritional(where) {
    var newwin = window.open(where,'product', 'scrollbars,menubar=0,resizable=1,dependent=0,status=0,width=300,height=650');
}

function findPos(obj) {
    var curleft = curtop = 0;
    var oReturn = new Object();
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
	        curleft += obj.offsetLeft
	        curtop += obj.offsetTop
        }
    }
	
    oReturn.top = curtop;
    oReturn.left = curleft;
	
    return oReturn;
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

/*
window.onload = function() {
    var namevals = RetrieveQueryStringVars();
    if (namevals != null) 
    {
        var product_id = namevals["pid"];
        view_product(product_id);
    }
}
*/
addLoadEvent(function(){
    window.setTimeout(function() {
        var namevals = RetrieveQueryStringVars();
        var product_id = namevals["pid"];
        if (product_id != null && product_id != "undefined")
        {
            view_product(product_id);
        }},
        500);
        
});
