	
	
var nbsp = 160;		// non-breaking space char
var node_text = 3;	// DOM text node-type
var emptyString = /^\s*$/ ;
var global_valfield;	// retain valfield for timer thread
var proceed = 2;  
	
function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '');
}
function setFocusDelayed()
{
  global_valfield.focus();
}

function setfocus(valfield)
{
  // save valfield in global variable so value retained when routine exits
  global_valfield = valfield;
  setTimeout( 'setFocusDelayed()', 100 );
}

function isBlank (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required
{
	//alert(valfield.value);
	
  if (!document.getElementById) 
    return true;  // not available on this browser - leave validation to the server
  var elem = document.getElementById(infofield);
  if (!elem.firstChild) return true;  // not available on this browser 
  if (elem.firstChild.nodeType != node_text) return true;  // infofield is wrong type of node  

  if (emptyString.test(valfield.value)) {
	
    if (required) {
      msg (infofield, "error", "ERROR: required");  
      setfocus(valfield);
      return false;
    }
    else {
      msg (infofield, "warn", "");   // OK
      return true;  
    }
	
  }else {
      msg (infofield, "warn", "");   // OK
      return true;  
    }
  
  
  
  //return proceed;
}


function commonCheck    (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required
{
	//alert(valfield.value);
	
  if (!document.getElementById) 
    return true;  // not available on this browser - leave validation to the server
  var elem = document.getElementById(infofield);
  if (!elem.firstChild) return true;  // not available on this browser 
  if (elem.firstChild.nodeType != node_text) return true;  // infofield is wrong type of node  

  if (emptyString.test(valfield.value)) {
	
    if (required) {
      msg (infofield, "error", "ERROR: required");  
      setfocus(valfield);
      return false;
    }
    else {
      msg (infofield, "warn", "");   // OK
      return true;  
    }
	
  }
  
  
  
  return proceed;
}
// --------------------------------------------
//                  msg
// Display warn/error message in HTML element.
// commonCheck routine must have previously been called
// --------------------------------------------

function msg(fld,     // id of element to display message in
             msgtype, // class to give element ("warn" or "error")
             message) // string to display
{
  // setting an empty string can give problems if later set to a 
  // non-empty string, so ensure a space present. (For Mozilla and Opera one could 
  // simply use a space, but IE demands something more, like a non-breaking space.)
  var dispmessage;
  if (emptyString.test(message)) 
    dispmessage = String.fromCharCode(nbsp);    
  else  
    dispmessage = message;

  var elem = document.getElementById(fld);
  elem.firstChild.nodeValue = dispmessage;  
  
  elem.className = msgtype;   // set the CSS class to adjust appearance of message
}
function checkentry()
	{
		i=0;
		
		//alert('hello')
		x=document.frm2.elements.length-1
		//alert(x)
		// common[]=0;
		for(;i<=x;i++)
		{
			xtype=document.frm2.elements[i].type
			//alert(xtype)
			if(xtype=='text' || xtype=='textarea' || xtype=='select-one' || xtype=='select' || xtype=='select-multiple' || xtype=='password'|| xtype=='file')
			{
				mdata=document.frm2.elements[i].value
				if(mdata=='')
				{
					alert('Enter '+document.frm2.elements[i].name)
					myinsert=0;
					document.frm2.elements[i].focus()					
					return(false);				
				}
				else
				{
					myinsert=1;
				}
				}
					
		}		

		
		return(true);
	}
	

	
function validateAppNo    (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required
			{
			 //alert("INVALID APPNO");
			  var stat = commonCheck (valfield, infofield, required);
				if (stat != proceed) return stat;
  
			  var tfld = trim(valfield.value);
			  var appRE = /^[0-9]{1,11}$/
			  if (!appRE.test(tfld)) {
			  msg (infofield, "error", "ERROR: not a valid Application No.");
			  //alert("INVALID APPNO");
			  //document.frm2.txt_Application_No.focus()	
				setfocus(valfield);
			    return false;
			  }
			  else
				msg (infofield, "warn", "");
			  
			  return true;
			}
/*................................................................................................................................................Validation for Numeric Value.................................................................................................................................................*/
function validateNumeric    (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required
			{
			 
			  var stat = commonCheck (valfield, infofield, required);
				if (stat != proceed) return stat;
  
			  var tfld = trim(valfield.value);
			  var appRE = /^[0-9]{1,11}$/
			  if (!appRE.test(tfld)) {
			  msg (infofield, "error", "ERROR: not a valid Value.");
			  //alert("INVALID APPNO");
			  //document.frm2.txt_Application_No.focus()	
				setfocus(valfield);
			    return false;
			  }
			  else
				msg (infofield, "warn", "");
			  
			  return true;
			}
/*................................................................................................................................................Validation for Taka.................................................................................................................................................*/
function validateTaka    (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required
			{
			 //alert(valfield.value);
			  var stat = commonCheck (valfield, infofield, required);
				if (stat != proceed) return stat;
				var numdigits=0;
				var tfld = trim(valfield.value);
				var appRE =/^[0-9 .]+[0-9]{0,2}$/
				if (!appRE.test(tfld)) {
				msg (infofield, "error", "ERROR: not a valid Value.");
			  //alert("INVALID APPNO");
			  //document.frm2.txt_Application_No.focus()	
				setfocus(valfield);
			    return false;
			  }
				
				else{
					for (var j=0; j<tfld.length; j++)
					if (tfld.charAt(j)>='0' && tfld.charAt(j)<='9' && tfld.charAt(j)!='.' ) numdigits++;
					msg (infofield, "warn", "");
			  }
			  return true;
			}
/*...........................................................................................Not Numeric Input Field...............................................................................................................*/
function validateCharField (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required
{
	//alert(valfield.value);
	
  if (!document.getElementById) 
    return true;  // not available on this browser - leave validation to the server
  var elem = document.getElementById(infofield);
  if (!elem.firstChild) return true;  // not available on this browser 
  if (elem.firstChild.nodeType != node_text) return true;  // infofield is wrong type of node  

  if (emptyString.test(valfield.value)) {
	
    if (required) {
      msg (infofield, "error", "ERROR: required");  
      setfocus(valfield);
      return false;
    }
    else {
      msg (infofield, "warn", "");   // OK
      return true;  
    }
	
  }else {
      msg (infofield, "warn", "");   // OK
      return true;  
    }
  
  
  
  //return proceed;
}

/*
function validateCharField    (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required
			{
			 //alert("INVALID APPNO");
			  var stat = commonCheck (valfield, infofield, required);
				if (stat != proceed) return stat;
  
			  var tfld = trim(valfield.value);
			  var appRE = /^[0-9]{1,11}$/
			  
			 /* if (appRE.test(tfld.charAt(0))) {
					msg (infofield, "error", "ERROR: not a valid Input.");
					setfocus(valfield);
					return false;
			  }*/
			  /*if (!alphanumeric(tfld)) {
					msg (infofield, "error", "ERROR: not a valid Input.");
					setfocus(valfield);
					return false;
			  }
			  else
					msg (infofield, "warn", "");
			  
			  return true;
			}*/

function alphanumeric(alphane)
{
	var numaric = alphane;
	for(var j=0; j<numaric.length; j++)
		{
		  var alphaa = numaric.charAt(j);
		  var hh = alphaa.charCodeAt(0);
		  if((hh > 47 && hh<58) || (hh > 64 && hh<91) || (hh > 96 && hh<123))
		  {
		  }
		else	{
			 return false;
		  }
		}
 return true;
}
			
			
			
/*.....................................................................................Telephone Number Validation..........................................................................................................................*/
function validateTelnr  (valfield,   // element to be validated
			 infofield,  // id of element to receive info/error msg
			 required)   // true if required
{
  var stat = commonCheck (valfield, infofield, required);
  
  if (stat != proceed) return stat;

  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  var telnr = /^\+?[0-9 ()-]+[0-9]$/  ;
  if (!telnr.test(tfld)) {
    msg (infofield, "error", "ERROR: not a valid telephone number. Characters permitted are digits, space ()- and leading +");
    setfocus(valfield);
    return false;
  }

  var numdigits = 0;
  for (var j=0; j<tfld.length; j++)
    if (tfld.charAt(j)>='0' && tfld.charAt(j)<='9') numdigits++;

	var code="+880";
	 for(var i=0;i<3;i++){
	 if(tfld.charAt(i)!=code.charAt(i)){
		msg (infofield, "error", "ERROR: first 4 characters must be country code +880 " );
		setfocus(valfield);
	    return false;
		}
		}
  if (numdigits<6) {
    msg (infofield, "error", "ERROR:" +numdigits+ "digits too - short first 4 characters must be country code +880" );
	//msg (infofield, "error", "ERROR: " + numdigits + " digits - too short");
    setfocus(valfield);
    return false;
  }
 

  if (numdigits>16)
    msg (infofield, "warn", numdigits + " digits - check if correct");
  else { 
    if (numdigits<10)
      msg (infofield, "warn", "Only " + numdigits + " digits - check if correct");
    else
      msg (infofield, "warn", "");
  }
  return true;
}

	function checkpassword()
	{
		if (frm2.txt_Password.value==frm2.txt_CPassword.value)
		{
			return(true);			
		}
		else
		{
			alert('Confirm password not match with new password, pls try again')
			document.frm2.txt_CPassword.focus();
			return(false);
		}
	}
	
	// --------------------------------------------
//               validateEmail
// Validate if e-mail address
// Returns true if so (and also if could not be executed because of old browser)
// --------------------------------------------

function validateEmail  (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required
{
  var stat = commonCheck (valfield, infofield, required);
  if (stat != proceed) return stat;

  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/  ;
  if (!email.test(tfld)) {
    msg (infofield, "error", "ERROR: not a valid e-mail address");
    setfocus(valfield);
    return false;
  }

  var email2 = /^[A-Za-z][\w.-]+@\w[\w.-]+\.[\w.-]*[A-Za-z][A-Za-z]$/  ;
  if (!email2.test(tfld)) 
    msg (infofield, "warn", "Unusual e-mail address - check if correct");
  else
    msg (infofield, "warn", "");
  return true;
}

	
	function search()
	{
		//removeAllOptions(frm2.drop_list.SELECT);
		//addOption(document.drop_list.SEARCH, "", "SELECT", "");

		
		//addOption(frm2.SELECT.value,"Mango");
		//document.frm2.SELECT.focus();
		
		
		
		if(frm2.SEARCH_CT.value == 'designation'){
		
		alert('call');
		
		var option_id=frm2.SEARCH_CT.value;
		
		var url='myLoadList.php?ajxption='+option_id;
		
		new Ajax(url,{
		method: 'get',
		update: $('box2')
		}).request();
		
		}	
		
	}
	function agepopulation(){
		if(frm2.txt_Date_Of_Birth.value){
			frm2.txt_Age.value=frm2.day.value.substr(0,4)-frm2.txt_Date_Of_Birth.value.substr(0,4) 
		}
		else
		alert("First Input Date Of Birth")
		
	}
	
	
	
	//-------------------------------------------for date validation------------------------------------------
	function validateDate  (valfield,   // element to be validated
			 infofield,  // id of element to receive info/error msg
			 required)   // true if required
{
	//alert(valfield.value);
	  var stat = commonCheck (valfield, infofield, required);
	  //alert(stat);
	  if (stat != proceed) return stat;

	  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
	 var dt=/^\d{4}\-\d{2}\-\d{2}$/ //Basic check for format validity
	  if (!dt.test(tfld)) {
	    msg (infofield, "error", "ERROR: not a valid Date. Correct Format is YYYY-MM-DD");
	    setfocus(valfield);
	    return false;
	  }
	  else{ //Detailed check for valid date ranges
		var yearfield=valfield.value.split("-")[0];
		var monthfield=valfield.value.split("-")[1];
		var dayfield=valfield.value.split("-")[2];
		var dayobj = new Date(yearfield, monthfield-1, dayfield);
		//alert("GOT IT");
		if ((dayobj.getFullYear()!=yearfield)||(dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)){
			msg (infofield, "error", "ERROR: not a valid Date. Correct Format is YYYY-MM-DD");
			 setfocus(valfield);
			return false;
		}
}
	  
	  return true;
}
//-------------------------------------------validate date difference-----------------------------
function validateDiff  (valfieldfrom,	//start date
			valfieldend,	//end date
			 infofield,  // id of element to receive info/error msg
			 required)   // true if required
{
	//alert(valfieldfrom.value);
	  
  if (valfieldfrom.value > valfieldend.value) {
	    msg (infofield, "error", "ERROR: Period To Must Be Greater Than Or Equal To Period From");
	    setfocus(valfieldend);
	    return false;
	  }
	  
	  return true;
}
	

	//Test Decimal Validation
	/*function FrontPage_Form1_Validator()
{
  var checkOK = "0123456789-.,";
  var checkStr = frm2.txt_Yearly_Income.value;
  var allValid = true;
  var validGroups = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch = = checkOK.charAt(j))
        break;
    if (j = = checkOK.length)
    {
      allValid = false;
      break;
    }
    if (ch = = ".")
    {
      allNum += ".";
      decPoints++;
    }
    else if (ch = = "," && decPoints != 0)
    {
      validGroups = false;
      break;
    }
    else if (ch != ",")
      allNum += ch;
  }
  if (!allValid)
  {
    alert("Please enter only digit characters in the \"theField\" field.");
    theForm.theField.focus();
    return (false);
  }

  if (decPoints > 1 || !validGroups)
  {
    alert("Please enter a valid number in the \"theField\" field.");
    theForm.theField.focus();
    return (false);
  }
  return (true);
} 
	*/

