/***
	CheckLength(field,FieldName,minLen,maxLen): 
***/

function CheckLength(field,fieldName,minLen,maxLen) {
	var strValue=Trim(field.value); 
	str = new String(strValue);
	l=strValue.length;
	
	if(l<minLen) {
		//alert(fieldName + ' must consist of minimum ' + minLen + ' characters.');
		alert(fieldName+ " : " + message("15") + minLen + message("14") );
		field.focus();
		return false;
	}
	
	if(l > maxLen){
		//alert(fieldName + ' can have maximum ' + maxLen + ' characeters.');
		alert(fieldName+ " : " + message("13") + maxLen + message("14") );
		field.focus();
		return false;
	}
	return true;
}


/***
	CheckExactLength(string, string,integer): 
***/

function CheckExactLength(field, fieldName, len) {
	var strValue=Trim(field.value); 
	str = new String(strValue);
	l=strValue.length;
	
	if(len!=l) {
		//alert(fieldName + ': Only Exact Number of Characters Permitted : ' + len + '.');
		alert(fieldName + ":" + message("17") + " : " + len );
		field.focus();
		return false;
	}
	return true;
}

function CheckMaxLength(fieldName, strValue, maxLength) {
		str = new String(strValue);
		len = str.length;
		if(len > maxLength){
			//alert(fieldName + ' can have maximum ' + maxLength + ' characeters.');
			alert(fieldName+ " : " + message("13") + maxLength + message("14") );
			return false;
		}
		return true;
}

function CheckMinLength(fieldName, strValue, minLength) {
		str = new String(strValue);
		len = str.length;
		if (len < minLength) {
			//alert(fieldName + ' must consist of minimum ' + minLength + ' characters.');
			alert(fieldName+ " : " + message("15") + minLength + message("14") );
			return false;
		}
		return true;
}

function CheckName(tbox, fieldName)
{
    var fieldValue = tbox.value;
    fieldValue = Trim(fieldValue);

	if(fieldValue != '')
	{
		str = new String(fieldValue);
		val = str.charAt(0);
			
		if (!(val >= "A" && val <= "Z"))
		{	//alert("Name must start with Capital letter.");
		//	alert(message("22"));
			tbox.focus();
			//return false;
		}	
		len = str.length;
		for (i = 0; i <= len - 1; i++) 
		{
			val = str.charAt(i);
			if (!((val >= "A" && val <= "Z") ||(val >= "a" && val <= "z") || (val == " ") || (val == "'")) )
			{	//alert("only alphabets and space allowed");
				//alert(message("19") + " " + fieldName);
				tbox.focus();
				return false;
			} 
		}
	}
	return true;
}	

/***
	CheckConfirmPassword(string, string): Checks the Password and Confirm Password to be same
***/

function CheckConfirmPassword(strVal, strVal1) {
	if(strVal != strVal1) {
	   	//alert('The Password and Confirm Password should match.');
	   	alert(message("16"));
		return false;
	}
	return true;
}


// Checks if the e-mail address is valid

function CheckValidEmail (emailStr) {

	if (! (/^\S+@\S+\.\S+$/i).test(emailStr) )
	{
	  // alert("Invalid e-mail address");
	   return false;
	}
return true;
}

	function chkDateFormat(CurrentVal) {
		strFormat = "dd/mm/yyyy";
		var strDate = new String(CurrentVal);
		var iDate = strDate.indexOf('/',1);
		var iMonth = strDate.indexOf('/',iDate+1);
		if (strFormat == 'dd/mm/yyyy') {
			var cDate = parseInt(strDate.substring(0,iDate), 10);
			var cMonth = parseInt(strDate.substr(iDate+1,iMonth-1), 10);
		} else {
			var cMonth = parseInt(strDate.substring(0,iDate), 10);
			var cDate = parseInt(strDate.substr(iDate+1,iMonth-1), 10);
		}
		var cYear = parseInt(strDate.substr(iMonth+1,6), 10);
		if (cMonth == 1 || cMonth == 3 || cMonth == 5 || cMonth == 7 || cMonth == 8 || cMonth == 10 || cMonth == 12) {
			var cDay = 31;
		}
		if ( cMonth == 4 || cMonth == 6 || cMonth == 9 || cMonth == 11 ) {
			var cDay = 30;
		}
		if (cMonth == 2) {
			if (((cYear % 4 == 0) && (cYear % 100 != 0)) || (cYear % 400 == 0)) {
				var cDay = 29;
			} else {
				var cDay = 28;
			}
		}

		if ((cDate >= 1 && cDate <= cDay) && (cMonth >= 1 && cMonth <= 12) && (cYear >= 1900 && cYear <= 9999)) {
			return true;
		} else {
			return false;
		}
	}

	function chkNumberCompare(CompareNum, WithCompareNum, condition) {
		CompareNum=Trim(CompareNum.value);
		WithCompareNum=Trim(WithCompareNum.value);		
		if  (CompareNum.length == 0) CompareNum =0;
		if  (WithCompareNum.length == 0) WithCompareNum =0;
		if(condition == 'le') {
			if (parseFloat(CompareNum) <= parseFloat(WithCompareNum)) {
				return true;
			} else {
				return false;
			}
		}
		if(condition == 'lt') {
			if (parseFloat(CompareNum) < parseFloat(WithCompareNum)) {
				return true;
			} else {
				return false;
			}
		}
		if(condition == 'gt') {
			if (parseFloat(CompareNum) > parseFloat(WithCompareNum)) {
				return true;
			} else {
				return false;
			}
		}
		if(condition == 'ge') {
			if (parseFloat(CompareNum)>=parseFloat(WithCompareNum)) {
				return true;
			} else {
				return false;
			}
		}
		if(condition == 'e') {
			if (parseFloat(CompareNum)==parseFloat(WithCompareNum)) {
				return true;
			} else {
				return false;
			}
		}
		return true;
	}

	
	function chkDateCompare(CompareDate, WithCompareDate, condition) {
		format = "dd/mm/yyyy";
		if(format == "dd/mm/yyyy") {
			CompDate = "";
			var strDate = new String(CompareDate);
			var iDate = strDate.indexOf('/',1);
			var iMonth = strDate.indexOf('/',iDate+1);
			
			var cDate = parseInt(strDate.substring(0,iDate), 10);
			var cMonth = parseInt(strDate.substr(iDate+1,iMonth-1), 10);
			var cYear = parseInt(strDate.substr(iMonth+1,4), 10);
			var CompDate = cMonth + '/' + cDate + '/' + cYear;
			
			
			WithDate = "";
			var strDate1 = new String(WithCompareDate);
			var iDate1 = strDate1.indexOf('/',1);
			var iMonth1 = strDate1.indexOf('/',iDate1+1);
			
			var cDate1 = parseInt(strDate1.substring(0,iDate1), 10);
			var cMonth1 = parseInt(strDate1.substr(iDate1+1,iMonth1-1), 10);
			var cYear1 = parseInt(strDate1.substr(iMonth1+1,4), 10);
			var WithDate = cMonth1 + '/' + cDate1 + '/' + cYear1;
		}
	if(condition == 'le') {
		if (Date.parse(CompDate) <= Date.parse(WithDate)) {
			return true;
		} else {
			return false;
		}
	}
	if(condition == 'lt') {
		if (Date.parse(CompDate) < Date.parse(WithDate)) {
			return true;
		} else {
			return false;
		}
	}
	if(condition == 'gt') {
		if (Date.parse(CompDate) > Date.parse(WithDate)) {
			return true;
		} else {
			return false;
		}
	}
	if(condition == 'ge') {
		if (Date.parse(CompDate)>=Date.parse(WithDate)) {
			return true;
		} else {
			return false;
		}
	}
	if(condition == 'e') {
		if (Date.parse(CompDate)==Date.parse(WithDate)) {
			return true;
		} else {
			return false;
		}
	}
	return true;
}

// Function compares two dates and return the difference in 
// Number of days. Return Negative if the Second date is Greater.
//
// The function can be used:
//    - Compare two dates: If Return value is -ve Second Date is greater
//    - Find Number of Days beween two dates.

function chkDateDiff(psDate1, psDate2) {
var inDay, inMonth;
alert(psDate1);
alert(psDate2);
// Convert Date to MM/DD/YYYY	
	inDay = psDate1.substring(0,2);
	inMonth = psDate1.substring(3,5);
	inYear = psDate1.substring(6,10);
	alert(inDay + inMonth + inYear);
	psDate1 = inMonth + "/" + inDay + "/" + inYear;
	inDay = psDate2.substring(0,2);
	inMonth = psDate2.substring(3,5);
	inYear = psDate2.substring(6,10);
	psDate2 = inMonth + "/" + inDay + "/" + inYear;
// Find the Number of Milliseconds elapsed since 01-01-1970 till Date	
	pnDate1=Date.parse(psDate1);
	pnDate2=Date.parse(psDate2);
// Calculate Difference in Days by dividing with 24 * 3600 * 1000
alert(Math.round((pnDate2-pnDate1)/86400000));
	return Math.round((pnDate2-pnDate1)/86400000);
}



//*** Checks whether the combination of day month and year makes a valid date or not.**///
function CheckValidDate(day, Month, Year)
{
   	if (Month == 1 || Month == 3 || Month == 5 || Month == 7 || Month == 8 || Month == 10 || Month == 12) 
	{var cDay = 31;	}
	if ( Month == 4 || Month == 6 || Month == 9 || Month == 11 ) 
	{var cDay = 30;}
	if (Month == 2)
	{if (((Year % 4 == 0) && (Year % 100 != 0)) || (Year % 400 == 0)) 
	{var cDay = 29;	}
	 else 
	{var cDay = 28;}
	}
	if ((day >= 1 && day <= cDay) && (Month >= 1 && Month <= 12) && (Year >= 1900 && Year <= 9999)) 
	{
		return true;
	}
	else 
	{
		return false;
	}
}

function chkMonthYearDiff(psMonth1, psYear1, psMonth2, psYear2) {
var inMonth1,inMonth2,inYear1,inYear2;
	inMonth1 = parseInt(eval(psMonth1 + "[" + psMonth1 + ".selectedIndex].value"));
	inYear1 =  parseInt(eval(psYear1 + "[" + psYear1 + ".selectedIndex].value"));
	inMonth2 = parseInt(eval(psMonth2 + "[" + psMonth2 + ".selectedIndex].value"));
	inYear2 = parseInt(eval(psYear2 + "[" + psYear2 + ".selectedIndex].value"));
	if (inYear1<=inYear2) 
	{
		if (inYear1==inYear2) 
		{
			if (inMonth1>inMonth2)
			{
				alert(message("25"));
				eval(psMonth1 + ".focus()");
				return false;
			}
		}
	}
	else 
	{
		alert(message("24"));
		eval(psYear1 + ".focus()");
		return false;
	}
	return true;
}

/***
	Usage:To disable Key strokes and Mouse Right Click events.
	Tested for:NN4,IE4+
	Code:disableKeyMouseEvents('document.form1.text1','Use Browse Button','Use Browse Button');
	Known Issues: In NN mouse clicks are not trapped along with functional keys.
	Note:Main Usage in site for disabling File Browse Input Tag Events.
	pass empty quotes if u dont want message.
***/

function disableKeyMouseEvents(objdoc,keyPressMsg,RightClickMsg)
{
	//keydown
	var str="''";
	eval(objdoc + ".onkeydown=  function (evt) { var c = document.layers ? evt.which : document.all ? event.keyCode : evt.keyCode;	if (document.all){ if (event.ctrlKey && c == 86) return false; if ((event.altKey)  || (event.ctrlKey)) return true; }if (c == 9 ||c==46 || c == 35  || c == 36 )  return true; if (keyPressMsg != '')alert(keyPressMsg); return false;  };");
 //IE4+ Right Clicks 
	eval(objdoc + ".onmousedown =  function (){ 		if (document.all) {			if (event.button == 2) { if (RightClickMsg != '') alert(RightClickMsg);			return false;	}		}		return true;	}; ");
	eval(objdoc + ".onkeypress = function (){ if (document.all)event.keyCode='';};");
	return true;
}


function popUpWindow(theURL,winName,features) {
	// Behaves just like window.open() but ensures visible child windows
	closePopUpWindow();
	openedWindow = open(theURL,winName,features+",left=0,top=0,screenX=0,screenY=0");
	openedWindow.onunload = confirmClosed;
	onunload = closePopUpWindow;
	windowOpen = true;
	return (openedWindow);
}
function closePopUpWindow(){
	// Closes a window's child window
	if (window.windowOpen&&window.openedWindow){
		openedWindow.close();
		confirmClosed();
	}
}
function confirmClosed(){
	// Used to confirm when the child window is closed
	windowOpen = false;
}
