// JavaScript Document
//-----------------------------
// Begin function validateForm
//-----------------------------

function validateForm() 
	{
//--------------------	
// Validate Last Name
//--------------------	
	if (document.accountapply.AA01_LasName.value === "")
		{
			document.accountapply.AA01_LasName.focus();
			alert("Please Enter" + " " + document.accountapply.AA01_LasName.id);
			return false;
		}
	else
		{
			changeCase(document.accountapply.AA01_LasName);
		}
		
//---------------------	
// Validate First Name
//---------------------
	if (document.accountapply.AA02_FirstName.value === "")
		{
			document.accountapply.AA02_FirstName.focus();
			alert("Please Enter" + " " + document.accountapply.AA02_FirstName.id);
			return false;
		}			
	else
		{
		changeCase(document.accountapply.AA02_FirstName);
		}
		
//--------------------------
// Validate Email User Name
//--------------------------
	if (document.accountapply.AA03_UserName.value === "")
		{
		document.accountapply.AA03_UserName.focus();
		alert("Please Enter" + " " + document.accountapply.AA03_UserName.id);
		return false;
		}					
//	atSign = document.accountapply.AA03_UserName.value.indexOf("@");
//	if (atSign != -1) 
//		{
//		tempUserName = "";
//		for (i=0; i<document.accountapply.AA03_UserName.length; i++)
//			{
//			tempUserName = tempUserName + document.accountapply.AA03_UserName.value.substr(i,1);
//			}
//		document.accountapply.AA03_UserName.value = tempUserName;
//		}
//	else
//		{
//		document.accountapply.AA03_UserName.focus();
//		alert("E-mail does not contain @");
//		return false;
//		}
//	}

//------------------------
// Validate Person Number
//------------------------
	if ((isNaN(document.accountapply.AA04_PersonNumber.value) === true) ||
			   (document.accountapply.AA04_PersonNumber.value.length != 8))
		{
			document.accountapply.AA04_PersonNumber.focus();
			document.accountapply.AA04_PersonNumber.select();
			alert(document.accountapply.AA04_PersonNumber.id + " must be 8 numeric digits");
			return false;
		}

//-----------------------
// Validate Home Address
//-----------------------
	if (document.accountapply.AA06_Address.value === "")
		{
			document.accountapply.AA06_Address.focus();
			alert("Please Enter" + " " + document.accountapply.AA06_Address.id);
			return false;
		}
	else
		{
		changeCase(document.accountapply.AA06_Address);
		}		
							
//---------------
// Validate City
//---------------
	if (document.accountapply.AA07_City.value === "")
		{
			document.accountapply.AA07_City.focus();
			alert("Please Enter" + " " + document.accountapply.AA07_City.id);
			return false;
		}
	else
		{
		changeCase(document.accountapply.AA07_City);
		}								
//--------------
// Validate Zip
//--------------
	if (document.accountapply.AA08_Zip.value === "")
		{
			document.accountapply.AA08_Zip.focus();
			alert("Please Enter" + " " + document.accountapply.AA08_Zip.id);
			return false;
		}
//---------------------
// Validate Home Phone
//---------------------
//	alert("validate HomePhone");
//	alert(document.accountapply.AA17_HomePhone.value);
//	alert(document.accountapply.AA17_HomePhone.value.length);
	
	//========================================
	// Strip Punctuation
	// Regular Expression RegExp will be used 
	// to remove all non-numeric characters
	//========================================
	regExp = /(\D)*/g;
//	alert(regExp);
	document.accountapply.AA17_HomePhone.value = 
	document.accountapply.AA17_HomePhone.value.replace(regExp, ""); 
//	alert(document.accountapply.AA17_HomePhone.value);
	
	strPhone = document.accountapply.AA17_HomePhone;
//	alert("strPhone = " + strPhone.value);
//	alert("strPhone Length = " + strPhone.value.length);
	
	if (strPhone.value.length === 0)
	{
		//====================================
		// Phone Number is empty
		// Set focus and request Phone Number
		//====================================
//		alert("strPhone.value.length = " + strPhone.value.length);
		document.accountapply.AA17_HomePhone.focus();
		alert("Please Enter Home Phone Number");
		return false;
	}
	else if (strPhone.value.length === 7)
	{
		//=========================================
		// Phone Number is a local 7 digit number
		// Reformat to 999.9999 format
		//=========================================
//		alert("strPhone.value.length = " + strPhone.value.length);
		strExchange = strPhone.value.substr(0, 3);
		strExtension = strPhone.value.substr(3, 4);
		document.accountapply.AA17_HomePhone.value = 
		(strExchange + "." +  strExtension);
	}
	else if (strPhone.value.length === 10)
	{
		//==================================================
		// Phone Number is a 10 digit number with Area Code
		// Reformat to 999.999.9999 format
		//==================================================
//		alert("strPhone.value.length = " + strPhone.value.length);
		strAreaCode = strPhone.value.substr(0, 3);
		strExchange = strPhone.value.substr(3, 3);
		strExtension = strPhone.value.substr(6, 4);
		document.accountapply.AA17_HomePhone.value = 
		(strAreaCode + "." + strExchange + "." +  strExtension);
	}
	else
	{
		//=================================================
		// Phone Number has an incorrect number of digits
		// Set focus and request correct number
		//=================================================
//		alert("strPhone.value.length = " + strPhone.value.length);
		document.accountapply.AA17_HomePhone.focus();
		document.accountapply.AA17_HomePhone.select();
		alert("Local Phone Number invalid");
		return false;
	}
//	alert("Phone =" + document.accountapply.AA17_HomePhone.value);

//	if (document.accountapply.AA17_HomePhone.value === "")
//		{
//			document.accountapply.AA17_HomePhone.focus();
//			alert("Please Enter" + " " + document.accountapply.AA17_HomePhone.id);
//			return false;
//		}					
//
//	enteredNumber=document.accountapply.AA17_HomePhone.value;
//	//-------------------
//	// Strip Punctuation
//	//-------------------
//	for(i=0;i<enteredNumber.length;i++) {
//		enteredNumber=enteredNumber.replace(" ","");
//		enteredNumber=enteredNumber.replace("(","");
//		enteredNumber=enteredNumber.replace(")","");
//		enteredNumber=enteredNumber.replace("-","");
//		enteredNumber=enteredNumber.replace(".","");
//		enteredNumber=enteredNumber.replace("X","");
//		enteredNumber=enteredNumber.replace("x","");
//	}
//
//	if(enteredNumber.length > 10){
//		document.accountapply.AA17_HomePhone.focus();
//		alert(document.accountapply.AA17_HomePhone.id + " must not exceed 10 digits");
//		return false;
//		}
//	
//	if(enteredNumber.length === 10){	
//			areaCode = enteredNumber.substr(0,3);
//			exchange = enteredNumber.substr(3,3);
//			extension = enteredNumber.substr(6,4);
//			document.accountapply.AA17_HomePhone.value = (areaCode + "." + exchange + "." +  extension);
//			}
//			
//	if(enteredNumber.length === 7){	
//			exchange = enteredNumber.substr(0,3);
//			extension = enteredNumber.substr(3,4);
//			document.accountapply.AA17_HomePhone.value = (exchange + "." +  extension);
//			}
//	if(enteredNumber.length < 5){
//		extension = "";
//		for (i=0;i<=enteredNumber.length;i++) {	
//				extension = extension + enteredNumber.substr(i,1);
//				}
//		document.accountapply.AA17_HomePhone.value = ("X-" +  extension);
//		}		
//---------------------
// Validate Department
//---------------------
/*
	if (document.accountapply.AA10_Department.value === "Select a Department")
		{
			document.accountapply.AA10_Department.focus();
			alert("Please Select a" + " " + document.accountapply.AA10_Department.id);
			return false;
		}
*/		
		
//--------------------------
// Validate Office Location 
//--------------------------
	if (document.accountapply.AA11_OfficeLocation.value !== "")
		{
		changeCase(document.accountapply.AA11_OfficeLocation);
		}								
//-----------------------
// Validate Office Phone
//-----------------------
//	alert("validate OfficePhone");
//	alert(document.accountapply.AA12_OfficePhone.value);
//	alert(document.accountapply.AA12_OfficePhone.value.length);
	
	//========================================
	// Strip Punctuation
	// Regular Expression RegExp will be used 
	// to remove all non-numeric characters
	//========================================
	regExp = /(\D)*/g;
//	alert(regExp);
	document.accountapply.AA12_OfficePhone.value = 
	document.accountapply.AA12_OfficePhone.value.replace(regExp, ""); 
//	alert(document.accountapply.AA12_OfficePhone.value);
	
	strPhone = document.accountapply.AA12_OfficePhone;
//	alert("strPhone = " + strPhone.value);
//	alert("strPhone Length = " + strPhone.value.length);
	
	if (strPhone.value.length === 0)
	{
		//====================================
		// Phone Number is empty
		// Set focus and request Phone Number
		//====================================
//		alert("strPhone.value.length = " + strPhone.value.length);
		document.accountapply.AA12_OfficePhone.focus();
		alert("Please Enter Office Phone Number");
		return false;
	}
	else if (strPhone.value.length === 7)
	{
		//=========================================
		// Phone Number is a local 7 digit number
		// Reformat to 999.9999 format
		//=========================================
//		alert("strPhone.value.length = " + strPhone.value.length);
		strExchange = strPhone.value.substr(0, 3);
		strExtension = strPhone.value.substr(3, 4);
		document.accountapply.AA12_OfficePhone.value = 
		(strExchange + "." +  strExtension);
	}
	else if (strPhone.value.length === 10)
	{
		//==================================================
		// Phone Number is a 10 digit number with Area Code
		// Reformat to 999.999.9999 format
		//==================================================
//		alert("strPhone.value.length = " + strPhone.value.length);
		strAreaCode = strPhone.value.substr(0, 3);
		strExchange = strPhone.value.substr(3, 3);
		strExtension = strPhone.value.substr(6, 4);
		document.accountapply.AA12_OfficePhone.value = 
		(strAreaCode + "." + strExchange + "." +  strExtension);
	}
	else
	{
		//=================================================
		// Phone Number has an incorrect number of digits
		// Set focus and request correct number
		//=================================================
//		alert("strPhone.value.length = " + strPhone.value.length);
		document.accountapply.AA12_OfficePhone.focus();
		document.accountapply.AA12_OfficePhone.select();
		alert("Local Phone Number invalid");
		return false;
	}
//	alert("Phone =" + document.accountapply.AA12_OfficePhone.value);

//	if (document.accountapply.AA12_OfficePhone.value === "")
//		{
//			document.accountapply.AA12_OfficePhone.focus();
//			alert("Please Enter" + " " + document.accountapply.AA12_OfficePhone.id);
//			return false;
//		}					
//
//	enteredNumber=document.accountapply.AA12_OfficePhone.value;
//	//-------------------
//	// Strip Punctuation
//	//-------------------
//	for(i=0;i<enteredNumber.length;i++) {
//		enteredNumber=enteredNumber.replace(" ","");
//		enteredNumber=enteredNumber.replace("(","");
//		enteredNumber=enteredNumber.replace(")","");
//		enteredNumber=enteredNumber.replace("-","");
//		enteredNumber=enteredNumber.replace(".","");
//		enteredNumber=enteredNumber.replace("X","");
//		enteredNumber=enteredNumber.replace("x","");
//	}
//
//	if(enteredNumber.length > 10){
//		document.accountapply.AA12_OfficePhone.focus();
//		alert(document.accountapply.AA12_OfficePhone.id + " must not exceed 10 digits");
//		return false;
//		}
//	
//	if(enteredNumber.length === 10){	
//			areaCode = enteredNumber.substr(0,3);
//			exchange = enteredNumber.substr(3,3);
//			extension = enteredNumber.substr(6,4);
//			document.accountapply.AA12_OfficePhone.value = (areaCode + "." + exchange + "." +  extension);
//			}
//			
//	if(enteredNumber.length === 7){	
//			exchange = enteredNumber.substr(0,3);
//			extension = enteredNumber.substr(3,4);
//			document.accountapply.AA12_OfficePhone.value = (exchange + "." +  extension);
//			}
//	if(enteredNumber.length < 5){
//		extension = "";
//		for (i=0;i<=enteredNumber.length;i++) {	
//				extension = extension + enteredNumber.substr(i,1);
//				}
//		document.accountapply.AA12_OfficePhone.value = ("X-" +  extension);
//		}
//---------------------------
// Validate Appointment Type
//---------------------------
//	if (document.accountapply.AA13_Position.value === "Select an Appointment Type")
//		{
//			document.accountapply.AA13_Position.focus();
//			alert("Please Select a " + document.accountapply.AA13_Position.id);
//			return false;
//		}
		
//----------------------
// Validate Affiliation
//----------------------
if (document.accountapply.AA18_Affiliation.value != ("Faculty" && "Staff" && "Student"))
		{
			document.accountapply.AA18_Affiliation.focus();
			alert("Please Select a " + document.accountapply.AA18_Affiliation.id);
			return false;
		}

//--------------------------
// Validate Hospital - None
//--------------------------				
		document.accountapply.SubmitButton.focus();
		strConfirm = "";
		strConfirm = "\t\t";
		strConfirm = strConfirm + "         Application Agreement";
		strConfirm = strConfirm + "\r\r";
		strConfirm = strConfirm + "You must obey the rules of the lab, network administrators, and copyright laws.";
		strConfirm = strConfirm + "\r";
		strConfirm = strConfirm + "Violators of these rules may face academic or legal consequences.";
		strConfirm = strConfirm + "\r";
		strConfirm = strConfirm + "Keep your account ID and password to yourself to avoid giving access to unauthorized users.";
		strConfirm = strConfirm + "\r";
		strConfirm = strConfirm + "You are fully responsible for all action taken and committed using your account.";
		strConfirm = strConfirm + "\r\r";
		strConfirm = strConfirm + "\t";
		strConfirm = strConfirm + "         Press <OK> to signify agreement and submit request";
		strConfirm = strConfirm + "\r\r";
		strConfirm = strConfirm + "\t\t";
		strConfirm = strConfirm + "          Press <Cancel> to terminate";
		
		if (confirm(strConfirm)) {		
				return true;		
				}
			else {
				return false;
				}
		
}	
//---------------------------
// End function validateForm
//---------------------------

//---------------------------
// Begin function changeCase
//---------------------------
function changeCase(strTextField) {
	var index;
	var tmpStr;
	var tmpChar;
	var preString;
	var postString;
	var strlen;
	tmpStr = strTextField.value.toLowerCase();
	strLen = tmpStr.length;

	if (strLen > 0)  {
		for (index = 0; index < strLen; index++)  {
			if (index === 0)  {
				tmpChar = tmpStr.substring(0,1).toUpperCase();
				postString = tmpStr.substring(1,strLen);
				tmpStr = tmpChar + postString;
			}
			else {
				tmpChar = tmpStr.substring(index, index+1);
				if (tmpChar === " " && index < (strLen-1))  {
					tmpChar = tmpStr.substring(index+1, index+2).toUpperCase();
					preString = tmpStr.substring(0, index+1);
					postString = tmpStr.substring(index+2,strLen);
					tmpStr = preString + tmpChar + postString;
				}
			}
		}
	}
	strTextField.value = tmpStr;
}
//-------------------------
// End function changeCase
//-------------------------
