/*  
	Function Name : resetForm(arg)
	Description   : to reset all elements on the form,
	                func argument is the name of form.
	Return        : none.
	
	Author        : Ravi Panchal
	Date Created  : 10/8/2004   
*/  
var nricLen = 9;
function resetForm(which){
	/*
	var pass=true;
	var first=-1;
	if (document.images){
		for (i=0;i<which.length;i++){
			var tempobj=which.elements[i];
		 	if (tempobj.type=="text"){
		  		eval(tempobj.value="");
		  		if (first==-1) {first=i;}
		 	}
		 	else if (tempobj.type=="checkbox") {
		  		eval(tempobj.checked=0);
		  		if (first==-1) {first=i;}
		 	}
		 	else if(tempobj.type=="file") {
		 		eval(tempobj.value="");
		 		if(first==-1) {first=i;}
		 	}
		 	else if(tempobj.type=="password") {
		 		eval(tempobj.value="");
		 		if(first==-1) {first=i;}
		 	}
		 	else if(tempobj.type=="radio") {
		 		eval(tempobj.checked=0);
		 		if(first==-1) {first=i;}
		 	}
		 	else if(tempobj.type=="textarea") {
		 		eval(tempobj.value="");
		 		if(first==-1) {first=i;}
		 	}
		 	else if(tempobj.type.indexOf("select") >= 0) {
		 		tempobj[0].selected = true;
		 		if(first==-1) {first=i;}
		 	}
		}
	}
	which.elements[first].focus();
	*/
	if(document.frm)
		document.frm.reset();
}

/*
	Function Name : trim(arg)
	Description   : to trim extra space in string 
	                func arguments can be replaced with "left", "right", "both", "all".
	Return        : variable trimvalue.
	
	Author        : Ravi Panchal
	Date Created  : 10/8/2004
*/
function trim(arg) {
	var trimvalue = "";
	var arglen = arg.length;
	var func="all";

	if (arglen < 1) return trimvalue;
	
	if (func == "left" || func== "both") {
		i = 0;
		pos = -1;
		while (i < arglen) {
			if (arg.charCodeAt(i) != 32 && !isNaN(arg.charCodeAt(i))) {
				pos = i;
				break;
			}
			i++;
		}
	}
	
	if (func == "right" || func== "both") {
		var lastpos = -1;
		i = arglen;
		while (i >= 0) {
			if (arg.charCodeAt(i) != 32 && !isNaN(arg.charCodeAt(i))) {
				lastpos = i;
				break;
			}
			i--;
		}
	}

	if (func == "left") {
		trimvalue = arg.substring(pos,arglen-1);
	}
	if (func == "right") {
		trimvalue = arg.substring(0,lastpos+1);	
	}
	if (func == "both") {
		trimvalue = arg.substring(pos,lastpos + 1);
	}

	if (func == "all") {

		var whitespace = new String(" \t\n\r");
  		var strArg = new String(arg);
	  
	    if (whitespace.indexOf(strArg.charAt(0)) != -1) {
	    	// We have a string with leading blank(s)...
	    	var j=0, i = strArg.length;
	  		// Iterate from the far left of string until we
	  		// don't have any more whitespace...
	  		while (j < i && whitespace.indexOf(strArg.charAt(j)) != -1)
	    		j++;
	    
			// Get the substring from the first non-whitespace
	    	// character to the end of the string...
	    	strArg = strArg.substring(j, i);
	  	}
	
	  	if (whitespace.indexOf(strArg.charAt(strArg.length-1)) != -1) {
	    	// We have a string with trailing blank(s)...
	    	var j = strArg.length - 1;       // Get length of string
	    	// Iterate from the far right of string until we
	    	// don't have any more whitespace...
	    	while (j >= 0 && whitespace.indexOf(strArg.charAt(j)) != -1)
	     		j--;
	      	// Get the substring from the front of the string to
	      	// where the last non-whitespace character is...
	      	strArg = strArg.substring(0, j+1);
	  	}
		trimvalue = strArg.toString();
	  }
	  
	  return trimvalue;
}	

/*
	Function Name : isPosInteger(object_value)
	Description   : to check if parameter object is an Integer value.
	Return        : true/false
	Dependencies  : trim, isPosNum
	
	Author        : Ravi Panchal
	Date Created  : 12/8/2004   
*/
function isPosInteger(object_value)
{
    /* Returns true if value is a number or is NULL, 
	   otherwise returns false	
    */
    var param_value = trim(object_value);
    if (param_value.length == 0)
        return true;
	
	var decimal_format = ".";
	var check_char;

	check_char = param_value.indexOf(decimal_format)
    if (check_char < 0)
		return isPosNum(param_value);
    else
		return false;
}

/*
	Function Name : isPosNum(object_value)
	Description   : to check if parameter object_value is a positive number, include decimal point.
	Return        : true/false
	Dependencies  : trim
	
	Author        : Ravi Panchal
	Date Created  : 12/8/2004
*/ 
function isPosNum(object_value) 
{   
	var start_format = ".0123456789";
	var number_format = ".0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;
	var param_value = trim(object_value);
	  
	if (param_value.length == 0) {
		return true;
	} else {
     
		// The first character can be +/./blank/digit.
		check_char = start_format.indexOf(param_value.charAt(0))
		if (check_char == 0)
		    decimal = true;
		else if (check_char < 1)
			return false;
		
		// Remaining characters can be only . or a digit, but only one decimal.
		for (var i = 1; i < param_value.length; i++)
		{
			check_char = number_format.indexOf(param_value.charAt(i))
			if (check_char < 0)
				return false;
			else if (check_char == 0)
			{
				if (decimal)		// Second decimal.
					return false;
				else
					decimal = true;
			}
			else
				digits = true;
		}	
	}
	
	return true;
}

/*
	Function Name : isEmail(object_email)
	Description   : to check if parameter object_email is a valid email.
	Return        : true/false
	Dependencies  : trim
	
	Author        : Ravi Panchal
	Date Created  : 16/08/2004
*/
function isEmail(object_email) {
  
  //alert('inside isEmail')
  
  // are regular expressions supported?
  var supported = 0;
  var param_email = trim(object_email);
  
  if(param_email.length == 0)
  	return true;
  	
  if (window.RegExp) {
	  var tempStr = "a";
	  var tempReg = new RegExp(tempStr);
	  if (tempReg.test(tempStr)) supported = 1;
  }
  
  if (!supported) 
	  return (param_email.indexOf(".") > 2) && (param_email.indexOf("@") > 0);
	  //var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	  //var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	  var strTest = /^.+@.+\..{2,3}$/;
	  ///^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-]+)+/
      return (strTest.test(param_email));
	  //return (!r1.test(param_email) && r2.test(param_email));
}

/*
	Function Name : isDay(year,month,day)
	Description   : to check if the day is a valid day in date.
	Return        : true/false
	Dependencies  : trim, isNumberRange
	
	Author        : Ravi Panchal
	Date Created  : 15/09/2004
    Modified by   : 
	Date Modified : 
*/
function isDay(year, month, day)
{
	maxDay = 31;
    
	var param_year = trim(year);
	var param_month = trim(month);
	var param_day = trim(day);
	var strDate = param_day + "/" + param_month + "/" + param_year;
	
	if (isNumberRange(param_month, 1, 12)) {
		
		if (parseInt(param_month) == 4 || parseInt(param_month) == 6 || parseInt(param_month) == 9 || parseInt(param_month) == 11) {
			maxDay = 30;
		} else if (parseInt(param_month) == 2) {
			if (parseInt(param_year) % 4 > 0)
				maxDay =28;
			else if (parseInt(param_year) % 100 == 0 && parseInt(param_year) % 400 > 0)
				maxDay = 28;
			else
				maxDay = 29;
		}
		
		return isNumberRange(param_day, 1, maxDay); //check day
	
	} else {
		return false;
	}
	
	return true;
}

/*
	Function Name : isNumber(object_value)
	Description   : to check if parameter object_value is a number.
	Return        : true/false
	Dependencies  : trim
	
	Author        : Ravi Panchal
	Date Created  : 15/09/2004
	Modified by   : 
	Date Modified : 
*/
function isNumber(object_value)
{
    /* Returns true if value is a number or is NULL
       otherwise returns false	
    */
	var param_value =  trim(object_value);
	//alert("isNumber " + param_value); 

    if (param_value.length == 0)
        return true;

    /* Returns true if value is a number defined as
       having an optional leading + or -.
       having at most 1 decimal point.
       otherwise containing only the characters 0-9.
	*/
	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;

    // The first character can be + - .  blank or a digit.
	check_char = start_format.indexOf(param_value.charAt(0))
   	if (check_char == 1)
	    decimal = true;
	else if (check_char < 1)
		return false;
        
	// Remaining characters can be only . or a digit, but only one decimal.
	for (var i = 1; i < param_value.length; i++)
	{
		check_char = number_format.indexOf(param_value.charAt(i))
		if (check_char < 0)
			return false;
		else if (check_char == 1)
		{
			if (decimal)		// Second decimal.
				return false;
			else
				decimal = true;
		}
		else if (check_char == 0)
		{
			if (decimal || digits)	
				trailing_blank = true;
        // ignore leading blanks
		}
	    else if (trailing_blank)
			return false;
		else
			digits = true;
	}	
    return true;
}


/*
	Function Name : isInteger(object_value)
	Description   : to check if parameter object is an Integer value.
	Return        : true/false
	Dependencies  : trim, isNumber
	
	Author        : Ravi Panchal
	Date Created  : 15/09/2005
	Modified by   : 
	Date Modified : 
*/
function isInteger(object_value)
{
    /* Returns true if value is a number or is NULL, 
	   otherwise returns false	
    */
    var param_value = trim(object_value);
    if (param_value.length == 0)
        return true;

	if (param_value.indexOf(".") == 0) {
	 	param_value = "0" + param_value;
	    //alert(param_value);
	}
		
    /* Returns true if value is an integer defined as
       having an optional leading + or -.
       otherwise containing only the characters 0-9.
	*/
	var decimal_format = ".";
	var check_char;

	check_char = param_value.indexOf(decimal_format)
    if (check_char < 1)
		return isNumber(param_value);
    else
		return false;
}

/*
	Function Name : isNumberRange(object_value,min_value,max_value)
	Description   : to check whether the number value is within min_value & max_value range.
	Return        : true/false
	Dependencies  : trim
	
	Author        : Ravi Panchal
	Date Created  : 15/09/2004   
	Modified by   : 
	Date Modified : 
*/
function isNumberRange(object_value, min_value, max_value)
{
    // check minimum
	var param_value = trim(object_value);
	
	if (param_value.indexOf("0") == 0) {
		param_value = param_value.substring(1);
	}
		
	if (isNumber(param_value) && isNumber(min_value) && isNumber(max_value)) {
	   
	    if (min_value != null) {
            if (parseInt(param_value) < min_value)
			   return false;
	    }

        // check maximum
        if (max_value != null) {
		    if (parseInt(param_value) > max_value)
				return false;
	    }
	
	} else {
		return false;	
	} 
	
	return true;
}


/*
	Function Name : checkDateFormat(object_value,format_value)
	Description   : to check if object_value is in correct format based on format_value.
	Return        : true/false
	Dependencies  : trim
	
	Author        : Ravi Panchal
	Date Created  : 15/09/2004
	Modified by   : 
	Date Modified : 
*/
function checkDateFormat(object_value)
{
    var param_value = trim(object_value);
	var format =  /^\d{1,2}(\/|-|\s)\d{1,2}(\/|-|\s)\d{4}$/
	
	if (param_value.length == 0) {
		return true;
	} 
	
	if (format.test(param_value)) {
		return true;	
	
	} else {
		return false;
	}
}

/*
	Function Name : isDate(object_value)
	Description   : to test if parameter object_value is a valid date & in dd/mm/yyyy format.
	Dependencies  : 
					1) trim
					2) isInteger
					3) isNumberRange
					4) isDay
					5) checkDateFormat
	Return        : true/false
	
	Author        : Ravi Panchal
	Date Created  : 15/09/2004
	Modified by   : 
	Date Modified : 
*/
function isDate(object_value)
{
		var param_value = trim(object_value);
		
		if (param_value.length == 0) {
		   return true;
		} else if (!checkDateFormat(param_value)) 
				return false;
	
		sDay=param_value.substring(0,2);	
		sMonth=param_value.substring(3,5);
		sYear=param_value.substring(6,10);	
		
		if (sYear.length < 4)
			return false;	
		else if (!isInteger(sMonth)) // check month
			return false;
		else if (!isNumberRange(sMonth, 1, 12)) // check month range
			return false;
		else if (!isInteger(sYear)) // check year
			return false;
		else if (!isNumberRange(sYear, 0, 9999)) // check year range
			return false;
		else if (!isInteger(sDay)) // check day
			return false;
		else if (!isDay(sYear, sMonth, sDay)) // check day
			return false;
		else
			return true;
}

/*
	Function Name : isNonAlphaNumeric(object_value)
	Description   : to check if parameter object_value is not an alphabet/number
	                (!@#$%^&*(){}[]<>?/.,;:|\~`_+=-) .
	Return        : true/false
	Dependencies  : trim.js
	
	Author        : Ravi Panchal
	Date Created  : 22/09/2004
	Modified by   : 
	Date Modified : 
*/
function isNonAlphaNum(object_value) {   
    
	var nonAN = "'!@#$%^&*(){}[]<>?/.,;:|\~`_+=-";
	var param_value = trim(object_value); 
	
	if (param_value.length == 0) {
		return true;
	}
	 
	for (var i = 0; i < param_value.length; i++)
	{
		check_nonan = nonAN.indexOf(param_value.charAt(i));

		if (check_nonan < 0) 
			return false;
	}	
    return true; 
}

/*
	Function Name : isAlphaNum(arg)
	Description   : to check if parameter object_value is an alphabet/number
	                (!@#$%^&*(){}[]<>?/.,;:|\~`_+=-) .
	Return        : true/false.
	Dependencies  : trim.js
	
	Author        : Ravi Panchal
	Date Created  : 10/8/2004   
*/

function isAlphaNum(object_value) {   
    
	var nonAN = "!@#$%^&*(){}[]<>?/.,;:|\~`_+=-'";
	var param_value = trim(object_value); 
	
	if (param_value.length == 0) {
		return true;
	}
	
	for (var i = 0; i < param_value.length; i++)
	{
		check_nonan = nonAN.indexOf(param_value.charAt(i));
		if (check_nonan >= 0) {
			return false;
		}
	}	
    return true; 
}

/*
	Function Name : chkSize(object,maxlength,msg)
	Description   : to check if field value has not exceeded given length.
	
	Return        : true/false.
	Dependencies  : trim.js
	
	Author        : Ravi Panchal
	Date Created  : 07/10/2004   
*/

function sizeExceeded(obj, maxlength, msg) {
	var param_value = trim(obj.value);
	if(param_value.length > maxlength) {
		alert(msg);
		obj.focus();
		return true;
	}
	return false;
}

/*
	Function Name : isNricValid(obj)
	Description   : to check if nric is valid
	
	Return        : true/false.
	Dependencies  : trim.js
	
	Author        : Ravi Panchal
	Date Created  : 22/10/2004   
*/
function isNricValid(obj) {
	var param_value = trim(obj.value);
	if(param_value.length != nricLen) {
		alert("NRIC should be exactly " + nricLen + " characters long.");
		obj.focus();
		return false;
	}
	
	/*
	 * If length is ok, check nric format
	 * i.e. 1st char : Alphabet, 2-8 characters : Digits, 9th char : Alphabet
	 */
	var r1 = /^[a-zA-Z]{1}\d{7}[a-zA-Z]{1}$/;

	if(r1.test(param_value)) {
		return true;
	} else {
		alert("Incorrect NRIC. Please enter in S9999999X format.");
		obj.focus();
		return false;
	}
}

/*
	Function Name : secToMinSecFormat(obj)
	Description   : converts given seconds into mins:sec format
	
	Return        : mins:sec 
	Dependencies  : trim
	
	Author        : Ravi Panchal
	Date Created  : 25/10/2004   
*/
var min=0;
var sec=0;
var minBeforeTimeOut = 5;	// for alerting user before 5 mins. of session timeout.

function secToMinSecFormat(sec_value) {
	var param_value = trim(sec_value); 
	if(param_value.length == 0) {
		return "0:00";
	}
	min = sec_value/60;
	// Subtract minBeforeTimeOut from min value for alerting user before minBeforeTimeOut mins. of session timeout.
	if(min >= minBeforeTimeOut)
		min = min-minBeforeTimeOut;
	
	sec = sec_value%60;
	//alert(min+":"+sec);
	return min+":"+sec;
}

/*
	Function Name : Down()
	Description   : triggers itself every 1 second and alerts user before 5 mins of session timeout.
	
	Return        : none
	Dependencies  : none
	
	Author        : Ravi Panchal
	Date Created  : 25/10/2004   
*/
 
function sessionAlertWindow(msg) {
	var f = document.frm;
	var newWin = window.open("",'Alert','height=250, width=300, top=200, left=200, status=no, resizable=no, menubar=no, titlebar=1, hotkeys=0, scrollbars=0');
	newWin.location.href = "sessTimeOutAlertMsg.jsp?alertMsg="+msg;
	//f.actionCommand.value = 'user.sesstimeout.alert';		
	//f.action="ActionServlet.servlet?alertMsg="+msg;
	//f.target='Alert';
	//f.submit();
}

/*
	Function Name : Down()
	Description   : triggers itself every 1 second and alerts user before 5 mins of session timeout.
	
	Return        : none
	Dependencies  : none
	
	Author        : Ravi Panchal
	Date Created  : 25/10/2004   
*/

function Down() {
	sec--;
	if (sec == -1) { sec = 59; min--; }
	if (min == 0 && sec == 0) {
		var msg = "";
		var generatedTime = strSessionTimeOut;
		if(generatedTime != null && generatedTime.length > 0)
			msg = "Generated Time : " + generatedTime + "\n";
			
		msg += "Your EASYS session will timeout after " + minBeforeTimeOut + " minutes. ";
		msg += "Please save your work if required.";
		//sessionAlertWindow(msg);
		alert(msg);
	}
	else down = setTimeout("Down()", 1000);
}

/*
	Function Name : timeIt()
	Description   : called to set timer for alerting user 5 mins. before session timeout.
	
	Return        : none
	Dependencies  : none
	
	Author        : Ravi Panchal
	Date Created  : 25/10/2004   
*/

function timeIt() {
	Down();
}

/*
	Function Name : chkPassword(objPwd)
	Description   : validates user password for its length and presence of mixture of alphanumeric chars.
	
	Return        : true/false
	Dependencies  : trim
	
	Author        : 
	Date Created  : 30/10/2004   
*/ 
function chkPassword(objPwd) {
	var str = trim(objPwd.value);
	var isAlpha = false;
    var isNumeric = false;

	// Check for password length
    if(objPwd.value.length<8)  {
         alert("Password should not be less than 8 characters");
         objPwd.select();
         objPwd.focus();
         return false;
   	}

   // Check for blank spaces in password
   for(var count=0; count < str.length; count++) {
     if(str.charAt(count)==' ') {
	      alert("Password should not have blank spaces");
	      objPwd.select();
	      objPwd.focus();
	      return false;
     }
   }
   // Check for numbers in password
   for(var count=0; count < str.length; count++) {
		if((str.charAt(count)>='0')&&(str.charAt(count)<='9')) {
			isNumeric = true;
            continue;                   
	    }                  
  	}
  	// Check for alphabets in password
  	for(var count=0; count < str.length; count++) {        
       if(((str.charAt(count)>='A')&&(str.charAt(count)<='Z')) || ((str.charAt(count)>='a')&&(str.charAt(count)<='z'))) {
             isAlpha = true;
             continue;
       }
  	}

	if(isNumeric == false || isAlpha == false) {
	    alert("Password should be AlphaNumeric");
	    objPwd.select();
	    objPwd.focus();
	    return false;
	}                  
	
	return true;
}

/*
	Function Name : chkDate(dateObj)
	Description   : validates date.
	
	Return        : true/false
	Dependencies  : isDate()
	
	Author        : Ravi Panchal
	Date Created  : 04/11/2004   
*/ 

function chkDate(dateObj) {
	if(!isDate(dateObj.value)) {
		alert("Please enter valid date in dd/MM/yyyy format");
		dateObj.focus();
		return false;
	}		
	return true;
}

/*
	Function Name : setUpper(event)
	Description   : converts text to uppercase on keypress event in textbox
	
	Return        : true/false
	Dependencies  : None
	
	Author        : Ravi Panchal
	Date Created  : 26/11/2004   
*/ 
function setUpper(evt)	{
	evt = (evt) ? evt : event;
	var kc = evt.keyCode;
	if (kc>96 && kc<123) {
		evt.keyCode=kc-32;
	}
	return true;
}


/*
	Function Name : isNricFollowsAlgo(obj)
	Description   : to check if nric is in valid format and follows UIN/FIN Algorithm
	
	Return        : true/false.
	Dependencies  : trim.js
	
	Author        : Ravi Panchal
	Date Created  : 25/04/2005 
*/
function isNricFollowsAlgo(obj) {
	var param_value = trim(obj.value);
	var multiplyArr = new Array('2', '7', '6', '5', '4', '3', '2');
	var chkDigit1 = new Array('A','B','C','D','E','F','G','H','I','Z','J');
	var chkDigit2 = new Array('K','L','M','N','P','Q','R','T','U','W','X');
	var sum = 0;
	var remainder = 0;
	var divideFactor = 11;
	var addFactor = 4;
	var P = 0;
	var lastChar;
	var validNric;
	
	// Check if NRIC is in valid format
	if(!isNricValid(obj)) {
		return false;
	}
 
	/*
	 * Now check if NRIC follows UIN/FIN Algorithm
	 * i.e. 1st char : one of S,T,F,G characters
	 */
	var r1 = /^[STFG]{1}\d{7}[a-zA-Z]{1}$/;
	if(!r1.test(param_value)) {
		alert("Incorrect NRIC");
		obj.focus();
		return false;
	} 
	
	for(var i=1; i <= param_value.length-2; i++) {
		sum += parseInt(multiplyArr[i-1])*parseInt(param_value.charAt(i));
	}
	
	if(param_value.charAt(0) == 'T' || param_value.charAt(0) == 'G') {
		sum += addFactor;
	}
	
	//alert("S1:" + sum);
	remainder = sum%divideFactor;
	//alert("R1:" + remainder);
	
	P = divideFactor - remainder;
	//alert("P:" + P);
	if(param_value.charAt(0) == 'T' || param_value.charAt(0) == 'S') {
		lastChar = chkDigit1[P-1];
	} else if(param_value.charAt(0) == 'G' || param_value.charAt(0) == 'F') {
		lastChar = chkDigit2[P-1];
	}
	
	validNric = param_value.substr(0,param_value.length-1)+lastChar;
	//alert(validNric);
	
	if(param_value != validNric) {
		alert("Incorrect NRIC");
		obj.focus();
		return false;	
	}
	return true;
}

/*
	Function Name : isSingaporeanNRIC(obj)
	Description   : to check if nric is a Singaporean NRIC
	
	Return        : true/false.
	Dependencies  : trim.js
	
	Author        : Ravi Panchal
	Date Created  : 15/06/2005 
*/
function isSingaporeanNRIC(obj) {	
	var param_value = trim(obj.value);
	// Check if NRIC is in valid format
	if(!isNricValid(obj)) {
		return false;
	}
 
	/*
	 * check if NRIC is Singaporean NRIC
	 * i.e. 1st char : one of S,T characters
	 */
	var r1 = /^[ST]{1}\d{7}[a-zA-Z]{1}$/;
	if(!r1.test(param_value)) {
		alert("Incorrect NRIC");
		obj.focus();
		return false;
	} 
	return true;
}

/*
	Function Name : isValidMukimNo(mukimTypeObj, mukimNoObj, lotNoObj)
	Description   : to check if mukim no is in valid format and follows the algorithm
	
	Return        : true/false.
	Dependencies  : trim.js
	
	Author        : Ravi Panchal
	Date Created  : 15/06/2005 
*/
function isValidMukimNo(mukimTypeObj, mukimNoObj, lotNoObj) {
	var mukimType = trim(mukimTypeObj.value);
	var mukimNo = trim(mukimNoObj.value);
	var lotNo = trim(lotNoObj.value);
	var multiplyArrLand = new Array('1', '17', '13', '11', '7', '5', '3', '2');
	var multiplyArrStrata = new Array('1', '19', '17', '13', '11', '7', '5', '3', '2');
	var multiplyArrAcc = new Array('1', '13', '11', '7', '5', '3', '2');
	var multiplyArr;
	var chkDigit = new Array('T','L','P','X','V','N','W','K','M','A','C');
 	
	var sum = 0;
	var remainder = 0;
	var divideFactor = 11;
	var P = 0;
	var lastChar;
	var validLotNo;
	var landType = 0;
	var tmpLotNo;
	var N1;
	var strMukimType;
	var param_value;
	var isValid = true;
	
	if(mukimType == 1) {
		strMukimType = "MK";
	} else if(mukimType == 2) {
		strMukimType = "TS";
	}
	param_value = strMukimType + mukimNo + lotNo;
	//alert(param_value);
	/*
	 * Now check if mukim no follows Algorithm
	 * i.e. A1A2N2N3 - N4N5N6N7N8 X or
	 *      A1A2N2N3 - N4N5N6N7N8N9 X or
	 *		A1A2N2N3 - N4N5N6N7 X
	 */
	var rLand = /^[A-Z]{2}\d{7}[A-Z]{1}$/;
	var rStrata = /^[A-Z]{2}\d{2}[A-Z]{1}\d{6}[A-Z]{1}$/;
	var rAcc = /^[A-Z]{2}\d{2}[A-Z]{1}\d{4}[A-Z]{1}$/;
	
	if(lotNo.charAt(0) == 'U') {
		if(!rStrata.test(param_value)) {
			isValid = false;
		} 
		landType = 2;
		tmpLotNo = lotNo.substring(1, lotNo.length-1);
		multiplyArr = multiplyArrStrata;
	} else if(lotNo.charAt(0) == 'A') {
		if(!rAcc.test(param_value)) {
			isValid = false;
		} 
		landType = 3;
		tmpLotNo = lotNo.substring(1, lotNo.length-1);
		multiplyArr = multiplyArrAcc;
	} else if(isPosInteger(lotNo.charAt(0))) {
		if(!rLand.test(param_value)) {
			isValid = false;
		} 
		landType = 1;
		tmpLotNo = lotNo.substring(0, lotNo.length-1);;
		multiplyArr = multiplyArrLand;
	} else {
		isValid = false;
	}

	if(!isValid) {
		alert("Incorrect LOT & Mukim/TS No");
		lotNoObj.focus();
		return false;
	}
	if(mukimType == 1) {
		N1 = 0;
	} else if(mukimType == 2) {	
		N1 = 1;
	}
	var test_value = "" + N1 + mukimNo + tmpLotNo;
	//alert("Multiply Arr:" + multiplyArr);
	//alert("Test Value:" + test_value);
	
	for(var i=0; i < test_value.length; i++) {
		sum += parseInt(multiplyArr[i])*parseInt(test_value.charAt(i));
	}
	
	//alert("S1:" + sum);
	remainder = sum%divideFactor;
	//alert("R1:" + remainder);
	
	P = divideFactor - remainder;
	//alert("P:" + P);
	lastChar = chkDigit[P-1];
	
	validLotNo = strMukimType + test_value.substr(1,test_value.length-1) + lastChar;
	//alert("Valid Lot Not:" + validLotNo);
	
	if(param_value != validLotNo) {
		alert("Incorrect LOT & Mukim/TS No");
		lotNoObj.focus();
		return false;
	}
	return true;
}

function checkMailId(mailids)
{

var arr = new Array('.com','.net','.org','.biz','.coop','.info','.museum','.name','.pro'
,'.edu','.gov','.int','.mil','.ac','.ad','.ae','.af','.ag','.ai','.al',
'.am','.an','.ao','.aq','.ar','.as','.at','.au','.aw','.az','.ba','.bb',
'.bd','.be','.bf','.bg','.bh','.bi','.bj','.bm','.bn','.bo','.br','.bs',
'.bt','.bv','.bw','.by','.bz','.ca','.cc','.cd','.cf','.cg','.ch','.ci',
'.ck','.cl','.cm','.cn','.co','.cr','.cu','.cv','.cx','.cy','.cz','.de',
'.dj','.dk','.dm','.do','.dz','.ec','.ee','.eg','.eh','.er','.es','.et',
'.fi','.fj','.fk','.fm','.fo','.fr','.ga','.gd','.ge','.gf','.gg','.gh',
'.gi','.gl','.gm','.gn','.gp','.gq','.gr','.gs','.gt','.gu','.gv','.gy',
'.hk','.hm','.hn','.hr','.ht','.hu','.id','.ie','.il','.im','.in','.io',
'.iq','.ir','.is','.it','.je','.jm','.jo','.jp','.ke','.kg','.kh','.ki',
'.km','.kn','.kp','.kr','.kw','.ky','.kz','.la','.lb','.lc','.li','.lk',
'.lr','.ls','.lt','.lu','.lv','.ly','.ma','.mc','.md','.mg','.mh','.mk',
'.ml','.mm','.mn','.mo','.mp','.mq','.mr','.ms','.mt','.mu','.mv','.mw',
'.mx','.my','.mz','.na','.nc','.ne','.nf','.ng','.ni','.nl','.no','.np',
'.nr','.nu','.nz','.om','.pa','.pe','.pf','.pg','.ph','.pk','.pl','.pm',
'.pn','.pr','.ps','.pt','.pw','.py','.qa','.re','.ro','.rw','.ru','.sa',
'.sb','.sc','.sd','.se','.sg','.sh','.si','.sj','.sk','.sl','.sm','.sn',
'.so','.sr','.st','.sv','.sy','.sz','.tc','.td','.tf','.tg','.th','.tj',
'.tk','.tm','.tn','.to','.tp','.tr','.tt','.tv','.tw','.tz','.ua','.ug',
'.uk','.um','.us','.uy','.uz','.va','.vc','.ve','.vg','.vi','.vn','.vu',
'.ws','.wf','.ye','.yt','.yu','.za','.zm','.zw'); 
var mai = mailids;
var val = true;

var dot = mai.lastIndexOf(".");
var ext = mai.substring(dot,mai.length);
//alert(ext);
var at = mai.indexOf("@");

if(dot > 5 && at >1)
{
for(var i=0; i<arr.length; i++)
{
if(ext == arr[i])
{
val = true;
break;
} 
else
{
val = false;
}
}
if(val == false)
{
alert("Your maild "+mai+" is not corrrrect");
return false;
}
}
else
{
alert("Your maild "+mai+" is not correct");
return false;
}

return true;
}

function submitForm(param) {	
  document.frm.target='_self';
  document.frm.actionCommand.value=param;
  document.frm.action="ActionServlet.servlet";
  document.frm.submit();
}

/*
	Function Name : dateDiff(fromDate, toDate)
	Description   : to computer the difference between two dates
	
	Return        : date difference.
	Dependencies  : 
	
	Author        : Ravi Panchal
	Date Created  : 15/06/2005 
*/
function dateDiff(fromDate,toDate) {
	var strTemp;
	if((fromDate != null) ||(fromDate != ""))
	{
		strTemp = fromDate.substring(0,2);
		if(strTemp.charAt(0)=='0')
			strTemp=strTemp.substr(1);
		var fromDay=parseInt(strTemp);
		strTemp = fromDate.substring(3,5);
		if(strTemp.charAt(0)=='0')
			strTemp=strTemp.substr(1);
		var fromMonth=parseInt(strTemp);
		var fromYear=parseInt(fromDate.substring(6,10));		
	}
	
	if((toDate != null) ||(toDate != ""))
	{
		strTemp = toDate.substring(0,2);
		if(strTemp.charAt(0)=='0')
			strTemp=strTemp.substr(1);
		var toDay=parseInt(strTemp);
		strTemp = toDate.substring(3,5);
		if(strTemp.charAt(0)=='0')
			strTemp=strTemp.substr(1);
		var toMonth=parseInt(strTemp);
		var toYear=parseInt(toDate.substring(6,10));		
	}
        
    if(toYear < fromYear)
    	return -1;
    else
    if((toYear == fromYear) && (toMonth < fromMonth))
  		return -1;
  	else
  	if((toYear == fromYear) && (toMonth == fromMonth) && (toDay < fromDay))
  		return -1;
  	else
  	if((toYear == fromYear) && (toMonth == fromMonth) && (toDay == fromDay))
  		return 0;
  	else
  	{
		date1 = new Date();
		date2 = new Date();
		diff  = new Date();

		date1temp = new Date(fromYear,fromMonth-1,fromDay);
		date1.setTime(date1temp.getTime());
	
		date2temp = new Date(toYear,toMonth-1,toDay);
		date2.setTime(date2temp.getTime());

		diff.setTime(Math.abs(date2.getTime() - date1.getTime()));

		timediff = diff.getTime();
		
		return Math.floor(timediff / (1000 * 60 * 60 * 24));		
  	}
}

function funReportPrinter(viewParam, command, param) {
	if(viewParam == "EXCEL")
		document.frm.viewType.value="EXCEL";
	else
		document.frm.viewType.value="PRINT";

	var newWin = window.open("",'report','height=400, width=755, top=100, left=110, status=yes, resizable=yes,menubar=yes, titlebar=0, hotkeys=0, scrollbars=yes');
	document.frm.actionCommand.value = command;				
	if(document.frm.parameter) {
		document.frm.parameter.value = param;
	}
	document.frm.target='report';
	document.frm.submit();
}

function valDateFmt(datefmt) {
	myOption = -1;
	for (i=0; i<datefmt.length; i++) {
	if (datefmt[i].checked) {myOption = i;}}
	if (myOption == -1) {alert("You must select a date format");return ' ';}
	return datefmt[myOption].value;
}

function valDateRng(daterng) {
	myOption = -1;
	for (i=0; i<daterng.length; i++) {
		if (daterng[i].checked) {myOption = i;}
	}
	if (myOption == -1) {
		alert("You must select a date range");return ' ';
	}
	return daterng[myOption].value;
}

function stripBlanks(fld) {
	var result = "";
	for (i=0; i<fld.length; i++) {
		if (fld.charAt(i) != " " || c > 0) {
			result += fld.charAt(i);
			if (fld.charAt(i) != " ") c = result.length;
		}
	}
	return result.substr(0,c);
}

var numb = '0123456789';
function isValid(parm,val) {
	if (parm == "") return true;
	for (i=0; i<parm.length; i++) {
		if (val.indexOf(parm.charAt(i),0) == -1)
		return false;
	}
	return true;
}

function isNum(parm) {
	return isValid(parm,numb);
}
var mth = new Array('','january','february','march','april','may','june','july','august','september','october','november','december');
var day = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

function validateDate(fld,fmt,rng) { 
	var dd, mm, yy;var today = new Date;var t = new Date;fld = stripBlanks(fld);
	if (fld == '') 
		return false;
	var d1 = fld.split('\/');
	if (d1.length != 3) d1 = fld.split(' ');
	if (d1.length != 3) return false;
	if (fmt == 'u' || fmt == 'U') {
	  	dd = d1[1]; mm = d1[0]; yy = d1[2];
	} else if (fmt == 'j' || fmt == 'J') {
	  	dd = d1[2]; mm = d1[1]; yy = d1[0];
	} else if (fmt == 'w' || fmt == 'W'){
	  	dd = d1[0]; mm = d1[1]; yy = d1[2];
	} else 
		return false;
	var n = dd.lastIndexOf('st');
	if (n > -1) dd = dd.substr(0,n);
	n = dd.lastIndexOf('nd');
	if (n > -1) dd = dd.substr(0,n);
	n = dd.lastIndexOf('rd');
	if (n > -1) dd = dd.substr(0,n);
	n = dd.lastIndexOf('th');
	if (n > -1) dd = dd.substr(0,n);
	n = dd.lastIndexOf(',');
	if (n > -1) dd = dd.substr(0,n);
	n = mm.lastIndexOf(',');
	if (n > -1) mm = mm.substr(0,n);
	if (!isNum(dd)) return false;
	if (!isNum(yy)) return false;
	if (!isNum(mm)) {
	  var nn = mm.toLowerCase();
	  for (var i=1; i < 13; i++) {
	    if (nn == mth[i] || nn == mth[i].substr(0,3)) {
	    	mm = i; i = 13;
	    }
	  }
	}
	if (!isNum(mm)) return false;
	dd = parseFloat(dd); mm = parseFloat(mm); yy = parseFloat(yy);
	if (yy < 100) yy += 2000;
	if (yy < 1582 || yy > 4881) return false;
	if (mm == 2 && (yy%400 == 0 || (yy%4 == 0 && yy%100 != 0))) day[mm-1]++;
	if (mm < 1 || mm > 12) return false;
	if (dd < 1 || dd > day[mm-1]) return false;
	t.setDate(dd); t.setMonth(mm-1); t.setFullYear(yy);
	if (rng == 'p' || rng == 'P') {
		if (t > today) return false;
	}
	else if (rng == 'f' || rng == 'F') {
		if (t < today) return false;
	}
	else if (rng != 'a' && rng != 'A') return false;
	return true;
}