// JavaScript Document
// Products Javascript
// NX 11/10/2005
//


function NX_qtyChange(fid){
	var theform, p, q, inc, u;
	//alert("NxqtyChange form="+document.forms["prod"+fid].name);
	theform="prod"+fid;
	stdiv="subtotaldiv"+fid;
	updiv="upricediv"+fid;
	//alert("the div="+stdiv);
	if (document.forms[theform].elements["qty"].value == 0){
		// set price and subtotal to 0 - leave size as is
		//document.forms[theform].elements["price"].selectedIndex = 0;
		document.forms[theform].elements["subtotal"].value = "0";
		document.getElementById(stdiv).innerHTML="0";
		document.forms[theform].elements["uprice"].value = "0";
		document.getElementById(updiv).innerHTML="0";
	} else {
		// recaclulate subtotal and set price to correct value
		if(document.forms[theform].elements["price"].selectedIndex > 0){
			document.forms[theform].elements["price"].selectedIndex = document.forms[theform].elements["size"].selectedIndex;
			if (document.forms[theform].elements["size"].value != 0){
				//t = document.forms[theform].elements["qty"].value * document.forms[theform].elements["price"].value;
				inc=NX_calcOptAdd(fid);
				q=parseInt(document.forms[theform].elements["qty"].value);
				p=parseInt(document.forms[theform].elements["price"].value);
				u=(p+inc)/100;
				document.forms[theform].elements["uprice"].value=u.toFixed(2);
				document.getElementById(updiv).innerHTML=u.toFixed(2);
				t = q* (p+inc);
				t = t/100;
				document.forms[theform].elements["subtotal"].value = t.toFixed(2);
				document.getElementById(stdiv).innerHTML = t.toFixed(2);

			}
		}
	}
	
}

function NX_onResetClick(fid){
	stdiv="subtotaldiv"+fid;
	updiv="upricediv"+fid;
	document.getElementById(stdiv).innerHTML="0";
	document.getElementById(updiv).innerHTML="0";
	return true;
}


function NX_initFormPrice(f, sub){
	for (var i=0; i< f.elements.length; i++){
		if (f.elements[i].name == "size"){
			f.elements["qty"].selectedIndex = sub;
			f.elements[i].selectedIndex = sub;
			fid = f.name.substring(4,f.name.length);
			sv=f.elements[i].value;
			if (sv.length > 0){
				NX_sizeChange(fid);
			}
		}
	}
}


function NX_sizeChange(fid){
	var t, theform, p, q, inc, u;
	//alert("NXsizeChange form="+document.forms["prod"+fid].name);
	theform="prod"+fid;
	stdiv="subtotaldiv"+fid;
	updiv="upricediv"+fid;
	if (document.forms[theform].elements["size"].selectedIndex == 0){
		// set price and subtotal to 0 - leave qty as is
		document.forms[theform].elements["price"].selectedIndex = 0;
		document.forms[theform].elements["subtotal"].value = "0";
		document.getElementById(stdiv).innerHTML="0";
		document.forms[theform].elements["uprice"].value = "0";
		document.getElementById(updiv).innerHTML="0";
	} else {
		// recaclulate subtotal and set price to correct value
			document.forms[theform].elements["price"].selectedIndex = document.forms[theform].elements["size"].selectedIndex;
		 	if (document.forms[theform].elements["qty"].value != "0"){
		 		//t = document.forms[theform].elements["qty"].value * (document.forms[theform].elements["price"].value+;
				inc=NX_calcOptAdd(fid);
				q=parseInt(document.forms[theform].elements["qty"].value);
				p=parseInt(document.forms[theform].elements["price"].value);
				u=(p+inc)/100;
				document.forms[theform].elements["uprice"].value=u.toFixed(2);
				document.getElementById(updiv).innerHTML=u.toFixed(2);
				t = q* (p+inc);
				t = t/100;
				document.forms[theform].elements["subtotal"].value = t.toFixed(2);
				document.getElementById(stdiv).innerHTML = t.toFixed(2);
			}
	} 
}

function NX_optChanged(popt, fid){
	var theform, theopt,t, inc, p, q, u;
	theform="prod"+fid;
	theopt="opt-"+popt;
	stdiv="subtotaldiv"+fid;
	updiv="upricediv"+fid;
	if(document.forms[theform].elements["subtotal"].value=="0" || document.forms[theform].elements["subtotal"].value=="0.00") {
		return; // no prices to change
	} else {
		// if we are blank adjust the item price
		inc=NX_calcOptAdd(fid);
		q=parseInt(document.forms[theform].elements["qty"].value);
		p=parseInt(document.forms[theform].elements["price"].value);
		u=(p+inc)/100;
		document.forms[theform].elements["uprice"].value=u.toFixed(2);
		//document.getElementById(stdiv).innerHTML = t.toFixed(2);
		t = q* (p+inc);
		t = t/100;
		document.forms[theform].elements["subtotal"].value = t.toFixed(2);
		document.getElementById(stdiv).innerHTML = t.toFixed(2);
	}
}

function NX_calcOptAdd(fid){
	var i, theform, addon, lb, rb, icost, op;
	theform="prod"+fid;
	addon=0;
	for(i=0;i<document.forms[theform].elements.length;i++){
		// filter for opt items only
		if(document.forms[theform].elements[i].name.substring(0,3)=="opt"){
			if((document.forms[theform].elements[i].value!="") && (document.forms[theform].elements[i].value.indexOf("[")>-1)){
				lb=document.forms[theform].elements[i].value.indexOf("[");																		   
				rb=document.forms[theform].elements[i].value.indexOf("]");
				icost=document.forms[theform].elements[i].value.substring(lb+2,rb);
				op=document.forms[theform].elements[i].value.indexOf("+");
				if(op>-1) addon+=1*icost;
				else addon-=1*icost;
			}
		}
	}
	return addon;
}

function NX_validateProduct(fid){
	var theform, inc,p,q,t,n;
	var oid, oname, oval, oprice;
	
	theform="prod"+fid;
	if(document.forms[theform].elements["qty"].selectedIndex==0) {
		alert("Error: You must select a quantity.");
		return false;
	}
	if(document.forms[theform].elements["size"].selectedIndex==0) {
		alert("Error: You must select a package size.");
		return false;
	}
	inc=NX_calcOptAdd(fid);
	q=parseInt(document.forms[theform].elements["qty"].value);
	p=parseInt(document.forms[theform].elements["price"].value);
	t = q* (p+inc);
	document.forms[theform].elements["stotal"].value=t;
	document.forms[theform].elements["unitprice"].value=p+inc;
	// get the options
	t="";
	for(i=0;i<document.forms[theform].elements.length;i++){
		oid=""; oname=""; oval=""; oprice="";
		// filter for opt items only
		if(document.forms[theform].elements[i].name.substring(0,4)=="opt-"){
			if(document.forms[theform].elements[i].selectedIndex>0){
				n=document.forms[theform].elements[i].name;
				// optid
				oid=n.substring(4);
				oval=document.forms[theform].elements[i].value;
				if(document.forms[theform].elements[i].value.indexOf("[")>-1){
					lb=document.forms[theform].elements[i].value.indexOf("[");																		   
					rb=document.forms[theform].elements[i].value.indexOf("]");
					oprice=document.forms[theform].elements[i].value.substring(lb+1,rb);
				}
				oname=document.forms[theform].elements["oname-"+oid].value;
			}
		} // end if opt-
		if(oid!="" && oname!="" && oval!=""){
			if(t!="") {
				t=t+"#"+oid+"~"+oname+"~"+oval+"~"+oprice;
			} else {
				t=t+oid+"~"+oname+"~"+oval+"~"+oprice;
			}
		}
	} // end for
	document.forms[theform].elements["poptions"].value=t;
	document.forms[theform].elements["iprice"].value=document.forms[theform].elements["price"].value;
	var x = document.forms[theform].elements["price"].value;
	document.forms[theform].elements["iprice"].value=x;

	return true;
	
	
}

