//*********************************************************************
// Category2007.js - 
// 08 Mar 2007 Initial version

window.onerror = stopError;
setInterval('blinkIt()',1000);
var arrMod2;
var arrPriceParams;
var arrProdID;
		
// ********************************************************************
// init - Called when page first loads
// ********************************************************************
function  init()
{ 
	if( strProdID.length > 0 )
	{
		arrProdID = strProdID.split('|');
	}

	for( i = 0; i < arrProdID.length; i++ )
	{
		getModDL( arrProdID[i] );		
	}

	return;
}

// ********************************************************************
// Update price
// ********************************************************************
function updatePrice( pid ){ 
	// Take the mod1selection and mod1selection2 values and find them in the modifier array
//alert('updating price');
	var idxPrice = -1;
	var strPrice = '';

	// Clear all divs
	writediv2('stock_'+pid,'');
	writediv2('pricing_'+pid,'');
	writediv2('add_'+pid,'');		  
	writediv2('stockcode_'+pid,'');
	writediv2('quantity_'+pid,'');
	
	for( var i = 0; i < eval('arrMod'+pid+'.length'); i++ ){
		// This is where both Mod1 and Mod2 is used

		if( ( eval('mod1selection'+pid) == eval('arrMod'+pid+'[i][1]') ) && ( eval('mod2selection'+pid) == eval('arrMod'+pid+'[i][2]') ) ) idxPrice = i;

		// This is where Mod1 only is used
		if( ( eval('mod1selection'+pid) == eval('arrMod'+pid+'[i][1]') ) && ( eval('arrMod'+pid+'[i][2]') == '' ) ) idxPrice = i;


		if( idxPrice >= 0 ){ // Price found
			// Stock
			var currentstock = eval('arrMod'+pid+'[idxPrice][7]');
			var backorder = eval('arrMod'+pid+'[idxPrice][10]');
			
			if( !isNaN(currentstock) ) currentstock = parseInt(currentstock);
			if( !isNaN(backorder) ) backorder = parseInt(backorder);
			
			var stockmsg = instockmsg;
			var backordermsg = '';
			if( currentstock < 1 ){
				stockmsg = outstockmsg;
				if(backorder == 1){
					backordermsg = '<br />This item is available for back order';
				}
			}
			
//			if( stock == 1 && stockmsg.length > 0 ) writediv2('stock_'+pid,stockmsg+': <span class=\"errors\">' + currentstock + '</span>' );

			if( stock == 1){ // Show Stock
				if( showstock == 'a' ){ // Default Messages
					writediv2('stock_'+pid,stockmsg+': <span class=\"errors\">' + currentstock + '</span>' + backordermsg);
				}else{ // Custom Messages
					writediv2('stock_'+pid, stockmsg + backordermsg);
				}
			}

			// Pricing
			var rrp = eval('arrMod'+pid+'[idxPrice][4]');
			var our = eval('arrMod'+pid+'[idxPrice][5]');
			if( (tradecustomer == 1)  && (eval('arrMod'+pid+'[idxPrice][6]') > 0) ) our = eval('arrMod'+pid+'[idxPrice][6]');
			if( eval('prodspecial'+pid+' == \'1\'') ) strPrice += '<span class="errors" style="text-decoration: blink;">SPECIAL OFFER</span> ';
			
			var strPrice = '';
			if( !isNaN(rrp) ){
				if(rrp > 0){
					if( eval('arrMod'+pid+'[idxPrice][4]') > 0 ) {
						if( tradecustomer == 1 ) rrp = (rrp/(100+tax))*100
						rrp = parseFloat(rrp).toFixed(2);
						strPrice += 'RRP <span class=\"errors\">'
						if(hide_our_price == 0) strPrice += '<strike>';
						strPrice += currency+rrp;
						if( tradecustomer == 1 ) strPrice += ' +Vat';
						if(hide_our_price == 0) strPrice += '</strike>';
						strPrice += '</span>'; 	
					}
				}
			}
			if( !isNaN(our) && hide_our_price == 0 ) 
			{
				if( tradecustomer == 1 ) our = (our/(100+tax))*100
				our = parseFloat(our).toFixed(2);			
				strPrice += '&nbsp; Our Price: <span class=\"errors\">'+currency + our;
				if( tradecustomer == 1 ) strPrice += ' +Vat ';
				strPrice += '</span>';
				
			}
			
			if(hide_prices == 1){
				strPrice = '<span style="color: #5793D6;">Price Shown In Basket</span>';
			}
			
			writediv2('pricing_'+pid,strPrice);
			
			// Quantity
			var stock_max = 500;
			if( stock == 1){ // Show Stock
				if(currentstock < stock_max){
					stock_max = currentstock;
				}
			}
			var quantity_form = '';
			if(stock_max > 0){
				for(q = 1; q <= stock_max; q++){
					quantity_form += '<option value="'+q+'">'+q+'</option>';
				}
				quantity_form = 'Quantity: <select name="quantity_'+pid+'" id="quantitydd_'+pid+'" class="formfields" onchange="javascript: updQty('+pid+','+idxPrice+');">'+quantity_form+'</select>';
			}else{
				quantity_form = '<span class="errors">&gt; </span><a href="notify_'+eval('arrMod'+pid+'[idxPrice][9]')+'.htm">Notify Me When This Item Is Back In Stock</a>';
			}
			writediv2('quantity_'+pid,quantity_form);
			
			
			// Add to basket
			var buyform = '<input class="formbuttons" name="submit_'+pid+'" type="submit" ';
			buyform 	+= 'onclick="document.getElementById(\'modID\').value=\''+eval('arrMod'+pid+'[idxPrice][9]')+'\';document.productlist.submit();" value="Add&nbsp;To&nbsp;Basket" />';
			
			
			if(hide_our_price == 1) buyform = '<span class="bold">Add to basket to view our price</span><br />' + buyform;
			
			
			if(hide_prices == 1) buyform += '<span class="bold">You can always remove it later</span>';
			
			if( stock == 1 ){
				if( currentstock > 0 || backorder == 1) writediv2('add_'+pid,buyform);	
			}else{
				writediv2( 'add_'+pid, buyform );
			}
			
			// Product code
			if( showstockcode == 1)
			{
				var stockhtm = '';
				writediv2( 'stockcode_'+pid, ' Code: ' +eval('arrMod'+pid+'[idxPrice][0]') );
			}
		
			break;	
		}
	}
	return;
}


// ********************************************************************
// Create the DL htm
// ********************************************************************
function getModDL( pid ){
	var strMod = '',strMod2 = '', i;
	for(i = 0; i < eval('arrMod'+pid+'.length'); i++){	
//		if( strMod.indexOf(eval('arrMod'+pid+'[i][1]')) < 0 )  strMod += eval('arrMod'+pid+'[i][1]') + '|';
//		strMod += eval('arrMod'+pid+'[i][1]') + '|';
		if( !listCheck(strMod, eval('arrMod'+pid+'['+i+'][1]')) ) strMod += eval('arrMod'+pid+'['+i+'][1]') + '|';
		if( eval('mod1selection'+pid) == eval('arrMod'+pid+'['+i+'][1]') ) strMod2 += eval('arrMod'+pid+'['+i+'][2]') + '|';
	}
	strMod 		= strMod.substr(0,strMod.length-1);
	var arrMod  = strMod.split('|');
	
	strMod = '<select name="mod1_'+pid+'" onchange="javascript:mod1selection'+pid+'=this.value;getModDL('+pid+')" class="formfields">';
	strMod += '<option value="'+modifiermenu+'">' + modifiermenu + '</option>';
	for( i = 0; i < arrMod.length; i++){
		strMod += '<option value="' + arrMod[i] + '" ';
		if( eval('mod1selection'+pid) == arrMod[i] ) strMod += ' selected ';
		strMod += '>' + arrMod[i] + '</option>';
	}
	strMod += '</select>';
	
	
	
	// Assemble the second modifier DL
	strMod2		= strMod2.substr(0,strMod2.length-1);		
	if( strMod2 != '' ){
		var s_arrMod2 = strMod2.split('|');
		
		strMod2 = '<select name="mod2_'+pid+'" onchange="javascript:mod2selection'+pid+'=this.value;updatePrice('+pid+');" class="formfields">';
		strMod2 += '<option value="'+modifiermenu2+'">' + modifiermenu2 + '</option>';

		for( i = 0; i < s_arrMod2.length; i++)
		{
			if( s_arrMod2[i] != '' ) 
			{
				strMod2 += '<option value="' + s_arrMod2[i] + '" ';
				if( eval('mod2selection'+pid) == s_arrMod2[i] ) strMod2 += ' selected ';		
				strMod2 += '>' + s_arrMod2[i] + '</option>';
			}
		}
		strMod2 += '</select>';
	}

	writediv2( 'modDL_'+pid, strMod + ' ' + strMod2 );	
	
	updatePrice(pid);
	return ;
}

// ********************************************************************
// writediv2 - deletes and replaces the htm inside the div element
// ********************************************************************
function writediv2(divname,htm){
	if (document.getElementById){
		//document.getElementById(divname).innerHTML = '';
		document.getElementById(divname).innerHTML = htm;
	}else if (document.all){
		document.all[divName].innerHTML = htm;	
	}else if (document.layers){
		document.layers[divName].document.open();				
		document.layers[divName].document.write(htm);
		document.layers[divName].document.close();	
	}	
}

// ********************************************************************
// Swap the main image
// ********************************************************************
function chgImg(imgField,newImg) 
{
	if (document.images) document['picField'].src= newImg.src;
}

// ********************************************************************
// blinkIt
// ********************************************************************
function blinkIt() 
{
	if (!document.all) return;
	else 
 	{
   		for( i = 0; i < document.all.tags('blink').length; i++ )
   		{
			  s=document.all.tags('blink')[i];
			  s.style.visibility=(s.style.visibility=='visible')?'hidden':'visible';
   		}
	}
}

// ********************************************************************
// stopError
// ********************************************************************
function stopError() 
{
 	return true;
}

// ********************************************************************
// alreadyExists - checks to see if item already exists (in full) in the list if it does true is returned
// ********************************************************************
function listCheck(sList, sItem){
	var boutput = false; // default to false
	var aList = sList.split('|');
	for(j=0; j <= aList.length ; j++){
		if(aList[j] == sItem) boutput = true;
	}
	return boutput;
}

function updQty(pid,idx){
	var qty = document.getElementById("quantitydd_" + pid).value;
	qty = parseInt(qty);
	
	var rrp = eval('arrMod'+pid+'['+idx+'][4]');
	var our = eval('arrMod'+pid+'['+idx+'][5]');
	if( (tradecustomer == 1) && (eval('arrMod'+pid+'['+idx+'][6]') > 0) ) our = eval('arrMod'+pid+'['+idx+'][6]');
	
	var mod1Qty = parseInt(eval('arrMod'+pid+'['+idx+'][11];'));
	var mod1Sell = eval('arrMod'+pid+'['+idx+'][12];');
	var mod1Trade = eval('arrMod'+pid+'['+idx+'][13];');
	
	var mod2Qty = parseInt(eval('arrMod'+pid+'['+idx+'][14];'));
	var mod2Sell = eval('arrMod'+pid+'['+idx+'][15];');
	var mod2Trade = eval('arrMod'+pid+'['+idx+'][16];');	
	
	var mod3Qty = parseInt(eval('arrMod'+pid+'['+idx+'][17];'));
	var mod3Sell = eval('arrMod'+pid+'['+idx+'][18];');
	var mod3Trade = eval('arrMod'+pid+'['+idx+'][19];');	
	
	var mod4Qty = parseInt(eval('arrMod'+pid+'['+idx+'][20];'));
	var mod4Sell = eval('arrMod'+pid+'['+idx+'][21];');
	var mod4Trade = eval('arrMod'+pid+'['+idx+'][22];');
	
	if(mod1Qty > 0 && qty >= mod1Qty){
		our = mod1Sell;
		if(tradecustomer == 1 && parseFloat(mod1Trade) > 0) our = mod1Trade;
	}
	
	if(mod2Qty > 0 && qty >= mod2Qty){
		our = mod2Sell;
		if(tradecustomer == 1 && parseFloat(mod2Trade) > 0) our = mod2Trade;
	}	
	
	if(mod3Qty > 0 && qty >= mod3Qty){
		our = mod3Sell;
		if(tradecustomer == 1 && parseFloat(mod3Trade) > 0) our = mod3Trade;
	}
	
	if(mod4Qty > 0 && qty >= mod4Qty){
		our = mod4Sell;
		if(tradecustomer == 1 && parseFloat(mod4Trade) > 0) our = mod4Trade;
	}
	our = parseFloat(our);

	var strPrice = '';
	if( eval('prodspecial'+pid+' == \'1\'') ) strPrice += '<span class="errors" style="text-decoration: blink;">SPECIAL OFFER</span> ';
	if( !isNaN(rrp) ){
		if(rrp > 0){
			if( eval('arrMod'+pid+'['+idx+'][4]') > 0 ) {
				if( tradecustomer == 1 ) rrp = (rrp/(100+tax))*100
				rrp = parseFloat(rrp).toFixed(2);
				strPrice += 'RRP <span class=\"errors\"><strike>'+currency+rrp;
				if( tradecustomer == 1 ) strPrice += ' +Vat';
				strPrice += '</strike></span>'; 	
			}
		}
	}
	if( !isNaN(our) && hide_our_price == 0 ) {
		if( tradecustomer == 1 ) our = (our/(100+tax))*100
		our = parseFloat(our).toFixed(2);			
		strPrice += '&nbsp; Our Price: <span class=\"errors\">'+currency + (our * qty).toFixed(2);
		if( tradecustomer == 1 ) strPrice += ' +Vat ';
		strPrice += '</span>';
		
		if(qty > 1){
			strPrice += '<br />Price Each: <span class=\"errors\">'+currency+our+'</span>';
		}
		if(hide_prices == 1){
			strPrice = '<span style="color: #5793D6;">Price Shown In Basket</span>';
		}
		writediv2('pricing_'+pid,strPrice);
	}
}

