/****************************************************************************
File Name          : js/formvalidation.js
Created By         : Shanthi.M
Created On         : 14th October 2009
Modified By        : N/A
Modified On        : N/A
Description        : This file contains all js functions for the entire site
*****************************************************************************/
// Checking for Non-Empty
function isEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return false;
	}
	return true;
}

// Selection Made
function madeSelection(elem){
	if (elem.value == "" || elem.value == "0" || elem.value == "-1" || elem.value.indexOf("Please") != -1) {
		alert("Please select an option in the list.");
		elem.focus();
		return false;
	} else {
		return true;
	}
}

// Checking for All Numbers
function isNumeric(elem, helperMsg){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

// Checking for All Letters
function isAlphabet(elem, helperMsg){
	var alphaExp = /^[a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

// Checking for Numbers and Letters
function isAlphanumeric(elem, helperMsg){
	var alphaExp = /^[0-9a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

// Restricting the Length
function lengthRestriction(elem, min, max){
	var uInput = elem.value;
	if(uInput.length >= min && uInput.length <= max){
		return true;
	}else{
		alert("Please enter between " +min+ " and " +max+ " characters");
		elem.focus();
		return false;
	}
}

// Email Validation
function emailValidator(elem, helperMsg){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

// Returns TimeDate value without seconds, its a little variation of the above 'dateTime()' function
function current_date_time() {
    var dt = new Date();
	var Dtime2  = (dt.getMonth()+1)+ '/' + dt.getDate() + '/' + dt.getFullYear()+" "+ dt.getHours() +":"+ dt.getMinutes(); // month values will be 0,1,2...11. Thats why we added a 1 with the month
	return Dtime2;
}

//To compare the from date with to date
function Comparedate(fdate,tdate)
{
	var strarr = fdate.value.split("-");
	
	splitter=fdate.value.substr(2,1);
	strarr = fdate.value.split(splitter);	
	var d = strarr[0];
	var m = strarr[1];
	var y = strarr[2];

	splitter=tdate.value.substr(2,1);
	var strarr = tdate.value.split(splitter);
	var d1 = strarr[0];
	var m1 = strarr[1];
	var y1 = strarr[2];
	if( y == y1)
	{
		if(m == m1)
		{
			if(d > d1)
			{
				alert("From date exceeds To date");		
				tdate.focus();
				tdate.select();
				return false;
			}
		}
		else if (m > m1)
		{
			alert("From date exceeds To date");		
			tdate.focus();
			tdate.select();
			return false;
		}
	}	
	if(y > y1)				
	{
		alert("From date exceeds To date");		
		tdate.focus();
		tdate.select();
		return false;			
	}
}
