/******************************************************************************
* Program 		: Validate.js
* Done By 		: Gandhali Samant
* Date      	: Dec 24, 2001
* System Name 	: G&B On-Line Recruitment
* For     		: All the Functions used for Validation 
*******************************************************************************
	Ammended by		:	Kedar Dalvi
	Ammended on		:	29/11/2005
	Purpose			:	Streamlining ESP
	Ammended by		:	5860351/56
*******************************************************************************
	Ammended by		:	Kedar Dalvi
	Ammended on		:	18/04/2006
	Purpose			:	to add functions for date checks
	Ammended by		:	5000361/6
*******************************************************************************/
/************************ Function To check whether a field is filled or not *********************/
function isFilled(item){
	if ((item.value==null)||(item.value=="")){
		item.focus();
		return false;
	}
	else{
		var filled = false;
		var i = 0;
		for (i = 0; i < item.value.length; i++){
			if (item.value.charAt(i) != " "){
				filled = true;
			}
		}
		return filled;
	}		
}
/************************* Function To check whether input is Valid String ***********************/
function isValidString(item){
	//var str="qwertyiuposadfghjklxzcvbmnQEWRTYUOIPLKJHGFDSAMNBVCXZ1234567890~!@#$%^&*():;,.?/<>{}[]| ";
	var str = "\"'";
	var l = item.value.length;
	for (i=0; i < l; i++)
	{
		if (str.indexOf(item.value.charAt(i)) != -1){
			item.focus();
			return false;
		}
	}
		return true;
}

/************************* Function To check whether input is Valid String ***********************/
function isValidString2(item)
{
	// RPK changed the name of this function from isValidString to isValidString2

	var str = "\"'0123456789";
	var l = item.value.length;
	for (i=0; i < l; i++)
	{
		if (str.indexOf(item.value.charAt(i)) != -1)
			return false;
	}
		return true;
}
/***************************** Function To check Email Validation ********************************/
function isEmailValid(item) 
{
	var l = item.value.length;
	var at = item.value.indexOf("@");
	var dot = item.value.indexOf(".",at+1);
	if (((at < 1)||(dot <= at+1))||(dot == l-1)){
		item.focus();
		return false;
	}
	return true;
}
/***************************** Function To check date Validation *********************************/
function isDate(item) {
	var matchPattern="0123456789/";
	if (!isValidString1(item,matchPattern)){		
		return false;
	}
	var l = item.value.length;
	var slash1 = item.value.indexOf("/",0); 
	var slash2 = item.value.indexOf("/",slash1+1);
	var d = item.value.substring(0,slash1), day = parseInt(d, 10);
	var m = item.value.substring(slash1+1,slash2), month = parseInt(m, 10);
	var y = item.value.substring(slash2+1,l), year = parseInt(y, 10);
	var date = new Date();
	if ((y.length != 2)&&(y.length != 4)) return false;
	if ((slash1 == -1)||(slash2 == -1)) return false;
	if (((isNaN(m))||(isNaN(d)))||(isNaN(y))) return false;
	if ((day < 1)||(day > 31)) return false;
	if ((month < 1)||(month > 12)) return false;
	if (((((month == 4)||(month == 6))||(month == 9))||(month == 11))&&(day > 30)) return false;
	date.setDate(day); date.setMonth(month); date.setYear(year);
	year = (date.getYear() >= 2000) ? date.getYear() : date.getFullYear();
	if ((year > 2060) || (year < 1900)) return false;
	if (month == 2) {
		 if (((year % 4 == 0)&&(year % 100 != 0))||(year % 400 == 0)) 
			{if (day > 29) return false;}
		else
			{if (day > 28) return false;}
	}
	return true;
}

/*************************** Function To check Date Time Validation ******************************/
// start SRF 5000361/6
function getToday(){
	var dd,mm,yy,date;
	dt = new Date();
	day = dt.getDate();
	if(day < 10) day = "0"+day;
	month = parseInt(dt.getMonth(),10) + 1;
	if(month < 10) month = "0"+month;
	today = day+"/"+month+"/"+dt.getFullYear();
	return today;
}

function isMoreThanToday(item){
    var frD=getToday();
    var toD=item.value;
	var lFD = frD.length, lTD = toD.length;
	var slash1FD = frD.indexOf("/",0), slash1TD = toD.indexOf("/",0); 
	var slash2FD = frD.indexOf("/",slash1FD+1), slash2TD = toD.indexOf("/",slash1TD+1);
	var dayFD = parseInt(frD.substring(0,slash1FD), 10), dayTD = parseInt(toD.substring(0,slash1TD), 10);
	var monthFD = parseInt(frD.substring(slash1FD+1,slash2FD), 10), monthTD = parseInt(toD.substring(slash1TD+1,slash2TD), 10);
	var yearFD = parseInt(frD.substring(slash2FD+1,lFD), 10), yearTD = parseInt(toD.substring(slash2TD+1,lTD), 10);
	var fromDate = new Date(), toDate = new Date();
	fromDate.setDate(dayFD); fromDate.setMonth(monthFD-1); fromDate.setYear(yearFD);
	toDate.setDate(dayTD); toDate.setMonth(monthTD-1); toDate.setYear(yearTD);
	if (fromDate.getTime() > toDate.getTime()) return false;
	return true;
}
// end SRF 5000361/6

function isDateTime(item) {
	var d = item.value.substring(0,slash1), day = parseInt(d, 10);
	var m = item.value.substring(slash1+1,slash2), month = parseInt(m, 10);
	var y = item.value.substring(slash2+1,space), year = parseInt(y, 10);
	var h = item.value.substring(space+1,colon1), hour = parseInt(h, 10);
	var min = item.value.substring(colon1+1,colon2), minute = parseInt(min, 10);
	var s = item.value.substring(colon2+1,l), second = parseInt(s, 10);
	var date = new Date();
	if ((y.length != 2)&&(y.length != 4)) return false;
	if ((slash1 == -1)||(slash2 == -1)) return false;
	if (((isNaN(m))||(isNaN(d)))||(isNaN(y))) return false;
	if (((isNaN(h))||(isNaN(min)))||(isNaN(s))) return false;
	if (((h=="")||(min==""))||(s=="")) return false;
	if ((day < 1)||(day > 31)) return false;
	if ((month < 1)||(month > 12)) return false;
	if (((((month == 4)||(month == 6))||(month == 9))||(month == 11))&&(day > 30)) return false;
	date.setDate(day); date.setMonth(month); date.setYear(year);
	year = (date.getYear() >= 2000) ? date.getYear() : date.getFullYear();
	if ((year > 2060) || (year < 1900)) return false;
	if (month == 2) {
		 if (((year % 4 == 0)&&(year % 100 != 0))||(year % 400 == 0)) 
			{if (day > 29) return false;}
		else
			{if (day > 28) return false;}
	}
	if ((hour > 24) || (hour < 0)) {return false;}
	if ((minute > 60) || (minute < 0)) {return false;}
	if ((second > 60) || (second < 0)) {return false;}
	return true;
}

function isDateDiffProper(item1,item2) {
        var frD=item1.value;
        var toD=item2.value;
	var lFD = frD.length, lTD = toD.length;
	var slash1FD = frD.indexOf("/",0), slash1TD = toD.indexOf("/",0); 
	var slash2FD = frD.indexOf("/",slash1FD+1), slash2TD = toD.indexOf("/",slash1TD+1);
	var dayFD = parseInt(frD.substring(0,slash1FD), 10), dayTD = parseInt(toD.substring(0,slash1TD), 10);
	var monthFD = parseInt(frD.substring(slash1FD+1,slash2FD), 10), monthTD = parseInt(toD.substring(slash1TD+1,slash2TD), 10);
	var yearFD = parseInt(frD.substring(slash2FD+1,lFD), 10), yearTD = parseInt(toD.substring(slash2TD+1,lTD), 10);
	var fromDate = new Date(), toDate = new Date();
	fromDate.setDate(dayFD); fromDate.setMonth(monthFD-1); fromDate.setYear(yearFD);
	toDate.setDate(dayTD); toDate.setMonth(monthTD-1); toDate.setYear(yearTD);
// start SRF 5000361/6
	if (fromDate.getTime() > toDate.getTime()) return false;
// end SRF 5000361/6
	return true;
}

/********************************* Function To Select Error **************************************/
function selectError(item, text) {
//	if (error) return;
	//error = true;
	window.alert(text);
	item.focus();
}
/***************************** Function To Give Error Messages ***********************************/
function inputError(item, text) {
//	if (error) return;
	//error = true;
	window.alert(text);
	item.select();
	item.focus();
}

 // This function whether a valid Division / Department is selected .
function validationForsearchVac()
{ 		errfound=false;
		errfound=(validateDiv2 (document.FrmDisplay.DivDept))
		if (errfound == false)
		   { return false;}
		errfound=(validateFunc2 (document.FrmDisplay.Function))
		if (errfound == false)
		   { return false;}
   		//document.FrmDisplay.submit();
		return true;	
}



 // This function whether a valid Division / Department is selected .
function validateDiv (item)
{
	item1=item[item.selectedIndex].value;
	if ( item1 == "Select Division/Department")
	   {
		alert("Please select a correct Division/Department");
		item.focus();
		return false;
	   }
	   return true;
}
function validateDiv2(item)
{
//	alert(item);
	item1=item[item.selectedIndex].value;
//	alert(item1);
	if ( item1 == "Select Division/Department")
	   {
		alert("Please select a correct Division/Department");
		item.focus();
		return false;
	   }
	   return true;
}

function validateSrNo (item)
{
	item1=item[item.selectedIndex].value;
	if ( item1 == "Select SrNo")
	   {
		alert("Please select a correct SrNo");
		item.focus();
		return false;
	   }
	   return true;
}

//This function whether a valid Function Description is selected .
function validateFunc (item)
{
item1=item[item.selectedIndex].value
if ( item1 == "Select Function")
	   {
		alert("Please select a correct Function");
		item.focus();
		return false;
	   }
	   return true;
}
function validateFunc2 (item)
{
item1=item[item.selectedIndex].value
if ( item1 == "Select Function")
	   {
		alert("Please select a correct Function");
		return false;
	   }
	   return true;
}

// This function whether a Valid Band is Selected.
function validateBand (item)
{
item1=item[item.selectedIndex].value
if ( item1 == "Select Band")
	   {
		alert("Please select a correct Band");
		item.focus();
		return false;
	   }
	   return true;
}
// Function for Validating the Establishment field .
function validateEstb (item)
{
item1=item[item.selectedIndex].value
if ( item1 == "Select Establishment")
	   {
		alert("Please select a correct Establishment");
		item.focus();
		return false;
	   }
	   return true;
}
// Function for Validating the date field .

function validatedate (item)
{	
	if ( !isDate(item))
	{
	 Error(item,"Please Enter a Valid Date");
	 return false;
	 }
}
// Function for Whether the date difference is Valid (From  -  To)

// Function for Whether the date difference is Valid (From  -  To)
function validatediff (item,item1) {
	if ( !isDateDiffProper(item,item1)) {
		 Error(item1,"Last Date of Application should be greater than current date");
		 return false;
	}
	return true;
}
// Function To validate Whether the No. of Posts is Numeric
function validateNoP (item) {
	if (!chkSpace(item)) {
			Error(item,"Please enter valid No. of Posts");
			return false;
	}
	var len=item.value.length;
	if ( len == 0) {
		 Error(item,"No. of Posts should not be blank");
		 return false;
	}
	if (isNaN(item.value)) {
	  Error(item,"No. of Posts should be numeric");
	  return false;
	}
	if (item.value == 0) {
	  Error(item,"No. of Posts should not be null or zero");
	  return false;
	} 
	return true;
}
//To validate string for being blank
function valstr(item)
{
	if(!isFilled(item))
	{
		return true;
	}
	return false;
}

//To validate string for being blank and whether it contains ' or "
function validatestr (item)
{
	if ( !isFilled(item))
	   {
		Error(item,"Field should not be blank");
		return false;
	   }
	   return true;
}
//To validate string for being blank and whether it contains ' or "
function validatestrNo (item)
{
	if ( !isFilled(item))
	   {
		Error(item,"Field should not be blank");
		return false;
	   }
	if ( !isValidString2(item))
	   {
		Error(item,"Field should not contain quotes or numbers");
		return false;
	   }
	   return true;
}
// To validate any string variable whether it contains ' or "
function validatestring (item)
{
	if ( !isValidString(item))
	   {
		Error(item,"Field should not contain quotes");
		return false;
	   }
	   return true;
}
function Error(item,text)
{
	window.alert(text);
	item.select();
	item.focus();
}
function isFilled1(item) { 
	var filled = false;
	var i = 0;
 if (item.value.length != 0){
	for (i = 0; i < item.value.length; i++) {
		if (item.value.charAt(i) != " ") {
			filled = true;
		}
	}
 }
 else
	filled = true;
 return filled;
}
function chkSpace(item) {
	for (i=0;i<item.value.length;i++)	{
		if (item.value.charAt(i) == " ") {
			return false;
		}
	}
	return true;
}

function isValidString1(item,matchPattern){
		var len=item.value.length;
		for (i=0; i < len; i++)
		{		if (matchPattern.indexOf(item.value.charAt(i)) == -1)
				return false;
		}
		return true;
}

function isValidPC(item){
		matchPattern = "0123456789 ";
		var len=item.value.length;
		for (i=0; i < len; i++)
		{		if (matchPattern.indexOf(item.value.charAt(i)) == -1)
				return false;
		}
		return true;
}

function isDate1(item) {
	var matchPattern="0123456789/";
	if (!isValidString1(item,matchPattern))
		return false;
	var l = item.value.length;
	var slash1 = item.value.indexOf("/",0); 
	var slash2 = item.value.indexOf("/",slash1+1);
	var d = item.value.substring(0,slash1), day = parseInt(d, 10);
	var m = item.value.substring(slash1+1,slash2), month = parseInt(m, 10);
	var y = item.value.substring(slash2+1,l), year = parseInt(y, 10);
	var date = new Date();
	if (y.length != 4) return false;
	if ((slash1 == -1)||(slash2 == -1)) return false;
	if (((isNaN(m))||(isNaN(d)))||(isNaN(y))) return false;
	if ((day < 1)||(day > 31)) return false;
	if ((month < 1)||(month > 12)) return false;
	if (((((month == 4)||(month == 6))||(month == 9))||(month == 11))&&(day > 30)) return false;
	date.setDate(day); date.setMonth(month); date.setYear(year);
	year = (date.getYear() >= 2000) ? date.getYear() : date.getFullYear();
	if ((year > 2060) || (year < 1900)) return false;
	if (month == 2) {
		 if (((year % 4 == 0)&&(year % 100 != 0))||(year % 400 == 0)) 
			{if (day > 29) return false;}
		else
			{if (day > 28) return false;}
	}
	return true;
}

//******* This function will check for Quotes in the text box and textarea when the formname is passed as String
function checkForQuotes(formname){
actualForm=eval("document."+formname);
for (i=0;i<(actualForm.length);i++)
{
	error=false;
	elementSelected=eval("actualForm.elements[i]");
	typeOfElement=actualForm.elements[i].type;
	if (typeOfElement=="text" || typeOfElement=="textarea" || typeOfElement=="password"){
		for(j=0;j<elementSelected.value.length;j++)
		{
			if (elementSelected.value.charAt(j) == "\'" || elementSelected.value.charAt(j) == "\""){
				window.alert("Single Quotes and Double Quotes are not allowed in any fields");
				elementSelected.select();
				elementSelected.focus();
				error=true;
				return error;
			}
		}
	}
	
}
	return error;
}
//* this function will check for the whitespaces entered without entering a single valid character.
function checkForWhiteSpace(formname){
actualForm=eval("document."+formname);
for (i=0;i<(actualForm.length);i++)
{
	filled=false;
	elementSelected=eval("actualForm.elements[i]");
	typeOfElement=actualForm.elements[i].type;
	if (typeOfElement=="text" || typeOfElement=="textarea" || typeOfElement=="password")
	{
		
		if (elementSelected.value.length != 0)
		{
			for(j=0;j<elementSelected.value.length;j++)
			{
				if (elementSelected.value.charAt(j) != " ")
						filled=true;
			}
		}
		else
			filled=true;
		if (!filled)
		{
			window.alert("White Spaces are not allowed. Leave blank if you don't want to enter anything");
			elementSelected.select();
			elementSelected.focus();
			return !filled;
		}	
		
	}
}
	return filled;
}

//** Numeric validations

function isValidInteger(item)
{	var str="1234567890";
	var l = item.value.length;
	for (i=0; i < l; i++){
		if (str.indexOf(item.value.charAt(i)) == -1){
			item.focus();
			return false;
		}
	}
	return true;
}
function isValidDouble(item)
{	var str="1234567890.";
	var l = item.value.length;
	for (i=0; i < l; i++){
		if (str.indexOf(item.value.charAt(i)) == -1){
			item.focus();
			return false;
		}
	}
	return true;
}
function isValidCurrencyDouble(item)
{	var str="1234567890.";
	var l = item.value.length;
	var singlePoint = false;
	for (var i=0; i < l; i++){
		if (str.indexOf(item.value.charAt(i)) == -1){
			item.focus();
			return false;
		}
		if (item.value.charAt(i) == '.'){
			if(singlePoint){
				item.focus();
				return false;
			}else{
				singlePoint = true;
			}
		}
	}
	return true;
}
function isValidPositiveInt(item){
	if (!isValidInteger(item)){
		return false;
	}
	else{
		if(parseInt(item.value) < 0){
			return false;
		}
	}
	return true;
}

function isValidNonZeroPositiveInt(item){
	if (!isValidInteger(item)){
		return false;
	}
	else{
		if(parseInt(item.value) <= 0){
			return false;
		}
	}
	return true;
}
function isValidNumber(item){
	if (isValidInteger(item) || isValidFloat(item)){
		return true;
	}
	else{
		return false;
	}
}

function isTime(item){
	if(eval(item.value=="")){
			return false;
	}
var matchPattern="0123456789:";
	if (!isValidString1(item,matchPattern)){	
		return false;
	}
	var l = item.value.length;
	var colon1 = item.value.indexOf(":",0);
	var h = item.value.substring(0,colon1), hour = parseInt(h, 10);
	var min = item.value.substring(colon1+1,l), minute = parseInt(min, 10);
	var date = new Date();
	if (((isNaN(h))||(isNaN(min)))) return false;
	if (((h=="")||(min==""))) return false;
	if ((hour > 24) || (hour < 0)) {return false;}
	if ((minute >= 60) || (minute < 0)) {return false;}
	return true;
return true;
}

function isValidStringName(item){
		var matchPattern="qwertyiuposadfghjklxzcvbmnQEWRTYUOIPLKJHGFDSAMNBVCXZ- "
		var len=item.value.length;
		if (!chkSpace(item))
					{
						item.focus();							
						return false;
					}
		for (i=0; i < len; i++)
		{			if (matchPattern.indexOf(item.value.charAt(i)) == -1){
						item.focus();							
						return false;
				}
		}
		if ((item.value==null))
		{
			item.focus();	
			return false;
		}
		return true;
}
function isValidStringName1(item,match){
		var matchPattern="qwertyiuposadfghjklxzcvbmnQEWRTYUOIPLKJHGFDSAMNBVCXZ "
		matchPattern=matchPattern+match;
		var len=item.value.length;
		for (i=0; i < len; i++)
		{			if (matchPattern.indexOf(item.value.charAt(i)) == -1){
						item.focus();							
						return false;
					}
		}
		if ((item.value==null))
		{
			item.focus();	
			return false;
		}
		return true;
}
function isValidStringName2(item,match){
		var matchPattern="qwertyiuposadfghjklxzcvbmnQEWRTYUOIPLKJHGFDSAMNBVCXZ "
		matchPattern=matchPattern+match;
		var len=item.value.length;
		for (i=0; i < len; i++)
		{			if (matchPattern.indexOf(item.value.charAt(i)) == -1){
						item.focus();							
						return false;
					}
		}
		if ((item.value==null))
		{
			item.focus();	
			return false;
		}
		return true;
}
function isValidStringAddress(item,match){
		var matchPattern="qwertyiuposadfghjklxzcvbmnQEWRTYUOIPLKJHGFDSAMNBVCXZ0123456789 "
		matchPattern=matchPattern+match;
		var len=item.value.length;
		for (i=0; i < len; i++)
		{		if (item.value.charAt(i)==13){	
					if (matchPattern.indexOf(item.value.charAt(i)) == -1){
						item.focus();
						return false;
					}
				}
		}
		if ((item.value==null))
		{
			item.focus();
			return false;
		}
		return true;
}

function getd(item,TextValue){
	var dd,mm,yy,date;
	date = new Date();
	mm=date.getMonth()+1;
	dd=date.getDate();
	yy=date.getFullYear();
	if (dd<10){
		dd="0"+dd;
	}
	if(mm<10){
		mm="0"+mm;
	}
	date=dd+"/"+mm+"/"+yy;
	item.value=date;
	if(!isDateDiffProper(eval(TextValue),item)){
		return false;
	}
	return true;
}

function getd1(item,TextValue){
	var dd,mm,yy,date;
	date = new Date();
	mm=date.getMonth()+1;
	dd=date.getDate();
	yy=date.getFullYear();
	if (dd<10){
		dd="0"+dd;
	}
	if(mm<10){
		mm="0"+mm;
	}
	date=dd+"/"+mm+"/"+yy;
	item.value=date;
	if(!isDateDiffProper(item,eval(TextValue))){
		return false;
	}
	return true;
}

/************************ Function To check whether a field is filled or not *********************/
function isYear(item){
	if ((item.value==null)||(item.value=="")){
		item.focus();
		return false;
	}
	else{
		if (item.value.length != 4){
			return false;
		}
		return true;
	}		
}
function replace(string,text,by) {
	var strLength = string.length, txtLength = text.length;
	if ((strLength == 0) || (txtLength == 0)) return string;
	var i = string.indexOf(text);
	if ((!i) && (text != string.substring(0,txtLength))) return string;
	if (i == -1) return string;
	var newstr = string.substring(0,i) + by;
	if (i+txtLength < strLength)
		newstr += replace(string.substring(i+txtLength,strLength),text,by);
	return newstr;
}
function findLength(formname,fieldname,fieldname1,length) {
		var maxLength = length;
		var enteredLength = eval("document."+formname+"."+fieldname).value.length;
		var textLength = maxLength - enteredLength;
		eval("document."+formname+"."+fieldname1).value = textLength;
//		if(eval("document."+formname+"."+fieldname1).value < 0){
//			alert("More than "+maxLength+" characters are Not allowed");
//		}
}
// SRF 5500044/4 STARTS - function to calculate the difference with Current Date
function caldatediff(item) {
	date1= new Date();
	date2=item;
	var b = date2.split("/")
	var frmdateyy=parseInt(date1.getFullYear())
	var todateyy=parseInt(b[2])
	var frmdatemm1=parseInt(date1.getMonth())
	var todatemm1=parseInt((b[1])-1)
	var frmdatedd=parseInt(date1.getDate())
	var todatedd=parseInt((b[0]))
	var difference =  Date.UTC((todateyy),todatemm1,todatedd) - Date.UTC((frmdateyy),frmdatemm1,frmdatedd);
	difference = difference / (24*60*60*1000);
	return difference; 	
}
// SRF 5500044/4 ENDS

//start of 5860513/2
function _stpinvalidkysN(evt){
try
{	
	evt = (evt) ? evt : (window.event) ? event : null;
 	 if (evt)
  	{
   		var charCode = (evt.charCode) ? evt.charCode :((evt.keyCode) ? evt.keyCode :((evt.which) ? evt.which : 0));
		//alert(charCode);
		if(!(charCode >= 48 && charCode<= 57)&&!(charCode==59)&&(charCode!= 35)&& (charCode!= 36)&& (charCode!= 37)&& (charCode!= 39)&& (charCode!= 8)&& (charCode!= 43)&& (charCode!= 45)&& (charCode!= 46))		
		{
 		  if (evt.cancelBubble != null)
 		     evt.cancelBubble = true;
 		  if (evt.stopPropagation)
		      evt.stopPropagation();
		   if (evt.preventDefault)
		      evt.preventDefault();
		   if (window.event)
		      evt.returnValue = false;
		   if (evt.cancel != null)
		      evt.cancel = true;
		}
 	 }
}
catch(Error)
{
	alert(Error);
}	
}
//End of 5860513/2