/* Modification Notes: 02-21-03

This original script was taken from this page:
	http://thesamplecenter.com/register/register_frame.cfm?TID=162103105716100295342&SID=1000&CID=0&UID=0&LAT=211002160357


The modifications were added so that this script could be an external 
file that validated two forms: "form_sample_center.htm" and "form_viewbook.htm".
These two pages are associated with the landing page, "www.i2byinterface.com/<name_of_landing_page.htm>".

Each form is numbered on the HTML page with an embedded javascript:
	form_sample_center.htm corresponds to form_num == 1
	form_viewbook.htm corresponds to form_num == 2
	
	
The validation script checks to see what form_num it is validating before executing the logic.
Some variables were added to set flags for the modified logic.  They are commented.
*/


// Declare validation vars
var error
var bag = "";
var acceptChars ="0123456789./-() "
var acceptNum = "0123456789"
var zipBag = ""
var acceptZipChars = "0123456789-"
var acceptZipNum = "0123456789"
var acceptAreaCodeNum = "0123456789"


// Modification:
// This flag is used to test if someone is registering for the sample center
// from the request a viewbook form (i.e., some fields are not required, but
// if the registration process is started, then those fields are required)
var form_num_2_reg = 0;


// Modification:
// This captures the state of the above listed form_num_2 option flags
// This is true if:
//		case 1: form_num == 1
//	    case 2: form_num == 2 and all form_num_2 vars are 0
//		case 3: form_num == 2 and all form_num_2 vars are 1
//
// This is false if:
//		case 4: form_num == 2 and one or more -- but not all -- form_num_2 vars is 1

// These flags track if the user in form_num 2 has filled out all password info.
// If any of these == 1, then all must == 1, or the user has not filled in all info
var form_num_2_userId = 0;
var form_num_2_passwordID = 0;
var form_num_2_password2 = 0;
var form_num_2_hint = 0;

// Modification: this global var tracks whether or not the user has filled in 
// if they have a dealer relationship or not.
// It is used to link the "yes" or "no" radio button with the dropdown list
// For more information, see the functions "checkDealerName" and "checkDealerRelationship"
var relationship = "";

// Modification:
// This function was "aired out" a little bit in order to "chunk" the validation
// logic to handle both forms.

function justSubmit()
{

//
var myNum = document.thisForm.AreaCode.value
	for (i = 0; i < myNum.length; i++)
	{
		temp = myNum.substring(i, i+1);
		if(acceptAreaCodeNum.indexOf(temp)== -1)
		{
			alert("Please check your area code. It does not appear to be valid.");
			document.thisForm.AreaCode.focus();
			return false;
		}
		
	}
	
	document.thisForm.submit();
//

   
}

function startform1()
{
   //document.thisForm.userID.focus();
}

function startform2()
{
   //document.thisForm.FirstName.focus();
}

function checkForm()
{
		// Modification:
		// Check the form_sample_center.htm for user, pass, and hint first -- these are required by form_num 1
		// Then check the stuff required by both forms
		// Then if form_num is 2, form_viewbook.htm, check for user, pass and hint last
		if (form_num == 1)
		{
			if (!checkUserName())
			{
				alert(error);
				document.thisForm.userID.focus();
				return false;
			}
			else if (!checkForSpace())
			{
				alert(error);
				document.thisForm.userID.focus();
				return false;
			}
			else if (!checkPassword())
			{
				alert (error);
				document.thisForm.userPasswordID.focus();
				return false
			}
			else if (!checkHint())
			{
				alert(error);
				document.thisForm.hint.focus();
				return false
			}
		}

		// Modification:
		// Continue checking form_num == 1 or
		// Start checking form_num == 2
		if ((form_num == 1) || (form_num == 2))
		{
			if (!checkFirstName())
			{
				alert(error);
				document.thisForm.FirstName.focus();
				return false
			}
			else if (!checkLastName())
			{
				alert(error);
				document.thisForm.LastName.focus();
				return false
			}
			else if (!checkTitle())
			{
				alert(error);
				document.thisForm.JobTitle.focus();
				return false	
			}
			else if (!checkAreaCode())
			{
				alert(error);
				document.thisForm.AreaCode.focus();
				return false
			}
			else if(!checkFirstThree())
			{
				alert(error);
				document.thisForm.FirstThree.focus();
				return false;
			}
			else if(!checkLastFour())
			{
				alert(error);
				document.thisForm.LastFour.focus();
				return false;	
			}
			else if (!checkCompany())
			{
				alert(error);
				document.thisForm.CompanyName.focus();
				return false
			}
			else if (!checkIndustry())
			{
				alert(error);
				document.thisForm.IndustryB.focus();
				return false
			}
			else if (!checkEmail())
			{
				alert(error);
				document.thisForm.FullName.focus();
				return false
			}
			//else if (!checkPhone())
			//{
			//	alert(error);
			//	document.thisForm.OfficePhoneNumber.focus();
			//	return false
			//}
			//else if(!checkPhone2())
			//{
			//	alert(error);
			//	document.thisForm.OfficePhoneNumber.focus();
			//	return false;
			//}
			//else if(!realPhoneNum())
			//{
			//	alert(error);
			//	document.thisForm.OfficePhoneNumber.focus();
			//	return false;	
			//}
			//
			
			else if (!checkAddress())
			{
				alert(error);
				document.thisForm.BStreetAddress.focus();
				return false
			}
			else if (!checkCity())
			{
				alert(error);
				document.thisForm.BCity.focus();
				return false
			}
			else if (!checkState())
			{
				alert(error);
				document.thisForm.BState.focus();
				return false
			}
			else if (!checkZip())
			{
				alert(error);
				document.thisForm.BZip.focus();
				return false
			}
			else  if (!checkWorked_w_Int())
			{
				// Modification:
				alert(error);
				// The statement below was problematic on Fri-2-21-03
				// In usablity terms, setting the focus() may not be necessary for these fields.
				//document.thisForm.worked_w_Int.focus();
				return false
			}
			else if (!checkDealerRelationship())
			{
				// Modification:
				alert(error);
				// The statement below was problematic on Fri-2-21-03
				// In usablity terms, setting the focus() may not be necessary for these fields.
				//document.thisForm.haveDealer.focus();
				return false
			}
			else if (!checkDealerName())
			{
				// Modification:
				alert(error);
				// The statement below was problematic on Fri-2-21-03
				// In usablity terms, setting the focus() may not be necessary for these fields.
				//document.thisForm.haveDealer.focus();
				return false
			}
			else if (!checkCurrent_work())
			{
				// Modification:
				alert(error);
				// The statement below was problematic on Fri-2-21-03
				// In usablity terms, setting the focus() may not be necessary for these fields.
				//document.thisForm.current_work.focus();
				return false
			}
		}
		else if (form_num == 1)
		{
			// Form 1 is complete
			return true
		}
		
		// Modification:
		// Check this stuff last for form_num == 2		
		if (form_num == 2)
		{
			if (!checkUserName())
			{
				alert(error);
				document.thisForm.userID.focus();
				return false;
			}
			else if (!checkForSpace())
			{
				alert(error);
				document.thisForm.userID.focus();
				return false;
			}
			else if (!checkPassword())
			{
				alert (error);
				document.thisForm.userPasswordID.focus();
				return false
			}
			else if (!checkHint())
			{
				alert(error);
				document.thisForm.hint.focus();
				return false
			}
			else if (!checkReg())
			{
				alert(error);
				document.thisForm.userID.focus();
				return false			
			}
			else
			{
				// Form 2 is complete
				return true
			}
		}
}

//did you enter a user name?
function checkUserName()
{
	// Modification:
	// Handle form_1 first
	// form_1 must have a userName
	if (form_num == 1)
	{
		if (document.thisForm.userID.value == "")
		{
			error = "You must enter a UserName."
			return false
		}
		else
		{
			return true
		}
	}
	else if (form_num == 2)
	{
		// This form does not require a username, so if it is blank
		// then that's okay, set form_num_2_reg to 0 to signal that they
		// are currently not signing up
		if (document.thisForm.userID.value == "")
		{
			// The user has NOT filled in the optional user name, so set that flag to 0
			form_num_2_userId = 0;
			return true
		}
		else if (document.thisForm.userID.value != "")
		{
			// The user has filled in the optional user name, 
			// so flag that the user is registering 
			// and flag that the userID has been set
			form_num_2_reg = 1;
			form_num_2_userId = 1;
			return true
		}
	}
}


//is there space in the user name?
function checkForSpace() 
{
	// Modification:
	// If the user is on form_num 1, then there must be a userID
	// If the user is on form_num 2, then there must be a userID if form_num_2_reg == 1 (meaning, they've opted to sign up)
	if ((form_num == 1) || ((form_num == 2) && (form_num_2_reg == 1)))
	{
		var mString = document.thisForm.userID.value;
		var sLength = mString.length - 1;
	
		for (i = 0; i < sLength; i++)
		{
			if (mString.charAt(i) == " ")
			{
				error = "Your username cannot contain spaces.\nPlease remove any spaces between characters."
				return false;
			}
		}
	
		return true;
	}
	else
	{
		return true;
	}
}


//did you enter a password and confirm it?
function checkPassword()
{
	// Modification:
	// Handle form_1 first
	// form_1 must have a passwordID
	if (form_num == 1)
	{
		if (document.thisForm.userPasswordID.value == "")
		{
			error = "You must enter a password."
			return false
		}
		else if (document.thisForm.userPasswordID.value != document.thisForm.password2.value)
		{
			error = "Sorry, it appears as if you may not have entered the same password twice.\nPlease retype your password again";
			return false
		}
		else
		{
			return true
		}
	}
	else if (form_num == 2)
	{
		// If the user has decided to sign up (form_num_2_reg == 1), check it
		if ((document.thisForm.userPasswordID.value == "") && ( (form_num_2_reg == 1) && (!checkReg_blanked()) ) )
		{
			error = "You must enter a password.";
			form_num_2_passwordID = 0;
			return false
		}
		else if (document.thisForm.userPasswordID.value != "")
		{
			// The user has entered something in the password so make sure the registration flag is set
			form_num_2_reg = 1;
			
			// Now check this against password2
			if (document.thisForm.userPasswordID.value != document.thisForm.password2.value)
			{
				error = "Sorry, it appears as if you may not have entered the same password twice.\nPlease retype your password again";
			
				// Reset the associated flags
				form_num_2_password2 = 0;
				form_num_2_password2 = 0;
				return false		
			}
			else
			{
				// Update the flags
				form_num_2_passwordID = 1;
				form_num_2_password2 = 1;
				return true
			}
		}
		else if ((document.thisForm.userPasswordID.value == "") && (form_num_2_reg == 0))
		{
			return true;
		}
	}
}



//did you enter a password hint?
function checkHint()
{
	// Modification:
	// Handle form_1 first
	// form_1 must have a hint
	if (form_num == 1)
	{
		if (document.thisForm.hint.value == "")
		{
			error = "Please enter a password hint. Should you forget your\npassword, this hint will be used to help remind you of it."
			return false
		}
		else
		{
			return true
		}
	}
	else if (form_num == 2)
	{
		// The user has decided to sign up (form_num_2_reg == 1)
		if ((document.thisForm.hint.value == "") && (form_num_2_reg == 1))
		{
			error = "Please enter a password hint. Should you forget your\npassword, this hint will be used to help remind you of it.";
			form_num_2_hint = 0;
			return false
		}
		else if (document.thisForm.hint.value != "")
		{
			// The user has entered something in the hint so make sure the registration flag is set and update hint flag
			form_num_2_reg == 1;
			form_num_2_hint = 1;
			
			return true
		}
		else
		{
			return true;
		}
	}
}

// Modification:
// This was added to check if the user started to register with the sample center,
// filled in some info, but then blanked out those fields
// This is only used for form_num == 2
function checkReg_blanked()
{
	// If the user has decided to blank the form after starting to fill out the form reset all flags
	// And allow the user to exit
	if ((document.thisForm.userID.value == "") && 
	     (document.thisForm.userPasswordID.value == "") && 
		 (document.thisForm.password2.value == "") &&
		 (document.thisForm.hint.value == "") )
	{
		// Reset all flags
		form_num_2_reg = 0;
		form_num_2_userId = 0;
		form_num_2_passwordID = 0;
		form_num_2_password2 = 0;
		form_num_2_hint = 0;
		
		return true
	}
	else
	{
		return false
	}
}

// Modification:
// This function checks the userName info that is option in form_num == 2
function checkReg()
{
	if (form_num == 2)
	{
		if (form_num_2_reg == 0)
		{
			return true
		}
		else if ((form_num_2_userId == 0) && (form_num_2_passwordID == 0) && (form_num_2_password2 == 0) && (form_num_2_hint == 0))
		{
			return true
		}
		else if ((form_num_2_userId == 1) && (form_num_2_passwordID == 1) && (form_num_2_password2 == 1) && (form_num_2_hint == 1))
		{
			return true
		}
		else
		{
			error = "Since you've decided to register,\nplease fill in all the user name information.";
			return false
		}
	}
	else
	{
		return true
	}

}


//did you enter your first name?
function checkFirstName()
{
	if (document.thisForm.FirstName.value == "")
	{
		error = "You must enter your First Name."
		return false
	}
	else
	{
		return true
	}
}


//did you enter your last name?
function checkLastName()
{
	if (document.thisForm.LastName.value == "")
	{
		error = "You must enter a Last Name."
		return false
	}
	else
	{
		return true
	}
}


//did you enter a title?
function checkTitle()
{
	if (document.thisForm.JobTitle.value == "")
	{
		error = "You must enter a Job Title."
		return false
	}
	else
	{
		return true
	}
}


//did you enter a company?
function checkCompany()
{
	if (document.thisForm.CompanyName.value == "")
	{
		error = "You must enter your company's name."
		return false
	}
	else
	{
		return true
	}
}


//did you enter an industry?
function checkIndustry()
{
	//alert ("about to check industry");

	if ((document.thisForm.IndustryA[document.thisForm.IndustryA.selectedIndex].value == "void") && (document.thisForm.IndustryB.value == ""))
	{
		//alert("Please select or type in your industry.")
		error = "Please type in an industry in the Other Industry field."
		return false
	}
	else if ((document.thisForm.IndustryA[document.thisForm.IndustryA.selectedIndex].value != "void") && (document.thisForm.IndustryB.value != ""))
	{
		//alert("Please select or type in your industry.")
		error = "You entered the name of your industry twice.\nPlease use either the pulldown or the text box,\nbut not both."
		return false
	}
	else if (document.thisForm.IndustryA[document.thisForm.IndustryA.selectedIndex].value == "void")
	{
		document.thisForm.Industry.value = document.thisForm.IndustryB.value
		//alert(form.Industry.value);
		return true
	} 
	else 
	{
		document.thisForm.Industry.value = document.thisForm.IndustryA[document.thisForm.IndustryA.selectedIndex].value
		//alert(document.thisForm.Industry.value);
		return true
	}
	
	return true
}


//preeti, check this function
//did you enter an email address?
function checkEmail()
{
	if ((document.thisForm.FullName.value.indexOf("@") == "-1") || 
	    (document.thisForm.FullName.value.indexOf(".") == "-1") || 
		(document.thisForm.FullName.value == ""))
	{
		error = "You must enter a valid email address."
		return false
	}
	else
	{
		return true
	}
}

//check area code
//did you enter a phone number?
function checkAreaCode()
{
	if (document.thisForm.AreaCode.value == "")
	{
		error = "You must enter your Area Code."
		return false
	}
	else
	{
		return true
	}
}

//check first three digits
//did you enter a phone number?
function checkFirstThree()
{
	if (document.thisForm.FirstThree.value == "")
	{
		error = "You must enter your Phone Number."
		return false
	}
	else
	{
		return true
	}
}

//check last four digits
//did you enter a phone number?
function checkLastFour()
{
	if (document.thisForm.LastFour.value == "")
	{
		error = "You must enter your Phone Number."
		return false
	}
	else
	{
		return true
	}
}


//did you enter a phone number?
function checkPhone()
{
	if (document.thisForm.OfficePhoneNumber.value == "")
	{
		error = "You must enter your phone number."
		return false
	}
	else
	{
		return true
	}
}

//does your phone number contain valid characters?
function checkPhone2()
{
	if (true) 
	{ 
		// short circuit test without significant impact
		return true;
	}
	
	bag = ""
	var myNum = document.thisForm.OfficePhoneNumber.value
	for (i = 0; i < myNum.length; i++)
	{
		temp = myNum.substring(i, i+1);
		if (acceptChars.indexOf(temp)== -1)
		{
			error = "Please check your phone number.\nIt does not appear to be valid.";
			return false;
		}
		else if (acceptNum.indexOf(temp) != -1)
		{
			bag = bag+temp;
		}
	}

	return true;
}

//is the phone number long enough to be valid?
function realPhoneNum()
{
	if (true) 
	{ 
		// short circuit test without significant impact
		return true;
	}
	
	// Note: bag is a side-effect of having called CheckPhone2() [see above].
	//alert(bag);
	if (bag.length < 10)
	{
		error = "Please check your phone number.\nIt does not appear to be valid."
		return false;
	}
	
	return true;
}



//did you enter your address?
function checkAddress()
{
	if (document.thisForm.BStreetAddress.value == "")
	{
		error = "You must enter your address."
		return false
	}
	else
	{
		return true
	}
}


//did you enter your city?
function checkCity()
{
	if (document.thisForm.BCity.value == "")
	{
		error = "You must enter your city."
		return false
	}
	else
	{
		return true
	}
}


//did you enter your state?
function checkState()
{
	if (document.thisForm.BState.value == "" || document.thisForm.BState.value == "void")
	{
		error = "You must enter your state."
		return false
	}
	else
	{
		return true
	}
}


//did you enter your zipcode?
//function checkzZip()
//{
//	if (true) 
//	{ 
		// short circuit test without significant impact
//		return true;
//	}
	
//	if (!checkZipChars())
//	{
//		return false;
//	}
//	else if(!checkZipLength())
//	{
//		return false;
//	}
//	else
//	{
//		return true;
//	}
//}

//did you enter your zip code?
function checkZip()
{
	if (document.thisForm.BZip.value == "")
	{
		error = "You must enter your zip code."
		return false
	}
	else
	{
		return true
	}
}



function checkZipChars()
{
	if (true) 
	{ 
		// short circuit test without significant impact
		return true;
	}
	
	var myNum = document.thisForm.BZip.value
	for (i = 0; i < myNum.length; i++)
	{
		temp = myNum.substring(i, i+1);
		if(acceptZipChars.indexOf(temp)== -1)
		{
			error = "Please check your Zip Code.\nIt does not appear to be valid.";
			return false;
		}
		else if (acceptZipNum.indexOf(temp) != -1)
		{
			zipBag = zipBag+temp;
		}
	}
	
	return true;
}

function checkZipLength()
{
	if (true) 
	{ 
		// short circuit test without significant impact
		return true;
	}
	
	// Note: zipBag is a side-effect of having called CheckZipChars() [see above].
	if (zipBag.length < 5)
	{
		error = "Please check your Zip Code.\nIt does not appear to be valid."
		return false;
	}
	
	return true;
}

// Modification:
// This was added to check the worked_w_Int radio button.
function checkWorked_w_Int()
{
	var n = document.thisForm.worked_w_Int.length;
	var i = 0;
	var is_checked = false;
	
	do
	{	
		//alert ("document.thisForm.worked_w_Int[i].value, checked = " + document.thisForm.worked_w_Int[i].value + ", " + document.thisForm.worked_w_Int[i].checked);
		if (document.thisForm.worked_w_Int[i].checked) {is_checked = true}
		i++;
    }
	while ((is_checked == false) && (i < n))

	if (!is_checked)
	{
		error = "Please indicate whether or not\nyou have worked with Interface before."
		return false
	}
	else
	{
		return true
	}
}

// Modification:
// This was added to check the haveDealer radio button.
function checkDealerRelationship()
{
	var n = document.thisForm.haveDealer.length;
	var i = 0;
	var is_checked = false;
	
	do
	{	
		//alert ("document.thisForm.haveDealer[i].value, checked = " + document.thisForm.haveDealer[i].value + ", " + document.thisForm.haveDealer[i].checked);
		if (document.thisForm.haveDealer[i].checked)
		{
			is_checked = true;
			
			// Set the global variable, relationship for later checking
			this.relationship = document.thisForm.haveDealer[i].value;
		}
		i++;
    }
	while ((is_checked == false) && (i < n))

	if (!is_checked)
	{
		error = "Please indicate whether or not\nyou have a relationship with a dealer."
		return false
	}
	else
	{
		return true
	}
}


// Modification:
// This was added to check the dealerName drop down and update the state of the haveDealer radio button if need be.
function checkDealerName()
{
	// Check if there is a relationship first
	// If yes, make sure we have a dealerName
	// If no, check if dealerName is blank
	// If dealerName is not blank, and relationship is no, set relationship to yes
	
	if (this.relationship == "yes")
	{
		if (document.thisForm.dealerName[document.thisForm.dealerName.selectedIndex].value == "void")
		{
			error = "Please select a dealer."
			return false
		}
		else
		{
			return true
		}
	}
	else if (this.relationship == "no")
	{
		if (document.thisForm.dealerName[document.thisForm.dealerName.selectedIndex].value != "void")
		{
			this.relationship = "yes";
			document.thisForm.haveDealer[0].click();
			document.thisForm.haveDealer[0].checked = true;			
			document.thisForm.haveDealer[1].checked = false;
			return true
		}
		else
		{
			return true
		}
	}
}


// Modification:
// This was added to check the current_work drop down.
function checkCurrent_work()
{
	if (document.thisForm.current_work[document.thisForm.current_work.selectedIndex].value == "void")
	{
		error = "Please select a a work status."
		return false
	}
	else
	{
		return true
	}
}





