/**
* This file allows you to overide core events used by the shop.js
*
* onStockUpdated
* onBeforeAddItem
**/

/**
* when stock is updated
**/

function pricecompare(str1,str2,orgprice,currency)
//function pricecompare(str1,orgprice,currency)
{

	
//	str = currency.substr(1,1)
//	str = str.toUpperCase()
//	
//	if(str=='P')
//	price =  str1;
//	else
//	price =  str2;
	
	var price = str1
	el = tsGetElementById('WasPrice');
	if(price!=orgprice && parseFloat(price)>parseFloat(orgprice))
	{
		el.innerHTML = currency + price;
	}
	else
	el.innerHTML = '';
}
function onStockUpdated(newmessage) {

}

/**
* Before an item is added to the basket
**/
function onBeforeAddItem(prcd, sku, stockMessage) {
	var ret = true;
	var stockmsg = stockMessage.tolower();

	if (stockmsg.indexOf('out of'))
	{
		alert('Sorry this item is out of stock');
		ret = false;
	}
	return ret;
}
function defaultImage(obj) {
	obj.src = "images/blank.gif";
}

//function getSelectedItemSkus(itemId)
//{
	//return '_'+tsGetElementById('current_colour_sku').value;
//}

/**
* Overrride shop.js method
* allows extra parameters to be sent to basket when b= is called by
* tsAddItem
* @return string
**/
function extraBasketInfo() {
	var ret = '';
	return ret;
}

/**
* Overrides shop.js method
*Allows you to add extra costs to unit total - shoudl over ride in custom-shop.js
**/
function getExtraCost() {
	var ret = 0.00;
	return ret;
}

/**
* Display info template in popup
**/
function customShopShowInfo(template,product) {
	w = 800, h = 600;
	w = screen.availWidth;
	h = screen.availHeight;
	popW = 450;
	popH = 560;
	leftPos = (w-popW)/2;
	topPos = (h-popH)/2;
	window.open( rootdir + 'index.php?j=iteminfo.php&mod=shop&p=' + product + '&t='+escape(template),'Detail','width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos);
}

var ts_productList = new Array();
var ts_upsellList = new Array();
function ts_addProduct(ident, productCode) {
	ts_productList.push([ident, productCode]);
}
function ts_addUpsellItem(ident, productCode) {
	ts_upsellList.push([ident, productCode])
}
var basketData = new Array();

function ts_buildBasketData(qualifiers, qualifierNames)
{
	var productSKUQualifier = qualifiers;

	for(var i = 0; i < ts_productList.length; i++)
	{
		var warning = "";
		var pSKU = "";
		for(var j = 0; j < productSKUQualifier.length; j++)
		{
			var item = tsGetElementById('options_'+ts_productList[i][0]+'_'+productSKUQualifier[j]);	// get SKU data
			
			if(item.options[item.selectedIndex].value == '-')
			{
				warning += qualifierNames[j] + " has not been selected\n";
			}
			else
			{
				pSKU += item.options[item.selectedIndex].value;
			}
		}
		if(warning != "")
		{
			alert("The following problems were encountered:\n\n"+warning);
			return 0;
		}
		var item = tsGetElementById('options_'+ts_productList[i][0]+'_productqty');		// get quantity
		basketData.push([ts_productList[i][1], pSKU, item.options[item.selectedIndex].value]);	// add to holder
	}
	
	// check stock
	if (tsGetElementById('stockstatus_'+ts_productList[0][0])!=null)
	{
		if (tsGetElementById('stockstatus_'+ts_productList[0][0]).innerHTML.indexOf('Out of')>-1 ||
		 tsGetElementById('stockstatus_'+ts_productList[0][0]).innerHTML.indexOf('No stock')>-1 ||
		 tsGetElementById('stockstatus_'+ts_productList[0][0]).innerHTML == '')
		{
			basketData.pop();
			alert('Sorry this item is out of stock');
			return 0;
		}
	}
	return 1;
}
function ts_buildCmpBasketData(qualifiers, qualifierNames,formname) {
	var productSKUQualifier = qualifiers;

	for(var i = 0; i < ts_productList.length; i++) 
	{
		
		var warning = "";
		var pSKU = "";
		
		for(var j = 0; j < productSKUQualifier.length; j++) {
			var item = tsGetElementById('options_'+formname+'_'+productSKUQualifier[j]);	// get SKU data
		
			if(item.options[item.selectedIndex].value == '-') {
				warning += qualifierNames[j] + " has not been selected\n";
			} else {
				pSKU += item.options[item.selectedIndex].value;
			}
		}
		if(warning != "") {
			alert("The following problems were encountered:\n\n"+warning);
			return 0;
		}
		var item = tsGetElementById('options_'+ts_productList[i][0]+'_productqty');		// get quantity
		basketData.push([ts_productList[i][1], pSKU, item.options[item.selectedIndex].value]);	// add to holder
	}
	
	// check stock
	if (tsGetElementById('stockstatus_'+formname)!=null)
	{
		var stkname = 'stockstatus_'+formname;		
		if (tsGetElementById(stkname).innerHTML.indexOf('Out of')>-1 ||		
		 tsGetElementById(stkname).innerHTML.indexOf('No stock')>-1 ||
		 tsGetElementById(stkname).innerHTML == '')
		{
			basketData.pop();
			alert('Sorry this item is out of stock');
			return 0;
		}
	}

	return 1;
}
function ts_sendBasketData() {
	var formattedData = [[], [], []];
	for(var i = 0; i < basketData.length; i++) {
		formattedData[0].push(basketData[i][0]);
		formattedData[1].push(basketData[i][1]);
		formattedData[2].push(basketData[i][2]);
	}
	var sendString = "?b="+formattedData[0].join(",")+"&sku="+formattedData[1].join(",")+"&qty="+formattedData[2].join(",");
	//alert(sendString); // uncomment for debugging issues
	window.location = sendString;

	return 1;
}


/**
* override g_OutOfStockMessage from core shop.js file.  Must match stockmessage.class.php outofstock message
**/
g_OutOfStockMessage = ' ';
