/* 	

	sProfile.js | School Profile JavaScript Functions
	----------------------------------------------------------
	ALL JavaScript included in this file is the property of 
	Teachers-Teachers.com
	
	None of this code may be used (in current or altered form) 
	by any other party without the express written consent of
	Teachers-Teachers.com
	
	Copyright 2000 Teachers-Teachers.com
	
*/

/***********************************************************************************
*
*	NAME:		checkSchoolInfo
*	AUTHOR:		shanon levenherz (shanonvl@hotmail.com)
*	DATE:		05.21.2000
*
*	DESCRIPTION:	checks school information page for schoolProfile section.
*
***********************************************************************************/
function checkSchoolInfo()
{

	var alerted = true;
	var formVar = document.forms[0];

	formVar.sSchoolName.value   = trimCapitals(formVar.sSchoolName.value);
	formVar.sAddress1.value 	= trimCapitals(formVar.sAddress1.value);
	formVar.sAddress2.value 	= trimCapitals(formVar.sAddress2.value);
	formVar.sCity.value 		= trimCapitals(formVar.sCity.value);

	alerted = chkNotNull(1,'sSchoolName,sAddress1,sCity,sZip');
	if(alerted)
		alerted = checkZip(formVar.sZip);

// web page address.
// mission statement.
// description of school/schoolSystem.
// neighborhood type.	
	
	return(alerted);
}

/***********************************************************************************
*
*	NAME:		chkNotNull
*	AUTHOR:		shanon levenherz (shanonvl@hotmail.com)
*	DATE:		05.21.2000
*
*	DESCRIPTION:	checks that a list of form elemnents have length > 0
*
***********************************************************************************/
function chkNotNull(page_num,listStr)
{
	
	if(page_num == 1) //personal info & School and Contact Info.
		alertStr = "One or more of the required fields have not been filled in.";
	else if(page_num == 2) // education
		alertStr = "Because you entered a college, the 'Degree','Major', and 'YOG' fields are required.";
	else if(page_num == 3)
		alertStr = "Because you entered a school, the 'Subject(s)','Grade(s)',\n'From' date, and 'To' date fields are required.";
	else if(page_num == 4)
		alertStr = "Because you entered an employer, the 'Position',\n'From' dates, and 'To' dates are required.";
	else if(page_num == 5)
		alertStr = "Because you entered a reference name, the 'Phone',\n'Company', and 'Position' fields are required.";

	var formStr = 'document.forms[0].';
	var listArray = listStr.split(',');
	
	for(var i=0;i<listArray.length;i++)
	{
		var formElem = eval(formStr+listArray[i]);
		var formVal = trim(formElem.value);		

		if(formVal.length == 0)
		{
			alert(alertStr);
			formElem.focus();
			return false;
		}
	}
	return true;

}

/***********************************************************************************
*
*	NAME:	checkContactInfo
*	AUTHOR:	shanon levenherz (shanonvl@hotmail.com)
*	DATE:	05.21.2000
*
*	DESCRIPTION:	checks contact information page for schoolProfile section.
*
***********************************************************************************/
function checkContactInfo()
{

	var alerted = true;
	var formVar = document.forms[0];

	alerted = chkNotNull(1,'sFirstName,sLastName,sPosition,sPhone,sEmail,sNumHiring');
	
	if(alerted)
	{
		formVar.sFirstName.value = trimCapitals(formVar.sFirstName.value);
		formVar.sLastName.value = trimCapitals(formVar.sLastName.value);
		formVar.sPosition.value = trimCapitals(formVar.sPosition.value);
		alerted = checkPhone(formVar.sPhone,'Phone');
	}
	
	if(alerted && isNotNull(formVar.sFax))
		alerted = checkPhone(formVar.sFax,'Fax');
	if(alerted)
		alerted = checkEmail(formVar.sEmail);

	// num schools.
	if(alerted)
	{
		if(!(parseInt(formVar.sNumHiring.value) > 0))
		{
			alert('You must specify an integer greater than zero in this field.');
			formVar.sNumHiring.focus();
			alerted = false;
		}
		else
		{
			formVar.sNumHiring.value = parseInt(formVar.sNumHiring.value);
		}
	}

	return(alerted);

}
/***********************************************************************************
*
*	NAME:	checkBillingInfo
*	AUTHOR:	shanon levenherz (shanonvl@hotmail.com)
*	DATE:	05.21.2000
*
*	DESCRIPTION: checks billing information page for schoolProfile section
*
***********************************************************************************/
function checkBillingInfo()
{
	var formVar = document.forms[0];
	
	if(!chkNotNull(1,'sPONumber'))
	{
		return false;
	}
	
	var good = confirm('Please confirm your purchase order number:\n\nYou entered: \''+formVar.sPONumber.value+'\'\n\nIf this is correct, click OK.');

	if(!good)
		formVar.sPONumber.focus();

	return(good);	

}
/***********************************************************************************
*
*	NAME:	checkUserProfile
*	AUTHOR:	shanon levenherz (shanonvl@hotmail.com)
*	DATE:	05.21.2000
*
*	DESCRIPTION: checks user profile page for schoolProfile section.
*
***********************************************************************************/
function checkUserProfile()
{
	var valid_pw = "abcdefghijklmnopqrstuvwxyz1234567890_!*-#+";
	var alertStr = "The 'Password' field is invalid.  Passwords contain no spaces, are between 6 and 50 characters, and can contain only alphanumeric characters, !, *, -, #, and +.";
	var formVar = document.forms[0];
	
	if(!chkNotNull(1,'sPassWord,sPassConf'))
	{
		return false;
	}

	formVar.sPassWord.value = trim(formVar.sPassWord.value);
	formVar.sPassConf.value = trim(formVar.sPassConf.value);

	pw_val = formVar.sPassWord.value.toLowerCase();
	pc_val = formVar.sPassConf.value.toLowerCase();

	if(pw_val.length > 50 || pw_val.length < 6)
	{

		alert(alertStr);
		pw_val = '';
		pc_val = '';
		formVar.sPassWord.focus();						
		return false;
	}
		
	for(var i=0;i<pw_val.length;i++)
	{
		if(valid_pw.indexOf(pw_val.charAt(i)) < 0)
		{
			alert(alertStr);
			pw_val = '';
			pc_val = '';
			formVar.sPassWord.focus();			
			return false;
		}
	}
	
	if(pw_val != pc_val)
	{
		alert('The \'Password\' and the \'Confirm Password\' fields do not match.  Please try again.');
		pw_val = '';
		pc_val = '';
		formVar.sPassWord.focus();			
		return false;
	}
	
	return true;

}
/***********************************************************************************
*
*	NAME: 	isNotNull(formElem)
*	AUTHOR:	shanon levenherz (shanonvl@hotmail.com)
*	DATE:	05.21.2000
*
*	DESCRIPTION: checks that the input element has a user-input value (length > 0)
*
***********************************************************************************/
function isNotNull(f_var)
{
	f_var.value = trim(f_var.value);
	if(f_var.value.length <= 0)
	{
		return false;
	}
	return true;		
}
/***********************************************************************************
*
*	NAME:		checkZip
*	AUTHOR:		shanon levenherz (shanonvl@hotmail.com)
*	DATE:		05.20.2000
*
*	DESCRIPTION: uses JavaScript regular expressions to check 5 digit zip codes.
*
*	RETURNS:	true if is valid zip code, else false.
*
***********************************************************************************/
function checkZip(c_zip)
{

	var zipExp = /^([0-9]{5})$/;

	if(!zipExp.test(c_zip.value))
	{
		alert('Invalid zip code \''+c_zip.value+'\'\nZip codes must be in 5-digit format: XXXXX');
		c_zip.focus();
		return false;
	}

	return true;
	
}
/***********************************************************************************
*
*	NAME:		checkPhone
*	AUTHOR:		shanon levenherz (shanonvl@hotmail.com)
*	DATE:		05.20.2000
*
*	DESCRIPTION: uses JavaScript regular expressions to check valid phone numbers.
*
*		These formats are valid:  	XXX-XXX-XXXX
*									(XXX)XXX-XXXX
*									XXX.XXX.XXXX
*
*	RETURNS:	true if is valid zip code, else false.
*
***********************************************************************************/
function checkPhone(c_phone,elemName)
{

	var tmpStr = '';
        tmpStr=c_phone.value;

	if (tmpStr.substring(0,1) == '1') 
        {
          c_phone.value=tmpStr.substring(1);
        }

	var phoneExp = (c_phone.value.indexOf('(') > -1) ? /\((\d{3})\)(\d{3})[-.](\d{4})/ : /(\d{3})[-. ]?(\d{3})[-. ]?(\d{4})/;
	
	//trim the values.
	c_phone.value = trim(c_phone.value);
	
	if(!phoneExp.test(c_phone.value))
	{
		alert('The \''+elemName+'\' field is invalid.\nPhone numbers must be formatted as follows:\n555-555-5555');
		c_phone.focus();
		return false;
	}
	else // if there's a match, fix the value how we want it.
	{
		c_phone.value = c_phone.value.replace(phoneExp,"$1-$2-$3");
	}
	return true;
	
}
/***********************************************************************************
*
*	NAME:	checkEmail
*	AUTHOR:	shanon levenherz (shanonvl@hotmail.com)
*	DATE:	05.20.2000
*
*	DESCRIPTION:	uses Javascript regular expressions to check valid email addresses.
*
*		Input is required to have at least the following pattern:
*			alphanumerictext@alphanumerictext.alphanumerictext
*
***********************************************************************************/
function checkEmail(c_email)
{

	var emailExp = /^[\S]+@[\S]+.[\S]+$/;
	
	c_email.value = trim(c_email.value);
	
	if(!emailExp.test(c_email.value))
	{
		alert('The \'Email\' field is invalid.\nEmail addresses must be formatted as follows:\njoesmith@teachers-teachers.com');
		c_email.focus();
		return false;
	}	
	
	return true;
	
}

function checkSchoolInfo2()
{

	var alerted = true;

	document.forms[0].sSchoolName.value = trimCapitals(document.forms[0].sSchoolName.value);
	document.forms[0].sCity.value 		= trimCapitals(document.forms[0].sCity.value);
	document.forms[0].sAddress1.value 	= trimCapitals(document.forms[0].sAddress1.value);
	document.forms[0].sAddress2.value 	= trimCapitals(document.forms[0].sAddress2.value);
	alerted = checkField(document.forms[0].sZip.value,'Zip Code',5,3);
	document.forms[0].sFirstName.value 	= trimCapitals(document.forms[0].sFirstName.value);
	document.forms[0].sLastName.value 	= trimCapitals(document.forms[0].sLastName.value);
	document.forms[0].sPosition.value 	= trimCapitals(document.forms[0].sPosition.value);
	if(alerted == true)
		alerted = checkField(document.forms[0].sPhone.value,'Phone',9,5);
	if(alerted == true)
		alerted = checkField(document.forms[0].sFax.value,'Fax',10,5);
	if(alerted == true)
		alerted = checkField(document.forms[0].sEmail.value,'E-mail',11,4);
	if(alerted == true)
		alerted = checkNotNull(1,"0,1,2,6,7,8,9,11");
		
	return(alerted);
}
