function Validator(frmname)
{
  this.formobj=document.forms[frmname];
	if(!this.formobj)
	{
	  alert("BUG: couldnot get Form object "+frmname);
		return;
	}
	if(this.formobj.onsubmit)
	{
	 this.formobj.old_onsubmit = this.formobj.onsubmit;
	 this.formobj.onsubmit=null;
	}
	else
	{
	 this.formobj.old_onsubmit = null;
	}
	this.formobj.onsubmit=form_submit_handler;
	this.addValidation = add_validation;
	this.setAddnlValidationFunction=set_addnl_vfunction;
	this.clearAllValidations = clear_all_validations;
}

function set_addnl_vfunction(functionname)
{
  this.formobj.addnlvalidation = functionname;
}

function clear_all_validations()
{
	for(var itr=0;itr < this.formobj.elements.length;itr++)
	{
		this.formobj.elements[itr].validationset = null;
	}
}

function form_submit_handler()
{
	for(var itr=0;itr < this.elements.length;itr++)
	{
		if(this.elements[itr].validationset &&
	   !this.elements[itr].validationset.validate())
		{
		  return false;
		}
	}
	if(this.addnlvalidation)
	{
	  str =" var ret = "+this.addnlvalidation+"()";
	  eval(str);
    if(!ret) return ret;
	}
	return true;
}

function add_validation(itemname,descriptor,errstr)
{
  if(!this.formobj)
	{
	  alert("BUG: the form object is not set properly");
		return;
	}//if
	var itemobj = this.formobj[itemname];
  if(!itemobj)
	{
	  alert("BUG: Couldnot get the input object named: "+itemname);
		return;
	}
	if(!itemobj.validationset)
	{
	  itemobj.validationset = new ValidationSet(itemobj);
	}
  itemobj.validationset.add(descriptor,errstr);
}

function ValidationDesc(inputitem,desc,error)
{
	this.desc=desc;
	this.error=error;
	this.itemobj = inputitem;
	this.validate=vdesc_validate;
}

function vdesc_validate()
{
	if(!V2validateData(this.desc,this.itemobj,this.error))
	{
		this.itemobj.focus();
		return false;
	}
	return true;
}

function ValidationSet(inputitem)
{
    this.vSet=new Array();
	this.add= add_validationdesc;
	this.validate= vset_validate;
	this.itemobj = inputitem;
}

function add_validationdesc(desc,error)
{
	this.vSet[this.vSet.length]= 
	new ValidationDesc(this.itemobj,desc,error);
}

function vset_validate()
{
   for(var itr=0;itr<this.vSet.length;itr++)
	 {
	   if(!this.vSet[itr].validate())
		 {
		   return false;
		 }
	 }
	 return true;
}
function hasWhiteSpace(s) 
{
	reWhiteSpace = new RegExp(/^\s+$/);

	// Check for white space
	if (reWhiteSpace.test(s)) 
	{
		return false;
	}
	return true;
}

function checkMail(s) 
{
	remail = new RegExp(/^([\w\-\.]+)@((\[([0-9]{1,3}\.){3}[0-9]{1,3}\])|(([\w\-]+\.)+)([a-zA-Z]{2,4}))$/);

	// Check for mail
	if(s!= "")
	{
	if (!remail.test(s)) 
	{
		return false;
	}
	}
	return true;
}

function checkCurrency(s) 
{
	recurrency = new RegExp(/^\d+(\.\d\d)?$/);
	
	// Check for valid Currency
	//format like 114.20, 100
	if (!recurrency.test(s)) 
	{
		return false;
	}
	return true;
}

function checkCurrency1(s) 
{
	recurrency = new RegExp(/\$\d{1,3}(,\d{3})*\.\d{2}/);
	
	// Check for valid Currency
	//format like $1,14.20
	if (!recurrency.test(s)) 
	{
		return false;
	}
	return true;
}

function checkCurrency2(s) 
{	
	recurrency = new RegExp(/\d{1,3}(,\d{3})*\.\d{2}/);
	
	// Check for valid Currency
	//format like 1,14.20
	if (!recurrency.test(s)) 
	{
		return false;
	}
	return true;
}
	
function checkCurrency3(s) 
{
	recurrency = new RegExp(/\d{1,3}(,\d{3})*(\.|)\d{0,2}/);
	
	// Check for valid Currency
	//format like 1,14.20, 100
	if (!recurrency.test(s)) 
	{
		return false;
	}
	return true;
}

function jm_currencymask(t)//to perform currency mask. Call this method in field's onkeyup="jm_currencymask(this)
{
	var patt = /(\d*)\.{1}(\d{0,2})/;
	var donepatt = /^(\d*)\.{1}(\d{2})$/;
	var str = t.value;
	var result;
	if (!str.match(donepatt))
	{
		result = str.match(patt);
		if (result!= null)
		{
			t.value = t.value.replace(/[^\d]/gi,'');
			str = result[1] + '.' + result[2] ;
			t.value = str;
		}
		else
		{
			if (t.value.match(/[^\d]/gi))
			t.value = t.value.replace(/[^\d]/gi,'');
		}
	}
}

function checkUSPhone(s) 
{
	reusphone = new RegExp(/^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$/);

	// Check for US Phone
	if (!reusphone.test(s)) 
	{
		return false;
	}
	return true;
}
var USStateCodeDelimiter = "|";
var USStateCodes = "AL|AK|AS|AZ|AR|CA|CO|CT|DE|DC|FM|FL|GA|GU|HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|MP|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY|AE|AA|AE|AE|AP";

function isUSStateCode(s) // to confirm usstatecode
{   
	return ((USStateCodes.indexOf(s) != -1) &&
	(s.indexOf(USStateCodeDelimiter) == -1))
}
function isUSZipCode(s)
{
	if(s != "")
	{
		reuszip = new RegExp(/^(\d{5}-\d{4}|\d{5}|\d{9})$/);
		if (!reuszip.test(s)) 
		{
			return false;
		}
	}
	return true;
}
function IsValidExpDate(s) 
{
	// Check credit expiry date is in valid format
	var CCexpire=s;
	var ArrayCCexpr=CCexpire.split("/");
	if ( (ArrayCCexpr.length!=2) || (ArrayCCexpr[0]=="") || (isNaN(ArrayCCexpr[0])) || (ArrayCCexpr[1]=="") || (isNaN(ArrayCCexpr[1])) || (ArrayCCexpr[0]<1) || (ArrayCCexpr[0]>12) ) 
	{
		alert("Please enter a valid credit card expiry date"); 
		return false;
	}
	var Char;
	var d = new Date();
	var yearstr = d.getFullYear() + '';
	var monthstr = d.getMonth() + '';
	
	var currentYear = yearstr.substring(2); 
	var currentmonth = parseInt(monthstr,10) + 1 ;
	if(parseInt(ArrayCCexpr[1],10)<= parseInt(currentYear,10)) 
	{
		if( (parseInt(ArrayCCexpr[0],10)< currentmonth) && (parseInt(ArrayCCexpr[1],10) == parseInt(currentYear,10)))
		{
			alert("Invalid expiry date. Please enter a valid credit card expiry date"); 
			return false;
		}
		if(parseInt(ArrayCCexpr[1],10)< parseInt(currentYear,10)) 
		{
			alert("Invalid expiry date. Please enter a valid credit card expiry date"); 
			return false;
		}
	}
	
	return true;
}
//call this method in textarea onKeyDown="textareacouter(this,document.myForm.remLen2,500)"
//onKeyUp="textareacouter(this,document.myForm.remLen2,500)
function textareacouter(field,countfield,maxlimit) //counter for textarea field
{
	if(field.value.length > maxlimit)
	{
		alert("Please Limit the " + field.name + " to " + maxlimit + " characters"); 
		return false;
	}
	else
	{
		countfield.value = maxlimit - field.value.length;
	}
}

function trim(s)
{
	return s.replace(/^\s+|\s+$/g, "");
}

function V2validateData(strValidateStr,objValue,strError) 
{ 
    var epos = strValidateStr.search("="); 
    var  command  = ""; 
    var  cmdvalue = ""; 
    if(epos >= 0) 
    { 
     command  = strValidateStr.substring(0,epos); 
     cmdvalue = strValidateStr.substr(epos+1); 
    } 
    else 
    { 
     command = strValidateStr; 
    } 
      
    switch(command) 
    { 
        case "req": 
        case "required": 
         { 
           if(eval(objValue.value.length) == 0) 
           { 
              if(!strError || strError.length ==0) 
              { 
                strError = "Please enter your " +  objValue.name; 
              }//if 
              alert(strError); 
              return false; 
           }//if 
           break;             
         
         }//case required
         
        case "ws": 
        case "whitespace": 
         { 
           if(!hasWhiteSpace(objValue.value)) 
               { 
                 if(!strError || strError.length ==0) 
                 { 
                    strError = "Please enter your " +  objValue.name;
                 }//if                                               
                 alert(strError);
                 objValue.value = ""; 
                 return false; 
               }//if 
           break;             
         }//case required 
         
        case "maxlength": 
        case "maxlen": 
          { 
           if(trim(objValue.value) != "")
           {
             if(eval(objValue.value.length) >  eval(cmdvalue)) 
             { 
               if(!strError || strError.length ==0) 
               { 
		strError = "Please enter a valid " + objValue.name +  " consisting of " + cmdvalue +" char/digits"; 
               }//if 
                alert(strError); 
               return false; 
             }//if 
            }
             break; 
          }//case maxlen 
          
        case "minlength": 
        case "minlen": 
           { 
             if(eval(objValue.value.length) <  eval(cmdvalue)) 
             { 
               if(!strError || strError.length ==0) 
               { 
		strError = "Please enter a valid " + objValue.name +  "consisting atleast " + cmdvalue +" char/digits"; 
               }//if               
               alert(strError); 
               return false;                 
             }//if 
             break; 
            }//case minlen 
            
        case "num": 
        case "numeric": 
           { 
              var charpos = objValue.value.search("[^0-9]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                if(!strError || strError.length ==0) 
                { 
                  strError = "Please enter a valid " + objValue.name + ". Only digits allowed "; 
                }//if               
                return false; 
              }//if 
              break;               
           }//numeric 
          
        case "alphabetic": 
        case "alpha": 
           { 
              var charpos = objValue.value.search("[^A-Za-z]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                if(!strError || strError.length ==0) 
                { 
		strError = "Please enter a valid " + objValue.name + ". Only alphabetic characters allowed ";
                }//if                             
                return false; 
              }//if 
              break; 
           }//alpha 
           
        case "alnum": 
        case "alphanumeric": 
           { 
              var charpos = objValue.value.search("[^A-Za-z0-9]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
               if(!strError || strError.length ==0) 
                { 
                  strError = "Please enter a valid " + objValue.name + ". Only alpha-numeric characters allowed "; 
                }//if 
                return false; 
              }//if 
              break; 
           }//case alphanumeric 
        
		case "alnumhyphen":
			{
              var charpos = objValue.value.search("[^A-Za-z0-9\-_]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                if(!strError || strError.length ==0) 
                { 
                  strError = objValue.name+": characters allowed are A-Z,a-z,0-9,- and _"; 
                }//if                             
                return false; 
              }//if 			
			break;
			}
			
		case "usphone": 
			{ 
				if(trim(objValue.value) != "")
				{
				if(!checkUSPhone(objValue.value)) 
				{ 
					if(!strError || strError.length ==0) 
					{ 
						strError = "Please enter a valid US Phone Number"; 
					}//if                                               
					alert(strError); 
					return false; 
				}//if 
				}
			break; 
			}//case US phone 
			
		case "uszip": 
			{
				if(objValue.value == "00000") 
				{ 
					alert("Please enter a valid 5-digit zip code"); 
					return false; 
				}//if 
				if(!isUSZipCode(objValue.value)) 
				{ 
					if(!strError || strError.length ==0) 
					{ 
						strError = "Please enter a valid US Zip Code"; 
					}//if                                               
					alert(strError); 
					return false; 
				}//if 
			break; 
			}//case US Zip
			
		case "email": 
			{ 
				if(!checkMail(objValue.value)) 
				{ 
					if(!strError || strError.length ==0) 
					{ 
						strError = "Please enter a valid email address"; 
					}//if                                               
					alert(strError); 
					return false; 
				}//if 
			break; 
			}//case email
		
		case "currency": 
			{ 
				if(objValue.value == 0 || objValue.value ==0.00 || objValue.value ==0.0 || objValue.value ==0.) 
				{ 
					alert("Please enter a valid amount"); 
					return false; 
				}//if 
				if(!checkCurrency(objValue.value)) 
				{ 
					if(!strError || strError.length ==0) 
					{ 
						strError = "Please enter a valid amount"; 
					}//if                                               
					alert(strError); 
					return false; 
				}//if 
			break; 
			}//case currency
			
		case "currency1": 
			{ 
				if(objValue.value == 0 || objValue.value ==0.00 || objValue.value ==0.0 || objValue.value ==0.) 
				{ 
					alert("Please enter a valid amount"); 
					return false; 
				}//if 
				if(!checkCurrency1(objValue.value)) 
				{ 
					if(!strError || strError.length ==0) 
					{ 
						strError = "Please enter a valid amount"; 
					}//if                                               
					alert(strError); 
					return false; 
				}//if 
			break; 
			}//case currency1 
			
		case "currency2": 
			{ 
				if(objValue.value == 0 || objValue.value ==0.00 || objValue.value ==0.0 || objValue.value ==0.) 
				{ 
					alert("Please enter a valid amount"); 
					return false; 
				}//if 
				if(!checkCurrency2(objValue.value)) 
				{ 
					if(!strError || strError.length ==0) 
					{ 
						strError = "Please enter a valid amount"; 
					}//if                                               
					alert(strError); 
					return false; 
				}//if 
			break; 
			}//case currency2 
			
		case "currency3": 
			{ 
				if(objValue.value == 0 || objValue.value ==0.00 || objValue.value ==0.0 || objValue.value ==0.) 
				{ 
					alert("Please enter a valid amount"); 
					return false; 
				}//if 
				if(!checkCurrency3(objValue.value)) 
				{ 
					if(!strError || strError.length ==0) 
					{ 
						strError = "Please enter a valid amount"; 
					}//if                                               
					alert(strError); 
					return false; 
				}//if 
			break; 
			}//case currency3  
          
        case "usstate": 
        { 
            if(!isUSStateCode(objValue.value.toUpperCase())) 
            { 
                if(!strError || strError.length ==0) 
                { 
					strError = "Please enter a valid 2 character US State code"; 
                }//if                                               
                alert(strError); 
                return false; 
            }//if 
        break; 
        }//case usstate 
        
        case "ccno":
        case "creditcardno": 
        { 
            if((objValue.value.length < 16) && (objValue.value.length != 13)) 
            { 
                if(!strError || strError.length ==0) 
                { 
					strError = "Please enter a valid credit card number consisting of 13 or 16 digits"; 
                }//if                                               
                alert(strError); 
                return false; 
            }//if 
        break; 
        }//case creditcardno
        
        case "ccexpdate":
        case "creditcardexpdate": 
        { 
            if(!IsValidExpDate(objValue.value))
            { 
                return false; 
            }//if 
        break; 
        }//case creditcardexpdate
                  
        case "lt": 
        case "lessthan": 
         { 
            if(isNaN(objValue.value)) 
            { 
              alert(objValue.name+" should be a number "); 
              return false; 
            }//if 
            if(eval(objValue.value) >=  eval(cmdvalue)) 
            { 
              if(!strError || strError.length ==0) 
              { 
                strError = objValue.name + " value should be less than "+ cmdvalue; 
              }//if               
              alert(strError); 
              return false;                 
             }//if             
            break; 
         }//case lessthan 
         
        case "gt": 
        case "greaterthan": 
         { 
            if(isNaN(objValue.value)) 
            { 
              alert(objValue.name+" should be a number "); 
              return false; 
            }//if 
             if(eval(objValue.value) <=  eval(cmdvalue)) 
             { 
               if(!strError || strError.length ==0) 
               { 
                 strError = objValue.name + " value should be greater than "+ cmdvalue; 
               }//if               
               alert(strError); 
               return false;                 
             }//if             
            break; 
         }//case greaterthan 
         
        case "percentage": 
         { 
            if(!checkCurrency(objValue.value)) 
            { 
              alert(objValue.name+" should be a valid number "); 
              return false; 
            }//if 
            if(eval(objValue.value) >=  eval(cmdvalue)) 
            { 
              if(!strError || strError.length ==0) 
              { 
                strError = objValue.name + " value should be less than "+ cmdvalue; 
              }//if               
              alert(strError); 
              return false;                 
             }//if             
            break; 
         }//case percentage 
         
        case "regexp": 
         { 
		 	if(objValue.value.length > 0)
			{
	            if(!objValue.value.match(cmdvalue)) 
	            { 
	              if(!strError || strError.length ==0) 
	              { 
	                strError = "Please enter a valid " + objValue.name; 
	              }//if                                                               
	              alert(strError); 
	              return false;                   
	            }//if 
			}
           break; 
         }//case regexp 
         
        case "dontselect": 
         { 
            if(objValue.selectedIndex == null) 
            { 
              alert("Error"); 
              return false; 
            } 
            if(objValue.selectedIndex == eval(cmdvalue)) 
            { 
             if(!strError || strError.length ==0) 
              { 
		strError = "Please select valid " +objValue.name ; 
              }//if                                                               
              alert(strError); 
              return false;                                   
             } 
             break; 
         }//case dontselect for dropdown
         
       case "validdate": 
         { 
         if(trim(objValue.value) != "")
				{
            if(!dateCheckNew(objValue.value)) 
            { 
             if(!strError || strError.length ==0) 
              { 
		strError = "Please Enter Valid Date"; 
              }//if                                                               
              alert(strError); 
              return false;                                   
             } 
             }
             break; 
         }//case validdate 
              
    }//switch 
    return true; 
}
var tokPat=new RegExp("^month_strict|month|Month|MONTH|yyyy|YYYY|mins|MINS|mon_strict|ampm|AMPM|mon|Mon|MON|min|MIN|dd|DD|mm|MM|yy|YY|hh|HH|ss|SS|m|M|d|D|y|Y|h|H|s|S");
var lowerMonArr={jan:1, feb:2, mar:3, apr:4, may:5, jun:6, jul:7, aug:8, sep:9, oct:10, nov:11, dec:12}


var monPatArr=new Array();
monPatArr['mon_strict']=new RegExp(/jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec/);
monPatArr['Mon']=new RegExp(/Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec/);
monPatArr['MON']=new RegExp(/JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC/);
monPatArr['mon']=new RegExp("jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec",'i');


var monthPatArr=new Array();
monthPatArr['month']=new RegExp(/^january|february|march|april|may|june|july|august|september|october|november|december/i);
monthPatArr['Month']=new RegExp(/^January|February|March|April|May|June|July|August|September|October|November|December/);
monthPatArr['MONTH']=new RegExp(/^JANUARY|FEBRUARY|MARCH|APRIL|MAY|JUNE|JULY|AUGUST|SEPTEMBER|OCTOBER|NOVEMBER|DECEMBER/);
monthPatArr['month_strict']=new RegExp(/^january|february|march|april|may|june|july|august|september|october|november|december/);


var cutoffYear=50;


function FormatToken (token, type) {
this.token=token;
this.type=type;
}

function parseFormatString (formatStr) {
var tokArr=new Array;
var tokInd=0;
var strInd=0;
var foundTok=0;
    
while (strInd < formatStr.length) {
if (formatStr.charAt(strInd)=="%" &&
(matchArray=formatStr.substr(strInd+1).match(tokPat)) != null) {
strInd+=matchArray[0].length+1;
tokArr[tokInd++]=new FormatToken(matchArray[0],"symbolic");
} else {

if (tokInd>0 && tokArr[tokInd-1].type=="literal") {


tokArr[tokInd-1].token+=formatStr.charAt(strInd++);
}
else {
tokArr[tokInd++]=new FormatToken(formatStr.charAt(strInd++), "literal");
      }
   }
}
return tokArr;
}


 function dateCheck(dateStr,formatStr) {
 var myObj=buildDate(dateStr,formatStr);
 if (typeof myObj=="object") {
 // We got a Date object, so good.
 return true;
 } else {
 // We got an error string.
 //alert(myObj);
 return false;
 }
 }
function dateCheckNew(dateStr)
{

    var RegExPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
    var errorMessage = 'Please enter valid date as month, day, and four digit year.\nYou may use a slash, hyphen or period to separate the values.\nThe date must be a real date. 2-30-2000 would not be accepted.\nFormay mm/dd/yyyy.';
    if ((dateStr.match(RegExPattern)) && (dateStr !=''))
    {
        return true;
    }
    else
    {
        return false;
    } 

}

function buildDate(dateStr,formatStr) {
// parse the format string first.
var tokArr=parseFormatString(formatStr);
var strInd=0;
var tokInd=0;
var intMonth;
var intDay;
var intYear;
var intHour;
var intMin;
var intSec;
var ampm="";
var strOffset;

var curdate=new Date();
intMonth=curdate.getMonth()+1;
intDay=curdate.getDate();
intYear=curdate.getFullYear();


intHour=0;
intMin=0;
intSec=0;


while (strInd < dateStr.length && tokInd < tokArr.length) {

if (tokArr[tokInd].type=="literal") {
if (dateStr.indexOf(tokArr[tokInd].token,strInd)==strInd) {


strInd+=tokArr[tokInd++].token.length;
continue;
}
else {

// ACK! There was a mismatch; return error.

//return "\"" + dateStr + "\" does not conform to the expected format: " + formatStr;
return false;
   }
}


switch (tokArr[tokInd].token) {
case 'm':
case 'M':
case 'd':
case 'D':
case 'h':
case 'H':
case 'min':
case 'MIN':
case 's':
case 'S':


curChar=dateStr.charAt(strInd);
nextChar=dateStr.charAt(strInd+1);
matchArr=dateStr.substr(strInd).match(/^\d{1,2}/);
if (matchArr==null) {


switch (tokArr[tokInd].token.toLowerCase()) {
case 'd': var unit="day"; break;
case 'm': var unit="month"; break;
case 'h': var unit="hour"; break;
case 'min': var unit="minute"; break;
case 's': var unit="second"; break;
}
//return "Bad " + unit + " \"" + curChar + "\" or \"" + curChar +
//nextChar + "\".";
return false;
}
strOffset=matchArr[0].length;
switch (tokArr[tokInd].token.toLowerCase()) {
case 'd': intDay=parseInt(matchArr[0],10); break;
case 'm': intMonth=parseInt(matchArr[0],10); break;
case 'h': intHour=parseInt(matchArr[0],10); break;
case 'min': intMin=parseInt(matchArr[0],10); break;
case 's': intSec=parseInt(matchArr[0],10); break;
}
break;
case 'mm':
case 'MM':
case 'dd':
case 'DD':
case 'hh':
case 'HH':
case 'mins':
case 'MINS':
case 'ss':
case 'SS':


strOffset=2;
matchArr=dateStr.substr(strInd).match(/^\d{2}/);
if (matchArr==null) {


switch (tokArr[tokInd].token.toLowerCase()) {
case 'dd': var unit="day"; break;
case 'mm': var unit="month"; break;
case 'hh': var unit="hour"; break;
case 'mins': var unit="minute"; break;
case 'ss': var unit="second"; break;
}
//return "Bad " + unit + " \"" + dateStr.substr(strInd,2) + 
//"\".";
return false;
}
switch (tokArr[tokInd].token.toLowerCase()) {
case 'dd': intDay=parseInt(matchArr[0],10); break;
case 'mm': intMonth=parseInt(matchArr[0],10); break;
case 'hh': intHour=parseInt(matchArr[0],10); break;
case 'mins': intMin=parseInt(matchArr[0],10); break;
case 'ss': intSec=parseInt(matchArr[0],10); break;
}
break;
case 'y':
case 'Y':

if (dateStr.substr(strInd,4).search(/\d{4}/) != -1) {

intYear=parseInt(dateStr.substr(strInd,4),10);
strOffset=4;
}
else {
if (dateStr.substr(strInd,2).search(/\d{2}/) != -1) {

intYear=parseInt(dateStr.substr(strInd,2),10);
if (intYear>=cutoffYear) {
intYear+=1900;
}
else {
intYear+=2000;
}
strOffset=2;
}
else {


//return "Bad year \"" + dateStr.substr(strInd,2) + 
//"\". Must be two or four digits.";
return false;
   }
}
break;
case 'yy':
case 'YY':

if (dateStr.substr(strInd,2).search(/\d{2}/) != -1) {

intYear=parseInt(dateStr.substr(strInd,2),10);
if (intYear>=cutoffYear) {
intYear+=1900;
}
else {
intYear+=2000;
}
strOffset=2;
} else {
// Bad year; return error
//return "Bad year \"" + dateStr.substr(strInd,2) + 
//"\". Must be two digits.";
return false;
}
break;
case 'yyyy':
case 'YYYY':

if (dateStr.substr(strInd,4).search(/\d{4}/) != -1) {


intYear=parseInt(dateStr.substr(strInd,4),10);
strOffset=4;
}
else {


//return "Bad year \"" + dateStr.substr(strInd,4) + 
//"\". Must be four digits.";
return false;
}
break;
case 'mon':
case 'Mon':
case 'MON':
case 'mon_strict':


monPat=monPatArr[tokArr[tokInd].token];
if (dateStr.substr(strInd,3).search(monPat) != -1) {
intMonth=lowerMonArr[dateStr.substr(strInd,3).toLowerCase()];
}
else {

switch (tokArr[tokInd].token) {
case 'mon_strict': caseStat="lower-case"; break;
case 'Mon': caseStat="mixed-case"; break;
case 'MON': caseStat="upper-case"; break;
case 'mon': caseStat="between Jan and Dec"; break;
}
//return "Bad month \"" + dateStr.substr(strInd,3) + 
//"\". Must be " + caseStat + ".";
return false;
}
strOffset=3;
break;
case 'month':
case 'Month':
case 'MONTH':
case 'month_strict':


monPat=monthPatArr[tokArr[tokInd].token];
matchArray=dateStr.substr(strInd).match(monPat);
if (matchArray==null) {


//return "Can't find a month beginning at \"" +
//dateStr.substr(strInd) + "\".";
return false;
}


intMonth=lowerMonArr[matchArray[0].substr(0,3).toLowerCase()];
strOffset=matchArray[0].length;
break;
case 'ampm':
case 'AMPM':
matchArr=dateStr.substr(strInd).match(/^(am|pm|AM|PM|a\.m\.|p\.m\.|A\.M\.|P\.M\.)/);
if (matchArr==null) {


//return "Missing am/pm designation.";
return false;
}


if (matchArr[0].substr(0,1).toLowerCase() == "a") {


ampm = "am";
}
else {
ampm = "pm";
}
strOffset = matchArr[0].length;
break;
}
strInd += strOffset;
tokInd++;
}
if (tokInd != tokArr.length || strInd != dateStr.length) {

//return "\"" + dateStr + "\" is either missing desired information or has more information than the expected format: " + formatStr;
return false;
}


if (intMonth < 1 || intMonth > 12) {
return "Month must be between 1 and 12.";
}
if (intDay < 1 || intDay > 31) {
return "Day must be between 1 and 31.";
}


if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && intDay == 31) {
return "Month "+intMonth+" doesn't have 31 days!";
}


if (intMonth == 2) {


var isleap=(intYear%4==0 && (intYear%100!=0 || intYear%400==0));
if (intDay > 29 || (intDay == 29 && !isleap)) {
return "February " + intYear + " doesn't have " + intDay + 
" days!";
   }
}

if (ampm == "") {
if (intHour < 0 || intHour > 23) {
return "Hour must be between 0 and 23 for military time.";
   }
}
else {


if (intHour < 1|| intHour > 12) {
return "Hour must be between 1 and 12 for standard time.";
   }
}


if (ampm=="am" && intHour==12) {
intHour=0;
}
if (ampm=="pm" && intHour < 12) {
intHour += 12;
}
if (intMin < 0 || intMin > 59) {
return "Minute must be between 0 and 59.";
}
if (intSec < 0 || intSec > 59) {
return "Second must be between 0 and 59.";
}
return new Date(intYear,intMonth-1,intDay,intHour,intMin,intSec);
}
function dateCheck(dateStr,formatStr) {
var myObj = buildDate(dateStr,formatStr);
if (typeof myObj == "object") {

return true;
}
else {

//alert(myObj);
return false;
   }
}

function validateDropdownDate(day, month, year)
   //check correct number of day for given month/year
   {
   if (day.selectedIndex != 0 && month.selectedIndex != 0 
     && year.selectedIndex != 0)
      {
      switch(month.value) 
         {
         case "02" :
            //February
            if (year.value == Math.round(year.value / 4) * 4)
	       //leap year
	       {
	       if (day.value > 29)
                  {
                  return false;
                  }
               }
            else
               {
               //non-leap year
               if (day.value > 28)
                  {
                  return false;
                  }
               }
               break;	
         case "04" :
         case "06":
         case "09":
         case "11":
            if (day.value > 30)
               {
               return false;
               }	
               break;
         default:
            //date is valid
            return true;
            break;
         }
      }
   }	
