﻿// JScript File

// ----------------------------------------------------------------------
// Javascript form validation routines.
// this file is create by omprakash on 25 of January 2007
// ----------------------------------------------------------------------
//Specification of main function which has been called by every control.
//valfield ------this is a id of control to be validate
//infofield-------this is a id of <TD> on which we will display message.
//required--this is a true/false which is to be used if input is required 
//ConditionParam--this is a conditon parameter based on which validation will perform
//ValidationEntry() is a comman function which will be called by every control.
//case which is used for validation
//1 email validation
//2 age validation(can be modify to use for range validation(e.g. 20 to 40 only) by user.
//3 phone no validation
//4 if only required condition has to be validate.
//5 if only character is needed
//6 if only numbers are needed
//7 if max length has to be decide. id for which validation will impliment like Max100,Max10 in which last integer value will validate.
//8 drop down at least item must be selected.
//9 check for check boxes
//10 check for radio
//11 validation for minimum length of string that uses give.
//12 validation for date entered by user
//13 validation for special character entry.
//14 validation for date difference in two textboxes.
//ValidateSpecialCharacters
function ValidationEntry(valfield,infofield,required,ConditionParam,ExtraInfo)
{

    var params;
    var strStat;
    
    params = ConditionParam.split(",");
  
     var intCounter;
     for (intCounter = 0; intCounter < params.length; intCounter++)
      {
           switch(params[intCounter])
            {    
            case'1':
                //function to validate email
                strStat =  validateemail(valfield,infofield,required);
                 if (strStat == false)
                    return strStat;
                break;
            case'2':
                //function to validate age 
                strStat =  validateAge(valfield,infofield,required);
                 if (strStat == false)
                    return strStat;
                break;
            case'3':
                //function to validate phone 
                strStat =  validatetelnr(valfield,infofield,required)
                 if (strStat == false)
                    return strStat;
                break;
            case '4':
                // only required validation.
                strStat = validatePresent(valfield,infofield);
                 if (strStat == false)
                    return strStat;
                break;
             case '5':
                // only character validation.
                strStat = validateCharacter(valfield,infofield,required);
                if (strStat == false)
                    return strStat;
                break;
             case '6':
                // only num validation.
                strStat = validateNumbers(valfield,infofield,required);
                if (strStat == false)
                    return strStat;
                break;
             case '7':
                //max length validation.
                strStat = validateMaxLength(ExtraInfo,valfield,infofield,required);
                if (strStat == false)
                    return strStat;
                break;
             case '8':
                //drop down and list box selection validation.
                strStat = validateDropDownListBox(valfield,infofield,ExtraInfo);
                if (strStat == false)
                    return strStat;
                break;
             case '9':
                //this function is call when no  check box is selected."
                strStat = OnSubmitCheckBox(valfield,infofield,ExtraInfo);
                if (strStat == false)
                    return strStat;
                break;
             case '10':
                //this function is call when no radion button  is selected."
                strStat = OnSubmitRadio(valfield,infofield,ExtraInfo);
                if (strStat == false)
                    return strStat;
                break;
            case '11':
                //min length validation.
                strStat = validateMinLength(valfield,infofield,required);
                if (strStat == false)
                    return strStat;
                break;
            case '12':
                //validate date in mm/dd/yyyy validation.
                strStat =  ValidateDate(valfield,infofield,required)
                if (strStat == false)
                    return strStat;
                break;
            case '13':
                //validate "!@#$%^&*()+=-[]\\\';,./{}|\":<>?"; characters.
                strStat =  ValidateSpecialCharacters(valfield,infofield,required)
                if (strStat == false)
                    return strStat;
                break;
             case '14':
                //validate max value.
                strStat =  ValidateMaxValue(valfield,infofield,required,ExtraInfo)
                if (strStat == false)
                    return strStat;
                break;
              case '15':
                //validate credit card number.
                strStat =  ValidateCCNumber (valfield,infofield,required)
                if (strStat == false)
                    return strStat;
                break;
              case '16':
                //validate "<>" characters.
                strStat =  ValidateAngleBrackets(valfield,infofield,required)
                if (strStat == false)
                    return strStat;
                break;
              case '17':
                 strStat =  ValidateSplCharsForEmail(valfield,infofield,required)
                if (strStat == false)
                    return strStat;
                break;
            }     
    }
    
 }
 //==============All function List to be used for validation
///differnet variable used in this function
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

// --------------------------------------------
//                  trim
// Trim leading/trailing whitespace off string
// --------------------------------------------


function trim(str)
{
return str.replace(/^\s+|\s+$/g, '');
}

// --------------------------------------------
//                  setfocus
// Delayed focus setting to get around IE bug
// --------------------------------------------

function setFocusDelayed()
{
//alert(global_valfield);
if(global_valfield==null || global_valfield=="undefined")
{

}
else
{
  global_valfield.focus();
  }
}

function setfocus(valfield)
{
 //var valfield123 = document.getElementById(valfield);
  // save valfield in global variable so value retained when routine exits
  global_valfield = valfield;
  //setTimeout( 'setFocusDelayed()', 100 );
}


// --------------------------------------------
//                  msg
// Display warn/error message in HTML element.
// commonCheck routine must have previously been called
// --------------------------------------------
   //fld id of element to display message in
              //msgtype class to give element ("warn" or "error")
              //message string to display
function msg(fld,msgtype,message)          
{
  // 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 elem11 = document.getElementById(fld);
  if (elem11.firstChild!=null && elem11.firstChild!=undefined)
  {
    elem11.firstChild.nodeValue = dispmessage;  
  }
  else
  {
     elem11.value= dispmessage; 
  }
    
  elem11.className = msgtype;   // set the CSS class to adjust appearance of message
  
  //var elem = document.getElementById(fld);
  //elem.firstChild.nodeValue = dispmessage;  
  //elem.className = msgtype;   // set the CSS class to adjust appearance of message
}

// --------------------------------------------
//            commonCheck
// Common code for all validation routines to:
// (a) check for older / less-equipped browsers
// (b) check if empty fields are required
// Returns true (validation passed), 
//         false (validation failed) or 
//         proceed (don't know yet)
// --------------------------------------------

var proceed = 2;

function commonCheck(valfield,   // element to be validated
                    infofield,  // id of element to receive info/error msg
                    required)   // true if required
{

  //if (!document.getElementById) 
  //  return true;  // not available on this browser - leave validation to the server
  var valfield123 = document.getElementById(valfield);
  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(trim(valfield123.value))) 
  {
   
    if (required=='true') 
    {
       msg(infofield, "errortext", "* Required!");  
      setfocus(valfield123);
      return false;
    }   
    else {
      msg (infofield, "errortext", "");   // OK
      return true;  
    }
  }
    return proceed;
}

// --------------------------------------------
//            validatePresent
// Validate if something has been entered
// Returns true if so 
// --------------------------------------------

function validatePresent(valfield,   // element to be validated
                         infofield) // id of element to receive info/error msg
{
  var stat = commonCheck(valfield,infofield,'true');
  if (stat != proceed) 
  {
    return stat;
  }
  else
  {
    msg (infofield, "errortext", "");  
    return true;
  }
}

// --------------------------------------------
//               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 valfield123 = document.getElementById(valfield);
  
  var tfld = trim(valfield123.value);  // value of field with whitespace trimmed off
  var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/  ;
  if (!email.test(tfld)) {
    msg (infofield, "errortext", "Invalid Email address");
    setfocus(valfield123);
    return false;
  }
   else
  {
    msg (infofield, "errortext", "");
  return true;
  }
//  var email2 = /^[A-Za-z][\w.-]+@\w[\w.-]+\.[\w.-]*[A-Za-z][A-Za-z]$/  ;
//  if (!email2.test(tfld)) 
//  {
//    msg (infofield, "errortext", "Unusual e-mail address - check if correct");
//  }
//  else
//  {
//    msg (infofield, "errortext", "");
//  return true;
//  }
}

// --------------------------------------------
//               ValidateCCNumber
// Validate CC Number format
// Returns true if so (and also if could not be executed because of old browser)
// --------------------------------------------

function ValidateCCNumber(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 valfield123 = document.getElementById(valfield);
      
      var tfld = trim(valfield123.value);  // value of field with whitespace trimmed off
      var regexCC = /^[0-9]{10,26}$/
      if (!regexCC.test(tfld)) {
        msg (infofield, "errortext", " Invalid Card Number");
        setfocus(valfield123);
        return false;
      }
       else
      {
          msg (infofield, "errortext", "");
          return true;
      }
}

// --------------------------------------------
//            validateTelnr
// Validate telephone number
// Returns true if so (and also if could not be executed because of old browser)
// Permits spaces, hyphens, brackets and leading +
// --------------------------------------------

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 valfield123 = document.getElementById(valfield);
  var tfld = trim(valfield123.value);  // value of field with whitespace trimmed off
  var telnr = /^\+?[0-9 ()-]+[0-9]$/  ;
  if (!telnr.test(tfld)) {
    msg (infofield, "errortext", "Invalid phone number");
    setfocus(valfield123);
    return false;
  }
  else
  {
    msg (infofield, "errortext", "");
    return true;
  }
  
//    var tfld = trim(valfield123.value);
//    if(tfld.length>strlength)
//    {
//        msg (infofield, "errortext", "Should be less than "+strlength+" characters. You entered "+tfld.length);
//        setfocus(valfield123);
//         return false;
//    }

/*
  var numdigits = 0;
  for (var j=0; j<tfld.length; j++)
    if (tfld.charAt(j)>='0' && tfld.charAt(j)<='9') numdigits++;

  if (numdigits<6) {
    msg (infofield, "errortext", "ERROR: " + numdigits + " digits - too short");
    setfocus(valfield123);
    return false;
  }
  */

//  if (numdigits>14)
//    msg (infofield, "errortext", numdigits + " digits - check if correct");
//  else 
//  { 
//    if (numdigits<10)
//      msg (infofield, "errortext", "Only " + numdigits + " digits - check if correct");
//    else
//      msg (infofield, "errortext", "");
//  }
  return true;
}

// --------------------------------------------
//             validateAge
// Validate person's age
// Returns true if OK 
// --------------------------------------------

function validateAge(valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required
{
var valfield123 = document.getElementById(valfield);
var stat = commonCheck (valfield, infofield, required);
  if (stat != proceed) return stat;
  
  var tfld = trim(valfield123.value);
  var ageRE = /^[0-9]{1,3}$/
  if (!ageRE.test(tfld)) {
    msg (infofield, "errortext", "Invalid age");
    setfocus(valfield123);
    return false;
  }  
  else
  {
       if (tfld>=200) 
      {
        msg (infofield, "errortext", "Invalid age");
        setfocus(valfield123);
        return false;
      }
      else
      {
        msg (infofield, "errortext", "");
        return true;
      }
   }
//  if (tfld>110) msg (infofield, "errortext", "Older than 110: check correct");
//  else {
//    if (tfld<7) msg (infofield, "errortext", "Bit young for this, aren't you?");
//    else        msg (infofield, "errortext", "");
//  }
//  return true;
}

///new function made by omprakash on 25 jan 2007
function validateCharacter(valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required
{

var valfield123 = document.getElementById(valfield);
var stat = commonCheck (valfield, infofield, required);
if (stat != proceed) return stat;

 var tfld = trim(valfield123.value);
//---  /^\+?[0-9 ()-]+[0-9]$/
  var ageRE = /^[a-zA-Z]{1,30}$/ //{1,10}$/

  if (!ageRE.test(tfld))
   {
    msg (infofield, "errortext", "Only Characters and Numbers allowed");
    setfocus(valfield123);
    return false;
  }
  else
  {
  msg (infofield, "errortext", "");
  return true;
  }
}

function validateNumbers(valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required
{

var valfield123 = document.getElementById(valfield);
var stat = commonCheck (valfield, infofield, required);
if (stat != proceed) return stat;

 var tfld = trim(valfield123.value);
//^[a-zA-Z]{1,40}$
  //var ageRE =/[0-9]{1,10}$/
  var ageRE =/^[0-9]{1,10}$/
  if (!ageRE.test(tfld))
   {
    msg (infofield, "errortext", "Invalid Number");
    setfocus(valfield123);
    return false;
  }
  else
  {
  msg (infofield, "errortext", "");
  return true;
  }
}

function validateMinLength(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 valfield123 = document.getElementById(valfield);
        var strlength=valfield123.id
        //this script is used to break the comming id taking last values after max
        strlength=strlength.substring(3);

        var stat = commonCheck (valfield, infofield, required);
        if (stat != proceed) return stat;

        var tfld = trim(valfield123.value);
        if(tfld.length<strlength)
        {
            msg (infofield, "errortext", "Minimum length should be "+strlength);
            setfocus(valfield123);
             return false;
        }
        else
            {
            msg (infofield, "errortext", "");
             return true;
            }
           
           
 }
 function ValidateMaxValue(valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required,
                         ExtraInfo)   // true if required
    {
   
     var stat = commonCheck (valfield, infofield, required);
     if (stat != proceed) return stat;
  
        var txt = document.getElementById(valfield);
        //var strlength=intLength;
        //var strlength=valfield123.id
        //this script is used to break the comming id taking last values after max
        //strlength=strlength.substring(3);

        var tfld = trim(txt.value);
        if(parseInt(tfld)>parseInt(ExtraInfo))
        {
            msg (infofield, "errortext", "Maximum value should be "+ExtraInfo);
            setfocus(txt);
             return false;
        }
        else
            {
            msg (infofield, "errortext", "");
             return true;
            }
}
function validateMaxLength(intLength,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 valfield123 = document.getElementById(valfield);
        var strlength=intLength;
        //var strlength=valfield123.id
        //this script is used to break the comming id taking last values after max
        //strlength=strlength.substring(3);

        var stat = commonCheck (valfield, infofield, required);
        if (stat != proceed) return stat;

        var tfld = trim(valfield123.value);
        if(tfld.length>strlength)
        {
            msg (infofield, "errortext", "Maximum length should be "+strlength);
            setfocus(valfield123);
             return false;
        }
        else
            {
            msg (infofield, "errortext", "");
             return true;
            }
}

function  validateDropDownListBox(valfield,infofield,ExtraInfo)
 {
        //alert('valfield:'+valfield);
        //alert('infofield:'+infofield);
        //alert('ExtraInfo:'+ExtraInfo);
        var valfield123 = document.getElementById(valfield);
        if (valfield123.selectedIndex == 0 || valfield123.selectedIndex == -1 )
            {
            var strMsg = ""
            
            if(ExtraInfo != "") 
            {
                //strMsg = ExtraInfo;
                strMsg = "* Required!"
            }
            else
            {
                strMsg = "* Required!";
            }    
            msg (infofield, "errortext", strMsg);
            setfocus(valfield123);
            return false;
            }
        else
            {
                msg (infofield, "errortext", "");
                return true;
            }
 }

function OnSubmitCheckBox(valfield,infofield,ExtraInfo)
    {
          var blnchecked = true;
          var totalcheck=0;
          var totalboxchecked=0;
            if (valfield.type =='checkbox')
                {

                }
        else
    {
        for (var i = 0; i < valfield.elements.length; i++)
   {
         
		var obj = valfield.elements[i];
		
		    if (obj.type == 'checkbox')
			{
			    totalcheck=totalcheck+1;
		         if (obj.checked == false)
					{
						blnchecked = false;
					   totalboxchecked= totalboxchecked+1;
					}
							
			}
						
    }
		 //alert(totalcheck+"=="+totalboxchecked+"<>"+totalradio+"=="+totalradiochecked)
			if (totalcheck == totalboxchecked)
				{
				    if(ExtraInfo != "")
				    {
				        //msg (infofield, "errortext",ExtraInfo);
				        msg (infofield, "errortext","* Required!");
				        return false;
				    }
				    else
				    {
				        msg (infofield, "errortext","* Required!");
				        return false;
				    }
					
				
				}
				else
				{
				msg (infofield, "errortext","");
				return true;
				}
   }
 }


function OnSubmitRadio(valfield,infofield,ExtraInfo)
{
          var blnchecked = true;
          var totalradio=0;
          var totalradiochecked=0;
        if (valfield.type =='radio')
        {

        }
     else
     {
        for (var i = 0; i < valfield.elements.length; i++)
         {
         	var obj = valfield.elements[i];
      	   
      	    if (obj.type == 'radio')
			    {
			        totalradio=totalradio+1;
				    if (obj.checked == false)
					    {
						    blnchecked = false;
						    totalradiochecked=totalradiochecked+1
					    }
    						
			    }
			
		  }
		if (totalradio == totalradiochecked)
				{
				    if(ExtraInfo == "")
				    {
					    msg (infofield, "errortext","* Required!");
				        return false;
				    }
				    else
				    {
				        //msg (infofield, "errortext",ExtraInfo);
				        msg (infofield, "errortext","* Required!");
				        return false;
				    }    
				
				}
				else
				{
				msg (infofield, "errortext","");
				return true;
				}
   }
}
////new function which check the special character in the value entered by user
function ValidateSpecialCharacters(valfield,infofield,required)
{
 var stat = commonCheck(valfield, infofield, required);
 if (stat != proceed) return stat; 
    
    var strNotAllowed = "!^<>";
    //var strNotAllowed = ">";
	var checkStr = document.getElementById(valfield).value
	var allValid = true;
	var UpdatedString=document.getElementById(valfield).value;
	if (checkStr!=null || checkStr!='undefined')
	{
	    for (i = 0;  i < strNotAllowed.length-1;  i++)
		    {
		        //alert(checkStr.indexOf('>'));
		        if (checkStr.indexOf('>')>=0)
		        {
		            checkStr=checkStr.replace(/>/g, '');
		        }    		    
		        if (checkStr.indexOf('^')>=0)
		        {
		            checkStr=checkStr.replace(/\^/g, '');
		        }    		    
			    if (checkStr.indexOf(strNotAllowed.charAt(i)) >= 0)
				    {					
					    checkStr=checkStr.replace(strNotAllowed.charAt(i), '');
					    checkStr=checkStr.replace(new RegExp(strNotAllowed.charAt(i),"g"), '');
				    }
		    }
		    document.getElementById(valfield).value=checkStr;
    }
  /*
  //Commented by Ramesh on 05-10-2007
  // earlier it was to validate, now ir will be replace spaeacial characters
var valfield123= document.getElementById(valfield)
//var iChars = "!@#$%^*+=[]\\\'{}\"<>";
// Chneged bY ramesh on 05-10-2007
//var iChars = "!@^*+=[]\\\'{}\"<>";
var iChars = "!^'<>";
var checkvalue=0
  for (var i = 0; i < valfield123.value.length; i++)
   {
  	if (iChars.indexOf(valfield123.value.charAt(i)) != -1)
  	 {
  	 //increase the value by 1
  	    checkvalue=checkvalue+1;
  	}
  }
        if(checkvalue>0)
            {
                //msg (infofield, "errortext","Invalid characters not allowed."); 
                msg (infofield, "errortext","Special characters ''!,@,^,*,+,=,[,],\\,\",< and >'' are not allowed."); 
  	           setfocus(valfield123);
  	            return false;
            }
        else
            {
               msg (infofield, "errortext",""); 
  	            return true; 
            }
            */
}

//ends here
//new function added by Rajesh on 24SEP for checking the existence of special characters on email address fields
//The characters we are considering are !#$%^&*()
function ValidateSplCharsForEmail(valfield,infofield,required)
{
     var stat = commonCheck(valfield, infofield, required);
     if (stat != proceed) return stat;
      
        var valfield123= document.getElementById(valfield)
        //added by Rajesh on 01 OCT 
        var tempValue;
        tempValue = valfield123.value;
        tempValue = tempValue.replace("<", "");
        tempValue = tempValue.replace(">", "");
        //alert(tempValue);
        document.getElementById(valfield).value = tempValue;
        //added by Rajesh on 01 OCT  END
        //var iChars = "<>!#$%^&*()";
        var iChars = "<>!#$%^&*()\"\'|/?,\\=:;+{}[]";
        var checkvalue=0
          for (var i = 0; i < valfield123.value.length; i++)
           {
  	        if (iChars.indexOf(valfield123.value.charAt(i)) != -1)
  	         {
  	         //increase the value by 1
  	            checkvalue=checkvalue+1;
  	        }
          }
            if(checkvalue>0)
                {
                    msg (infofield, "errortext","Special characters '<>!#$%^&*()\\/\"\'?|,=:;+{}[]' are not allowed."); 
                   setfocus(valfield123);
                    return false;
                }
            else
                {
                    // By Ramesh Ubbu on 17-04-2008, Check space in between email id
                    var vEmail= trim(valfield123.value);
                    var vEmailNew=vEmail.replace(/\s+/g,'');
                    if (vEmail!=vEmailNew)
                    {
                        msg (infofield, "errortext","Blank space is not allowed."); 
                        setfocus(valfield);
                        return false;
                    }
                    else
                    {
                        msg (infofield, "errortext",""); 
                        return true; 
                    }
                }
}

//new function to check whether angle brackets are entered by the user
function ValidateAngleBrackets(valfield,infofield,required)
{
 var stat = commonCheck(valfield, infofield, required);
 if (stat != proceed) return stat;
  
var valfield123= document.getElementById(valfield)

var iChars = "<>";
var checkvalue=0
  for (var i = 0; i < valfield123.value.length; i++)
   {
  	if (iChars.indexOf(valfield123.value.charAt(i)) != -1)
  	 {
  	 //increase the value by 1
  	    checkvalue=checkvalue+1;
  	}
  }
        if(checkvalue>0)
            {
                msg (infofield, "errortext","Special characters '<, >' are not allowed."); 
  	           setfocus(valfield123);
  	            return false;
            }
        else
            {
               msg (infofield, "errortext",""); 
  	            return true; 
            }
}
//end here

/////new function which holds highlight event for row.
    function OnFocusRowAll(valfield,infofield,msgContent)
        {
            document.getElementById(valfield).className = 'rowbgcolor';
            msg (infofield, "normaltextgrey",msgContent);
            return true;
        }
        /////new function ends here
//////new function which holds date checking for inputbox.
//////Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var dtYear = new Date();
var maxYear=dtYear.getFullYear();

function isInteger(s)
{
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year)
{
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr,infofield)
{
   
  
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strDay=dtStr.substring(0,pos1)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)

	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)

	if (pos1==-1 || pos2==-1)
	{
		msg (infofield, "errortext","The date format should be : dd/mm/yyyy");
		//alert("The date format should be : dd/mm/yyyy");
		setfocus(dtStr);
		return false;
	}
	if (strMonth.length<1 || parseInt(month)<1 || parseInt(month)>12)
	{
		msg (infofield, "errortext","Please enter a valid month");
		//alert("Please enter a valid month");
		setfocus(dtStr);
		return false;
	}
	
		if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)
	{
		msg (infofield, "errortext","Please enter a valid date");
		//alert("Please enter a valid date");
		setfocus(dtStr);
		return false;
	}
	if (strDay.length<1 || parseInt(day)<1 || parseInt(day)>31 || (parseInt(month)==2 && parseInt(day)>parseInt(daysInFebruary(year))) || parseInt(day) > parseInt(daysInMonth[month])){
		msg (infofield, "errortext","Please enter a valid day");
		//alert("Please enter a valid day");
		setfocus(dtStr);
		return false;
	}
	if (strYear.length != 4 || year==0 || parseInt(year)<parseInt(minYear) || parseInt(year)>parseInt(maxYear)){
		//msg (infofield, "errortext","Please enter a valid 4 digit year between "+minYear+" and "+maxYear);
		msg (infofield, "errortext","Please enter a valid year");
		//alert("Please enter a valid year");
		setfocus(dtStr);
		return false;
	}

	msg (infofield, "errortext","");
    return true;
}

function ValidateDate(valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required
{
	var valfield123 = document.getElementById(valfield);
	if (isDate(valfield123.value,infofield)==false)
	{
		setfocus(valfield123);
        return false;		
	}
    return true;
 }
//create by omprakash on 31 jan
function ValidateDateInTwoBox(valfield1,valfield2,infofield,required)    
   {
    var date1 = document.getElementById(valfield1);
    var date2 = document.getElementById(valfield2);
    var dtStr1=date1.value;
    var dtStr2=date2.value;
    
   	var dtCh="/"
   	var pos1=dtStr1.indexOf(dtCh)
	var pos2=dtStr1.indexOf(dtCh,pos1+1)
	var strMonth1=dtStr1.substring(0,pos1)
	var strDay1=dtStr1.substring(pos1+1,pos2)
	var strYear1=dtStr1.substring(pos2+1)
	
	var dtCh="/"
   	var pos11=dtStr2.indexOf(dtCh)
	var pos22=dtStr2.indexOf(dtCh,pos11+1)
	var strMonth2=dtStr2.substring(0,pos11)
	var strDay2=dtStr2.substring(pos11+1,pos22)
	var strYear2=dtStr2.substring(pos22+1)

  if(isDate(date2.value,infofield)==true)//we will first check the value in second field.
	{
	 if(isDate(date1.value,infofield)==true)//we will now go for first check.
	    {
	          if((strYear2+strMonth2+strDay2)-(strYear1+strMonth1+strDay1)>0)
                  {
                      msg (infofield, "errortext","");
	                  return true;
                  }
                  else
                  {
                   msg (infofield, "errortext","Second date should be greater that first date.");
	              return false;
                  }
          }
       else
	      {
	           msg (infofield, "errortext","First date Invalid!");
	           return false;
	       }
    }
	else//if second value is not proper date then it goes to inner part
	{
	msg (infofield, "errortext","Invalid date!");
	return false;
	}
}



function CheckPostCode(strDropDown, valField, infoField)
{
    var ddl = document.getElementById(strDropDown);
    var txt = document.getElementById(valField);
     if (ddl.value != 182)//United Kingdom
    {
            
            var tfld = trim(txt.value);
            var strlength = 10;
             if(tfld.length == 0)
             {
                return false;
             }
            if(tfld.length>strlength)
            {
                //alert("Post Code minimum length should be "+strlength);
                alert("Maximum lenght of the PostCode should be "+strlength);
                //msg (infoField, "errortext", " Minimum length should be "+strlength);
                setfocus(txt);
                return false;
            }
            else
            {
                msg (infoField, "errortext", "");
                return true;
            }
            
    }       
    else
    {
        
        var tfld = trim(txt.value);
        var strlength = 4;
        if(tfld.length == 0)
        {
            return false;
        }
        if(tfld.length>strlength)
        {
            alert("Post Code maximum length should be "+strlength);
            //msg (infoField, "errortext", "Minimum length should be "+strlength);
            setfocus(txt);
            return false;
        }
        else
        {
        msg (infoField, "errortext", "");
         return true;
        }
    }
}

//Should be triggered in Message box
function CheckPasswords(strPassword,strConfirmPassword,infoField)
{

//     var stat = commonCheck (strConfirmPassword, infoField, 'true');
//     if (stat != proceed) return stat;
    var pass = document.getElementById(strPassword);
     var cpass = document.getElementById(strConfirmPassword);
   
    if(pass.value != cpass.value)
    {
         msg (infoField, "errortext", "The passwords you have typed do not match. Please retype the new password in both boxes");
         //alert("Passwords are not matching")
         setfocus(pass);
         return false;
    }
    else
    {
         msg (infoField, "errortext", "");
         return true;
    }
}

//Should be triggered in Message box
function CheckEmails(strEmail,strConfirmEmail,infoField)
{

//     var stat = commonCheck (strConfirmEmail, infoField, 'true');
//     if (stat != proceed) return stat;
    var newEmail = document.getElementById(strEmail);
     var conEmail = document.getElementById(strConfirmEmail);
   //alert(trim(newEmail.value.toLowerCase()));
   //alert(trim(conEmail.value.toLowerCase()));
    if( trim(newEmail.value.toLowerCase()) != trim(conEmail.value.toLowerCase()))
    {
         msg (infoField, "errortext", "Emails are not matching");
         //alert("Email addresses are not matching")
         setfocus(newEmail);
         return false;
    }
    else
    {
         msg (infoField, "errortext", "");
         return true;
    }
}
/////new function ends here
// JScript File ends


//Saroj
//Should be triggered in Message box
function ValidateTwoDate(date1,date2,infofield,ExtraInfo)    
{
    
    var dtCh="/";
    
    //var dtStr1 = date1;
   	//var dtStr2 = date2;
   	
   	  	
	/*
	//Rajesh commented this as there was error in other_details.aspx and all date validation pages
	if(isDate(date1.value,infofield)==false)//we will first check the value in second field.
	{
	    msg (infofield, "errortext","Invalid date!");
	    return false;
	}*/
    
   
    
    if(date2=='current')
    {
        var mydate=new Date();
        var year2=mydate.getFullYear();
        var month2=mydate.getMonth()+ 1;
        var daym2=mydate.getDate();
            
     }
        
    else
    {
        
        /*
        if(isDate(date2.value,infofield)==false)//we will first check the value in second field.
	    {
	        msg (infofield, "errortext","Invalid date!");
	        return false;
	    }
	    else
	    {
	    */
            var dtStr2=date2;
            var pos11=dtStr2.indexOf(dtCh);
	        var pos22=dtStr2.indexOf(dtCh,pos11+1);
	        var daym2=dtStr2.substring(0,pos11);
	        var month2=dtStr2.substring(pos11+1,pos22);
	        var year2=dtStr2.substring(pos22+1);
        
    }  
    
    var dtSecondDate = new Date(year2,month2-1,daym2);
    
    var dtStr1 = date1;
   	var pos1=dtStr1.indexOf(dtCh);
   	var pos2=dtStr1.indexOf(dtCh,pos1+1);
	var daym1=dtStr1.substring(0,pos1);
	var month1=dtStr1.substring(pos1+1,pos2);
	var year1=dtStr1.substring(pos2+1);
	
	var dtFirstDate = new Date(year1,month1-1,daym1);
		
	
	if(dtFirstDate.getTime() > dtSecondDate.getTime())
	{	    
	    if (date2=='current')
	    {
	        msg (infofield, "errortext","Selected Date is greater than current date");
	        //alert(ExtraInfo);
	        //alert("Selected Date is greater than current date");
	        return false;
	    }    
	    else
	    {
	        //alert(month1);
	        //msg (infofield, "errortext", month1 + '/' + year1 + " is greater than " + month2+'/'year2 + ". This is not valid");
	        //alert(date1.getMonth());
	        msg (infofield, "errortext", date1 + " is greater than " + date2 + ". This is not valid");
	        //alert(date1 + " is greater than " + date2 + ". This is not valid");
	        //alert(date1 + " is greater than " + date2 + ". This is not valid");
	        return false;
	    }
	}
	else
	{
         msg (infofield, "errortext","");
          return true;
	}
	
 	
}
// Created by Ramesh on 04-02-2008
// to Clear Messages
function ClearLabelMessage(ctrlID,LabelId)
{   
    if (document.getElementById(ctrlID).value !='')
    {
        var e = document.getElementById(LabelId);
        if (e.firstChild!=null && e.firstChild!=undefined)
        {
            e.firstChild.nodeValue = '';              
        }
        else
        {
            e.value= '';
        }
    }                
}