//---------------------------------------------------------------------------------
// Function name: changeDate
// Inputs: object as form object, myvalue as string
// Description: This function adds a leading zero to any number below zero
// also attempts to parse the string into a valid number
//---------------------------------------------------------------------------------
function changeDate(object){
	myvalue = object.value;
	// Parse the value in the box into an integer, base 10
	myvalue = parseInt(myvalue, 10);
	// If its a number and its less than 10, return value with a leading zero
	if (myvalue < 10 && myvalue > 0) {
		object.value = "0" + myvalue;
	}
	// If the value is not a valid positive number, set it to zero
	else if( isNaN(myvalue) || myvalue < 0){
		object.value = 0;
	}
	// Otherwise just return the parsed value (should always be a number)
	else{
		object.value = myvalue;
	}
}



//---------------------------------------------------------------------------------
// Function name: changeYear
// Inputs: object as form object, myvalue as string
// Description: This function attempts to turn myvalue into a valid 4 digit date
//---------------------------------------------------------------------------------
function changeYear(object){
	myvalue = object.value;
	// Parse the value into an integer, base 10
	myvalue = parseInt(myvalue, 10);
	if(myvalue < 100 && myvalue > 5){
		object.value = myvalue + 1900;
	}
	else if(myvalue < 100 && myvalue < 5){
		object.value = myvalue + 2000;
	}
	else if(isNaN(myvalue)){
		alert("Please enter a 4 digit year eg. 1970");
		object.focus();
	}
	else{
		object.value = myvalue;
	}
}

// Checks to see if the date passed in is in the past
function checkPastDate(day, month, year){
	var oldDate = new Date(year, (month - 1), day, 0, 0, 0, 0);
	var today = new Date();
	if(Date.parse(oldDate) < Date.parse(today)){
		return true;
	}
	return false;
}

// Email validation
function isEmailAddr(email){
	var result = false
	var theStr = new String(email)
	var index = theStr.indexOf("@");
	if (index > 0){
		var pindex = theStr.indexOf(".",index);
		if ((pindex > index+1) && (theStr.length > pindex+1))
			result = true;
		}
	return result;
}


//---------------------------------------------------------------------------------
// Function name: makeNumber
// Inputs: object as form object
// Description: This function checks that the form element contains numeric digits
// 	only.  If something other than numbers are present, it will be made blank
//---------------------------------------------------------------------------------
function makeNumber(object){
	// Parse the value in the box into an integer, base 10
	myvalue = new String(object.value);
	object.value = myvalue.replace(/[^0-9]/g, '');
}


// Pops up an alert with the error message, then returns false, this just makes the validate function a little cleaner
function showError(oElement, errorMessage){
	alert(errorMessage);
	if(oElement != false){
		oElement.focus();
	}
	return false;
}

function validDate(myday, mymonth, myyear){
	// Return false if any of the fields are empty
	if(myday == '' || mymonth == '' || myyear == ''){
		return false;
	}
	
	// Array holding the amount of days for each month
	var monthDays = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
	
	// Check we have a valid month
	if(mymonth < 1 || mymonth > 12){
		return false;
	}
	
	// Check if there are more than the allowed number of days for this month
	if(myday > monthDays[(mymonth - 1)] || myday < 1){
		return false;
		}
	// Check if its a leap year if they have selected February 29th
	else if(mymonth == 2 && myday == 29){
		if( !(( myyear%4==0 && myyear%100 !=0 ) || ( myyear%400==0 )) ){
			return false;
			}
		else{
			return true;
			}
		}
	else{
		return true;
	}
}

function validate(frm){
	// Don't validate if we are deleting a client
	if(frm.frmAction.value == 'delete'){
		return true;
	}
	
	if(frm.givenname.value == ''){
		return showError(frm.givenname, 'Please enter the clients given name');
	}
	else if(!validDate(frm.dobday.value, frm.dobmonth.value, frm.dobyear.value)){
		return showError(frm.dobday, 'Please enter a valid date of birth');
	}
	else if(frm.username.value == ''){
		return showError(frm.username, 'Please enter a username');
	}
	else if(frm.surname.value == ''){
		return showError(frm.surname, 'Please enter a surname');
	}
	else if(frm.password.value == '' && frm.name != 'frmEditClient'){
		return showError(frm.password, 'Please enter a password');
	}
	
}