// Module: CartLogic.js
//
// All cart and order-related logic should be placed here and referenced in the appropriate file.


// AddToCart function
// this code populates the hidden input fields for the cart ready for checkout. 
// 
// "Add to Cart" form variable fields
//			
//		form action=https://www.linkpointcart.net/cgi-bin/cart.cgi
//		form input type=hidden name=additem
//
//		cartid|itemname|itemcost|itemquantity|itemno|itemhandling|itemshipmethod|itemweight|
//		itemexempt|itemdiscount|itemsilentpost|itemdropshipzip|itemESD|itemRecurring
//
//		01 - cartid
//		02 - itemname
//		03 - itemcost
// 		04 - itemquantity
// 		05 - itemno
//		06 - itemhandling
//		07 - itemshipmethod
//		08 - itemweight
//		09 - itemexempt
//		10 - itemdiscount
//		11 - itemsilentpost
//		12 - itemdropship
//		13 - itemESD
//		14 - itemRecurring
//
//		example  :  583872|name|cost|quantity|num|||||||||


function AddToCart(idesc, caller){
	//create the variables
	var icost;
	var itypedesc;
	var iquant;

	//get the values from the form
	icost=eval('document.cartform.'+caller+'Type.options[document.cartform.'+caller+'Type.selectedIndex].value;')
	itypedesc=eval('document.cartform.'+caller+'Type.options[document.cartform.'+caller+'Type.selectedIndex].text;')
	iquant=eval('document.cartform.'+caller+'Quantity.value;')

	//check that there is a type selected
	if(isEmpty(icost)){
		alert('Please select a product type for '+idesc+'.');
		return false;
	}

	//checkthat there is a quantity selected and it is valid
	if(isEmpty(iquant)){
		alert('Please select a quantity for '+idesc+' - '+itypedesc+'.');
		return false;
	}
	if(parseInt(iquant)<1 || parseInt(iquant)!=iquant){
		alert('You have entered an invalid quantity for '+idesc+' - '+itypedesc+'.  Your quantity must be a whole number.');
		return false;
	}

	//set the values to be passed to the cart
	document.cartform.additem.value = "583872|" + idesc + " - " + itypedesc + ".|VARcost|VARquantity";
	document.cartform.VARcost.value=icost;
	document.cartform.VARquantity.value=iquant;

	//submit the form
	document.cartform.submit();

}


// Utility function for AddToCart routine

function isEmpty(x){
	if(x.length==0) {
		return true;
	} 
	else {	
		return false;
	}
}

function validateCartForm()
{
	thisform = document.forms[0];
	types = 0;
	quantities = 0;
	thiselementsname = new String();

	// loop through all form elements

	for( i = 0; i < thisform.elements.length; i++ ) 
	{
		thiselementname = thisform.elements[i].name;

		if(thiselementname.substring(0, 7)  == "AddItem")		// if it's an item option
		{
			if(thisform.elements[thiselementname].selectedIndex > 0 && thisform.elements[thiselementname].selectedIndex != undefined) 
			{
				types++;
			}
		}		

		// count how many quantities have been inputed
	
		if(thiselementname.substring(0, 7)  == "VARQuan") 
		{
			if(thisform.elements[thiselementname].value != undefined && thisform.elements[thiselementname].value > 0) 
			{
				quantities++;
			}
		}
	}

	if(types > quantities)																// hmmm.  an item without a quantity.
	{
			alert(" Please specify a quantity for the item you selected. ");
	}
	else if(quantities > types)															// hmmm. we might have entered a quantity
	{																					// with no matching selection
			alert(" You forgot to select an option for the item you selected. ");
	} 
	else if( quantities == 0 && types == 0 )											// there is no selection, do not submit the form
	{
			alert(" Please select an item to add to your cart. ");
	} 
	else if(quantities == types && ((quantities > 0) && (types > 0)) )					// user has selected an item and it has a 
	{																					// matching quantities
			// alert(" Submitting the form now. ");
			thisform.submit();
	}
}
