/* =========================================================================

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 ) ) {	// Notification 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 ) ) {	// Notification 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 ( getCheckedValue( objForm.Holds ) == "" ) {		// Holds notification
		strError += "- Must choose notification type\r\n";
	}
	
	if ( objForm.LastName.value.length == 0 ) {		// Last Name
		strError += "- Last name is required\r\n";
		objForm.LastName.focus();
	}
	
	if ( objForm.CardNo.value.length < 14 ) {		// Card Number too short. Maxsize on the for keeps it from being too long
		strError += "- Library card number must be 14 characters with no spaces\r\n";
		objForm.CardNo.select();
		objForm.CardNo.focus();
	}
	
	if ( objForm.CardNo.value.length == 0 ) {		// Card Number
		strError += "- Library card number is required\r\n";
		objForm.CardNo.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.CardNo.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 = 1000 * 60 * 60 * 24 * 365.25	//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 );
	}
	
}

