// JavaScript subroutines for PRODUCTS.PHP

// edit a quantity field for non-zero numeric value (returns true if valid)
function validQty(theQty) {
	var err=false;
	// treat empty, spaces or zero value as error (qty is required)	
	if ((theQty.length==0)||(parseInt(theQty)==0)) {
		err=true;
	} else {
		// acceptable integer value?
		if (isNaN(theQty)) {
			err=true;
		} else {
			// parse the field and convert to number
			if ((isNaN(parseInt(theQty)))||(parseInt(theQty)==0)) {
				err=true;
			}
		}
	}
	if (err) return false;
	return true;
}


// perform edits for the Springs Order Form (quantity)
function SorderEditQty(theElement) {
	var msgObj=findObjId('SorderErr_qty');
	var nosubmitObj=findObjId('SorderNoSubmit');
	var submitObj=findObjId('SorderSubmit');
	var theQty=theElement.value;
	if (!validQty(theQty)) {
		theElement.style.backgroundColor='#ff9999';
		if (nosubmitObj) nosubmitObj.style.display='none';
		if (submitObj) submitObj.style.display='none';
		if (msgObj) msgObj.style.display='block';
		return false;
	}
	theElement.style.backgroundColor='#ffffff';
	theElement.value = parseInt(theQty);
	if (msgObj) msgObj.style.display='none';
	checkSorderFields();
	return true;
}


// display price when spring is selected (force qty to 1 if null)
function updatePriceQty(theElement) {
	var theSpring=theElement.value;
	var theObj=findObjId('springPrice');
	theObj.innerHTML='&nbsp;';
	var idx=-1;
	for (i=0;i<spring_partslist.length;i++) { if (spring_partslist[i]==theSpring) idx=i; }
	if ((idx>-1)&&(theObj)) theObj.innerHTML=spring_pricelist[idx];
	theObj=findObjId('qty');
	if (theObj) { if (theObj.value.length==0) theObj.value='1'; }
	checkSorderFields();
	return;
}


// handle changes to checkboxes (Adapters or Bearings) to determine value of addon field
function handleAddonChg(theElement) {
	var theForm=findObjId('SorderForm');
	if (theElement.checked) {
		// uncheck the other checkbox if this element checked
		if ((theElement.name=='adapter')&&(theForm.bearing)) theForm.bearing.checked=false;
		if ((theElement.name=='bearing')&&(theForm.adapter)) theForm.adapter.checked=false;
	}
	var theInp=findObjId('addon');
	theInp.value='';
	if (theElement.checked) theInp.value=theElement.value;
}


// display appropriate Add-on checkboxes associated with the selected shock
function updateShockSelection(theElement) {
	theInp=findObjId('springAddon');
	theMsg=findObjId('springAddonMsg');
	if ((theInp)&&(theMsg)) {
		theMsg.style.display='none';
		theInp.style.display='none';
		theMsg.innerHTML='&nbsp;';
		theInp.innerHTML='<input type="text" name="addon" id="addon" size="5" tabindex="4" value="" />';
		var theShockCode=theElement[theElement.selectedIndex].value;
		var idx=-1;
		for (i=0;i<shock_partslist.length;i++) { if (shock_partslist[i]==theShockCode) idx=i; }
		if (idx>-1) {
			if ((shock_adapterlist[idx].length>0)&&(shock_bearinglist[idx].length>0)) {
				theMsg.innerHTML='This shock requires<br />Performance Adapters<br />or Bearings<br />to fit properly.';
				theMsg.style.display='block';
				theInp.innerHTML='<span style="font-weight:bold">Include:</span><br />&nbsp;&nbsp;<input type="checkbox" name="adapter" tabindex="4" value="' + shock_adapterlist[idx] + '" onclick="handleAddonChg(this);" /> Adapters';
				theInp.innerHTML=theInp.innerHTML+'<br />&nbsp;&nbsp;<input type="checkbox" name="bearing" tabindex="4" value="' + shock_bearinglist[idx] + '" checked onclick="handleAddonChg(this);" /> Bearings';
				theInp.innerHTML=theInp.innerHTML+'<br /><input type="hidden" size="5" name="addon" tabindex="6" value="' + shock_bearinglist[idx] + '" />';
				theInp.style.display='block';
			}
			if ((shock_adapterlist[idx].length>0)&&(shock_bearinglist[idx].length==0)) {
				theMsg.innerHTML='This shock requires a<br />Performance Adapter Kit<br />to fit properly.';
				theMsg.style.display='block';
				theInp.innerHTML='<span style="font-weight:bold">Include:</span><br />&nbsp;&nbsp;<input type="checkbox" name="adapter" tabindex="4" value="' + shock_adapterlist[idx] + '" checked onclick="handleAddonChg(this);" /> Adapters';
				theInp.innerHTML=theInp.innerHTML+'<br /><input type="hidden" size="5" name="addon" tabindex="6" value="' + shock_adapterlist[idx] + '" />';
				theInp.style.display='block';
			}
			if ((shock_adapterlist[idx].length==0)&&(shock_bearinglist[idx].length>0)) { 
				theMsg.innerHTML='This shock requires a<br />Bearings Kit<br />to fit properly.';
				theMsg.style.display='block';
				theInp.innerHTML='<span style="font-weight:bold">Include:</span><br />&nbsp;&nbsp;<input type="checkbox" name="bearing" tabindex="4" value="' + shock_bearinglist[idx] + '" checked onclick="handleAddonChg(this);" /> Bearings';
				theInp.innerHTML=theInp.innerHTML+'<br /><input type="hidden" size="5" name="addon" tabindex="6" value="' + shock_bearinglist[idx] + '" />';
				theInp.style.display='block';
			}
		}
	}
	checkSorderFields();
}


// check if all fields are complete for Springs Order
function checkSorderFields()	{
	var formObj=findObjId('SorderForm');
	var nosubmitObj=findObjId('SorderNoSubmit');
	var submitObj=findObjId('SorderSubmit');
	if ((formObj.qty.value.length>0)&&(!formObj.spring[0].selected)&&(!formObj.shock[0].selected)) {
		// form is complete
		formObj.spring[0].style.backgroundColor='transparent';
		formObj.shock[0].style.backgroundColor='transparent';
		if (nosubmitObj) nosubmitObj.style.display='none';
		if (submitObj) submitObj.style.display='block';
	} else {
		// form is incomplete
		if (formObj.spring[0].selected) formObj.spring[0].style.backgroundColor='#ff9999';
		if (!formObj.spring[0].selected) formObj.spring[0].style.backgroundColor='transparent';
		if (formObj.shock[0].selected) formObj.shock[0].style.backgroundColor='#ff9999';
		if (!formObj.shock[0].selected) formObj.shock[0].style.backgroundColor='transparent';
		if (submitObj) submitObj.style.display='none';
		if (nosubmitObj) nosubmitObj.style.display='block';
	}
}


// pre-process the Spring form submission (create a hidden text element containing the cartadd string)
function processSorderSubmit(theForm) {
//	theForm.cartadd.value = '#S=1';
//	if (theForm.addon.value.length>0) theForm.cartadd.value += '|A=1';
//	theForm.cartadd.value += '#';
//	theForm.cartadd.value += theForm.spring.value + '=' + theForm.qty.value + ':' + theForm.shock.value;
//	if (theForm.addon.value.length>0) theForm.cartadd.value += '|' + theForm.addon.value + '=' + theForm.qty.value;
	theForm.cartadd.value='#S=1#'+ theForm.spring.value + '=' + theForm.qty.value + ':' + theForm.shock.value;
	if (theForm.addon.value.length>0) theForm.cartadd.value += ',' + theForm.addon.value;
	theForm.submit();
	return false;
}


// perform edits for the Adapters or Gear Order Form (all quantity entries)
function orderEditQty() {
	var theElem, theQty;
	var err=false;
	var theForm=findObjId('orderForm');
	var msgObj=findObjId('orderErr_qty');
	var submitObj=findObjId('orderSubmit');
	if (theForm) {
		var partno=theForm.partlist.value.split('|');
		for (i=0;i<partno.length;i++) {
			eval("var theElem=theForm."+partno[i]+"qty;");
			theQty=theElem.value;
			if ((theQty.length==0)||(parseInt(theQty)==0)) {
				theElem.value='';
			} else {
				if (!validQty(theQty)) {
					err=true;
					theElem.style.backgroundColor='#ff9999';
				} else {
					theElem.style.backgroundColor='#ffffff';
					theElem.value=parseInt(theQty);
				}
			}
		}
	}
	if (err) {
		if (submitObj) submitObj.style.display='none';
		if (msgObj) msgObj.style.display='block';
		return false;
	}
	if (msgObj) msgObj.style.display='none';
	if (submitObj) submitObj.style.display='block';
	return true;
}


// pre-process the Adapter or Gear form submission (create a hidden text element containing the cartadd string)
function processOrderSubmit(theForm,theCat) {
	var theCnt=0;
	var addlist='';
	theForm.cartadd.value='';
	var partno=theForm.partlist.value.split('|');
	for (i=0;i<partno.length;i++) {
		eval("var theQty=theForm."+partno[i]+"qty.value;");
		if ((theQty.length>0)&&(parseInt(theQty)>0)) {
			if (theCnt==0) { addlist=partno[i]+'='+theQty; } else { addlist=addlist+'|'+partno[i]+'='+theQty; }
			theCnt=theCnt+1;
		}
	}
	if (theCnt>0) {
		theForm.cartadd.value='#'+theCat+'='+theCnt+'#'+addlist;
		theForm.submit();
	}
	return false;
}
