var quantitychanged = 0;

function infopoppaywin(w,h,page)
{
wintype = "location=no,scrollbars=yes,status=yes,menubar=0,left=0,top=0,screenx=0,screeny=0,width="+w+",height="+h;
win=window.open(page,"newwin",wintype); 
win.focus();
}

function infopopscroll(w,h,page)
{
wintype = "location=no,scrollbars=yes,status=no,menubar=0,left=0,top=0,screenx=0,screeny=0,width="+w+",height="+h;
win=window.open(page,"newwin",wintype); 
win.focus();
}

function currencypop(currencypage)
{
win=window.open(currencypage,"currencywin","location=no,scrollbars=no,status=no,menubar=0,left=0,top=0,screenx=0,screeny=0,width=550,height=130"); 
win.focus();
}

function changeqty(refnumtodo,chgqty)
{
window.alert("QUANTITY CHANGE\n\nThe quantity of this item will be now changed");
window.document.location="t_checkorder.php?dowhat=QTY&refnum=" + refnumtodo + "&qty=" + chgqty + "";
}


function MM_displayStatusMsg(msgStr) 
{
status=msgStr;
document.MM_returnValue = true;
}

function validate_details()
{
//PICK UP CUSTOMER DETAILS
var title = window.document.forms.orderform.title.value;
var firstname = window.document.forms.orderform.firstname.value;
var lastname = window.document.forms.orderform.lastname.value;
var address1 = window.document.forms.orderform.address1.value;
var town = window.document.forms.orderform.town.value;
var telephone = window.document.forms.orderform.telephone.value;
var email = window.document.forms.orderform.email.value;
var postcode = window.document.forms.orderform.postcode.value;

//DO VALIDATIONS
if(title=="")
  { window.alert("ORDER ERROR\n\nYou have not entered your title.....");  }
else if(firstname=="")
  { window.alert("ORDER ERROR\n\nYou have not entered your first name.....");  }
else if(lastname=="")
  { window.alert("ORDER ERROR\n\nYou have not entered your last name.....");  }
else if(town=="")
  { window.alert("ORDER ERROR\n\nYou have not entered your city/town.....");  }
else if(email.indexOf("@") == -1)
  { window.alert("ORDER ERROR\n\nYou have not entered a valid e-mail address.....");  }
else if(email.indexOf(".") == -1)
  { window.alert("ORDER ERROR\n\nYou have not entered a valid e-mail address.....");  }
else if(telephone.length < 6)
  { window.alert("ORDER ERROR\n\nYou have not entered a valid telephone number.....");  }
else if(address1 == "")
  { window.alert("ORDER ERROR\n\nYou have not entered your postal address correctly.....");  }
else if(postcode == "")
  { window.alert("ORDER ERROR\n\nYou have not entered your postcode.....");  }  
else
  {
  window.document.orderform.submit();
  }

}

function dodiscount()
{
var discountcode = window.document.forms.itemsform.discountcode.value;
if(discountcode=="")
	{window.alert("DISCOUNT ERROR\n\nYou have not entered a discount code.....");}
else
	{window.document.location.href="control/H_discount_action.php?discountcode="+discountcode;}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function doCheckPrices()
{
var priceTotal=0;
totalOptsVal=0;
var maxSels = eval(window.document.compForm.maxSels.value);
var productPrice = eval(window.document.compForm.productPrice.value);
var thisID="";
var valArray=new Array();
var stockCodeString="";

	//window.alert(maxComps);	
	
for(var i=0; i<maxSels; i++)
	{
	thisVal=0;
		
	thisID="sel"+i;
	thisShow="priceShow"+i;	
	//window.alert(thisID);	
	selObject = window.document.getElementById(thisID);
	
	if(selObject.checked)
	{thisVal=selObject.value;
	
	valArray=thisVal.split("#~#");
	thisVal=eval(valArray[0]);
	thisStockCode=valArray[1];
	
	stockCodeString=stockCodeString+"#"+thisStockCode;
	}
	
	if(thisVal > 1) 
		{
		SthisVal=thisVal.toString();
		SthisVal=doCurrency(SthisVal);
		
		window.document.getElementById(thisShow).innerHTML="&pound;"+SthisVal;
		productPrice += thisVal;
		totalOptsVal += thisVal;
		//window.alert(totalOptsVal);
		}
	else
		{
		window.document.getElementById(thisShow).innerHTML="";
		}	
	}

//productPrice=doCurrency(productPrice);
SproductPrice=productPrice.toString();
productPrice=doCurrency(SproductPrice);

window.document.getElementById('priceTotal').innerHTML=productPrice;


// now the buyform(s)
// THIS IS FOR MULTI OPTIONS ON THE SAME SCREEN
selObject = window.document.getElementById('numOfOptions');
var numOfOptions=eval(selObject.value);
for(var i=0; i<numOfOptions; i++)
	{	
	thisForm="buyForm"+i;
	thisPriceSpan="priceSpan"+i;	
	//window.alert(thisID);
	thisOptPrice=eval(window.document.getElementById(thisForm).optionPrice.value);
	thisOptPrice+=totalOptsVal;
	thisOptPrice=doCurrency(thisOptPrice.toString());	
	window.document.getElementById(thisPriceSpan).innerHTML="&pound;"+thisOptPrice;
	window.document.getElementById(thisForm).componentList.value=stockCodeString;
	}

//THIS IS FOR A SINGLE OPTION
	
//	thisForm="buyForm"+i;
//	thisPriceSpan="priceSpan"+i;	


}
 
function doSelectPrices()
{
// need to get: all QTYS and current SELECTION
var priceTotal=0;
var maxComps = window.document.buyForm.maxComps.value;




// CURRENT ITEM ***********************************************************
// get currently selected product price
var currentItem =document.getElementById('refnum').selectedIndex;

var currentPriceField="price"+currentItem;

//alert(document.getElementById("price1").value);

var productPrice=document.getElementById(currentPriceField).value;

//window.alert(productPrice);

// DW SPECIAL - no use anywhere else!!! ***********************************
// OR HERE BY ALL ACCOUNTS

//var ItemType=document.getElementById('refnum').options[currentItem].text;

//window.alert(ItemType);

//if(ItemType.indexOf("PDF") > -1)
//{
//window.alert(ItemType);
//document.getElementById('qty').selectedIndex=0;
//document.getElementById('qty').disabled=true;
//}
//else
//{
//document.getElementById('qty').disabled=false;	
//}
//*************************************************************************

// how many of this product? what qty is selected?
var index =document.getElementById('qty').selectedIndex;
// index is almost the number selected! Very handy....
// increase, base is actually 1
index++;
//window.alert(itemPrice+" * "+index);
var newPrice = eval(productPrice*index);
newPriceS=addCommas(newPrice);
newPriceS=removeCurrency(newPriceS);
document.getElementById("priceSpanMainItem").innerHTML="£"+newPriceS;
// ****************************************************************************

var compPrice=0;
var thisID="";
var valArray=new Array();
var stockCodeString="";
var totalOptsVal=0;
	//window.alert(maxComps);	
	
for(var i=0; i<maxComps; i++)
	{	
	thisID="comp"+i;
	thisShow="priceShow"+i;	
	//window.alert(thisID);	
	selObject = window.document.getElementById(thisID);
	thisValFullCode=selObject.options[selObject.selectedIndex].value;
	valArray=thisValFullCode.split("#~#");
	thisVal=eval(valArray[0]);
	thisStockCode=valArray[1];
	
	if(stockCodeString.length<1)
	{stockCodeString=thisValFullCode;}
	else
	{stockCodeString=stockCodeString+"##"+thisValFullCode;}
	
	if(thisVal > 1) 
		{
		SthisVal=thisVal.toString();
		SthisVal=removeCurrency(SthisVal);
		SthisVal=addCommas(SthisVal);
		
		window.document.getElementById(thisShow).innerHTML="&pound;"+SthisVal;
		compPrice += thisVal;
		totalOptsVal += thisVal;
		}
	else
		{
		window.document.getElementById(thisShow).innerHTML="";
		}	
	}

// now we do the totals! ****************************************************************
// need to add total product to total components
var priceTotal=newPrice+compPrice;
SpriceTotal=priceTotal.toString();
SpriceTotal=removeCurrency(SpriceTotal);
SpriceTotal=addCommas(SpriceTotal);

// **************************************************************************************

window.document.getElementById('priceTotal').innerHTML=SpriceTotal;


// now the buyform(s)

selObject = window.document.getElementById('numOfOptions');
var numOfOptions=eval(selObject.value);
// HELP
//window.alert("NUM OF OPTIONS:"+numOfOptions);
if(numOfOptions>0)
{
// THIS IS FOR MULTI OPTIONS ON THE SAME SCREEN
for(var i=0; i<numOfOptions; i++)
	{	
	thisForm="buyForm"+i;
	thisPriceSpan="priceSpan"+i;	
	//window.alert(thisID);
	thisOptPrice=eval(window.document.getElementById(thisForm).optionPrice.value);
	thisOptPrice+=totalOptsVal;
	thisOptPrice=doCurrency(thisOptPrice.toString());		
	window.document.getElementById(thisPriceSpan).innerHTML="&pound;"+thisOptPrice;
	window.document.getElementById(thisForm).componentList.value=stockCodeString;
	
	// HELP
	//window.alert(stockCodeString);
	}
}
else
{
//THIS IS FOR A SINGLE OPTION
window.document.getElementById("buyForm").componentList.value=stockCodeString;
//	window.alert(stockCodeString);
}	

}



function removeCurrency(nStr)
{
//	window.alert(nStr);
	
	if(nStr.indexOf(".") > 0)
	{
//	window.alert(nStr);
	x = nStr.split('.');
	nStr=x[0];
	}
	return nStr;
}

function doCurrency(nStr)
{
	
	if(nStr.indexOf(".") == -1)
	{nStr +=".00";}
	else
	{
	x = nStr.split('.');
	strlength=x[1].length;
	if(strlength=="1"){nStr +="0";}
	}
	return nStr;
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

