/* =========================================================================

JavaScript Source File -- Created with SAPIEN Technologies PrimalScript 3.1

NAME: standard.js

AUTHOR: Anthony Pollock , County of Henrico Public Library
DATE  : 3/20/2009

COMMENT: Javascript routines common to most pages on this site.


============================================================================ */
function setFocus( oArg ){
	// This function sets focus on the first text field on the first form of the calling page.
	var i;
	var objForm;
	var strElementType;

	objForm = document.forms[0];
	// 07/02/2008: modified so a field name can be passed to specify the field to have focus.
	//				See the init() function in this file to see where the argument comes from.
	//				Also see the HTMLHeader function in the ASP code.
	
	var tempvar;
	
 	// alert ( "Typeof=" + typeof( objForm ) )
 	if( ( typeof( objForm ) != "undefined" ) ){
		if( ( typeof( oArg ) != "undefined" ) && ( oArg.length != 0 ) && ( objForm.elements[ oArg ] ) ){	//if oArg is undefined or has a length of zero then no arqument was passed.
			objForm.elements[ oArg ].focus();								//if an argument was passed then set the focus to that field.
			objForm.elements[ oArg ].select();
		} else {								//if no arqument was passed then loop through the elements until
			i=0;								//a text field is found.
			do{
				strElementType = objForm.elements[i].type;
				i++;
			}
			while( strElementType.toLowerCase() != "text" || i >= (objForm.elements.length - 1) );
			
			objForm.elements[i-1].focus(); // Subtract one from i because it gets incremented last in the 'do' loop.
			objForm.elements[i-1].select();
		}
	}
	return true;
}

// Function to determine if the date entered is valid.
function IsDate( sDate, sType ) {
	
	var validDate = true;
	var currentDate = new Date();
	var aDays = new Array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
	var aDate = sDate.value.split( '/' );
	
	//Check to see if the year is a leap year. If so set February (aDays[1]) to 29 days
	if( isLeapYear( aDate[2] ) ) {
		aDays[1] = 29;
	}
	
	if( aDate.length == 3 ) {
		
		if ( ( aDate[0] > 0 ) && ( aDate[0] < 13 ) ) { // Month is between 1 and 12 inclusive
			if ( ( aDate[1] > 0 ) && ( aDate[1] <= aDays[ aDate[0] - 1 ] ) ) { // Days is greater than zero and less than or
																				// equal to the number of days in the month. This
																				// is determined by the month stored in the aDate(0)
																				// element minus one because arrays start at zero
				if( ( aDate[2] >= 1880 ) && ( aDate[2] <= currentDate.getFullYear() ) ) {
					validDate = true;
				} else {
					validDate = false;
				}
			} else {
				validDate = false;
			}
		} else {
			validDate = false;
		}
	} else {
		validDate = false;
	}

	return validDate;
}

function isLeapYear( iYear ){
	//Determine if the year passed is a leap year.
	if( iYear%400 == 0 ){	//If divisible by 400 then it is a leap year
		return true;
	} else {
		if( iYear%100 == 0 ){	//If divisible by 100, but not by 400 then it is not a leap year
			return false;
		} else {
			if( iYear%4 == 0 ){	//Unless it is divisible by 4 then it is a leap year
				return true;
			} else {
				return false;	//Otherwise it is not a leap year
			}
		}
	}
}

//function to check valid email address
function isValidEmail( strEmail ){
	var isValid = true;
	var validRegExp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		
	// search email text for regular exp matches
   	if (strEmail.search(validRegExp) == -1){
     	isValid = false;
   	} 
   	return isValid; 
}

function formatPhone( sPhone ) {
	
	var validChars = '0123456789';
	var formattedPhone = '';
	var Char;
	var i;
	
	for( i=0; i < sPhone.value.length; i++ ) {
		Char = sPhone.value.charAt(i);
		if( validChars.indexOf( Char ) != -1 ) {
			formattedPhone += sPhone.value.charAt(i);
		}
	}
	
	if ( formattedPhone.length < 5 ) { // If 4 digits or less, no formatting is necessary
		return;
	}
	
	if( formattedPhone.length > 7 ) {
		sPhone.value = "(" + formattedPhone.substr( 0, 3 ) + ") " + formattedPhone.substr( 3, 3 ) + "-" + formattedPhone.substr( 6 );
	} else {
		sPhone.value = formattedPhone.substr( 0, 3 ) + "-" + formattedPhone.substr( 3 );
	}
	
}

