//*********************************************************************
// productDetail2007.js - 
// 15 Feb 1007 Initial version

window.onerror = stopError;
setInterval('blinkIt()',1000);
var arrMod2;
var mod1selection = modifiermenu, mod2selection = modifiermenu2;
var arrPriceParams;
var selectedImg = '';

// ********************************************************************
// Preload images into javascript
// ********************************************************************

loadImages();


// ********************************************************************
// Load images into javascript
// ********************************************************************
function loadImages(){
	if( document.images ) {
		if( strProdImages.length > 0 ){
			var AllImages = strProdImages.split(",");
			arrProdImages 	= new Array();
			for( i = 0; i < AllImages.length; i++ ){
				loadImg = new Image();
				loadImg.src = AllImages[i];
				arrProdImages[i] = AllImages[i];
			}
		}
	}
}

// ********************************************************************
// init - Called when page first loads
// ********************************************************************
function  init(){ 
	if( document.getElementById("stock") ){
		if( arrModifiers.length > 2 ){
			getModDL();
		}else{
			updatePrice();
		}
		selectHighestStock();
	}
	return;
}

// ********************************************************************
// Update price
// ********************************************************************
function updatePrice( ){
	// Take the mod1selection and mod1selection2 values and find them in the modifier array
	var idxPrice = 0;
	var strPrice = '';
	var selMod1Text = '';
	
	// Clear all divs
	writediv2('stock','');
	writediv2('ourprice','');
	writediv2('notify','');
	writediv2('addtobasket','');		  
	writediv2('stockcode','');
	writediv2('delivery','');
	
	for( var i = 1; i < arrModifiers.length; i++ ){
		// This is where both Mod1 and Mod2 is used
//		if( ( mod1selection == arrModifiers[i][3] ) && ( mod2selection == arrModifiers[i][4] ) ) idxPrice = i;
		if( mod1selection == arrModifiers[i][0] ) selMod1Text = arrModifiers[i][3];

//		if( ( mod1selection == arrModifiers[i][0] ) && ( mod2selection == arrModifiers[i][4] ) ) idxPrice = i;
		if( ( selMod1Text == arrModifiers[i][3] ) && ( mod2selection == arrModifiers[i][4] ) ) idxPrice = i;
		
		// This is where Mod1 only is used
//		if( ( mod1selection == arrModifiers[i][3] ) && ( arrModifiers[i][4] == '' ) ) idxPrice = i;
		if( ( mod1selection == arrModifiers[i][0] ) && ( arrModifiers[i][4] == '' ) ) idxPrice = i;
				
		var alertText = "mod1selection: " + mod1selection + "\n";
		alertText += "arrModifiers[i][0]: " + arrModifiers[i][0] + "\n";
		alertText += "arrModifiers[i][4]: " + arrModifiers[i][4] + "\n";
		alertText += "selMod1Text: " + selMod1Text + "\n";
		alertText += "mod2selection: " + mod2selection + "\n";
		
		
		// This is where there is only one modifier
		if( arrModifiers.length == 2 ) idxPrice = i;
		
		if( idxPrice > 0 ){ // Price found
		
			var lastEle = arrModifiers[idxPrice].length -1;
			
			// Stock message
			var currentstock = parseInt(arrModifiers[idxPrice][21]);
			var onorder = parseInt(arrModifiers[idxPrice][26]);
			if(isNaN(currentstock)) currentstock = 0;
			if(isNaN(onorder)) onorder = 0;
			var backorder = arrModifiers[idxPrice][lastEle];
			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 />'+lang_translate("Item is available for back order")+'<br />';
					if(document.getElementById){
						var stocktd = document.getElementById("stocktd");
						stocktd.height = 40;
					}
				}
			}else{
				if(document.getElementById){
					var stocktd = document.getElementById("stocktd");
					stocktd.height = 25;
				}
			}
			if( stock == 1){ // Show Stock
				if( showstock == 'a' ){ // Default Messages
					writediv2('stock',stockmsg+': <span class=\"errors\">' + currentstock + '</span>' + backordermsg );
				}else{ // Custom Messages
					writediv2('stock',stockmsg + backordermsg)
				}
			}
			
			// Pricing
			var rrp = arrModifiers[idxPrice][6];
			var our = arrModifiers[idxPrice][7];
			var rrpEa = '';
			if( (tradecustomer == 1)  && (arrModifiers[idxPrice][8] > 0) ) our = arrModifiers[idxPrice][8];
			
			if( prodspecial == '1' ) strPrice += '<span class="errors" style="text-decoration: blink;">'+lang_translate("SPECIAL OFFER")+'</span><br />';
			
			strPrice += '<table width="100%" cellspacing="0" cellpadding="0" border="0">'
			if( !isNaN(rrp) ){
				if( arrModifiers[idxPrice][6] > 0 ) {
					strPrice += '<tr><td align="center">';
					if( tradecustomer == 1 ) rrp = (rrp/(100+tax))*100;
					rrp = parseFloat(rrp).toFixed(2);
					strPrice += lang_translate("RRP");
					strPrice += ' <span class=\"errors\">';
					if(hide_our_price == 0) strPrice += '<strike>';
					if(currencytext.length > 0) strPrice += currencytext+'&nbsp;';
					strPrice += currencysymbol+ '<span id="rprice">' + rrp + '</span>';
					if( tradecustomer == 1 ) strPrice += ' +' + lang_translate("VAT/Tax");
					
					if(hide_our_price == 0) strPrice += '</strike>';
					
					strPrice += '</span>';
					rrpEa = '<span style="visibility:hidden; width:0px;" id="rpriceea">' + rrp + '</span>';
					strPrice += rrpEa;
					strPrice += '</td></tr>';
				}		
			}
			if( !isNaN(our) && hide_our_price == 0 ) {
				strPrice += '<tr><td align="center" id="our_price">';
				if( tradecustomer == 1 ) our = (our/(100+tax))*100
				our = parseFloat(our).toFixed(2);
				if(display_tax == 1 && tradecustomer == 0 && tax > 0){
					var no_tax = (100 / ( 100 + tax) ) * our;
					no_tax = parseFloat(no_tax).toFixed(2);
					strPrice += 'Price: <span class=\"errors\">'+currencytext+'&nbsp;'+currencysymbol + '<span id="no_tax_price">' + no_tax + '</span></span><br />';
				}
				strPrice += '&nbsp;'+(display_tax == 1 && tradecustomer == 0 && tax > 0 ? '<span style=\"font-size:8pt;\" id=\"our_price\">' : '')+lang_translate("Our Price") + (display_tax == 1 && tradecustomer == 0 && tax > 0 ? ' ('+lang_translate("inc VAT/Tax")+')': '') +': <span class=\"errors\">'+currencytext+'&nbsp;'+currencysymbol + '<span id="oprice">' + our + '</span>'+(display_tax == 1 && tradecustomer == 0 && tax > 0 ? '</span>' : '');
				if( tradecustomer == 1 && tax > 0 ) strPrice += ' +'+lang_translate("VAT/Tax")+' ';
				strPrice += '</span>';
				//if(display_tax == 1 && tradecustomer == 0 && tax > 0) strPrice += '<br />' + lang_translate("VAT/Tax") + ' @ <span class=\"errors\">' + tax + '%</span>';
				strPrice += '<span style="visibility:hidden; width:0px;" id="opriceea">' + our + '</span>';
				strPrice += '</td></tr>';
			}
			strPrice += '</table>';
			if(hide_prices == 1){
				strPrice = '<span style="color: #5793D6;">Price Shown In Basket</span>';
			}
			writediv2('ourprice',strPrice);
			
			// Notify when in stock
			if( stock == 1 ) {
				if( currentstock < 1 && backorder == 0 ){
					var notifyhtm = '<blink><span class="errors">> </span></blink><a href=notify_'+arrModifiers[idxPrice][0]+'.htm>'+lang_translate("Notify Me When This Item Is Back In Stock")+'</a>';
					writediv2('notify',notifyhtm );
				}else
				{
					writediv2('notify','');
				}
			}
			
			// Add to basket
			var buyform  = '<form name="addtobasket" id="addtobasket" action="addtobasket.php" method="post">';
			buyform		+= '<input name="prodID" type="hidden" value="'+ our +'">';
			buyform		+= '<input name="modID" id="modID" type="hidden" value="'+ arrModifiers[idxPrice][0] +'">';
			
			buyform += lang_translate("Quantity")+': <select name="quantity" id="quantity" class="formfields" style="width:55px; margin-top:5px;" onchange="javascript: updateQuantity();" >';
			var maxstock = currentstock;
			
			if(maxstock == 0) maxstock = onorder;
			if(maxstock > 10000) maxstock = 9999;
			for(stockqty=1; stockqty <= maxstock; stockqty++){
				buyform += '<option value="'+stockqty+'">'+stockqty+'</option>';
			}
			buyform += '</select><br />';
			
			if(custOn == 1 && custItem == 1){
				buyform += custItemList;
			}
			
			buyform 	+= '<input class="formbuttons" name="submit" type="submit" id="add_to_basket" value="'+lang_translate("Add to Basket")+'" style="margin-top:5px;" onclick="return chkErrors();"; /></form>';
			if(hide_our_price == 1) buyform = '<span class="bold">Add to basket to view our price</span><br />' + buyform;
			if(hide_prices == 1) buyform += '<br /><span class="bold">You can always remove it later</span>';
			
			if( stock == 1 ){
				if( currentstock > 0 || backorder == 1){
					writediv2('addtobasket',buyform);
				}
			}else{
				writediv2( 'addtobasket', buyform );
			}
			
			// Product code
			if( showstockcode == 1){
				var stockhtm = '';
				var modifier = false;
				if( mod1selection != modifiermenu ){
					stockhtm += mod1selection + ' ';
					modifier = true;
				}
				if( mod2selection != modifiermenu2 ){
					stockhtm += mod2selection + ' ';
					modifier = true;
				}
				if(modifier == false){
					stockhtm += ' Code: ' + arrModifiers[idxPrice][2]; // Not Needed
				}else if(modifier == true){
					stockhtm  = ' Code: ' + arrModifiers[idxPrice][2];
				}
				writediv2( 'stockcode', stockhtm );
			}
			
			// Update Price with quantity modifier
			updateQuantity();
			
			// Delivery
			postageEstimator();
			break;	
		}
	}
	if(jQuery){
		$(document).ready(function(){
			var modifier_1_text = $.trim($("select#mod1DL option:selected").attr("text"));
			var modifier_2_text = $.trim($("select#mod2DL option:selected").attr("text"));
			if(modifier_1_text.length == 0 && modifier_2_text.length == 0){
				$("span#modifier_description strong").html($.trim($("td#product_name").text()));
			}else{
				$("span#modifier_description strong").html(modifier_1_text + (modifier_2_text.length > 0 ? "<br />" + modifier_2_text : ""));
			}
		});
	}
	return;
}

// ********************************************************************
// Create the DL htm
// ********************************************************************
function getModDL( ){
	// Cycle through each of the Mod1 array
	var strMod = '',strMod2 = '', strMod2Count = 0, i;
	var modValue = ''; var modValue2 = '';
	var modDesc = ''; var modDesc2 = '';
	for(i = 1; i < arrModifiers.length; i++){	
		if( !listCheck(strMod, arrModifiers[i][0] + '¬' + arrModifiers[i][3]) ){
			strMod += arrModifiers[i][0] + '¬' + arrModifiers[i][3] + '¬' + arrModifiers[i][2] + '|';
			if(arrModifiers[i][4].length > 0) strMod2Count ++;
		}
	}
	strMod 		= strMod.substr(0,strMod.length-1);
	var arrMod  = strMod.split('|');
	
	modifiermenu = jQuery.trim(modifiermenu);
	modifiermenu2 = jQuery.trim(modifiermenu2);
	
	strMod = (modifiermenu.length > 0 ? modifiermenu + '&nbsp;&nbsp;': '')+'<select name="mod1DL" id="mod1DL" onchange="javascript:mod1selection=this.value;getModDL();" class="formfields">';
	
	if(mod1selection == modifiermenu){
		var firstMod = arrMod[0].split('¬');
		mod1selection = firstMod[0];
	}
	
	for( i = 0; i < arrMod.length; i++){
		modValue = arrMod[i].split('¬');
		modDesc = modValue[1];
		if(showstockcode == '1' && strMod2Count == 0 ) modDesc += ' - Code: ' + modValue[2];
		strMod += '<option value="' + modValue[0] + '" ';
		if( mod1selection == modValue[0] ) strMod += ' selected="selected" ';
		strMod += ' title="'+ modDesc +'">' + modDesc + '</option>';
	}
	strMod += '</select>';

	// Assemble the second modifier DL
	strMod2		= strMod2.substr(0,strMod2.length-1);
	var selMod1Text = '';
	for(k = 1; k < arrModifiers.length; k++){
		if(arrModifiers[k][0] == mod1selection) selMod1Text = arrModifiers[k][3];
	}
	for(m = 1; m < arrModifiers.length; m++){
		if(arrModifiers[m][3] == selMod1Text && arrModifiers[m][4].length > 0) strMod2 += arrModifiers[m][4] + (arrModifiers[m][4].length > 0 ? '¬' + arrModifiers[m][2] : '') + '|';
	}	


	if(strMod2.length > 0){
		var arrMod2 = strMod2.split('|');

		strMod2 = '<br /><img src="images/blank.gif" height="5" width="200" border="0" /><br />'+(modifiermenu2 != modifiermenu && modifiermenu2.length > 0 ? modifiermenu2 + '&nbsp;&nbsp;': '')+'<select name="mod2DL" id="mod2DL" onchange="javascript:mod2selection=this.value;updatePrice();" class="formfields">';
		
		var firstMod2 = arrMod2[0].split('¬');
		mod2selection = firstMod2[0];

		for( i = 0; i < arrMod2.length; i++){
			modValue2 = arrMod2[i].split('¬');
			if(modValue2[0] != ''){
				modDesc2 = modValue2[0];
				if(showstockcode == '1') modDesc2 += ' - Code: ' + modValue2[1];
				strMod2 += '<option value="' + modValue2[0] + '" ';
				if( mod2selection == modValue2[0]) strMod2 += ' selected ';
				strMod2 += ' title="' + modDesc2 + '">' + modDesc2 + '</option>';
			}
		}
		strMod2 += '</select>';
	}
	writediv2( 'pricing', strMod + strMod2 );	
	updatePrice();
	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){
		var chngDelay = setTimeout("document['"+imgField+"'].src = '"+newImg.src+"'; selectedImg = '"+newImg.src+"';",100);
		
		// change the main zoom link
		var imgurl = newImg.src
		var imgbits = '';
		
		imgbits = imgurl.split("/");
		var nobits = imgbits.length;
		var imgname = imgbits[(nobits-1)];
		var haslrg = lgIMG[imgname];
		
		// hide all links first
/*		for(i = 1 ; i <= 4 ; i ++){
			var spanName = 'zoomImg' + i;
			if(document.getElementById(spanName)){
				document.getElementById(spanName).style.visibility = 'hidden';
			}
		}
		
		if(haslrg != 0){ // has large image - change link	
				
			// show the right link
			document.getElementById(haslrg).style.visibility = 'visible';
		
		}
*/		
		//zoomImg.innerHTML = zoomLink;

//		document[imgField].src = newImg.src;
//		selectedImg = newImg.src;
	}
}

// ********************************************************************
// Rollover Image
// ********************************************************************
function rollOver(imgField,newImg){
	if(document.images){
		var chngDelay = setTimeout("document['"+imgField+"'].src = '"+newImg+"';",100);
	}
}

// ********************************************************************
// Restore Image
// ********************************************************************
function restoreImg(imgField){
	if(document.images){
		if(selectedImg == ''){
			document[imgField].src = arrProdImages[0];
		}else{
			document[imgField].src = selectedImg;
		}
	}
}

// ********************************************************************
// 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){
//alert("List: "+ sList + "\nItem: " + sItem);
	var boutput = false; // default to false
	var aList = sList.split('|');
	for(j=0; j < aList.length-1 ; j++){
		if( (aList[j].indexOf('¬') > 0) && (sItem.indexOf('¬') > 0) ){
			aList2 = aList[j].split('¬');
			sItem2 = sItem.split('¬');
			if(aList2[1] == sItem2[1]) boutput = true;
		}
	}
	return boutput;
}

// ********************************************************************
// Displays Cheapest Postage option for modifier and postage zone
// ********************************************************************
function estimatePostage(){
	var vMod = '';
	var vZone = '';
	var vPostage = '&nbsp;<br /><br />';
	writediv2( 'postageResult', vPostage );

	vMod = document.getElementById("mod1DL").value;
	vZone = document.getElementById("delZones").value;

	vPostage = arrPZOpt[vMod][vZone];
	vPostage = vPostage.replace('@','<br />@');
	if(vZone == 0) vPostage = '&nbsp;<br /><br />';
	writediv2( 'postageResult', vPostage );
}

// ********************************************************************
// Draws the postage estimator
// ********************************************************************
function postageEstimator(){
	var arrPZSplit = '';
	var deliveryhtm = '';
	
	if(arrPZ.length > 1){
		deliveryhtm  = '<span style="font-weight:bold;">'+lang_translate("Postage Estimator")+'</span><br />';
		deliveryhtm += '<select name="delZones" id="delZones" class="formfields" onchange="javascript: estimatePostage();" style="width:190px;">';
		deliveryhtm += '<option value="0">'+lang_translate("Please Choose a Zone")+'</option>';
		for(zz = 0; zz < arrPZ.length; zz++){
			arrPZSplit = arrPZ[zz].split('|');
			deliveryhtm += '<option value="' + arrPZSplit[0] + '"';
			if(defaultPZ == arrPZSplit[0]) deliveryhtm += ' selected="selected"';
			deliveryhtm += '>' + arrPZSplit[1] + '</option>';
		}
		deliveryhtm += '</select>';
		deliveryhtm += '<br /><div id="postageResult" style="margin-top:5px;">&nbsp;<br /><br /></div>';
		writediv2( 'delivery', deliveryhtm);
		if(defaultPZ > 0) estimatePostage();
	}else{
		arrPZSplit = arrPZ[0].split('|');
		deliveryhtm  = '<span style="font-weight:bold;">'+lang_translate("Postage Estimator")+'</span>';
		deliveryhtm += '<input name="delZones" id="delZones" type="hidden" value="'+arrPZSplit[0]+'" />';
		deliveryhtm += '<br /><div id="postageResult" style="margin-top:5px;">&nbsp;<br /><br /></div>';
		writediv2( 'delivery', deliveryhtm);
		estimatePostage();
	}
}


// ********************************************************************
// Updates the price to reflect the quantity selected
// ********************************************************************
function updateQuantity(){
	var vQty = document.getElementById("quantity");
	var qty = parseInt(vQty.value);
			
	var vOur = document.getElementById("oprice");
	var vOurEa = document.getElementById("opriceea");
	var vModID = document.getElementById("modID");
	vModID = vModID.value;
	
	var selIndex = 0;
	for(iModLoop = 0; iModLoop < arrModifiers.length; iModLoop++){
		if(arrModifiers[iModLoop][0] == vModID){
			selIndex = iModLoop;
		}
	}
	
	var ourEa = arrModifiers[selIndex][7]; // Standard Price
	if(tradecustomer == 1 && arrModifiers[selIndex][8] > 0) ourEa = arrModifiers[selIndex][8];
	
	var discount_applied = false;
	// Bulk 1 Discount
	if(parseInt(arrModifiers[selIndex][9]) > 0 && qty >= parseInt(arrModifiers[selIndex][9])){
		ourEa = arrModifiers[selIndex][10];
		if(tradecustomer == 1 && arrModifiers[selIndex][11] > 0) ourEa = arrModifiers[selIndex][11];
		discount_applied = true;
	}
	// Bulk 2 Discount
	if(parseInt(arrModifiers[selIndex][12]) > 0 && qty >= parseInt(arrModifiers[selIndex][12])){
		ourEa = arrModifiers[selIndex][13];
		if(tradecustomer == 1 && arrModifiers[selIndex][14] > 0) ourEa = arrModifiers[selIndex][14];
		discount_applied = true;
	}
	// Bulk 3 Discount
	if(arrModifiers[selIndex][15] > 0 && qty >= arrModifiers[selIndex][15]){
		ourEa = arrModifiers[selIndex][16];
		if(tradecustomer == 1 && arrModifiers[selIndex][17] > 0) ourEa = arrModifiers[selIndex][17];
		discount_applied = true;
	}
	// Bulk 4 Discount
	if(arrModifiers[selIndex][18] > 0 && qty >= arrModifiers[selIndex][18]){
		ourEa = arrModifiers[selIndex][19];
		if(tradecustomer == 1 && arrModifiers[selIndex][20] > 0) ourEa = arrModifiers[selIndex][20];
		discount_applied = true;
	}
	
	if(tradecustomer == 1){
		ourEa = ourEa / ((tax/100)+1);
	}
	
	ourEa = parseFloat(ourEa).toFixed(2);
	var our = ourEa * qty;
	our = parseFloat(our).toFixed(2);
	vOur.innerHTML = our;
	
	if(qty > 1){
		vOurEa.style.visibility = "visible";
		
		if(display_tax == 1 && tradecustomer == 0 && tax > 0){
			var no_tax_price = document.getElementById("no_tax_price");
			var no_tax = (100 / ( 100 + tax) ) * ourEa;
			no_tax = parseFloat(no_tax).toFixed(2);
			no_tax_price.innerHTML = no_tax;
		}
		ourEa = lang_translate("Price Each") + (display_tax == 1 && tradecustomer == 0 && tax > 0 ? ' ('+lang_translate("inc VAT/Tax")+')': '') + ": <span class=\"errors\">" + currencysymbol + ourEa + "</span>";
		if(discount_applied === true) ourEa += '<br /><span class="errors">'+lang_translate("Bulk Discount Applied")+'</span>';
	}else{
		vOurEa.style.visibility = "hidden";
	}
	
	if(hide_prices == 1){
		ourEa = "";
	}
	
	vOurEa.innerHTML = "<br />" + ourEa;
	
	if(document.getElementById("rprice")){

		var vRRP = document.getElementById("rprice");
		var rrpEa = document.getElementById("rpriceea");
		rrpEa = rrpEa.innerHTML;
		var rrp = rrpEa * qty;
		rrp = parseFloat(rrp).toFixed(2);
		vRRP.innerHTML = rrp;
	}
}


function eBayit(){
	var modID = '';
	
	if(document.getElementById('modID')){
		modID = document.getElementById('modID').value;
		location.href='ebayAPI/listitem.php?modID=' + modID;
		
		//alert('mod selected: ' + modID);
	}else{
		alert('Please choose an in stock modifier to list on eBay');
	}
}

// ********************************************************************
// check if custom name is selected from cutomise drop down
// ********************************************************************

function customNames(){
	var customOption = document.getElementById("customOption").value;
	var customBoxes;
	//var lenCust = document.getElementById("lenCust").value;
	//alert('customOption: ' + customOption);
	
	if(customOption == 'c'){
		var custmsg = '';
		var nameboxcaption = '';

		if(usenum == 1){
			custmsg = 'Please enter your choosen name and number in the boxes. The name can be up to ' + lenCust + ' characters long &amp; the number 2 characters long.';
			nameboxcaption = 'Name';
		}else{
			custmsg = basketText + '. This can be up to ' + lenCust + ' characters long';
			nameboxcaption = basketText;
		};
		
		customBoxes = '<div align="left" ><br/>' + custmsg + '<br/><table cellpadding="3" cellspacing="o" border="0" >';
		
		if(usenum == 1){
			customBoxes += '<tr><td>Number</td><td><input type="text" name="customNumber" id="customNumber" class="formfields" style="width: 50px;" onKeyUp="checkCustNumLen();" /></td></tr>';	
		}
		
		var box_width = '10em';
		var loc = (window.location).toString();
		
		loc = loc.replace("http://","");
		loc = loc.replace("https://","");
		loc = loc.split("/");
		loc = loc[0];
		
		if(loc == "www.jakkcountryfurniture.co.uk"){
			nameboxcaption = "";
			box_width = '15em';
		}
		
		customBoxes += '<tr><td valign="top">' + nameboxcaption + '</td><td align="center"><input type="text" name="customName" id="customName" class="formfields" style="width: '+box_width+';" onKeyUp="checkNameLen();" /><br/>(<span id="customLenLeft" ><strong>' + lenCust + '</strong></span> characters left)</td></tr></table>';
		
		if(usenum == 0){
			customBoxes += '<input type="hidden" name="customNumber" id="customNumber" value="-" />';
		}
		
		customBoxes += '</div>';
		
	}else{
		customBoxes = '';
	}
	
	document.getElementById("custBoxes").innerHTML = customBoxes;
	
}

function checkNameLen(){
	var customName = document.getElementById("customName").value;
	//var lenCust = document.getElementById("lenCust").value;
	
	var lenName = customName.length;
	
	var charLeft = lenCust - lenName;
	
	//alert('Name Entered: ' + customName + '\n\nMax Length: ' + lenCust + '\n\nCurrent Length: ' + lenName + '\n\nLeft: ' + charLeft);
	
	//check its not to long
	if(lenName > lenCust){
		customName = customName.substr(0, lenCust);
		document.getElementById("customName").value = customName;
		
		lenName = customName.length;
	
		charLeft = lenCust - lenName;
		
	}
	
	document.getElementById("customLenLeft").innerHTML = '<strong>' + charLeft + '</strong>';
	
}

function checkCustNumLen(){
	var customNumber = document.getElementById('customNumber').value;
	var customNumberLen = customNumber.length;

	if(customNumberLen > 2){
		customNumber = customNumber.substr(0,2);
		document.getElementById('customNumber').value = customNumber;
		
	}
}

function chkErrors(){

	//check if the customise name is selcted
	var errors1 = 0;  //number 0 = ok 1 = blank 2 = not a number
	var errors2 = 0;  //name 0 = ok 1 = blank
	
	// if(document.getElementById('customNumber') && document.getElementById('customName')){
	if(document.getElementById('customName')){ // changed as number may not be there

		if(usenum == 1){
			var customNumber = document.getElementById('customNumber').value;		
		}else{
			var customNumber = 0;
		}
		
		var customName = document.getElementById('customName').value;
		
		var customNumberLen = customNumber.length;
		var customNameLen = customName.length;
		
		var errormsg = '';
		
		//check the custom number
		if(customNumber.length <= 0){
			//its blank
			errors1 = 1;
		}else{
			//its not blank, check its a number
			if(isNaN(customNumber)){
				//its not a number
				errors1 = 2;
			}else{
				//it is a numebr - its ok
				errors1 = 0;
			}
		}
		
		//check the custom name
		if(customName.length <= 0){
			//its blank
			errors2 = 1;
		}else{
			//its ok
			errors2 = 0;
		}
		
		//alert('errors1: ' + errors1 + '\nerrors2: ' + errors2);
		
		if(errors1 + errors2 == 0){
			//everythings ok
			return true;
		}else{
			//theres a problem
			errormsg = 'You have choosen to personalise this item and ';
			if(errors1 > 0){
				if(errors1 == 1){
					errormsg += 'you have not added a number';
				}else{
					errormsg += 'you need to add a number';
				}
			}
			
			if(errors2 > 0){
				if(errors1 > 0){
					errormsg += ' and';
				}
				
				errormsg += ' you need to enter your name';
			}
			
			errormsg += '\n\nPlease correct these problems and try again.';
			
			alert(errormsg);
			
			return false;
		}
		
		
		
		
	}else{
		return true; //they are not selected
	}
}

function selectHighestStock(){
	var highStock = 0;
	var selDesc = '';
	for(i = 1; i < arrModifiers.length; i++){
		if(arrModifiers[i][21] > highStock){
			selDesc = arrModifiers[i][3];
		}
	}
	var prodMods = document.getElementById("mod1DL");
	var selIndex = 0;
	var selValue = 0;
	for(j = 0; j < prodMods.options.length; j++){
		var optText = prodMods.options[j].text;
		if(optText == selDesc){
			selIndex = prodMods.options[j].index;
			selValue = prodMods.options[j].value;
		}
	}
}


function lang_translate(text){
	if(translate_lang[text]){
		return translate_lang[text];
	}else{
		return text;
	}
}


