/*
====================================================================
validateForm(Form) : Returns boolean value, true if no errors found.
====================================================================
*/
function validateForm(f) 
/*
   PURPOSE: Check the required fields in the form.
   IN: f - the current form we want to validate
*/
{
	var date					= Trim(f.date.value);
	var fedID					= Trim(f.fedID.value);
	var dunNumber				= Trim(f.dunNumber.value);

	// 1. Company Information
	var companyName				= Trim(f.companyName.value);
	var dbaCompanyName			= Trim(f.dbaCompanyName.value);
	var ownerName				= Trim(f.ownerName.value);
	var companyContact			= Trim(f.companyContact.value);
	var companyContactTitle		= Trim(f.companyContactTitle.value);
	var contactNPA				= Trim(f.contactNPA.value);
	var contactNXX				= Trim(f.contactNXX.value);
	var contactLine				= Trim(f.contactLine.value);
	var contactFaxNPA			= Trim(f.contactFaxNPA.value);
	var contactFaxNXX			= Trim(f.contactFaxNXX.value);
	var contactFaxLine			= Trim(f.contactFaxLine.value);
	var address1				= Trim(f.address1.value);
	var address2				= Trim(f.address2.value);
	var city					= Trim(f.city.value);
	var state					= Trim(f.state.value);
	var zip						= Trim(f.zip.value);
	var email					= Trim(f.email.value);
	var webURL					= Trim(f.webURL.value);

	// 2. Sales Representative Business Classification Information
	var gender					= Trim(f.gender.value);
	var ethnicClass				= Trim(f.ethnicClass.value);
	var otherClass				= Trim(f.otherClass.value);
	var category				= Trim(f.category.value);
	var businessType			= Trim(f.businessType.value);
	var otherBusinessType		= Trim(f.otherBusinessType.value);
	var certificate				= Trim(f.certificate.value);
	var certifyingAgencyName	= Trim(f.certifyingAgencyName.value);
	var certificateType			= Trim(f.certificateType.value);
	var certificateNumber		= Trim(f.certificateNumber.value);
	var certificateExp			= Trim(f.certificateExp.value);

	// 3. Financial Information
	var revenue1				= Trim(f.revenue1.value);
	var revenue2				= Trim(f.revenue2.value);
	var revenue3				= Trim(f.revenue3.value);
	var legalStructure			= Trim(f.legalStructure.value);
	var numberEmployees			= Trim(f.numberEmployees.value);
	var areaCoverage			= Trim(f.areaCoverage.value);
	var yearsInBusiness			= Trim(f.yearsInBusiness.value);

	// 4. Product and Service Offerings
	var productOffering			= Trim(f.productOffering.value);
	var primaryNAICS			= Trim(f.primaryNAICS.value);
	var primarySIC				= Trim(f.primarySIC.value);
	var secondaryProductOffering= Trim(f.secondaryProductOffering.value);
	var secondaryNAICS			= Trim(f.secondaryNAICS.value);
	var secondarySIC			= Trim(f.secondarySIC.value);
	var otherProductOffering	= Trim(f.otherProductOffering.value);
	var tertiaryNAICS			= Trim(f.tertiaryNAICS.value);
	var tertiarySIC				= Trim(f.tertiarySIC.value);
	var otherNAICSorSIC			= Trim(f.otherNAICSorSIC.value);

	// 5. Three Business References
	var busRef1					= Trim(f.busRef1.value);
	var busRef1Name				= Trim(f.busRef1Name.value);
	var busRef1NPA				= Trim(f.busRef1NPA.value);
	var busRef1NXX				= Trim(f.busRef1NXX.value);
	var busRef1Line				= Trim(f.busRef1Line.value);
	var busRef2					= Trim(f.busRef2.value);
	var busRef2Name				= Trim(f.busRef2Name.value);
	var busRef2NPA				= Trim(f.busRef2NPA.value);
	var busRef2NXX				= Trim(f.busRef2NXX.value);
	var busRef2Line				= Trim(f.busRef2Line.value);
	var busRef3					= Trim(f.busRef3.value);
	var busRef3Name				= Trim(f.busRef3Name.value);
	var busRef3NPA				= Trim(f.busRef3NPA.value);
	var busRef3NXX				= Trim(f.busRef3NXX.value);
	var busRef3Line				= Trim(f.busRef3Line.value);
	var projectDescription		= Trim(f.projectDescription.value);

	// 6. Additional Information and Comments
	var ContactName		        = Trim(f.ContactName.value);
	var referral				= Trim(f.referral.value);
	var sourceName				= Trim(f.sourceName.value);
	var additionalComments		= Trim(f.additionalComments.value);

	var error = '';
	var error1 = '';
	var error1Header = '';
	var error2 = '';
	var error2Header = '';
	var error3 = '';
	var error3Header = '';
	var error4 = '';
	var error4Header = '';
	var error5 = '';
	var error5Header = '';
	var error6 = '';
	var error6Header = '';


	if (date == "") { error += '  Date\n'; }
	else if (!checkInvalidChars(date)) { error += '* Date\n'; }
	// Check the date format - Must be Month Day, Year like May 17, 2004
	else {
		var oldDateFormat = date;
		var newDate;
		var foundError = 0;

		if (date.indexOf(',') == -1) {
			error += '  Date Format is Invalide - \',\' must be appear before year.\n'; 
		}
		else {
			var dateArray = date.split (",");
			var getYear = dateArray[1];
			var monthDayArray = dateArray[0].split(" ");
			if (monthDayArray.length != 2) {
				error += '  Date Format is Invalide - There must be only 1 space between Month and Date.\n'; 
			}
			else {
				var getMonth = monthDayArray[0];
				var getDay = monthDayArray[1];
				var month	= Trim(getMonth);
				var day		= Trim(getDay);
				var year	= Trim(getYear);

				// Check the month
				if (!isValidMonth (month)) { 
					foundError = 1;
					error += '  Date Format is Invalide - Check the month\'s spelling.\n';  
				}
				else { newDate = month; }

				// Check the date of the month
				if (day > 0 && day < 10) {
					if (day.length == 1) {
						newDay = "0" + day;
						day = newDay;
					}
				}
				if (!isValidDay(month, day)) { 
					foundError = 1;
					error += '  Date Format is Invalide - Check the month\'s day.\n'; 
				}
				else { newDate += ' ' + day; }

				// Check the length of the year
				if ((year.length != 4) && (!isInteger(year))) { 
					foundError = 1;
					error += '  Date Format is Invalide - Check the year - Must be 4 digits.\n'; 
				}
				else { newDate += ', ' + year; }
				date = newDate;
				if (foundError == 0) { document.form.date.value = date; }			
			}
		}	
	}

	if (fedID == "") { error += '  Federal ID #\n'; }
	else if (!checkInvalidChars(fedID)) { error += '* Federal ID #\n'; }

	if (!checkInvalidChars(dunNumber)) { error += '* Dun & Bradstreet Number\n'; }

	// 1. Company Information
	if (companyName == "") { error1 += '  Company\'s Legal Name\n'; }
	else if (!checkInvalidChars(companyName)) { error1 += '* Company\'s Legal Name\n'; }

	if (!checkInvalidChars(dbaCompanyName)) { error += '* D/B/A Company Name\n'; }
	if (!checkInvalidChars(ownerName)) { error += '* Owners Name\n'; }
	
	if (companyContact == "") { error1 += '  Company Contact Name\n'; }
	else if (!checkInvalidChars(companyContact)) { error1 += '* Company Contact Name\n'; }

	if (!checkInvalidChars(companyContactTitle)) { error += '* Title of Contact\n'; }

	if ((contactNPA == "") || (contactNXX == "") || (contactLine == "")) { error1 += '  Company Phone\n'; }
	else if (!checkInvalidChars(contactNPA+contactNXX+contactLine)) { error1 += '* Company Phone\n'; }
	else if (!isInteger(contactNPA+contactNXX+contactLine)) { error1 += '* Company Phone (required numbers)\n'; }

	if (!checkInvalidChars(contactFaxNPA+contactFaxNXX+contactFaxLine)) { error1 += '* Company Fax\n'; }
	else if (!isInteger(contactFaxNPA+contactFaxNXX+contactFaxLine)) { error1 += '* Company Fax (required numbers)\n'; }


	if (address1 == "") { error1 += '  Address 1\n'; }
	else if (!checkInvalidChars(address1)) { error1 += '* Address 1\n'; }

	if (!checkInvalidChars(address2)) { error1 += '* Address 2\n'; }

	if (city == "") { error1 += '  City\n'; }
	else if (!checkInvalidChars(city)) { error1 += '* City\n'; }

	if (!checkInvalidChars(state)) { error1 += '* State\n'; }

	if (zip == "") { error1 += '  Zip\n'; }
	else if (!checkInvalidChars(zip)) { error1 += '* Zip\n'; }

	if (email != "") { 
		if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))) {
			error1 += '  Email Address\n';
		}
	 }

	if (!checkInvalidChars(webURL)) { error1 += '* Website Address URL\n'; }

	if (error1 != "") {
		error1Header  = '\n1. Company Information\n';
		error1Header += '------------------------------\n';
	}

	// 2. Sales Representative Business Classification Information
	if (gender == "") { error2 += '  Gender\n'; }
	else if (!checkInvalidChars(gender)) { error2 += '* Gender\n'; }

	if ((ethnicClass == "") && (otherClass == "")) { error2 += '  Class\n'; }
	else if (!checkInvalidChars(ethnicClass)) { error2 += '* Class\n'; }
	else if (!checkInvalidChars(otherClass))  { error2 += '* Class\n'; }

	if (category == "") { error2 += '  Category\n'; }
	else if (!checkInvalidChars(category)) { error2 += '* Category\n'; }

	if ((businessType == "") && (otherBusinessType == "")){ error2 += '  Business Type\n'; }
	else if (!checkInvalidChars(businessType)) { error2 += '* Business Type\n'; }
	else if (!checkInvalidChars(otherBusinessType)) { error2 += '* Business Type\n'; }

	if (certificate == "") { error2 += '  Do you have a certificate?\n'; }
	else if (!checkInvalidChars(certificate)) { error2 += '* Do you have a certificate?\n'; }

	if ((certificate != "") && (certificate != "No")) {
		if (certifyingAgencyName == "") { error2 += '  Certifying Agencies Name\n'; }
		else if (!checkInvalidChars(certifyingAgencyName)) { error2 += '* Certifying Agencies Name\n'; }

		if (certificateType == "") { error2 += '  Certificate Type\n'; }
		else if (!checkInvalidChars(certificateType))   { error2 += '* Certificate Type\n'; }

		if (certificateNumber == "") { error2 += '  Certificate Number\n'; }
		else if (!checkInvalidChars(certificateNumber)) { error2 += '* Certificate Number\n'; }

		if (certificateExp == "") { error2 += '  Certificate Expire Date\n'; }
		else if (!checkInvalidChars(certificateExp))    { error2 += '* Certificate Expire Date\n'; }
		else {
			if (certificateExp.indexOf("/") == -1) { error2 += '* Invalid Certificate Expire Date: Must be \'/\' between month and year\n'; }
			else {
				var expireArray = certificateExp.split("/");
				if (expireArray.length != 2) { error2 += '  Invalid Certificate Expire Date: Must be MM/YYYY\n'; }
				else {
					var foundError = 0;
					var cMonth = expireArray[0];
					var cYear  = expireArray[1];
					cMonth = Trim (cMonth);
					cYear  = Trim (cYear);
					if (cMonth > 1 && cMonth < 10) {
						cMonth = "0" + cMonth;
					}
					else if (cMonth > 12 || cMonth < 1) { 
						foundError = 1;
						error2 += '  Invalid Certificate Expire Date: Check Month\n'; 
					}

					if (cYear.length != 4) { 
						foundError = 1;
						error2 += '  Invalid Certificate Expire Date: Check Year\n'; 
					}
					if (foundError == 0) { document.form.certificateExp.value = cMonth + '/' + cYear; }
				}
			}
		}
	}
	if (certificate == "No") {
		if ((certifyingAgencyName != "") || (certificateType != "") || (certificateNumber != "") || (certificateExp != "")) {
			var answer = confirm ("You said that you don't have certificate but\n" +
								  "some data is found in the details\n" +
								  "Are you sure you don't have certificate?\n\n" +
								  "Clicking on OK, empties all the certificate fields\n");
			if (answer == true) {
				document.form.certifyingAgencyName.value = '';
				document.form.certificateType.value		 = '';
				document.form.certificateNumber.value	 = '';
				document.form.certificateExp.value		 = '';
			}
			else { return false; }
		}
	}

	
	
	

	if (error2 != "") {
		error2Header  = '\n2. Sales Representative Business Classification Information\n';
		error2Header += '------------------------------------------------------------------------------\n';
	}

	// 3. Financial Information
	if (revenue1 == "") { error3 += '  Gross Annual Sales Revenue 2004\n'; }
	else if (!checkInvalidChars(revenue1))   { error3 += '* Gross Annual Sales Revenue 2004\n'; }

	if (revenue2 == "") { error3 += '  Gross Annual Sales Revenue 2003\n'; }
	else if (!checkInvalidChars(revenue2))   { error3 += '* Gross Annual Sales Revenue 2003\n'; }

	if (revenue3 == "") { error3 += '  Gross Annual Sales Revenue 2002\n'; }
	else if (!checkInvalidChars(revenue3))   { error3 += '* Gross Annual Sales Revenue 2002\n'; }

	if (legalStructure == "") { error3 += '  Legal Structure\n'; }
	else if (!checkInvalidChars(legalStructure))   { error3 += '* Legal Structure\n'; }

	if (numberEmployees == "") { error3 += '  Number of employees (full time)\n'; }
	else if (!checkInvalidChars(numberEmployees))   { error3 += '* Number of employees (full time)\n'; }

	if (areaCoverage == "") { error3 += '  Geographic Area Coverage\n'; }
	else if (!checkInvalidChars(areaCoverage))   { error3 += '* Geographic Area Coverage\n'; }

	if (yearsInBusiness == "") { error3 += '  Years In Business\n'; }
	else if (!checkInvalidChars(yearsInBusiness))   { error3 += '* Years In Business\n'; }

	if (error3 != "") {
		error3Header  = '\n3. Financial Information\n';
		error3Header += '-----------------------------\n';
	}

	// 4. Product and Service Offerings
	if (productOffering == "") { error4 += '  Product/Service Offered(primary)\n'; }
	else if (!checkInvalidChars(productOffering))   { error4 += '* Product/Service Offered(primary)\n'; }

	if (!checkInvalidChars(primaryNAICS))   { error4 += '* NAICS Code\n'; }
	if (!checkInvalidChars(primarySIC))   { error4 += '* SIC Codes\n'; }

	if (secondaryProductOffering == "") { error4 += '  Product/Service Offered(secondary)\n'; }
	else if (!checkInvalidChars(secondaryProductOffering))   { error4 += '* Product/Service Offered(secondary)\n'; }

	if (!checkInvalidChars(secondaryNAICS))   { error4 += '* NAICS Code\n'; }
	if (!checkInvalidChars(secondarySIC))   { error4 += '* SIC Codes\n'; }

	if (!checkInvalidChars(tertiaryNAICS))   { error4 += '* NAICS Code(Tertiary)\n'; }
	if (!checkInvalidChars(tertiarySIC))   { error4 += '* SIC Codes(Tertiary)\n'; }

	if (!checkInvalidChars(otherNAICSorSIC))   { error4 += '* Other NAICS Codes or SIC Codes\n'; }

	if (error4 != "") {
		error4Header  = '\n4. Product and Service Offerings\n';
		error4Header += '---------------------------------------\n';
	}

	// 5. Three Business References
	if (busRef1 == "") { error5 += '  Business Reference #1\n'; }
	else if (!checkInvalidChars(busRef1))   { error5 += '* Business Reference #1\n'; }

	if (busRef1Name == "") { error5 += '  Contact Name\n'; }
	else if (!checkInvalidChars(busRef1Name))   { error5 += '* Contact Name\n'; }

	if ((busRef1NPA == "") || (busRef1NXX == "") || (busRef1Line == "")) { error5 += '  Business Ref 1 Phone\n'; }
	else if (!checkInvalidChars(busRef1NPA+busRef1NXX+busRef1Line)) { error5 += '* Business Ref 1 Phone\n'; }
	else if (!isInteger(busRef1NPA+busRef1NXX+busRef1Line)) { error5 += '* Business Ref 1 Phone (required numbers)\n'; }

	if (busRef2 == "") { error5 += '  Business Reference #2\n'; }
	else if (!checkInvalidChars(busRef2))   { error5 += '* Business Reference #2\n'; }

	if (busRef2Name == "") { error5 += '  Contact Name\n'; }
	else if (!checkInvalidChars(busRef2Name))   { error5 += '* Contact Name\n'; }

	if ((busRef2NPA == "") || (busRef2NXX == "") || (busRef2Line == "")) { error5 += '  Business Ref 2 Phone\n'; }
	else if (!checkInvalidChars(busRef2NPA+busRef2NXX+busRef2Line)) { error5 += '* Business Ref 2 Phone\n'; }
	else if (!isInteger(busRef2NPA+busRef2NXX+busRef2Line)) { error5 += '* Business Ref 2 Phone (required numbers)\n'; }

	if (busRef3 == "") { error5 += '  Business Reference #3\n'; }
	else if (!checkInvalidChars(busRef3))   { error5 += '* Business Reference #3\n'; }

	if (busRef3Name == "") { error5 += '  Contact Name\n'; }
	else if (!checkInvalidChars(busRef3Name))   { error5 += '* Contact Name\n'; }

	if ((busRef3NPA == "") || (busRef3NXX == "") || (busRef3Line == "")) { error5 += '  Business Ref 3 Phone\n'; }
	else if (!checkInvalidChars(busRef3NPA+busRef3NXX+busRef3Line)) { error5 += '* Business Ref 3 Phone\n'; }
	else if (!isInteger(busRef3NPA+busRef3NXX+busRef3Line)) { error5 += '* Business Ref 3 Phone (required numbers)\n'; }

	if (error5 != "") {
		error5Header  = '\n5. Three Business References\n';
		error5Header += '------------------------------------\n';
	}

	if (!checkInvalidChars(referral)) { error6 += '* Referral\n'; }
	if (!checkInvalidChars(sourceName)) { error6 += '* Source Name\n'; }
	if (!checkInvalidChars(additionalComments)) { error6 += '* Additional Comments\n'; }

	if (error6 != "") {
		error6Header  = '\n6. Additional Information and Comments\n';
		error6Header += '----------------------------------------------------\n';
	}


	if ((error == '') && (error1 == '') && (error2 == '') && (error3 == '') && (error4 == '') && (error5 == '') && (error6 == '')) { 
		return true; 
	}
	else {
		var errorHeader  = 'Incomplete Form !!\n';
		errorHeader		+= 'Please complete the following requirements\n';
		errorHeader		+= 'Fields marked with * contains Invalid Characters\n';
		errorHeader		+= '---------------------------------------------------------------------------\n\n';
		alert (errorHeader + error + error1Header + error1 + error2Header + error2 + error3Header + error3 + error4Header + error4 + error5Header + error5 + error6Header + error6);
	}
	
	return false;

}

/*
==============================================================================
checkInvalidChars(String) : Check the string if it contains invalid characters
==============================================================================
*/
function checkInvalidChars(testString)
/*
   PURPOSE: Check the passing string if it contains any invalid character.
   IN: str - the string we want to Validate

   RETVAL: True - if no Invalid characters, False - If it contains Invalid Chars!
*/
{

		var invalidChars = "><:%+&\|!`~#%^*?;";
		for (var i=0; i<testString.length; i++) {
			var singleChar = testString.charAt(i);
			if (invalidChars.indexOf(singleChar) != -1) { return false; }
		}
		return true;
}

/*
==============================================================================
isInteger(String) : Check the string if it contains Only Number
==============================================================================
*/

function isInteger(s)
/*
   PURPOSE: Check the passing string if it contains Only Numbers.
   IN: str - the string we want to Validate

   RETVAL: True - if contains ONLY Number, False - If it contains Invalid Chars!
*/
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

/*
============================================================================
Trim(string) : Returns a copy of a string without leading or trailing spaces
============================================================================
*/
function Trim(str)
/*
   PURPOSE: Remove trailing and leading blanks from our string.
   IN: str - the string we want to Trim

   RETVAL: A Trimmed string!
*/
{
   return RTrim(LTrim(str));
}


/*
==================================================================
LTrim(string) : Returns a copy of a string without leading spaces.
==================================================================
*/
function LTrim(str)
/*
   PURPOSE: Remove leading blanks from our string.
   IN: str - the string we want to LTrim
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

/*
==================================================================
RTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function RTrim(str)
/*
   PURPOSE: Remove trailing blanks from our string.
   IN: str - the string we want to RTrim

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

	function isValidDay(monthEntered, dayEntered) {

		if (monthEntered == "January") {		   
			if ((dayEntered <= 31) && (dayEntered > 0)) { return true; }
		}
		else if (monthEntered == "February") {
			if ((dayEntered <= 31) && (dayEntered > 0)) { return true; }
		}
		else if (monthEntered == "March") {
			if ((dayEntered <= 31) && (dayEntered > 0)) { return true; }
		}
		else if (monthEntered == "April") {
			if ((dayEntered <= 30) && (dayEntered > 0)) { return true; }
		}
		else if (monthEntered == "May") {
			if ((dayEntered <= 31) && (dayEntered > 0)) { return true; }
		}
		else if (monthEntered == "June") {
			if ((dayEntered <= 30) && (dayEntered > 0)) { return true; }
		}
		else if (monthEntered == "July") {
			if ((dayEntered <= 31) && (dayEntered > 0)) { return true; }
		}
		else if (monthEntered == "August") {
			if ((dayEntered <= 31) && (dayEntered > 0)) { return true; }
		}
		else if (monthEntered == "September") {
			if ((dayEntered <= 30) && (dayEntered > 0)) { return true; }
		}
		else if (monthEntered == "October") {
			if ((dayEntered <= 31) && (dayEntered > 0)) { return true; }
		}
		else if (monthEntered == "November") {
			if ((dayEntered <= 30) && (dayEntered > 0)) { return true; }
		}
		else if (monthEntered == "December") {
			if ((dayEntered <= 31) && (dayEntered > 0)) { return true; }
		}
		else { return false; }
	}

	function isValidMonth (monthEntered) {

		var months = new Array(13);
		months[0]  = "January";
		months[1]  = "February";
		months[2]  = "March";
		months[3]  = "April";
		months[4]  = "May";
		months[5]  = "June";
		months[6]  = "July";
		months[7]  = "August";
		months[8]  = "September";
		months[9]  = "October";
	    months[10] = "November";
		months[11] = "December";
		var len = months.length;

		for (var i=0; i<len; i++) {
			if (months[i] == monthEntered) { return true; }
		}
		if (i == len) { return false; }
	}
