/* 	
	teachers.js | Teachers-Teachers.com General 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 1999 Teachers-Teachers.com
*/

/****************************************************************
*	loadFocus(elem)
*
*	Sets the 'elem' element of the form to focus 
*	'elem' -> the first user inputted field in the current content.
*
*	This is usually 0(zero), but can be any form element.
*
****************************************************************/
function loadFocus(elem)
{
	document.forms[0].elements[elem].focus();
}	
/******
*
* 	openWin (opens window for Resume)
*
*******/
/*
MFC: I think this function can be deleted

function openWin(id,k) {
	window.open("v.cfm?i="+id+"&k="+k, ""+id, "width=640,height=560,resizable=yes,scrollbars=yes,toolbar=0,location=0,status=0,directories=0");
}

function openWin2(id,sState) {
	if (document.all)
        var xMax = screen.width, yMax = screen.height;
    else
        if (document.layers)
            var xMax = window.outerWidth, yMax = window.outerHeight;
        else
            var xMax = 640, yMax=480;

    var xOffset = xMax/2, yOffset = yMax/2;
	var bigstr = "width=" + (screen.width-10) + ",height=" + (screen.height-50) + ",top=0,left=0,resizable=yes,scrollbars=0,toolbar=0,location=0,status=0,directories=0,screenX=0";
	window.open("resViewfree2.cfm?ID="+id+"&State="+sState, ""+id, bigstr);
}*/

/*function openPopupWin(templateName,width,height) {
    if (document.all)
        var xMax = screen.width, yMax = screen.height;
    else
        if (document.layers)
            var xMax = window.outerWidth, yMax = window.outerHeight;
        else
            var xMax = 640, yMax=480;

    var xOffset = xMax/2, yOffset = yMax/2;
	var bigstr = "width=" + width + ",height=" + height + ",top=0,left=0,resizable=yes,scrollbars=0,toolbar=0,location=0,status=0,directories=0,left=" + ((xMax-300)/2) + ",top=" + ((yMax-200)/2) + ",screenX=" + ((xMax-300)/2) + ",top=" + ((yMax-200)/2) + "'";
	var fullName = "./popupWindow.cfm" + "?template=" + templateName;
	newwin = window.open(fullName,'popupWindow', bigstr);
}*/

function openPopupWinNoTemplate(fileName,width,height) {
    if (navigator.appName.substring(0,8) == "Netscape") height += 50;
    if (document.all)
        var xMax = screen.width, yMax = screen.height;
    else
        if (document.layers)
            var xMax = window.outerWidth, yMax = window.outerHeight;
        else
            var xMax = 640, yMax=480;

    var xOffset = xMax/2, yOffset = yMax/2;
	var bigstr = "width=" + width + ",height=" + height + ",top=0,left=0,resizable=yes,scrollbars=0,toolbar=0,location=0,status=0,directories=0,left=" + ((xMax-300)/2) + ",top=" + ((yMax-200)/2) + ",screenX=" + ((xMax-300)/2) + ",top=" + ((yMax-200)/2) + "'";
	var fullName = fileName;
	newwin = window.open(fullName,'popupWindow', bigstr);
}

function openPopupWinNoTemplateScroll(fileName,width,height) {
    if (navigator.appName.substring(0,8) == "Netscape") height += 50;
    if (document.all)
        var xMax = screen.width, yMax = screen.height;
    else
        if (document.layers)
            var xMax = window.outerWidth, yMax = window.outerHeight;
        else
            var xMax = 640, yMax=480;

    var xOffset = xMax/2, yOffset = yMax/2;
	var bigstr = "width=" + width + ",height=" + height + ",top=0,left=0,resizable=yes,scrollbars=2,toolbar=0,location=0,status=0,directories=0,left=" + ((xMax-300)/2) + ",top=" + ((yMax-200)/2) + ",screenX=" + ((xMax-300)/2) + ",top=" + ((yMax-200)/2) + "'";
	var fullName = fileName;
	newwin = window.open(fullName,'popupWindow', bigstr);
}

function openPopupWindowJobPosting(fileName,width,height) {
    if (navigator.appName.substring(0,8) == "Netscape") height += 50;
    if (document.all)
        var xMax = screen.width, yMax = screen.height;
    else
        if (document.layers)
            var xMax = window.outerWidth,
                yMax = window.outerHeight;
        else
            var xMax = 640,
                yMax = 480;

    var xOffset = xMax/2,
        yOffset = yMax/2;
        
    var theLeft = ((xMax-100)/2),
        theTop = ((yMax-100)/2),
        theScreenX = ((xMax-100)/2);

    var theLeft = 300,
        theTop = 150,
        theScreenX = ((xMax-100)/2);

	var bigstr = "width=" + width + ",height=" + height + ",top=0,left=0,resizable=yes,scrollbars=2,toolbar=0,location=0,status=0,directories=0,left=" + theLeft + ",top=" + theTop + ",screenX=" + theScreenX + "'";
	var fullName = fileName;
	newwin = window.open(fullName,'popupWindow', bigstr);
}

/*mfc 2006-05-09 not used
function openPopupWinInterview(fileName,width,height) {
    if (navigator.appName.substring(0,8) == "Netscape") height += 50;
    if (document.all)
        var xMax = screen.width, yMax = screen.height;
    else
        if (document.layers)
            var xMax = window.outerWidth, yMax = window.outerHeight;
        else
            var xMax = 640, yMax=480;

    var xOffset = xMax/2, yOffset = yMax/2;
	var bigstr = "width=" + width + ",height=" + height + ",top=0,left=0,resizable=yes,scrollbars=2,toolbar=0,location=0,status=0,directories=0,left=150,top=150,screenX=100,top=100'";
	var fullName = fileName;
	newwin = window.open(fullName,'popupWindow', bigstr);
}*/

function openPopupWinNoTemplateBasic(fileName,width,height) {
    if (document.all)
        var xMax = screen.width, yMax = screen.height;
    else
        if (document.layers)
            var xMax = window.outerWidth, yMax = window.outerHeight;
        else
            var xMax = 640, yMax=480;

    var xOffset = xMax/2, yOffset = yMax/2;
	var bigstr = "width=" + width + ",height=" + height + ",top=0,left=0,resizable=yes,scrollbars=0,toolbar=0,location=0,status=0,directories=0,left=" + ((xMax-300)/2) + ",top=" + ((yMax-200)/2) + ",screenX=" + ((xMax-300)/2) + ",top=" + ((yMax-200)/2) + "'";
	var fullName = fileName;
	newwin = window.open(fullName,'popupWindow', bigstr);
}

/****************************************************************
*
*	trim
*
*	trims the whitespace from the beginning and end of a string.
*
*	receives: 'theString' = the string to be trimmed.
*	returns: the trimmed 'theString'
*
****************************************************************/	
function trim(theString) 
{
	
	var kBlank = " ";

	if(theString.indexOf(kBlank) >= 0)
	{
		for(var i=0; i<theString.length;i++)
			if (theString.charAt(i) != kBlank)				
				break;
		for(var j=theString.length-1; j>=0; j--)
			if (theString.toLowerCase().charAt(j) != kBlank)
				break;
		if(i == theString.length && j == -1)
			// there are no non-blank characters
			newString = "";
		else
			newString = theString.substring(i,j+1);
	}
	else newString = theString;
	return newString;
}

/****************************************************************
*
*	trimCapitals
*
*	trims the whitespace from the beginning and end of a string.
*	
*	Capitalizes words according to the following rules:
*
*	1)	The first character of the input string is always capped.
*	2) 	All words are capitalized UNLESS they are in the following
*		list:
*				a,an,the,is,of,with,or,and,in
*			
*	receives: 'theString' = the string to be trimmed.
*	returns: the trimmed and capitalized 'theString'
*
*
****************************************************************/	
function trimCapitals(theString)
{

	var theBlank = " ";

	var noCapsList = "a,an,the,is,of,with,or,and,in";

	//trim the string first.
	theString = trim(theString);

	//capitalize the first word.
	theString = theString.charAt(0).toUpperCase()+
				theString.substring(1);

	//loop through the string, breaking it into words.
	var myWord = "";
	
	for(var i=0;i<theString.length;i++)
	{
		var startI = i;
		while((theString.charAt(i) != theBlank) && (i < theString.length))
		{
			myWord += theString.charAt(i)
			i++;
		}
		// we've reached the end of the word.
		// now loop through the noCaps list and check it against the words.
		var goingToCap = true;
		var checkWord = "";
		
		for(var j=0;j<noCapsList.length;j++)
		{
			var startJ = j;
			while((noCapsList.charAt(j) != ",") && (j < noCapsList.length))
			{
				checkWord += noCapsList.charAt(j);
				j++;
			}
	
			// now we've got the checkWord.
			if(myWord.toLowerCase() == checkWord.toLowerCase())
			{
				
				goingToCap = false;
				break;
		
			}
			
			checkWord = "";
		}
		
		if(goingToCap)
		{

		theString = theString.substring(0,startI)+
					myWord.charAt(0).toUpperCase()+
					myWord.substring(1)+
					theString.substring(startI+myWord.length);								

		}
		
			myWord = "";				
		}	

	return theString;
}	
/****************************************************************
*
*	trimSentence
*
*	trims the whitespace from the beginning and end of a string.
*	
*	Capitalizes the first word of each sentence.  (The first word
*	in the string, and each word following a period(.))
*
*	receives: 'theString' = the string to be trimmed.
*	returns: the trimmed and capitalized 'theString'
*
****************************************************************/	
function trimSentence(theString)
{

	var theBlank = " ";
	var sentEnd = ".?!";
	
	// trim theString first.
	theString = trim(theString);			
	
	// capitalize the first letter.
	theString = theString.charAt(0).toUpperCase() + 
				theString.substring(1);
	
	// now loop through theString looking for periods.
	// change the number of spaces in between periods to 2
	// and capitalize the first word after the period.
	
	for(var i=0;i<theString.length;i++)
	{
		var startI = i;		
		var isEnd = false;
		if(sentEnd.indexOf(theString.charAt(i)) != -1)
		{
			i++;
			while(theString.charAt(i) == theBlank && i < theString.length)
				i++;
		
			theString = theString.substring(0,startI+1)+"  "+
						theString.charAt(i).toUpperCase()+
						theString.substring(i+1);
		
		}								
	}
	
	return theString;
}		
/******************************************************************
*
*	isNumeric(inputStr)
*
*	makes sure that all characters in inputStr are digits.
*
*******************************************************************/				
function isNumeric(inputStr)
{
	var valid = "1234567890";
		
	for(var i=0;i<inputStr.length;i++)
	{
		if(valid.indexOf(inputStr.charAt(i)) == -1)
		{
			return(false);
		}
	}
	return(true);
}
/****************************************************************
*
*		checkNotNull(listStr)
*
*		receives:  page that it's checking:
*			1 : personal info and School and Contact Info.
*			2 : education
*			3 : teaching experience
*			4 : related experience
*			5 : references
*		
*		also, it gets a comma delimited list of form fields and checks
*		if they're not null.  If they are, it displays error message
*		and sets the focus to the field that caused the error.
*			i.e. checkNotNull(1,"1,2,3,4,5,6,7")  would check fields 1-7
*			and if anything failed, it would display the personal info error msg.
*
*****************************************************************/
function checkNotNull(pagenum,listStr)	
{

	if(pagenum == 1) //personal info & School and Contact Info.
		alertStr = "One or more of the required fields have not been filled in.";
	else if(pagenum == 2) // education
		alertStr = "Because you entered a college, the 'Degree','Major', and 'YOG' fields are required.";
	else if(pagenum == 3)
		alertStr = "Because you entered a school, the 'Subject(s)','Grade(s)',\n'From' date, and 'To' date fields are required.";
	else if(pagenum == 4)
		alertStr = "Because you entered an employer, the 'Position',\n'From' dates, and 'To' dates are required.";
	else if(pagenum == 5)
		alertStr = "Because you entered a reference name, the 'Phone',\n'Company', and 'Position' fields are required.";

	for(var i=0;i<listStr.length;)
	{
		var checkField = "";
		var j = 0;
		
		while(listStr.charAt(i+j) != ',' && listStr.length >= i+j)
		{
			checkField += listStr.charAt(i+j);
			j++;
		}		
		if(trim(document.forms[0].elements[checkField].value) == "")
		{			
			alert(alertStr);			
			document.forms[0].elements[checkField].focus();		
			return(false);
		}
		i+=(j+1);
	}
	return(true);
}

/***********************************************************************************
*
*	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;

}
/****************************************************************
*
*	checkField 
*
*	main user form checking function.  It takes the following parameters:
*
*	checkField(inputstr,elemname,elemindex,funct)
*
*		inputstr = the string to be examined.
*		elemname = the name of the field (to be displayed in the alert box if
*				   the check fails)
*		elemindex = the index of this <input> field (used with trim and focus)
*		funct 	 = the desired check to be applied:
*						1 : Alphabetic
*						2 : AlphaNumeric
*						3 : Zip Code
*						4 : Email Address
*						5 : Phone Number
*
*****************************************************************/
function checkField(inputstr,elemname,elemindex,funct)
{	

	var alerted = 0;
	var lastChar = "";
	
	switch(funct)
	{
	case 1: 
		var valid = " abcdefghijklmnopqrstuvwxyz";
		break;
	case 2:case 6:
		var valid = " abcdefghijklmnopqrstuvwxyz12345678910-.'";
		break;
	case 3:
		var valid = "1234567890-";
		break;
	case 4:
		var valid = "abcdefghijklmnopqrstuvwxyz1234567890-_.@";
		break;
	case 5:
		var valid = "(1234567890).- )";		
		break;	
	}
	if(funct == 6)
		valid += ",+";
		
	inputstr = trim(inputstr);
	
	if(inputstr.length > 0) 
	{
		for(var i=0;i<inputstr.length;i++)
		{
			if(valid.indexOf(inputstr.charAt(i).toLowerCase()) < 0)
			{			
				switch(funct)
				{
					case 1:
						alert("The '"+elemname+"' field contains invalid characters.\nOnly alphabetic characters are valid.");
						alerted = 1;
						break;
					case 2:
						alert("The '"+elemname+"' field contains invalid characters.\nOnly alphanumeric characters, apostrophe, '-', and '.' are valid.");
						alerted = 1;
						break;
					case 3:
						alert("The '"+elemname+"' field contains invalid characters.\nOnly numeric characters and '-' are valid.");
						alerted = 1;
						break;
					case 4:
						alert("The '"+elemname+"' field contains invalid characters.\nOnly alphanumeric characters, '-' , '.' , '@' , and '_' are valid.");
						alerted = 1;
						break;
					case 5:
						alert("The '"+elemname+"' field contains invalid characters.\nOnly numeric characters, parenthesis, '-' , and '.' are valid.");
						alerted = 1;
						break;
					case 6:
						alert("The '"+elemname+"' field contains invalid characters.\nOnly alphanumeric characters, ',' , '.' , '+' , apostrophe, and '-' are valid.");
						alerted = 1;
						break;
				}
				document.forms[0].elements[elemindex].focus();
				break;
			}
			if(lastChar == " " || lastChar == "" || lastChar == ".")
				{
					inputstr = inputstr.substring(0,i) + inputstr.charAt(i).toUpperCase() + inputstr.substring(i+1);
				}	
			lastChar = inputstr.charAt(i);
		}			
		/*** ZIP CODE ***/
		if(funct == 3 && alerted == 0)
		{
			var zipStr = "The '"+elemname+"' field is invalid.\nZip Codes must be formated as follows: XXXXX"		
			
			if(inputstr.length == 10)
			{
				if(inputstr.indexOf('-') != 5 || inputstr.lastIndexOf('-') != 5)
				{
					alert(zipStr);
					alerted = 1;
					document.forms[0].elements[elemindex].focus();			
				}
			}		
			else if(inputstr.length != 5)
			{
					alert(zipStr);
					alerted = 1;
					document.forms[0].elements[elemindex].focus();			
			}	
			else 
			{
				if(inputstr.indexOf('-') != -1)
				{
					alert(zipStr);
					alerted = 1;
					document.forms[0].elements[elemindex].focus();			
				}
			}				 
		}		
		/***EMAIL ADDRESS***/
		if(funct == 4 && alerted == 0)
		{
			if(inputstr.indexOf('@') == -1 || (inputstr.indexOf('@') != inputstr.lastIndexOf('@')))
			{
				alert("The '"+elemname+"' field is invalid.\nEmail addresses must be formatted as follows:\njoesmith@teachers-teachers.com");
				alerted = 1;
				document.forms[0].elements[elemindex].focus();
			}
		}	
		/***PHONE NUMBER***
		
		Max Length of phone number field = 13.
		
			valid formats:			length:
				410-410-4101		12
				(410)430-4032		13
				410.410.4104		12
				4033029393			10
		------------------------------------
				01234567890123
							***/
		if(funct == 5 && alerted == 0)
		{
		
			var strLen = inputstr.length;
			var phonestr = "The '"+elemname+"' field is invalid.\nPhone numbers must be formatted as follows:\n555-555-5555";
			
			switch(strLen)
			{
				case 10:
					for(var t=0;t<inputstr.length;t++)
					{
						if(!(isNumeric(inputstr.charAt(t))))
						{
							alert(phonestr);
							alerted = 1;
							document.forms[0].elements[elemindex].focus();
							break;
						}
					}
					if(alerted == 0)						
						document.forms[0].elements[elemindex].value = inputstr.substring(0,3)+"-"+
																	  inputstr.substring(3,6)+"-"+
																	  inputstr.substring(6);
					break;
				case 12:													
					for(var s=0;s<9;s+=4)
					{
						for(var t=0;t<3;t++)
						{
							if(!(isNumeric(inputstr.charAt(s+t))))
							{
								alerted = 1;
								break;
							}
							if(t==2)
							{

								if(s!=8)
								{
								var hypper = "-.";
									if(hypper.indexOf(inputstr.charAt(s+3)) == -1)
									{
										alerted = 1;
										break;
									}
								}		
								else
								{
									if(!(isNumeric(inputstr.charAt(12))))
									{
										alerted = 1;
										break;
									}
								}
							}									 
						}
						if(alerted == 1) 
						{
							alert(phonestr);
							document.forms[0].elements[elemindex].focus();
							break;
						}						
						else 
						document.forms[0].elements[elemindex].value = inputstr.substring(0,3)+"-"+
																	  inputstr.substring(4,7)+"-"+
																	  inputstr.substring(8);
					}
					break;
				case 13:
					var hypper = "-.";

					if(inputstr.charAt(0) != '(')
						alerted = 1;
					else if(inputstr.charAt(4) != ')')
						alerted = 1;
					else if(hypper.indexOf(inputstr.charAt(8)) == -1)
						alerted = 1;					
					if(alerted == 0)
					{
					for(var s=1;s<10;s+=4)
					{
						for(var t=0;t<3;t++)
						{
							if(!(isNumeric(inputstr.charAt(s+t))))
							{
								alerted = 1;
								break;
							}
							if(t==2)
							{																
								if(s==10)
								{
									if(!(isNumeric(inputstr.charAt(12))))
									{
										alerted = 1;
										break;
									}
								}
							}									 							
						}
					}
					}
						if(alerted == 1) 
						{
							alert(phonestr);
							document.forms[0].elements[elemindex].focus();
							break;
						}						
						else 
						document.forms[0].elements[elemindex].value = inputstr.substring(1,4)+"-"+
																	  inputstr.substring(5,8)+"-"+
																	  inputstr.substring(9);
					break;		
					default:
						alert(phonestr);
						alerted = 1;
						document.forms[0].elements[elemindex].focus();								
						break;
			}	
		}
		if(funct != 5)
		{
			if(funct == 4)
				inputstr = inputstr.toLowerCase();
			document.forms[0].elements[elemindex].value = inputstr;			
		}
	}	
	if(alerted==1)
		return(false);
	else 
		return(true);

}



/****************************************************************
*
*		checkNull(listStr)
*
*		gets a comma delimited list of form fields and checks
*		if they're null.  If they ALL are, returns true.  
*		returns false otherwise.
*
*****************************************************************/		
function checkNull(listStr)
{
	for(var i=0;i<listStr.length;)
	{
		var checkField = "";
		var j = 0;
		
		while(listStr.charAt(i+j) != ',' && listStr.length >= i+j)
		{
			checkField += listStr.charAt(i+j);
			j++;
		}		
		if(document.forms[0].elements[checkField].value != "")
		{			
			return(false);
		}
		i+=(j+1);
	}
	return(true);
}
/****************************************************************
*
*	navSet(lnk,od)
*
*	sets the 'link' and 'oid' form fields.
*	(for use with navigation)
*
****************************************************************/
function navSet(lnk,od)
{

	document.forms[0].link.value = lnk;
	if(document.forms[0].oid)
		document.forms[0].oid.value = od;

	document.forms[0].submit();

}

/****************************************************************
*
*	navGo(lnk,pd)
*
*	sets the 'link' and 'oid' form fields.
*	(for use with navigation)
*
****************************************************************/
function navGo(lnk,pd)
{

	document.forms[0].link.value = lnk;
	if(document.forms[0].pid)
		document.forms[0].pid.value = pd;

	document.forms[0].submit();

}

/****************************************************************
*
*	postJob()
*
*	Copies data from the postJob popupbox to the main window
*	for posting
*<a href="javascript:window.opener.document.forms[0].oid.value='accept';window.opener.document.forms[0].link.value='Search_Results';window.opener.document.forms[0].submit();window.submit();window.close();"><img src="http://www.teachers-teachers.com/images/submit.gif" border="0"></a>
****************************************************************/
function postJob()
{

	document.forms[0].hJobDescription.value = document.forms[0].Job_Description.value;
	document.forms[0].hQualifications.value = document.forms[0].Qualifications.value;
	document.forms[0].hStartDate.value = document.forms[0].date1.value;
	document.forms[0].hJobCode.value = document.forms[0].JobCode.value;
	document.forms[0].submit();
}




function prevNext(num)
{
	document.forms[0].startrow.value = parseInt(document.forms[0].startrow.value) + parseInt(num);	
}

function DatePosition(dateString) {
    var now = new Date();
    var today = new Date(now.getYear(),now.getMonth(),now.getDate());
    	var date = new Date(dateString.substring(dateString.lastIndexOf("/")+1,dateString.length ),
        	                    dateString.substring(0,dateString.indexOf("/"))-1,
                	            dateString.substring(dateString.indexOf("/")+1,dateString.lastIndexOf("/")));
   //alert(date.getMonth()+'/'+date.getDate()+'/'+date.getFullYear());
   // if the current new date is earlier than the current date then check the immediately button
    if (date <= today)
    {
//        document.write(date.toString(),' is behind ',today.toString(),'<P>');
	document.forms[0].nostartCheck.checked = true;
	document.forms[0].startCheck.checked = false;
        return -1;
    }
    // if the current new date is earlier than the current date then uncheck the immediately button
    else (date > today)
    {
	document.forms[0].nostartCheck.checked = false;
	document.forms[0].startCheck.checked = true;
    }
}

function restrictResumeSearch(oid) {

	if (document.forms[0].usonly.checked == true)
		document.forms[0].link.value = "usonly";
	else
		document.forms[0].link.value = "worldwide";
	document.forms[0].submit();
}
