 var globalQuickprice={};
var globalFullprice={};
var globalBestprice={};
     
     
function loadProductList()
{
	var options={};
	var url="/php/productlist/productlist.php?style=none";
	
	if($('product_list'))
		new Ajax.Updater('product_list', url,options);
		
	url="/php/productlist/productlist.php?placement=middle&frontpage=yes";
	if($('product_list_middle'))
		new Ajax.Updater('product_list_middle', url,options);


	if($('product_list_new_notable'))
	{
		url="/php/productlist/productgroup.php?id=53&placement=middle&frontpage=yes";
		new Ajax.Updater('product_list_new_notable', url,options);
	}
}
function listGroup(product_group_id)
{
	
	var options={ onComplete: loadComplete };
	var url="/php/productlist/productgroup.php?id="+product_group_id;
	
	var div_id="group_"+product_group_id;

	if($(div_id).visible())
	{
		$(div_id).hide();
		return;
	}
	
	new Ajax.Updater(div_id, url,options);


	url="/php/productlist/productgroup.php?id="+product_group_id+"&placement=middle";
	
	window.dhtmlHistory.add(escape(url));
//	alert(escape(url));
	new Ajax.Updater('maincontent', url,options);
	
	$(div_id).show();

}

function loadProduct(product_id, nolimit)
{
	var options={ onComplete: loadComplete };
	var url="/php/productlist/productitem.php?id="+product_id+"&mode=ajax";
	if(nolimit==true)
		url=url+"&nolimit=true";
		
	window.dhtmlHistory.add(escape(url));
	new Ajax.Updater('maincontent', url,options);
	
}


function loadComplete(transport)
{
	$('page_body').scrollTo();
}



function previewProduct(product_template_id, product_heading, preview_type)
{
	var url='/php/preview/defaultpreview.php?id='+product_template_id;
	url=url+'&type='+preview_type;
	var title=product_heading+" Preview";
		
    // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	//window.open(url,title,'width=1000,scrollbars=yes'); // this line does not work in IE!
    window.open(url,'_blank','width=1000,scrollbars=yes');
}


function adjustPricing(product_id, envelope_item_yes, envelope_item_no, checkbox)
{
	
	var options={onSuccess: adjustPrices};
	
	var stock_item_id=envelope_item_yes;
	if(!checkbox.checked==false) // this logic is wrong, I had to reverse it to make it work, I dont know why yet
		stock_item_id=envelope_item_no;
	
	$('envelopes').value=stock_item_id;

	var url='/php/product_price.php?productid='+product_id+'&stockid='+stock_item_id;
	
	new Ajax.Updater('console',url,options);
	
}

function adjustPrices(oReq, oJSN) {
    var data = oReq.responseText.evalJSON();
	
    $('console').innerHTML = '';
    //populate the list
   
    globalFullprice=data['fullprice'];
	updateOneSet(globalFullprice);

    globalBestprice=data['bestprice'];
    updateOneSet(globalBestprice);
    
    globalQuickprice=data['quickprice'];
    updateOneSet(globalQuickprice);
    
     var difference=data['difference'];
    updateOneSet(difference);
    
    var selectionPrice=data['selectionPrice'];
    var total=selectionPrice['price'];
    var element=$('prices_may_vary')
    if(element!=null && total!=0)
    	element.innerHTML="Pricing Estimate: $"+total;
//    alert(selectionPrice['price']);


}

function updateOneSet(values)
{
    var keyArray=Object.keys(values);
    keyArray.each(function(key) {
    							var value=values[key];
    							element=$(key);
    							if(element)
    								element.innerHTML=value;
    							}
    			);
}

