//

function resetErrMsg(){
	var  x =1;
	while(document.getElementById('errMsg'+x)!=null){
			document.getElementById('errMsg'+x).style.display = "none";
			x++	;
	}
}

function showErrMsg(msgId){
	if(document.getElementById('errMsg'+msgId)!=null){
			document.getElementById('errMsg'+msgId).style.display = "block";
	}
}

//Check valid number formation
function validFormatNumber(sText,x,y)
{
	if(sText.charAt(0) == "-"){
		sText = sText.substring(1,sText.length);
	}
   var ValidChars = "0123456789.";
   var valid=true;
   var Char;
   var hasDot;
   var front =0 ;
   var back = 0;
   for (i = 0; i < sText.length && valid == true; i++) {
      Char = sText.charAt(i); 
      if(Char=="."){ hasDot = true;}
      else{
  	  if(!hasDot){	front++;}
	  else { back++;}
	  }
	}      
	if(front+back>x){ valid=false;}
	if(back>y){ valid = false;}
	if(front>x-y){ valid = false;}
   return valid;
}



function IsInteger(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
   hyphenCount = 0 ;
  
	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}		
	}
   return IsNumber;   
 }

//Check is numeric
function IsNumeric(sText)
{
   var ValidChars = "-0123456789.";
   var IsNumber=true;
   var Char;
   hyphenCount = 0 ;
   
   if(sText.charAt(0) == "-" && Trim(sText).length == 1){
   	  return false;
   }
 
	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
		if (Char == "-") {
			hyphenCount++;
		}
		if (hyphenCount > 1) {
			IsNumber = false;
		}
	}
   return IsNumber;
   
 }
 
 //Check is numeric
function IsNumeric2(sText,maxLength)
{

   if(sText.charAt(0) == "-"){	// non positive 
   
   	if(sText.length == 1){
   		return false;
   	}
   	
   	if(sText.length-1 > maxLength){
   		return false;
   	}else{
	   	return IsNumeric(sText.substring(1,sText.length));
   	}

   }else{						//is positive
   
   	if(sText.length > maxLength){
   		return false;
   	}else{
   		return IsNumeric(sText);
   	}
   }
 }



// Trim function
function Trim(TRIM_VALUE){
if(TRIM_VALUE.length < 1){
return"";
}
TRIM_VALUE = RTrim(TRIM_VALUE);
TRIM_VALUE = LTrim(TRIM_VALUE);
if(TRIM_VALUE==""){
return "";
}
else{
return TRIM_VALUE;
}
} //End Function

function RTrim(VALUE){
var w_space = String.fromCharCode(32);
var v_length = VALUE.length;
var strTemp = "";
if(v_length < 0){
return"";
}
var iTemp = v_length -1;

while(iTemp > -1){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(0,iTemp +1);
break;
}
iTemp = iTemp-1;

} //End While
return strTemp;

} //End Function

function LTrim(VALUE){
var w_space = String.fromCharCode(32);
if(v_length < 1){
return"";
}
var v_length = VALUE.length;
var strTemp = "";

var iTemp = 0;

while(iTemp < v_length){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(iTemp,v_length);
break;
}
iTemp = iTemp + 1;
} //End While
return strTemp;
} //End Function




//--------------------------------------------------------------------------------------------//
<!-- Begin
function emailCheck (emailStr) {
/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	//alert("Email address seems incorrect (check @ and .)")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    //alert("The username doesn't seem to be valid.")
    return false
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        //alert("Destination IP address is invalid!")
		return false
	    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	//alert("The domain name doesn't seem to be valid.")
    return false
}

/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding 
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
   //alert("The address must end in a three-letter domain, or two letter country.")
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="This address is missing a hostname!"
   //alert(errStr)
   return false
}

// If we've gotten this far, everything's valid!
return true;
}
//  End -->

function checkdate(objName) {
var datefield = objName;
if (chkdate(objName) == false) {
//datefield.select();
//alert("That date is invalid.  Please try again.");
//datefield.focus();
return false;
}
else {
return true;
   }
}
function chkdate(objName) {
//var strDatestyle = "US"; //United States date style
var strDatestyle = "EU";  //European date style
var strDate;
var strDateArray;
var strDay;
var strMonth;
var strYear;
var intday;
var intMonth;
var intYear;
var booFound = false;
var datefield = objName;
var strSeparatorArray = new Array("-"," ","/",".");
var intElementNr;
var err = 0;
var strMonthArray = new Array(12);
strMonthArray[0] = "Jan";
strMonthArray[1] = "Feb";
strMonthArray[2] = "Mar";
strMonthArray[3] = "Apr";
strMonthArray[4] = "May";
strMonthArray[5] = "Jun";
strMonthArray[6] = "Jul";
strMonthArray[7] = "Aug";
strMonthArray[8] = "Sep";
strMonthArray[9] = "Oct";
strMonthArray[10] = "Nov";
strMonthArray[11] = "Dec";
strDate = datefield.value;
if (strDate.length < 1) {
return true;
}
for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
strDateArray = strDate.split(strSeparatorArray[intElementNr]);
if (strDateArray.length != 3) {
err = 1;
return false;
}
else {
strDay = strDateArray[0];
strMonth = strDateArray[1];
strYear = strDateArray[2];
}
booFound = true;
   }
}
if (booFound == false) {
if (strDate.length>5) {
strDay = strDate.substr(0, 2);
strMonth = strDate.substr(2, 2);
strYear = strDate.substr(4);
   }
}
if (strYear.length == 2) {
strYear = '20' + strYear;
}
// US style
if (strDatestyle == "US") {
strTemp = strDay;
strDay = strMonth;
strMonth = strTemp;
}
intday = parseInt(strDay, 10);
if (isNaN(intday)) {
err = 2;
return false;
}
intMonth = parseInt(strMonth, 10);
if (isNaN(intMonth)) {
for (i = 0;i<12;i++) {
if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
intMonth = i+1;
strMonth = strMonthArray[i];
i = 12;
   }
}
if (isNaN(intMonth)) {
err = 3;
return false;
   }
}
intYear = parseInt(strYear, 10);
if (isNaN(intYear)) {
err = 4;
return false;
}
if (intMonth>12 || intMonth<1) {
err = 5;
return false;
}
if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
err = 6;
return false;
}
if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
err = 7;
return false;
}
if (intMonth == 2) {
if (intday < 1) {
err = 8;
return false;
}
if (LeapYear(intYear) == true) {
if (intday > 29) {
err = 9;
return false;
}
}
else {
if (intday > 28) {
err = 10;
return false;
}
}
}
if (strDatestyle == "US") {
datefield.value = strMonthArray[intMonth-1] + " " + intday+" " + strYear;
}
else {
datefield.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear;
}
return true;
}
function LeapYear(intYear) {
if (intYear % 100 == 0) {
if (intYear % 400 == 0) { return true; }
}
else {
if ((intYear % 4) == 0) { return true; }
}
return false;
}
function doDateCheck(from, to) {
if (Date.parse(from.value) <= Date.parse(to.value)) {
alert("The dates are valid.");
}
else {
if (from.value == "" || to.value == "") 
alert("Both dates must be entered.");
else 
alert("To date must occur after the from date.");
   }
}

	function isValidMMYYYY(mm_yyyy){
			var adate = new Date();
			yyyy = adate.getYear();
			if(mm_yyyy.length!=7){
				return false;
			}
			else if(mm_yyyy.substring(0,2)*1<1 || mm_yyyy.substring(0,2)*1>12){
				return false;
			}
			else if(mm_yyyy.substr(3,4)*1 <1900 || mm_yyyy.substr(3,4)*1 > yyyy){
				return false;
			}
			else if(mm_yyyy.substr(2,1)!='/'){
				return false;
			}
			else{
				return true;
			}
	}

	function checkMMYYYYformat(str){

		var errFlg1 = false;
		var re = new RegExp("[0][0-9]/[0-9]{4}|[1][0-2]/[0-9]{4}");
	
		if(str.length!=15){
				errFlg1 = true;
		}
		else if(!str.match(re)){
				errFlg1 = true;
		}
		errFlg1 = errFlg1  || !isValidMMYYYY(str.substring(0,7)) || !isValidMMYYYY(str.substring(8));

		date1 = (str.substr(3,4)+ str.substr(0,2))*1;
		date2 = (str.substr(11,4)+ str.substr(8,2))*1;

		if(date2-date1<0){
			errFlg1 = true;
		}
		
		return errFlg1;
	}

//  End -->
function checkDateYYYYMMDD(str){
	if(Trim(str).length==0){

		return true;
	}
	if(Trim(str).length!=10){

		return false;
	}
	aYear = str.substr(0,4);
	aMonth = str.substr(5,2);
	aDay = str.substr(8,2);
	if(!IsInteger(aYear) || !IsInteger(aMonth) || !IsInteger(aDay)){
		alert("1");
		return false;
	}

	if(eval(aYear)<1980){
		alert("2");
		return false;
	}
	if(eval(aMonth)<1 || eval(aMonth) > 12){
		alert("3");
		return false;
	}

	if ((aMonth == "01" || aMonth == "03" || aMonth == "05" || aMonth == "07" || aMonth == "08" || aMonth == "10" || aMonth == "12") && (eval(aDay) > 31 || eval(aDay) < 1)) {
		alert("4");
		return false;
	}
	if ((aMonth == "04" || aMonth == "06" || aMonth == "09" || aMonth == "11") && (eval(aDay )> 30 || eval(aDay) < 1)) {
		alert("5");
		return false;
	}
	if(LeapYear(aYear)){
		if(eval(aMonth)==2 && eval(aDay)>=30 ){
			return false;
		}
	}
	else{
		if(eval(aMonth)==2 && eval(aDay)>=29 ){
			return false;
		}
	}
	return true;
}

var PatternAlphaNumeric = /[^A-Za-z0-9 ]/
var PatternAlpha = /[^A-Za-z ]/
var PatternNumeric = /[^0-9 ]/
function isAlphaNumeric(value) {
	return checkPattern(value, PatternAlphaNumeric);
}

function isAlpha(value){
	return checkPattern(value,PatternAlpha);
}

function isNumber(value){
	return checkPattern(value,PatternNumeric);
}

function atLeastOneAlphaNumber(value){
	var c = '';
	var containAlpha = false;
	var containNumber = false;
	
	for ( x=0; x < value.length; x++){
		c = value.charAt(x);
		containAlpha = containAlpha || isAlpha(c);
		containNumber = containNumber || isNumber(c);
	}
	
	return containNumber && containAlpha;
}


function checkPattern(value, pattern) {
	if(value.search(pattern) != -1)
		return false
	else
		return true
}

function consectivePasswdCheck(valueOld,valueNew){
	if(valueOld.length<3){
		return false;
	}
	else{
		idx = 0;
		while (idx+2 < valueOld.length){
			tmpStr = valueOld.substring(idx,idx+3);
			if(valueNew.indexOf(tmpStr)>-1){
				return false;
			}
			idx++;
		}
		return true;
	}
}

function consecutiveSameCharacter(value){
	containConsecutiveCharacter = false;
	for (i = 0; i < value.length ; i++) 
	{ 
		Char = value.charAt(i); 
		if (value.indexOf(Char+Char+Char)>-1) {
			containConsecutiveCharacter = true;
		}
	}
	return containConsecutiveCharacter;
}

//BASE64 Encode / Decode
var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var base64DecodeChars = new Array(
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
-1,0,1,2,3,  4,5,6,7,8,9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);

function base64encode(str) {

var out, i, len;

var c1, c2, c3;

len = str.length;

i = 0;

out = "";

while(i < len) {

 c1 = str.charCodeAt(i++) & 0xff;

 if(i == len)

 {

 out += base64EncodeChars.charAt(c1 >> 2);

 out += base64EncodeChars.charAt((c1 & 0x3) << 4);

 out += "==";

 break;

 }

 c2 = str.charCodeAt(i++);

 if(i == len)

 {

 out += base64EncodeChars.charAt(c1 >> 2);

 out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));

 out += base64EncodeChars.charAt((c2 & 0xF) << 2);

 out += "=";

 break;

 }

 c3 = str.charCodeAt(i++);

 out += base64EncodeChars.charAt(c1 >> 2);

 out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));

 out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));

 out += base64EncodeChars.charAt(c3 & 0x3F);

}
return out;

}

function base64decode(str) {

var c1, c2, c3, c4;

var i, len, out;

len = str.length;

i = 0;

out = "";

while(i < len) {

 /* c1 */

 do {

 c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];

 } while(i < len && c1 == -1);

 if(c1 == -1)

 break;

 /* c2 */

 do {

 c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];

 } while(i < len && c2 == -1);

 if(c2 == -1)

 break;

 out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));

 /* c3 */

 do {

 c3 = str.charCodeAt(i++) & 0xff;

 if(c3 == 61)

return out;

 c3 = base64DecodeChars[c3];

 } while(i < len && c3 == -1);

 if(c3 == -1)

 break;

 out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));

 /* c4 */

 do {

 c4 = str.charCodeAt(i++) & 0xff;

 if(c4 == 61)

return out;

 c4 = base64DecodeChars[c4];

 } while(i < len && c4 == -1);

 if(c4 == -1)

 break;

 out += String.fromCharCode(((c3 & 0x03) << 6) | c4);

}

return out;

}



