 /**
 * 
 * File		:		data_validation.js  
 * Author	:		Peter Cotton 
 * Date		:		Friday 2nd June 2006
 * Purpose	:		Functions to verify data from HTML forms.
 * Note		:		Much of the code is taken from a file used for
 *					HAN1.0 development (2005).
 * 
 * History	:		Version 2.0
 *			:		28th June 2006
 *			:		By Peter Cotton
 *			:		Added isEmail(value) to check string format is ___@___._
 * 
 * 					Version 1.0 Original
 * 
 *  */
 
 
 
 
 /**
 * Method to check if a given value is a valid email string e.g. ___@___._
 * @param	value
 * @return	void
 * */
 function isEmail(value){

	// if an empty string has been passed
	if (value.length == 0)
		return false;

	// if there are any spaces in the string
	if (value.indexOf(' ') != -1)
		return false;

	// get the indexes of the @ symbol and the . (full stop)
	var index_of_a = value.indexOf('@');
	var index_of_fs = value.lastIndexOf('.');
	
	// check that there is an @ symbol
	if (index_of_a == -1)
		return false;

	// check that there is a '.' symbol
	if (index_of_fs == -1)
		return false;

	// check that the @ is NOT the first symbol
	if (index_of_a == 0)
		return false;

	// check that the . is NOT the last symbol
	if (index_of_fs == value.length-1)
		return false;

	// check that there is more than one character seperating the @ and the .
	if (index_of_fs < index_of_a+2)
		return false;	

// TAKEN OUT AS THIS IS ACTUALLY OK
	// check that there is only a single . (full stop)
//	var after_a = value.substring(index_of_a, value.length-1);
//	if (after_a.indexOf('.') != after_a.lastIndexOf('.'))
//		return false;

	// check that there is only one @ symbol
	if (index_of_a != value.lastIndexOf('@'))
		return false;

	// the value is an email address
	return true;
 }
 
 
 
 
 
 /**
 * Method to check if a given value is a percentage (int between 0 and 100)
 * @param value
 * @return none
 * */
 function isPercent(value){
 
 	// use isPosInt to see if its a positive integer
 	if(!isPosInt(value))
 		return false;
 		
 	// if the value is greater than 100
 	if(value > 100)
 		return false;
 		
 	// the value is a percent
 	return true;
 }
 
 
 
 /**
 * Method to check if a value is an integer
 * @param value
 * @return none
 * */
 function isInt(value){
 
 	// check it isn't all white space
 	reWhiteSpace = new RegExp(/^\s+$/);
 	if (reWhiteSpace.test(value))
 		return false;
 	
 	// if the value is empty
 	if(value.length == 0)
 		return false;
 
 	// if the value is not numeric
 	if(isNaN(value))
 		return false;
 		
 	// if the value is not equal to the closest integer
 	// best way i've found for checking if its an integer
 	if(value != Math.round(value))
 		return false;
 	
 	// variable is an integer
 	return true;
 
 }
 
 
 /**
 * Method to check if a value is a positive integer
 * @param value
 * @return none
 * */
 function isPosInt(value){
 
 	// use isInt to see if it is an integer
 	if(!isInt(value))
 		return false;
 		
 	// if the value is less than 0
 	if(value < 0)
 		return false;
 	
 	// the value is a positive integer
 	return true;
 
 }
 
 
  /**
 * Method to check if a variable is a Unix Time
 * The input is a string where the format must be checked
 * "hh:mm:ss"
 * @param $value = the variable in question
 * @return boolean result
 * */
function isTime(value){
 		
 		success = true;
 		
 		// Extract the Time components.
	   	var time = value.split(":");
	   	
		// check that there are three time components
		if (time.length != 3)
			return false;
	   	
		// check that all time components were retrieved
		if (time[0] == null || time[0].length == 0)
			return false;
		if (time[1] == null || time[1].length == 0)
			return false;
		if (time[2] == null || time[2].length == 0)
			return false;
			
	   	var hour = time[0];
	   	var minute = time[1];
	  	var second = time[2];
	   	
	   	if (hour.length != 2 || !isPosInt(hour)) {
	   		success = false;	
	   	}
	   	if (minute.length != 2 || !isPosInt(minute)) {
	   		success = false;	
	   	}
	   	if (second.length != 2 || !isPosInt(second)) {
	   		success = false;	
	   	}
	   	
	   	return success;
 	}
 
 
 /**
 * Method to check if a variable is a Unix Timestamp
 * The input is a string where the format must be checked
 * "YYYY-MM-DD hh:mm:ss"
 * @param	value = the variable in question
 * 			inFuture = boolean (if true, check the timestamp is += today)
 * @return boolean result
 * */
function isTimestamp(value, inFuture){
 		
 		success = true;
 		
 		// Extract the Timestamp components.
		var date_time = value.split(" ");
		
		// check that there are two components
		if (date_time.length != 2)
			return false;
		
		// check that two string components were retrieved (date and time)
		if (date_time[0] == null || date_time[0].length == 0)
			return false;
		if (date_time[1] == null || date_time[1].length == 0)
			return false;
		
	  	var date = date_time[0].split("-");
	   	var time = date_time[1].split(":");
	   	
	   	// check that there are three date components
		if (date.length != 3)
			return false;
			
		// check that there are three time components
		if (time.length != 3)
			return false;
	   	
	   	// check that all date components were retrieved
		if (date[0] == null || date[0].length == 0)
			return false;
		if (date[1] == null || date[1].length == 0)
			return false;
		if (date[2] == null || date[2].length == 0)
			return false;
			
		// check that all time components were retrieved
		if (time[0] == null || time[0].length == 0)
			return false;
		if (time[1] == null || time[1].length == 0)
			return false;
		if (time[2] == null || time[2].length == 0)
			return false;
			
	   	var year = date[0];
	  	var month = date[1];
	  	var day = date[2];
	   	var hour = time[0];
	   	var minute = time[1];
	  	var second = time[2];
	   	
	   	// validate each component
	   	if (year.length != 4 || !isPosInt(year)) {
	   		success = false;	
	   	}
	   	if (month.length != 2 || !isPosInt(month)) {
	   		success = false;	
	   	}
	   	if (day.length != 2 || !isPosInt(day)) {
	   		success = false;	
	   	}
	   	if (hour.length != 2 || !isPosInt(hour)) {
	   		success = false;	
	   	}
	   	if (minute.length != 2 || !isPosInt(minute)) {
	   		success = false;	
	   	}
	   	if (second.length != 2 || !isPosInt(second)) {
	   		success = false;	
	   	}
	   	
	   	
	   	// validate month
	   	if (month < 1 || month > 12){
	   		success = false;
	   	}
	   	
	   	// validate day of month
		if(day < 1)
			success = false;
		
		// max days depends on month
	   	switch(month){
	   		case '02':
	   			if(day > 28){
	   				success = false;
	   			}
	   		break;
	   		case '04':
	   		case '06':
	   		case '09':
	   		case '10':
	   			if(day > 30){
	   				success = false;
	   			}
	   		break;
	   		case '01':
	   		case '03':
	   		case '05':
	   		case '07':
	   		case '08':
	   		case '11':
	   		case '12':
	   			if(day > 31){
	   				success = false;
	   			}
	   		break;
	   	}
	   	
	   	// validate hour
	   	if (hour < 0 || hour > 23){
	   		success = false;
	   	}
	   	
	   	// validate minute
	   	if (minute < 0 || minute > 59){
	   		success = false;
	   	}
	   	
	   	// validate seconds
	   	if (second < 0 || second > 59){
	   		success = false;
	   	}
	   	
	   	// if inFuture, check that the timestamp is for before today
	   	if (inFuture){
	   		// get new datetime
	   		Stamp = new Date();
	   		
	   		// if the year is in the past, fail
	   		if (year < Stamp.getYear()){
	   			success = false;
	   		} else {
	   		
	   			// if the year is this year
	   			if (year == Stamp.getYear()){
	   				
	   				// if the month is in the past, fail
	   				if (month < (Stamp.getMonth()+1)){
	   					success = false;
	   				} else {
	   				
	   					// if the month is this month
	   					if (month == (Stamp.getMonth()+1)){
	   						
	   						// if the day is in the past, fail
	   						if (day < Stamp.getDate()){
	   							success = false;
	   						}// end of check day if
	   					}
	   				}// end of check month if
	   			}
	   		}// end of check year if
	   	}
	   	
	   	return success;
 	}
 	
/**
 * Method to check if a variable is a Unix Date
 * The input is a string where the format must be checked
 * "YYYY-MM-DD"
 * @param $value = the variable in question
 * @return boolean result
 * */
function isDate(value){
 		
 		success = true;
 		
	  	var date = value.split("-");
	   	
	   	// check that there are three date components
		if (date.length != 3)
			return false;
		
	   	// check that all date components were retrieved
		if (date[0] == null || date[0].length == 0)
			return false;
		if (date[1] == null || date[1].length == 0)
			return false;
		if (date[2] == null || date[2].length == 0)
			return false;
			
	   	var year = date[0];
	  	var month = date[1];
	  	var day = date[2];
	   	
	   	// validate each component
	   	if (year.length != 4 || !isPosInt(year)) {
	   		success = false;	
	   	}
	   	if (month.length != 2 || !isPosInt(month)) {
	   		success = false;	
	   	}
	   	if (day.length != 2 || !isPosInt(day)) {
	   		success = false;	
	   	}
	   	
	   	return success;
 	} 	