
/******************************************************************************************************************************************************************************/
//	Author:	Greg Rechtin
//	Create:	04/21/2009
//	App:		Event Registration (http://www.toyotawarpsupplier.com/AAE/Event/)
//	includes standard form validation variables and functions for Event Reg app on TWS.com
/******************************************************************************************************************************************************************************/


/******************************************************************************************************************************************************************************/
/*					standard constants																					*/
/******************************************************************************************************************************************************************************/


/******************************************************************************************************************************************************************************/
/*					standard variables																					*/
/******************************************************************************************************************************************************************************/


/******************************************************************************************************************************************************************************/
/*					standard files																						*/
/******************************************************************************************************************************************************************************/

document.write('<SCRIPT LANGUAGE="JavaScript" SRC="../../include/js/gValidation.js"></SCRIPT>');									//	include standard files & functions


/******************************************************************************************************************************************************************************/
/*					standard functions																					*/
/******************************************************************************************************************************************************************************/
/*
function list()																											//	for easy documentation
{
	function validatePlant(theForm)																						//	validate entry on plant page
	function validateForm(theForm)																						//	validate entry on main page
	function validateExpend(theForm)																						//	validate entry on expenditure detail page
	function validateMBE(theForm)																						//	validate entry on MBE detail page
}
*/

function validatePlant(theForm)
{
	if(theForm.ddlISOStatus.selectedIndex == theForm.ddlISOStatus.length - 1)			//	not certified
	{
		if(!theForm.optPlanCert[0].checked && !theForm.optPlanCert[1].checked)
		{
			alert("Please select Yes or No for Plan to be Certified.");
			return false;
		}
		else if(theForm.optPlanCert[0].checked && theForm.txtPlanCertDate.value =="")
		{
			alert("Please click calendar to select a date for \"Certification Plan Date\".");
			theForm.txtPlanCertDate.focus();
			return false;
		}
		else if(theForm.optPlanCert[1].checked)
		{
			if(!verifyRequired(theForm.elements))
			{
				return false;
			}
//			if(!verifyChecked(theForm.elements, theForm.optWhyNot))
//			{
//				alert("Please select one of the \"" + theForm.optWhyNot[0].title + "\" options.");
//				return false;
//			}
			else if(theForm.txtExplainNoCert.value == "")
			{
				alert("Please provide explanation.");
				theForm.txtExplainNoCert.focus();
				return false;
			}
		}
	}
	else																	//	certified
	{
		if(!verifyRequired(theForm.elements))
		{
			return false;
		}
		else
		{
			var today	= new Date();

			if(!valiDate(theForm.txtOrigCertDate.value, today.toDateString()))
			{
				alert("Please select a date before today.");
				theForm.txtOrigCertDate.select();
				return false;
			}
			else if(theForm.txtLastCertDate.value == "")
			{
				var now	= new Date();
				var dtCert	= new Date(theForm.txtOrigCertDate.value);
				var dtExp	= new Date(theForm.txtExpireDate.value);
				dtCert.setFullYear(dtCert.getFullYear()+3);

				if((!valiDate(now.toLocaleDateString(), dtCert.toLocaleDateString()) && !valiDate(theForm.txtExpireDate.value, now.toLocaleDateString())) || dtCert.getFullYear() != dtExp.getFullYear())
				{
					alert("Certification is expected every three years. Please select date for Re-Certification Date or modify the Expiration Date.");
					theForm.txtLastCertDate.select();
					return false;
				}
				else if(!valiDate(theForm.txtOrigCertDate.value, theForm.txtExpireDate.value))
				{
					alert("Please select date greater than "+ theForm.txtOrigCertDate.title +".");
					theForm.txtExpireDate.select();
					return false;
				}
			}
			else
			{
				var dtCert	= new Date(theForm.txtLastCertDate.value);
				var dtExp	= new Date(theForm.txtExpireDate.value);
				dtCert.setFullYear(dtCert.getFullYear()+3);

				if(!valiDate(theForm.txtLastCertDate.value, today.toDateString()))
				{
					alert("Please select date before today.");
					theForm.txtLastCertDate.select();
					return false;
				}
				else if(!valiDate(theForm.txtOrigCertDate.value, theForm.txtLastCertDate.value))
				{
					alert("Please select date greater than "+ theForm.txtOrigCertDate.title +".");
					theForm.txtLastCertDate.select();
					return false;
				}
				else if(!valiDate(theForm.txtLastCertDate.value, theForm.txtExpireDate.value))
				{
					alert("Please select date greater than Re-Certification Date.");
					theForm.txtExpireDate.select();
					return false;
				}
				else if(dtCert.getFullYear() != dtExp.getFullYear())
				{
					alert("Certification is expected every three years. Please modify the Expiration Date.");
					theForm.txtExpireDate.select();
					return false;
				}
			}
		}
	}

	return true; //true
}


//-------------	may or may not use in future	-------------------//

function validateForm1(theForm)
{
	var correct	= true;
	var aryErrs;
	var errorMssg	= "";
	//	remove formatting from money fields for int conversion and compare
	var strDirMBE	= theForm.txtDirMBEPurch.value.replace(/,/g,"");
	var strIndMBE	= theForm.txtIndMBEPurch.value.replace(/,/g,"");
	var strDirNA	= theForm.txtDirect.value.replace(/,/g,"");
	var strIndNA	= theForm.txtInDirect.value.replace(/,/g,"");
	strDirMBE		= strDirMBE.replace(".00","");
	strIndMBE	= strIndMBE.replace(".00","");
	strDirNA		= strDirNA.replace(".00","");
	strIndNA		= strIndNA.replace(".00","");
	
	if(strDirNA == "" || parseInt(strDirNA) == 0)
	{
		correct	= false;
		errorMssg	+= "TEMA Direct Expenditures must be greater than 0.\n";
		theForm.txtDirect.select();
	}
	if(strIndNA == "" || parseInt(strIndNA) == 0)
	{
		correct	= false;
		errorMssg	+= "TEMA Indirect Expenditures must be greater than 0.\n";
		theForm.txtInDirect.select();
	}
	if(numValue(strDirNA) < numValue(strDirMBE))
	{
		correct	= false;
		errorMssg	+= "TEMA Direct Expenditures must be greater than MBE Direct Expenditures.\n";
		theForm.txtDirect.focus();
	}
	if(numValue(strIndNA) < numValue(strIndMBE))
	{
		correct	= false;
		errorMssg	+= "TEMA Indirect Expenditures must be greater than MBE Indirect Expenditures.\n";
		theForm.txtInDirect.focus();
	}

//	check all plants complete
	for(var i = 0; i < theForm.elements.length; i++)
		if(theForm.elements[i].name.substring(0,9) == "PlantCode" && theForm.elements[i].value != "")
		{
			correct	= false;
			aryErrs	= theForm.elements[i].value.split("&")
			errorMssg	+= "For plant " + theForm.elements[i].name.substring(9,13) + "-" + theForm.elements[i].name.substring(13,14) + ":\n";
			for(var j = 0; j < aryErrs.length && aryErrs[j] != ""; j++)
				errorMssg	+= "\t" + aryErrs[j] + ".\n";
		}

	if(!correct)
		alert(errorMssg + "\nYou may click 'Save and Close' and return later to continue.");

	return correct; 
}

function validExpend(frmField)
{
	trim(frmField);
	if(validAlphaNum(frmField,'n'))
	{
		frmField.value = numValue(frmField.value);
		return true;
	}
	else
		return false;
}

function validateExpend(theForm)
{
	if(theForm.ddlMBE.value != "0")
	{
		if(verifyRequired(theForm.elements))
			if(validAlphaNum(theForm.txtExpend,'n'))
				if(parseInt(theForm.txtExpend.value) == 0)
				{
					alert("Expenditures must be greater than 0.");
					theForm.txtExpend.focus();
				}
				else
					return true;
	}
	else
	{
		alert("Please select Minority Company.");
		theForm.ddlMBE.focus();
	}

	return false; 
}

function validateMBE(theForm)
{
//**	see note in T2MMBECompany.asp about DUNS = EIN	**//	
	var strEIN = theForm.txtDUNS.value.replace(/-/g,"");

	if(verifyRequired(theForm.elements))
	{
		if(theForm.txtDUNS.value == "" && theForm.txtCert.value =="")
		{
			alert("Please provide a value for either Tax ID or Certification ID.");
			theForm.txtDUNS.select();
		}
		else
		{
			if(theForm.txtDUNS.value != "")
			{
				if(strEIN.length == 9 && strEIN.match(/\d{9}/))
				{ 
					theForm.txtDUNS.value	= strEIN.substr(0,2) + "-" + strEIN.substr(2)
				}
				else
				{
					alert("Tax ID must be 9 digits.");
					theForm.txtDUNS.select();
					return false;
				}
			}
			if(theForm.txtCert.value != "")
				if(!validCertID(theForm.txtCert,theForm.ddlCouncil.value))
					return false;
			if(theForm.ddlMetMBE.selectedIndex == theForm.ddlMetMBE.length - 1 && theForm.txtOther.value == "")
			{
				alert("Please explain how you met this MBE.");
				theForm.txtOther.focus();
			}
			else
				return true;
		}
	}
	return false; 
}
