function isBlank(val){
	if(val==null){
		return true;
	}
	for(var i=0;i<val.length;i++){
		if((val.charAt(i)!=' ')&&(val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r")){
			return false;
		}
	}
	return true;
}
	
function isDigit(num){
	if(num.length>1){
		return false;
	}
	var string="1234567890";
	if(string.indexOf(num)!=-1){
		return true;
	}
	return false;
}

function isInteger(val){
	if(isBlank(val)){
		return false;
	}
	for(var i=0;i<val.length;i++){
		if(!isDigit(val.charAt(i))){
			return false;
		}
	}
	return true;
}
	
function MakeSum() {
	// Initialize the total variable to 0
	var t = 0;
	var p = 0;
	var ts = 0;
	var qty_form_field;
	var item_price;
	var list_price;
	var twoplaces;
	
	for (var i = 1; i<=(document.myform.NumItems.value); i++){
		// Add the values
		// Note that if a value is not numeric,
	    // it is ignored without formally raising an error
		qty_form_field="document.myform.Qty"+i+".value";
		qty_form_field=(eval(qty_form_field));
	  	
		CalcCasePrice="document.myform.CalcCasePrice"+i+".value";
		CalcCasePrice=(eval(CalcCasePrice));
		CasePrice="document.myform.CasePrice"+i+".value";
		CasePrice=(eval(CasePrice));
		CalcCasePack="document.myform.CalcCasePack"+i+".value";
		CalcCasePack=(eval(CalcCasePack));
		CalcItemPrice="document.myform.CalcItemPrice"+i+".value";
		CalcItemPrice=(eval(CalcItemPrice));
		ItemPrice="document.myform.ItemPrice"+i+".value";
		ItemPrice=(eval(ItemPrice));
		//if (form_field.length>0 && form_field != null &&form_field)
		if (isInteger(qty_form_field)){
			t = t + parseInt(qty_form_field);
			if (parseInt(qty_form_field)>=parseInt(CalcCasePack)){
				item_price=CalcCasePrice;
				list_price=ItemPrice;
			}
			else{
				item_price=CalcItemPrice;
				list_price=ItemPrice;
			}
			twoplaces=(parseFloat(item_price)*parseInt(qty_form_field));
			twoplaces=twoplaces.toFixed(2);
			p = p + parseFloat(twoplaces);
			ts = ts + (parseFloat(list_price)*parseInt(qty_form_field) - parseFloat(twoplaces));
		}
		else if (qty_form_field != ""){
			var thisfield = eval("document.myform.Qty"+i+".style");
			thisfield.backgroundColor="#FF0000";
			alert ("Please adjust the highlighted quantity.\nQuantity must be a positive number.");
			error_flag=1;
		}
	}

	// Display the total in the CategoryTotal text box
	document.myform.QtyTotal.value = t;
	document.myform.RunningTotal.value = CommaFormatted(p.toFixed(2));
	document.myform.TotalSav.value = CommaFormatted(ts.toFixed(2));
}	
	
function QuantityCheck(){
	var error_flag
	error_flag=0;
	for (var i = 1; i<=(document.myform.NumItems.value); i++){
		form_field="document.myform.Qty"+i+".value";
   		form_field=(eval(form_field));
		QinC_field="document.myform.QinC"+i+".value";
		QinC_field=(eval(QinC_field));
		quant_field="document.myform.QtyAvail"+i+".value";
		quant_field=(eval(quant_field));
	 	 
	 	var thisfield = eval("document.myform.Qty"+i+".style");
		if (isInteger(form_field)){
			if ((parseInt(form_field)+parseInt(QinC_field)) > parseInt(quant_field)){
				thisfield.backgroundColor="#FF0000";
				alert ("Please adjust the highlighted quantity.\nQuantity in cart exceeds quantity available.");
				error_flag=1;
		 	}
			else{
			 	thisfield.backgroundColor="#FFFFFF";
			}
			thisfield.borderColor="#cccccc";
			thisfield.borderWidth="1px";
		}
		else if (form_field != ""){
			thisfield.backgroundColor="#FF0000";
			//alert ("Please adjust the highlighted quantity.\nQuantity must be a positive number.");
			error_flag=1;
		}
		if (form_field == ""){
			thisfield.backgroundColor="#FFFFFF";
		}
	}
	if (error_flag==1){
		document.myform.validCart.value="NO";
		return false;
	}
	else{
		document.myform.validCart.value="YES";
		return true;
	}
}
	
function checkForm(){
	var quantityCheck;
	quantityCheck=QuantityCheck();
	if (quantityCheck==true){
		for (var i = 1; i<=(document.myform.NumItems.value); i++){
			quant_field="document.myform.QtyAvail"+i+".value";
			quant_field=(eval(quant_field));
			//ModifyCartItem(quant_field)
		}
		//window.close(); 
		//window.opener.location.replace('express_2.cfm');
		document.myform.submit();
		return true;
	}
	else{
		
		return false;
	}
}
	
function CommaFormatted(amount)
{
	var delimiter = ",";
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	return amount;
}

function updateDisplayInfo(thisitem)
{
	// Initialize the total variable to 0
	var t = 0;
	var qty_form_field;
	var item_price;

	var i;
	i=thisitem;
		// Note that if a value is not numeric,
	    // it is ignored without formally raising an error
		qty_form_field="document.myform.Qty"+i+".value";
		qty_form_field=(eval(qty_form_field));
	  	
		CalcCasePrice="document.myform.CalcCasePrice"+i+".value";
		CalcCasePrice=(eval(CalcCasePrice));
		CalcCasePack="document.myform.CalcCasePack"+i+".value";
		CalcCasePack=(eval(CalcCasePack));
		CalcItemPrice="document.myform.CalcItemPrice"+i+".value";
		CalcItemPrice=(eval(CalcItemPrice));
		//if (form_field.length>0 && form_field != null &&form_field)
		if (isInteger(qty_form_field)){
			t = t + parseInt(qty_form_field);
			if (parseInt(qty_form_field)>=parseInt(CalcCasePack)){
				item_price=CalcCasePrice;
				
			}
			else{
				item_price=CalcItemPrice;
				
			}
			GetPaid=document.getElementById("PayingPrice"+i);
			GetPaid.value=formatCurrency(item_price);
			GetPaidQty=document.getElementById("PayingPriceQty"+i);
			GetPaidQty.value=formatCurrency(item_price*qty_form_field);
		}

}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num	 = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
		num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function CheckItemQuant2(thisFieldNum) {
	var this_qty_form_field;
	var this_item_id;
	var this_variant_id;
	this_qty_form_field="document.myform.Qty"+thisFieldNum+".value";
	this_qty_form_field=(eval(this_qty_form_field));
	this_item_id="document.myform.item_id"+thisFieldNum+".value";
	this_item_id=(eval(this_item_id));
	this_variant_id="document.myform.variant_id"+thisFieldNum+".value";
	this_variant_id=(eval(this_variant_id));
	quant_field="document.myform.QtyAvail"+thisFieldNum+".value";
	quant_field=(eval(quant_field));
	var qty_form_field;
	var item_id
	var variant_id;
	var running_quant=0;
	
	for (var i = 1; i<=(document.myform.NumItems.value); i++){
		item_id="document.myform.item_id"+i+".value";
		item_id=(eval(item_id));
		variant_id="document.myform.variant_id"+i+".value";
		variant_id=(eval(variant_id));
		
		//does tihs item share the same item id and variant id??
		if (item_id==this_item_id && variant_id==this_variant_id){
			//add to the running quanity total
			qty_form_field="document.myform.Qty"+i+".value";
			qty_form_field=(eval(qty_form_field));
			running_quant=parseInt(running_quant)+parseInt(qty_form_field);	
		}
	}
	//now check the running quantity against the alloted for this item
	//if the quantity test fails, hightlight the row that was passed to this function
	for (var i = 1; i<=(document.myform.NumItems.value); i++){
		var thisfield = eval("document.myform.Qty"+i+".style");
		//thisfield.backgroundColor="#FFFFFF";
	}
	var error_flag;
	error_flag=0;
	var thisfield = eval("document.myform.Qty"+thisFieldNum+".style");
	if (running_quant > quant_field){
		thisfield.backgroundColor="#FF0000";
		error_flag=1;
	} 
	else if (!isInteger(qty_form_field)){
		thisfield.backgroundColor="#FF0000";
	}
	else
	{
		thisfield.backgroundColor="#FFFFFF";
	}
	if (this_qty_form_field == ""){
		thisfield.backgroundColor="#FFFFFF";
	}
	return error_flag;	
}

function CheckItemQuantAll2() {
	var qty_form_field;
	var item_price;
	var error_flag;
	error_flag=0;
	for (var i = 1; i<=(document.myform.NumItems.value); i++){
		if (CheckItemQuant(i)==1){
			error_flag=1;
		}
	}
	if (error_flag==1){
		alert ("Please adjust the highlighted quantity.\nQuantity in cart exceeds quantity available.");
		return false;
	}
	else
		return true;
}

function CheckItemQuantAll(jQuantityDesired, jQuantityAvailable, jItemName,jelementName) {
	var error_flag;
	var errorItems;
	error_flag=0;
	errorItems = "";
	var keynum = 0
	var iteration=0
	var currentelement=""
	var currentstyle=""
	var elementname
	var elementinError = ""
	for (var key in jQuantityDesired) {
		elementname = "Qty" + jelementName[key];
		currentelement = document.getElementById(elementname);
		currentstyle =currentelement.style;
		currentstyle.backgroundColor="#FFFFFF";
		
		if (currentelement.value > jquantityAvailable[key]) {
			errorItems += jItemName[key] + ",\n";
			error_flag=1;
			currentstyle.backgroundColor="#FF0000";
		
			if (elementinError == "")
				elementinError = currentelement;
		}
	}
	if (error_flag==1){
		elementinError.focus();
		alert ("Please adjust the quantity of the following Item(s):\n\n"+ errorItems.substr(0, errorItems.length - 2) +"\n\nQuantity in cart exceeds quantity available.");
		return false;
	}
	else
		return true;
}

function unsavedChanges(){
	document.myform.unsaved_changes.value=1;
}

function savedChanges(){
	document.myform.unsaved_changes.value=0;
}


function checkUpdate(){
	var confirmed_text = confirm ("You have updated item quantites and have not selected 'Update Quantity'.\nChoose 'OK' to continue and discard these changes\nor 'Cancel' to go back and update the quantity.")
	return confirmed_text;
}
				
function finalCheck(){
	if (CheckItemQuantAll() == true){
		if (document.myform.unsaved_changes.value==1){
			if (checkUpdate() == true){
				return true;
			}
			else{
				return false;
			}
		}
		else{
			return true;
		}
	}
	else{
		return false;
	}
}