//### Function definitions for use with validation of forms through out the site for.

function validValue (ctl, label) {
	var j=0;
	var str=ctl.value;
	var regexp = /^([-a-z\d,\s\.-]){3,50}$/i;
	//alert(str);
	var checkName = regexp.test(str);
	for(var i=0;i<str.length;i++) {if (str.charAt(i)!=" ") j++;}
	if (str.length==0) {alert("Please enter " + label + "."); ctl.focus(); return(false);}
	else if (!checkName) {alert("Please enter valid " + label + "."); ctl.focus(); return(false);}
	return true;
}

function valid_address_Value (ctl, label) {
	var j=0;
	var str=ctl.value;
	var regexp = /^([-a-z\d,\s\.-]){3,500}$/i;
	//alert(str);
	var checkName = regexp.test(str);
	for(var i=0;i<str.length;i++) {if (str.charAt(i)!=" ") j++;}
	if (str.length==0) {alert("Please enter " + label + "."); ctl.focus(); return(false);}
	else if (!checkName) {alert("Please enter valid " + label + "."); ctl.focus(); return(false);}
	return true;
}

function checkNums () {
	
	var ctlEvent = checkNums.arguments [0];
	var iKeyCode = ctlEvent.which ? ctlEvent.which : ctlEvent.keyCode;

	if(iKeyCode >= 48 && iKeyCode <= 58 ||iKeyCode==127)
		return true;
		if (iKeyCode == '13' || iKeyCode == '46' || iKeyCode == '37' || iKeyCode == '39')
			return true;
		if (parseInt(iKeyCode) == 8) {
			return true;
		}
		if (parseInt(iKeyCode) == 127) {
			return true;
		}
		
		if (parseInt(iKeyCode) == 9) {
			return true;
		}
	var blnNumRet = (iKeyCode <= '9' && iKeyCode >= '0');
	var blnOprRet = (iKeyCode == '13' || iKeyCode == '8');	
	return (blnOprRet || blnNumRet);
}
function ValidateAlpha(ctl, label)
{
	if(ctl.value.match(/^[a-zA-Z\s]+$/)){
		return true;
	}else{
		alert("Please enter valid " + label + ".");
		return(false);
	}
}
function checkCombo () {
	//var defSelL = "-> ", defSelR = " <-";
	var cmbName = checkCombo.arguments [0];
	var cmbType = checkCombo.arguments [1];
	var defSelMid = checkCombo.arguments [2];
	//alert (defSelMid);
	if (cmbName.value.substring (0,6).toLowerCase () == "select") {
		alert ("Please select proper value for " + cmbType);
		cmbName.focus ();
		return false;
	}
	return true;
}

function checkPhone(phone)
{
	if(phone.match(/^\+?[0-9]+([\s\-]*[0-9]+)*$/))
		return true;
	else
		return false;
}


function isblank(val,label)
{
	if (val.length==0){
		alert("Please enter " + label + "."); val.focus(); return(false);}
	else
		return true;
}
/*
### START : OLD SCRIPT ###
### NOT VERY USEFUL SINCE IT ONLY CHECKES WEATHER A PERTICULAR E MAIL ID CONTAINS TEXT AND AN '&' AND A '.' SYMBOL !!!
### BUT STILL WE USE IT AS THE NEW SCRIPT NEEDS SOME MODIFICATION...
*/
function checkEmail (ctl, label)
{
	if (ctl.value == "")
	{
		alert("Please enter " + label);
		ctl.focus(); return (false);
	}
	if(ctl.value.match(/^[a-z|0-9]+[\_\.\-]*[a-z|0-9]+\@(([a-zA-Z0-9\-\_])+\.)+([a-zA-Z0-9]{2,4})+$/i))
        return true;
    else
	{
		alert("Please enter valid " + label);
		ctl.focus();
        return false;
	}
}

function checkURL (ctl, label)
{
	//var regexp = new RegExp("^(ftp|https|http?):\/\/(www\.)?[a-z0-9\-\.]{3,}\.[a-z]{3}");
	//var regexp = new RegExp("/^((ftp|https?):\/\/)?((w){3}$\.)?[a-z0-9\-]+?\.((.*))/i");
	if (ctl.value == "")
	{
		alert("Please enter " + label);
		ctl.focus(); return (false);
	}
	if(ctl.value.match(/^((ftp|https?):\/\/)?([A-Z]+)?\.[^\.]+.*/i)){
        return true;
	}else{
		alert("Please enter valid " + label);
		ctl.focus();
        return false;
	}
}
/*
### End : OLD SCRIPT ###
*/

/*
// ### YET TO WORK ON THIS PROCEDURE ####
function checkEmail (ctl, label) {
	//for(var i=0;i<ctl.value.length;i++) {if (ctl.value.charAt(i)!=" ") j++;}
	if (!validValue (ctl,label)) return false;
	else {
		var eInvalid = validateEmail (ctl, ctl.value, label);
		if (eInvalid != "") {
			alert (eInvalid); ctl.focus (); return false;		
		}
		return true;
	}
}

function validateEmail () {
	var ctl = validateEmail.arguments [0];
	var sEM = validateEmail.arguments [1];
	var label = validateEmail.arguments [2];
	var sErrMsg = "";

	if (sEM == "") {sErrMsg = "E-mail ID required \n"; return sErrMsg;}
	else if (sEM.search ('@') == -1 || sEM.search ('@') == 0 || sEM.search ('[\.]') == -1 || sEM.search ('[\.]') == 0) {
		sErrMsg = "Invalid E-mail id found..!\n\'" + sEM + "\' for " + label;
		return sErrMsg;
	}
	else {
		var moreAt = 0;
		var moreDot = 0;
		if ((sEM.lastIndexOf ('.') < sEM.indexOf ('@') + 2) || sEM.lastIndexOf ('.') == sEM.length-1) return "Invalid E-mail id found..!\n\'" + sEM + "\' for " + label;
		sErrMsg = "Found repeating characters in E-mail ID :\n";
		for (var inCh = 0;inCh <= sEM.length;inCh ++) {
			if (sEM.substr (inCh, 1) == '@') {
				moreAt ++;}
			//if (sEM.substr (inCh, 1) == '.') {
			//	moreDot ++;}
		}
		if (moreAt > 1) {// || moreDot > 1) 
			//sMsg += (moreDot > 1) ? ("\'.\' " + moreDot + " times\n") : "";
			sErrMsg += (moreAt > 1) ? ("\'@\' " + moreAt + " times \n") : " for " + label;
			return sErrMsg;
		}
	} return "";
}

// ### 
*/

function BirthDateValidation(dt)

{
    var dtToday = new Date();
    
	date = dtToday.getDate(); // Get the numerical date
	month = dtToday.getMonth()+1; // Get the month
	year = dtToday.getFullYear(); // Get the year
	
	if(month < 10)
		month = "0"+month;
	
	inputteddate = dt.split("/");
	
	//return false;
	if( inputteddate[0] > date && inputteddate[1]  > month && inputteddate[2] > year)
	{
		alert("Please insert proper birth date");
		return false;	
	}
	else if( inputteddate[1] > month && inputteddate[2] == year)
	{
		alert("Please insert proper birth date");
		return false;
	}
	else if(inputteddate[0] > date && inputteddate[1] == month )
	{
	  	alert("Please insert proper birth date");
		return false;
    }
    else
    {
        return true;
    }
}
