/********************************************************************************
* Filename		: validate.js													*
* Date			: 5-May-09													  	*
* Author		: Harmandeep Dhillon (http://www.harmandhillon.com)				*
* Description	: This js file conatains the code for validation. The functions	*
*				  included in this files are the ones which will be used across	*
*				  various page. A desription of each function is provided		*
* Version	Date		Author					Comments					  	*
* 1.0		5-May-09	Harmandeep Dhillon		Initial file created		  	*
*********************************************************************************/

// Store global variable (to be used as static)
// If any change is required do it here once only.
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

/********************************************************************************
* Function		: isInteger														*
* Description	: Checks if the passed value is an integer or not.		 		*
* Arguments 										  				  			*
*	s			: The argument to be checked									*
* Return		: true or false based on weither the number is integer or not	*
*********************************************************************************/
function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

/********************************************************************************
* Function		: daysInFebruary												*
* Description	: Calculate the number of days February month will have based 	*
*				  on the year passed (leap year calculation)					*
* Arguments 										  				  			*
*	year		: The year														*
* Return		: The number of days in february								*
*********************************************************************************/
function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // Also, special case - centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

/********************************************************************************
* Function		: isDate														*
* Description	: Checks if the passed value is a valid date or not.	 		*
* Arguments 										  				  			*
*	dtStr		: The string with the date to validate							*
* Return		: Blank string if everything is ok, error string otherwise		*
*********************************************************************************/
function isCheckDate(dtStr){
	// create an array storing the days in a month
	// Temporarily store 29(max) for february
	var daysInMonth =new Array(31,29,31,30,31,30,31,31,30,31,30,31);
	
	// Find the two position of date character (for DD/MM/YYYY format its '/')
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);

	// Check if the format is correct	
	if (pos1==-1 || pos2==-1){
		return("The date format should be : DD/MM/YYYY")
	}
	// Else perform remaining checks
	else{
	
		// find the day,month and year
		var strDay=dtStr.substring(0,pos1);
		var strMonth=dtStr.substring(pos1+1,pos2);
		var strYr=dtStr.substring(pos2+1);
		
		// Remove the leading 0 if present (for days and months)
		if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
		if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
	
		// Convert to integers
		day		= parseInt(strDay);
		month	= parseInt(strMonth);
		year	= parseInt(strYr);
		
		// Performs the checks - validate date
		// Check if the month is correct
		if (strMonth.length<1 || month<1 || month>12){
			return("Please enter a valid month.");
		}
		// Now check if the day is correct
		else if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month-1]){
			return("Please enter a valid day.");
		}
		// now check the year
		else if (strYr.length != 4 || year==0 || year<minYear || year>maxYear){
			return("Please enter a valid 4 digit year between "+minYear+" and "+maxYear+".");
		}
		// If this statement has been reached, means all checks have been validated
		else return ("");
	}
}

/********************************************************************************
* Function		: trim															*
* Description	: Removes leading and trailing spaces					 		*
* Arguments 										  				  			*
*	str			: The string which needs to be 'trimmed'						*
* Return		: The trimmed string											*
*********************************************************************************/
function trim(str){
	return str.replace(/^\s+|\s+$/g,'');
}

/********************************************************************************
* Function		: isPassword													*
* Description	: Checks required for the password are put here.		 		*
* Arguments 										  				  			*
*	strPwd		: The password passed as string									*
* Return		: Blank string if everything is ok, error string otherwise		*
*********************************************************************************/
function isPassword(strPwd){
	//All the password validations will be done in this function
	if (strPwd.length < 6){
		return("Password should be atleast 6 characters long");
	}

	// If this statement has been reached, means all checks have been validated
	return ("");
}

/********************************************************************************
* Function		: isPhone														*
* Description	: Checks if the passed value is valid phone number.		 		*
* Arguments 										  				  			*
*	s			: The argument to be checked									*
* Return		: Blank string if everything is ok, error string otherwise		*
*********************************************************************************/
function isPhone(strPhone){
	// Remove space and '+()-'
	strPhone = strip(strPhone);

	// Test for characters
	if (!isInteger(strPhone)){
		return("Please enter only numeric data");
	}

	// If this statement has been reached, means all checks have been validated
	return ("");
}

/********************************************************************************
* Function		: strip															*
* Description	: Removes Spaces,+,(,),- from the given string. Can be useful 	*
*				  to convert strings into numbers (eg phone numbers)			*
* Arguments 										  				  			*
*	str			: The argument to be stripped									*
* Return		: The stripped value											*
*********************************************************************************/
function strip(str){
	var i;
	var returnString="";
	// Search through string's characters one by one.
	// If character is not a whitespace, append to returnString.
	for (i = 0; i < str.length; i++){
		// Check that current character isn't Spaces,+,(,),-.
		var c = str.charAt(i);
		if (c != " " && c != "+" && c != "(" && c != ")" && c != "-"){
			returnString += c;
		}
	}
	return returnString;
}

/********************************************************************************
* Function		: isEmail														*
* Description	: Checks if the passed value is a valid email or not.	 		*
* Arguments 										  				  			*
*	s			: The argument to be checked									*
* Return		: true/false based on weither the string is a valid email or not*
*********************************************************************************/
function isEmail(str){
	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}


