/* =========================================================================

JavaScript Source File -- Created with SAPIEN Technologies PrimalScript 3.1

NAME: 

AUTHOR: Anthony Pollock , County of Henrico Public Library
DATE  : 2/17/2009

COMMENT: 

============================================================================ */

function validateForm(){
	var objForm = document.forms[0];
	var strError = "";
	
	if ( ( getCheckedValue( objForm.Holds ) == "Phone" ) &&  ( objForm.Phone.value.length == 0 ) ) {	// Card Type
		strError += "- You selected phone for your holds notification, but no phone number was entered\r\n";
		objForm.Phone.select();
		objForm.Phone.focus();
	}
	
	if ( ( getCheckedValue( objForm.Holds ) == "Email" ) &&  ( objForm.Email.value.length == 0 ) ) {	// Card Type
		strError += "- You selected email as your holds notification, but no email address was entered\r\n";
		objForm.Email.select();
		objForm.Email.focus();
	}
	
	if ( objForm.Email.value.length > 0 ) {
		if ( !isValidEmail( objForm.Email.value ) ) {	// Email format
			strError += "- Not a valid email address\r\n";
			objForm.Email.select();
			objForm.Email.focus();
		}
	}
	
	if ( ( getAge( objForm.DOB.value ) < 18 ) && ( objForm.Parent.value.length == 0 ) ) { // Parent name required if under 18
		strError += "- Parent/Guardian name required\r\n"
		objForm.Parent.select();
		objForm.Parent.focus();
	}
	
	if ( objForm.Email.value != objForm.EmailRetype.value ) { // Compare email addresses
		strError += "- Email addresses do not match\r\n";
		objForm.Email.select();
		objForm.Email.focus();
	}

	if ( getCheckedValue( objForm.CardType ) == "" ) {	// Card Type
		strError += "- Must choose the type of card\r\n";
	}
	
	if ( getCheckedValue( objForm.GetCard ) == "" ) {	// Receive card method
		strError += "- Must choose how to receive your card\r\n";
	}
	
	if ( getSelectedValue( "HomeBranch" ) == "void" ) {	// Home branch
		strError += "- Must choose your home branch\r\n";
		objForm.HomeBranch.focus();
	}
	
	if ( getCheckedValue( objForm.Holds ) == "" ) {		// Holds notification
		strError += "- Must choose notification type\r\n";
	}
	
	if ( getSelectedValue( "Residence" ) == "None" ) {	// County/City of Residence
		strError += "- County/City of residence is required\r\n";
		objForm.Residence.focus();
	}
	
	if ( objForm.Zip.value.length == 0 ) {			// Zip
		strError += "- Zip is required\r\n";
		objForm.Zip.select();
		objForm.Zip.focus();
	}
	
	if ( objForm.City.value.length == 0 ) {			// City
		strError += "- City is required\r\n";
		objForm.City.select();
		objForm.City.focus();
	}
	
	if ( objForm.Street.value.length == 0 ) {		// Street
		strError += "- Street is required\r\n";
		objForm.Street.select();
		objForm.Street.focus();
	}
	
	if ( objForm.DOB.value.length == 0 ) {			// DOB
		strError += "- Date of Birth is required\r\n";
		objForm.DOB.select();
		objForm.DOB.focus();
	}
	
	if ( !IsDate( objForm.DOB, "field" ) ) {		// DOB format and valid date
		strError += "- Not a valid Date of Birth\r\n";
		objForm.DOB.select();
		objForm.DOB.focus();
	}		
	
	if ( objForm.FirstName.value.length == 0 ) {	// First Name
		strError += "- First name is required\r\n";
		objForm.FirstName.focus();
	}
	
	if ( objForm.LastName.value.length == 0 ) {		// Last Name
		strError += "- Last name is required\r\n";
		objForm.LastName.focus();
	}
	
	if ( strError.length > 0 ) {
		alert( "Please correct the following errors:\r\n\r\n" + strError );
		return false;
	} else {
 		objForm.submit();
	}
}

function resetForm() {
	var objForm = document.forms[0];
	
	objForm.reset();
	objForm.LastName.focus();
}

//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 IsDate( sDate, sType ) {
	
	var validDate = true;
	var currentDate = new Date();
	var aDays = new Array( 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
	var aDate = sDate.value.split( '/' );
	
	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;
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// function to return the selected value of the SELECT tag with the ID passed.	
function getSelectedValue( SelectID ) {

	var ddllist= window.document.getElementById(SelectID);
	var itemName= ddllist.options[ddllist.selectedIndex].value;
		
	return itemName;
}

function getAge( strDOB ) {
	var DOB = new Date( strDOB );
	var today = new Date();
	var oneYear = 3.1556926 * Math.pow(10, 10)	//milliseconds in one year
//	alert ( ( today.getTime() - DOB.getTime() ) / oneYear )
	if ( strDOB.length > 0 ) {
		return ( today.getTime() - DOB.getTime() ) / oneYear ;
	} else {
		return 0;
	}
}

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 );
	}
	
}

