/*
	purpose:
		used in the catalog order review page
	page:
		/ordering/popup_order_form/order_review.cfm
	Parameters:
		theform - object reference to the form being passed
*/

function CheckCatalogOrderReview(theform)
{
	var the_id_list=theform.orderdetailidlist.value.split(",");
	var the_quantity_list=theform.orderdetailquantityrestriction.value.split(",");
	var the_product_name_list=theform.orderdetailproductnamelist.value.split(",");
	
	var temp_cutting_qty=0;
	var temp_tagging_qty=0;
	var temp_cutting_qty_restriction=0;
	var temp_id=0;
	var temp_product_name="";
	var the_remainder=0;
	
	
	for (var i=0;i<the_id_list.length; i++)
	{
		temp_id=parseInt(the_id_list[i]);
		temp_cutting_qty_restriction=parseInt(the_quantity_list[i]);
		
		temp_cutting_qty=parseInt(document.getElementById("quantity_cutting_"+temp_id).value);
		temp_tagging_qty=parseInt(document.getElementById("quantity_tag_"+temp_id).value);
		temp_product_name=the_product_name_list[i];
		
		if (!IsNumber(temp_cutting_qty))
		{
			alert("Please enter a number into the cutting quantity field for "+temp_product_name+".");
			document.getElementById("quantity_cutting_"+temp_id).focus();
			return false;
		}
		
		if (!IsNumber(temp_tagging_qty))
		{
			alert("Please enter a number into the tag quantity field for "+temp_product_name+".");
			document.getElementById("quantity_tag_"+temp_id).focus();
			return false;
		}
		
		//make sure tag is less then cutting
		if (temp_tagging_qty > temp_cutting_qty)
		{
			alert("Sorry, the tagging quantity needs to be equal to or small then the cutting quantity for "+temp_product_name+".");;
			document.getElementById("quantity_tag_"+temp_id).focus();
			return false;
		}
		
		//make sure both values are greater then zero
		if ((temp_cutting_qty <= 0) && (temp_tagging_qty <= 0))
		{
			alert("Please enter a quantity greater then zero for cutting and tagging for "+temp_product_name+".");;
			document.getElementById("quantity_cutting_"+temp_id).focus();
			return false;
		}
				
		//check quantities
		if (temp_cutting_qty_restriction > 0)
		{
			the_remainder=temp_cutting_qty%temp_cutting_qty_restriction;
			
			if (the_remainder != 0)
			{
				alert("Sorry, "+temp_product_name+" must be ordered in quantities of "+temp_cutting_qty_restriction+".");
				document.getElementById("quantity_cutting_"+temp_id).focus();
				return false;
			}
		}
	}
	
	return true;
}


/*
	purpose:
		used in the catalog ordering page
	page:
		/ordering/popup_order_form/order_review.cfm
	Parameters:
		theform - object reference to the form being passed
*/

function CheckCatalogOrderForm(theform)
{
	var item_checked=false;

	if (!IsWordNumberSpecial(theform.firstname.value))
	{
		alert("Please enter your first name.");
		theform.firstname.focus();
		return false;
	}
	
	if (!IsWordNumberSpecial(theform.lastname.value))
	{
		alert("Please enter your last name.");
		theform.lastname.focus();
		return false;
	}
	
	if (!IsWordNumberSpecial(theform.company.value))
	{
		alert("Please enter your company name.");
		theform.company.focus();
		return false;
	}
	
	if (!IsWordNumberSpecial(theform.address1.value))
	{
		alert("Please enter your address.");
		theform.address1.focus();
		return false;
	}
	
	if (theform.address2.value.length > 0)
	{
		if (!IsWordNumberSpecial(theform.address2.value))
		{
			alert("Please enter your address 2 value.");
			theform.address2.focus();
			return false;
		}
	}
	
	if (!IsWordNumberSpecial(theform.city.value))
	{
		alert("Please enter your city.");
		theform.city.focus();
		return false;
	}
	
	if (theform.stateid[theform.stateid.selectedIndex].value == "")
	{
		alert("Please select a valid state/province.");
		theform.stateid.focus();
		return false;
	}
	
	if (theform.countryid[theform.countryid.selectedIndex].value == "")
	{
		alert("Please select a valid country.");
		theform.countryid.focus();
		return false;
	}
	
	if (!IsWordNumberSpecial(theform.postalcode.value))
	{
		alert("Please enter your postal/zip code.");
		theform.postalcode.focus();
		return false;
	}

	if (!IsPhone(theform.phone.value))
	{
		alert("Please enter a valid phone number. I.e. xxx-xxx-xxxx");
		theform.phone.focus();
		return false;
	}
	
	if (theform.phoneextension.value.length > 0)
	{
		if (!IsPhoneExtension(theform.phoneextension.value))
		{
			alert("Please enter a valid extension. i.e. 222");
			theform.phoneextension.focus();
			return false;
		}
	}

	if (theform.fax.value.length > 0)
	{
		if (!IsPhone(theform.fax.value))
		{
			alert("Please enter a valid fax number.");
			theform.fax.focus();
			return false;
		}
	}
	
	if (!IsEmail(theform.emailaddress.value))
	{
		alert("Please enter a valid email address.");
		theform.emailaddress.focus();
		return false;
	}
	
	if (!IsEmail(theform.confirmemailaddress.value))
	{
		alert("Please enter a valid confirmation email address.");
		theform.confirmemailaddress.focus();
		return false;
	}
	
	if (theform.emailaddress.value != theform.confirmemailaddress.value)
	{
		alert("Your email address and confirmation email address do not match.");
		theform.confirmemailaddress.focus();
		return false;
	}
	
	if (!CheckDate(theform,'shippingdate','Shipping Date'))
		return false;

	return true;
}

/*
	purpose:
		used in the contact page
	page:
		/contactus/index.cfm
	Parameters:
		theform - object reference to the form being passed
*/

function CheckContactForm(theform)
{
	var item_checked=false;

	if (!IsWordNumberSpecial(theform.contactfirstname.value))
	{
		alert("Please enter your first name.");
		theform.contactfirstname.focus();
		return false;
	}
	
	if (!IsWordNumberSpecial(theform.contactlastname.value))
	{
		alert("Please enter your last name.");
		theform.contactlastname.focus();
		return false;
	}
	
	if (!IsWordNumberSpecial(theform.contactaddress.value))
	{
		alert("Please enter your address.");
		theform.contactaddress.focus();
		return false;
	}
	
	if (!IsWordNumberSpecial(theform.contactcity.value))
	{
		alert("Please enter your city.");
		theform.contactcity.focus();
		return false;
	}
	
	if (theform.stateid[theform.stateid.selectedIndex].value == "")
	{
		alert("Please select a valid state/province.");
		theform.stateid.focus();
		return false;
	}
	
	if (theform.countryid[theform.countryid.selectedIndex].value == "")
	{
		alert("Please select a valid country.");
		theform.countryid.focus();
		return false;
	}
	
	if (!IsWordNumberSpecial(theform.contactpostal.value))
	{
		alert("Please enter your postal/zip code.");
		theform.contactpostal.focus();
		return false;
	}

	if (!IsPhone(theform.contactphone.value))
	{
		alert("Please enter a valid phone number. I.e. xxx-xxx-xxxx");
		theform.contactphone.focus();
		return false;
	}
	
	
	if (theform.contactphoneextension.value.length > 0)
	{
		if (!IsPhoneExtension(theform.contactphoneextension.value))
		{
			alert("Please enter a valid extension. i.e. 222");
			theform.contactphoneextension.focus();
			return false;
		}
	}

	if (theform.contactfax.value.length > 0)
	{
		if (!IsPhone(theform.contactfax.value))
		{
			alert("Please enter a valid fax number.");
			theform.contactfax.focus();
			return false;
		}
	}
	
	if (!IsEmail(theform.contactemail.value))
	{
		alert("Please enter a valid email address.");
		theform.contactemail.focus();
		return false;
	}
	
	if (!IsEmail(theform.contactconfirmemail.value))
	{
		alert("Please enter a valid confirmation email address.");
		theform.contactconfirmemail.focus();
		return false;
	}
	
	if (theform.contactemail.value != theform.contactconfirmemail.value)
	{
		alert("Your email address and confirmation email address do not match.");
		theform.contactconfirmemail.focus();
		return false;
	}

	return true;
}


/*
	purpose:
		used in the product catalog add to cart page
	page:
		/ordering/popup_order_form/index.cfm
	Parameters:
		cuttingfield - this is the name of the quantity cutting field
		tagfield - this is the name of the tag cutting field
		quantities - this is the number that tells us the quantities this product must be ordered in 
*/

function CheckCuttingTagIsNumber(cuttingfield,tagfield,quantities)
{
	var the_result=0;
	var the_cutting_field=parseInt(cuttingfield.value);
	var the_tagging_field=parseInt(tagfield.value);
	
	if (!IsNumber(the_cutting_field))
	{
		alert("Please enter a number into the cutting quantity field.");
		cuttingfield.focus();
		return false;
	}
	
	if (!IsNumber(the_tagging_field))
	{
		alert("Please enter a number into the tag quantity field.");
		tagfield.focus();
		return false;
	}
	
	//make sure tag is less then cutting
	if (the_tagging_field > the_cutting_field)
	{
		alert("Sorry, the tagging quantity needs to be equal to or small then the cutting quantity.");
		tagfield.focus();
		return false;
	}
	
	//make sure both values are greater then zero
	if ((the_cutting_field <= 0) && (the_tagging_field <= 0))
	{
		alert("Please enter a quantity greater then zero for cutting and tagging.");
		cuttingfield.focus();
		return false;
	}
	
	
	//check quantities
	if (quantities > 0)
	{
		the_remainder=the_cutting_field%quantities;
		
		if (the_remainder != 0)
		{
			alert("Sorry, this product must be ordered in quantities of "+quantities+".");
			cuttingfield.focus();
			return false;
		}
	}
	
	return true;
}



/*
	purpose:
		used in the control panel
	page:
		all /module_setorder.cfm pages
	Parameters:
		theform - object reference to the form being passed
*/
function CheckDisplayOrderForm(theform)
{
	if (theform.orderlist.value <= 0)
	{
		alert("The order has not changed, no update required.");
		return false;
	}

	return true;
}




/*
	purpose:
		used in the control panel
	page:
		/admin/group/module_edit.cfm
	Parameters:
		theform - object reference to the form being passed
*/

function CheckGroupForm(theform)
{
	var test=false;
	
	if (!IsWordSpace(theform.groupinfoname.value))
	{
		alert("Please enter a valid group name.");
		theform.groupinfoname.focus();
		return false;
	}

	if (theform.groupinfodescription.value.length < 4)
	{
		alert("Please enter a valid group description.");
		theform.groupinfodescription.focus();
		return false;
	}
						
	return true;
}


/*
	purpose:
		used in the control panel
	page:
		main control panel login page
	Parameters:
		theform - object reference to the form being passed
*/
function CheckLoginForm(theform)
{

	if (!IsUsername(theform.username.value))
	{
		alert("Please enter a valid username. (MIN. 5 characters)");
		theform.username.focus();
		return false;
	}

	if (!IsPassword(theform.password.value))
	{
		alert("Please enter a valid password. (MIN. 5 characters)");
		theform.password.focus();
		return false;
	}

	return true;
}





/*
	purpose:
		used in the control panel
	page:
		/admin/product/
	Parameters:
		theform - object reference to the form being passed
*/

function CheckProductForm(theform)
{
	var IsSurplusProduct=0;
	var thumbnail_value=null;
	var image_value=null;
	
	if (!IsWordNumberSpecial(theform.productname.value))
	{
		alert("Please enter the product name.");
		theform.productname.focus();
		return false;
	}
	
	if (theform.productsubname.value.length > 0)
	{
		if (!IsWordNumberSpecial(theform.productsubname.value))
		{
			alert("Please enter a valid product sub name.");
			theform.productsubname.focus();
			return false;
		}
		
	}
	
	if (!IsNumber(theform.productquantity.value))
	{
		alert("Please enter a valid product quantity.");
		theform.productquantity.focus();
		return false;
	}
	
	if (!IsNumber(theform.productminimumorderquantity.value))
	{
		alert("Please enter the minimum order quantity.");
		theform.productminimumorderquantity.focus();
		return false;
	}
	
	if (!IsNumber(theform.productquantitybox.value))
	{
		alert("Please enter the quantity per box.");
		theform.productquantitybox.focus();
		return false;
	}
	
	/*
	//check if this is a surplus product
	for(var i=0; i < theform.issurplus.length; i++)
	{
		if(theform.issurplus[i].checked)
		{
			IsSurplusProduct=theform.issurplus[i].value;
		}
	}
			
	
	//if it is a surplus product, validate the quantity and date
	if (IsSurplusProduct == 1)
	{
		if (!IsNumber(theform.surplusproductquantity.value))
		{
			alert("Please enter the surplus quantity.");
			theform.surplusproductquantity.focus();
			return false;
		}
		
		if (!CheckDate(theform,'surplusproductexpirydate','Surplus Order Expiration Date'))
			return false;
		
	}
	*/

	//if we are supposed to upload an thumbnail
	if (theform.uploadthumbnail.value == 1)
	{
		thumbnail_value=theform.productthumbnail_file.value;
		
		if (!IsImageExtension(thumbnail_value.substring(thumbnail_value.lastIndexOf("."),thumbnail_value.length)))
		{
			alert("Please select an thumbnail file for uploading (must be .jpg or .gif).");
			theform.productthumbnail_file.focus();
			return false;
		}
	}
						
	//if we are supposed to upload an image
	if (theform.uploadimage.value == 1)
	{
	 	image_value=theform.productimage_file.value;
		
		if (!IsImageExtension(image_value.substring(image_value.lastIndexOf("."),image_value.length)))
		{
			alert("Please select an image file for uploading (must be .jpg or .gif).");
			theform.productimage_file.focus();
			return false;
		}
	}
			
			
	return true;
}

/*
	purpose:
		used in the catalog order review page
	page:
		/ordering/popup_order_form/order_review.cfm
	Parameters:
		theform - object reference to the form being passed
*/

function CheckSurplusProductIsNumber(thefield)
{
	var the_value=thefield.value;
	var the_new_value="";
	
	//loop over all the characters typed into the box
	for (var i=0;i<the_value.length; i++)
	{
		
		if (IsNumber(the_value.charAt(i)))
			the_new_value+=the_value.charAt(i);
		
		//alert(the_new_value);
	}

	// if the new value is blank
	if (the_new_value == "")
		the_new_value=0;
		
	thefield.value=the_new_value;

	return false;
}


/*
	purpose:
		used in the surplus ordering page
	page:
		/surplus/surplus_review.cfm
	Parameters:
		theform - object reference to the form being passed
*/

function CheckSurplusOrderForm(theform)
{
	var item_checked=false;

	if (!IsWordNumberSpecial(theform.firstname.value))
	{
		alert("Please enter your first name.");
		theform.firstname.focus();
		return false;
	}
	
	if (!IsWordNumberSpecial(theform.lastname.value))
	{
		alert("Please enter your last name.");
		theform.lastname.focus();
		return false;
	}
	
	if (!IsWordNumberSpecial(theform.company.value))
	{
		alert("Please enter your company name.");
		theform.company.focus();
		return false;
	}
	
	if (!IsWordNumberSpecial(theform.address1.value))
	{
		alert("Please enter your address.");
		theform.address1.focus();
		return false;
	}
	
	if (theform.address2.value.length > 0)
	{
		if (!IsWordNumberSpecial(theform.address2.value))
		{
			alert("Please enter your address 2 value.");
			theform.address2.focus();
			return false;
		}
	}
	
	if (!IsWordNumberSpecial(theform.city.value))
	{
		alert("Please enter your city.");
		theform.city.focus();
		return false;
	}
	
	if (theform.stateid[theform.stateid.selectedIndex].value == "")
	{
		alert("Please select a valid state/province.");
		theform.stateid.focus();
		return false;
	}
	
	if (theform.countryid[theform.countryid.selectedIndex].value == "")
	{
		alert("Please select a valid country.");
		theform.countryid.focus();
		return false;
	}
	
	if (!IsWordNumberSpecial(theform.postalcode.value))
	{
		alert("Please enter your postal/zip code.");
		theform.postalcode.focus();
		return false;
	}

	if (!IsPhone(theform.phone.value))
	{
		alert("Please enter a valid phone number. I.e. xxx-xxx-xxxx");
		theform.phone.focus();
		return false;
	}
	
	if (theform.phoneextension.value.length > 0)
	{
		if (!IsPhoneExtension(theform.phoneextension.value))
		{
			alert("Please enter a valid extension. i.e. 222");
			theform.phoneextension.focus();
			return false;
		}
	}

	if (theform.fax.value.length > 0)
	{
		if (!IsPhone(theform.fax.value))
		{
			alert("Please enter a valid fax number.");
			theform.fax.focus();
			return false;
		}
	}
	
	if (!IsEmail(theform.emailaddress.value))
	{
		alert("Please enter a valid email address.");
		theform.emailaddress.focus();
		return false;
	}
	
	if (!IsEmail(theform.confirmemailaddress.value))
	{
		alert("Please enter a valid confirmation email address.");
		theform.confirmemailaddress.focus();
		return false;
	}
	
	if (theform.emailaddress.value != theform.confirmemailaddress.value)
	{
		alert("Your email address and confirmation email address do not match.");
		theform.confirmemailaddress.focus();
		return false;
	}
	
	if (!CheckDate(theform,'shippingdate','Shipping Date'))
		return false;

	return true;
}

/*
	purpose:
		used in the control panel
	page:
		/admin/user/module_edit.cfm
	Parameters:
		theform - object reference to the form being passed
*/

function CheckUserForm(theform)
{
	
	if (!IsWordSpace(theform.userinfofirstname.value))
	{
		alert("Please enter a valid first name.");
		theform.userinfofirstname.focus();
		return false;
	}
	
	if (!IsWordSpace(theform.userinfolastname.value))
	{
		alert("Please enter a valid last name.");
		theform.userinfolastname.focus();
		return false;
	}

	if (!IsUsername(theform.userinfousername.value))
	{
		alert("Please enter a valid username.");
		theform.userinfousername.focus();
		return false;
	}

	if ((theform.updatepassword.checked == 1) || (theform.submit_type.value == "add"))
	{
		if (!IsPassword(theform.userinfopassword.value))
		{
			alert("Please enter a valid password.");
			theform.userinfopassword.focus();
			return false;
		}
		
		if (!IsPassword(theform.userinfopasswordconfirm.value))
		{
			alert("Please enter a valid password confirmation password.");
			theform.userinfopasswordconfirm.focus();
			return false;
		}
	 	
		if (theform.userinfopassword.value != theform.userinfopasswordconfirm.value)
		{
		 	alert ("You Password and Confirm Password do not match");
			theform.userinfopasswordconfirm.focus();
			return false;
		}
	}

	if (!IsWordSpace(theform.userinfojobtitle.value))
	{
		alert("Please enter a valid job title.");
		theform.userinfojobtitle.focus();
		return false;
	}
	
	if (!IsEmail(theform.userinfoemailaddress.value))
	{
		alert("Please enter a valid email address.");
		theform.userinfoemailaddress.focus();
		return false;
	}

	if (theform.groupinfoid.selectedIndex == -1)
	{
		alert("Please select at least one group.");
		theform.groupinfoid[0].focus();
		return false;
	}

	return true;
}


/*
	purpose:
		used in the cart page
	page:
		/ordering/popup_order_form/index.cfm
		/ordering/popup_order_form/order_review.cfm
	Parameters:
		theorderdetailid - surplusorderdetail id
		thepage - step in the checkout process hte person came from
		pagenumber - what page are we reviewing in the ordering section (products are broken into pages)
		isrooted - rooted or unrooted products
*/
	
function RemoveCatalogItem(theorderdetailid,thepage,thepagenumber,isrooted)
{
	var delete_item=true;
	
	delete_item=confirm("Do you really want to remove this item from your order?");
	
	if (delete_item)
		window.location.href="/form_action/cf_remove_catalog_order_item.cfm?requesttimeout=5000&recordid="+theorderdetailid+"&from="+thepage+"&pagenumber="+thepagenumber+"&isrooted="+isrooted;
}


/*
	purpose:
		used in the cart page
	page:
		/surplus/surplus_review.cfm
	Parameters:
		theorderdetailid - surplusorderdetail id
		thepage - step in the checkout process hte person came from
*/
	
function RemoveSurplusItem(theorderdetailid,thepage)
{
	var delete_item=true;
	
	delete_item=confirm("Do you really want to remove this item from your order?");
	
	if (delete_item)
		window.location.href="/form_action/cf_remove_surplus_order_item.cfm?requesttimeout=5000&recordid="+theorderdetailid+"&from="+thepage;
}