/**
 * <p><h3>Title:</h3>Supplier On Ramp</p>
 * <p><h4>Description:</h4>
 * SPFormValidation has user defined javascript functions
 * to handle validations of Input Fields.
 * 
 * <p><h4>Company:</h4> Cognizant Technology Solutions</p>
 * <p><h4>@author</h4> Cognizant</p>
 * <h4>@version</h4> 1.0
 */

function validateAddress(address)
{
  
    //var charpos = address.search("[[+)(;?*^$@!<>%]");
    var charpos = address.search("[[+;?*^$@!<>%]");
    if(address.length > 0 &&  charpos >= 0)
  	{
  	    return false;
  	}
  	return true;
}


/*
 * Check the Date Format
   Date Format must be (mm/dd/yy)
*/

function checkEmail(email)
{
    var pos = email.search("[@]");
    var dotpos = email.search("[.]");
    if((pos != -1) && (dotpos != -1))
    {
    	//if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))
    	//CHANGED BY ANIL ON 01-08-2005 FOR 4 CHARACTER CHECKING FOR EMAIL ID
     //CHANGED BY SUDHAKAR ON 26-07-2006 FOR 4 CHARACTER CHECKING FOR EMAIL ID
         //if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,6})+$/.test(email))
         if (/^\w+([\.-][\-]?\w+)*@\w+([\.-][\-]?\w+)*(\.\w{2,6})+$/.test(email))
      {
        return true;
      }    
      return false;
    }
    else
    {
      return false;
    }
}

/*
 * Function to Check Spaces
*/
function checkSpaces(objValue)
{
  var trimmedString="";
  var objArray = convertStringToArray(objValue);
  for(i=0;i<objArray.length;i++)
  {
    if(objArray[i] != " ")
	{
	  trimmedString=trimmedString.concat(objArray[i]);
	}
  }
  if(trimmedString.length == 0)
  {
	return false;
  }
  else
  {
    return true;
  }
  
}

/*Function To Convert String into Array of Characters*/
function convertStringToArray(s)
{
  var length = s.length;
  var arrayString = new Array(length);
  for(i=0;i<length;i++)
  {
    arrayString[i]=s.substring(i,i+1);
  }
  return arrayString;
}

/*
 * Function checks for Alphabets
*/

function containsAlphabets(objValue,myName)
{
    var charpos = objValue.search("[^a-zA-Z ]");
    if(objValue.length > 0 &&  charpos >= 0)
  	{
  	    return false;
  	}
  	return true;
}

/*
 * Function used to check for only numeric characters 
*/
function containsNumericCharacters(objValue,myName)
{
    var charpos = objValue.search("[^0-9]");
    if(objValue.length > 0 &&  charpos >= 0)
  	{
  	    return false;
  	}
  	return true;
}


function containsDecimalCharacters(objValue,myName)
{
    var charpos = objValue.search("[^.0-9]");
    if(charpos == -1)
    {
      //check for multiple dots
	  var count = 0;
	  var objArray = convertStringToArray(objValue);
	 
	  for(i=0;i<objArray.length;i++)
	  {
	    if(objArray[i] == ".")
		{
		  count++;
		}
	  }
     if(count > 1)
	  {
	    return "numeric";
	  }
	  else 
	  {
	    if(count == 1)
		  {
			 var length = objValue.length;
			 var indexDot = objValue.indexOf(".");
			 if((length -indexDot) > 3)
			  {
				 return "precision"; 
			  }
			  
			  if((length -1)== indexDot)
			  {
			     
			     return "whole";
			  }
             else{return "valid";}
		  }
		  
        else
		  {
		   return "valid";
		  }  
	  }
   }
   else
   {
     return "numeric";
   }
}
/*
 * Function used to check first character as space 
*/
function checkSpace(object)
{
   if(object[0] == ' ')
   { 
      return false;
   }
   return true;
}
/*
 * Function used to check for special characters
*/
function containsSpecialChar(objValue,myName)
{
 	var charpos = objValue.search("[+)(;?*^$@!<>%]");
   	if(objValue.length > 0 &&  charpos >= 0)
  	{
  	    return false;
  	}
  	return true;
}
/* 
 * Phone validation which can accept special characters like +
*/
function containsAplhaNumeric(objValue,myName)
{
 	var charpos = objValue.search("[^-+()./0-9A-Za-z ]");
   	if(objValue.length > 0 &&  charpos >= 0)
  	{
  	    return false;
  	}
  	return true;
}

function isAlphaNumeric(objValue,myName)
{
   var charpos = objValue.search("[^0-9A-Za-z ]");
   	if(objValue.length > 0 &&  charpos >= 0)
  	{
  	    return false;
  	}
  	return true;
}
/*
 * Function used to check for Slash as well as special characters
*/

function checkslash(objValue,myName)
{
  var charpos = objValue.search("[^/A-Za-z0-9 ]");
   	if(objValue.length > 0 &&  charpos >= 0)
  	{
  	    return false;
  	}
  	return true;
}
/*
 * Keycode events which captures the characters typed 
 * and validate.
*/

function inputCharacters( event )
{
	if ( event.which == 8 )
	{
		return true; // BackSpace
	}
	else if ( event.which >= 65 && event.which <= 90 )
	{
		return true; // A - Z
	}
	else if ( event.which >= 97 && event.which <= 122 )
	{
		return true; // a - z
	}
	return false;
}

/*
 * Function used to reset the form fields
*/

function clearFormFields()
{
   var length = document.forms[0].elements.length;
   for(i=0;i<length;i++)
   {
     var fieldtype = document.forms[0].elements[i].type;
     var disabledType = document.forms[0].elements[i].disabled;
     if(fieldtype == "select-one")
	 {
	   var readType = false;   
	 }
	 else
	 {
	   var readType = document.forms[0].elements[i].readOnly;
	 }
     if((fieldtype != "hidden") && (readType == false) && (disabledType == false)) 
     {
	    if(fieldtype == "select-one")
		 {
		   document.forms[0].elements[i].value = "";
		 }
	    if((fieldtype == "radio") || (fieldtype == "checkbox"))
		 {
		   document.forms[0].elements[i].checked=false; 
		 }
		else
		 {
		   document.forms[0].elements[i].value="";
		 }
     }
   }
}
/*
 * Function used to Set the fields readOnly
*/

function setReadOnly(field,token)
{
  var fieldString = field.value;
  var readFields = fieldString.split(token);

  for(i=0;i<readFields.length;i++)
  {
      for(j=0;j<document.forms[0].elements.length;j++)
	  {
	     
		 if(readFields[i] == document.forms[0].elements[j].name)
		 {
		  	 if(document.forms[0].elements[j].type == "text")
			 {
			   document.forms[0].elements[j].readOnly=true;
			 }
			 if((document.forms[0].elements[j].type == "radio") || (document.forms[0].elements[j].type == "select-one"))
			 {
			   document.forms[0].elements[j].disabled=true;
			 }
			 
         }
		 
	  }
  }
}


/*
 * Function used to compare the dates
*/

function checkDates(firstDate,secondDate)
{
     numFirstDate = firstDate.substring(6,10) + firstDate.substring(0,2) + firstDate.substring(3,5);
     numSecondDate = secondDate.substring(6,10) + secondDate.substring(0,2)  + secondDate.substring(3,5);

     if(numFirstDate > numSecondDate)
     {
           return true;
     }
     else
     {
          return false;
     }
}
/*
 * Function used to check equal dates
*/

function checkEqualDates(firstDate,secondDate)
{
     numFirstDate = firstDate.substring(6,10) + firstDate.substring(0,2) + firstDate.substring(3,5);
     numSecondDate = secondDate.substring(6,10) + secondDate.substring(0,2)  + secondDate.substring(3,5);

     if(numFirstDate == numSecondDate)
     {
           return true;
     }
     else
     {
          return false;
     }
}

/*
 *  Function to check date format
 */
  	
function validateDate(date)
{
   //date should allow only slash and numeric values.
    var charpos = date.search("[^/0-9]");
    if(date.length > 0 &&  charpos >= 0)
  	{
  	    return false;
  	}
  	return true;
}

/*
 * Check the Date Format
   Date Format must be (mm/dd/yy)
*/
function isValidDate(date)
{
    var month = date.substring(0,2);
    var dat = date.substring(3,5);
    var year = date.substring(6,date.length);
    var message="";
    if(month > 12)
    {
      message+="\nMonth cannot be greater than 12";
    }
    if(dat > 31)
    {
      message+="\nDate cannot be greater than 31";
    }
    if((year.length < 4) || (year.length > 4)) 
    {
      message+="\nPlease Enter the Year in Correct Format(YYYY)";
    }
   return message;   
}


/*
 * Check the Date Format
   Date Format must be (mm/dd/yy)
*/
function isValidDate(date)
{
    var month = date.substring(0,2);
    var dat = date.substring(3,5);
    var year = date.substring(6,date.length);
    
    if(month > 12)
    {
      alert("Month cannot be greater than 12");
      return false;
    }
    if(dat > 31)
    {
      alert("Date cannot be greater than 31");
      return false;
    }
    if((year.length < 4) || (year.length > 4)) 
    {
      alert("Please Enter the Year in Correct Format(YYYY)");
      return false;
    }
    
}
//*********************************************************************/
//	Function Name		: isDate
//	Purpose				: checks for a valid date 
//	Parameters in		: MM,DD,YYYY 
//	Parameters out		: true/false 
//	Returns				: false in case of an invalid date else true
//	Notes				: This function is for internal use in JSFunctions.js
//						  and is not meant to be called from outside.
//*********************************************************************/
function isDate(mm,dd,yyyy)
{
  if ((yyyy  < 1753) || (yyyy > 9999)) //valid window : 1753 - 9999 
 	return false;	
  var arrMonthDays = 	new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
  if ((mm == "") || (mm == 0) || (mm > 12)) 
	return false;	
  if ((dd == "") || (dd == 0) || (dd > 31)) 
 	return false;	
  if ((yyyy % 4) == 0)	
  {
   	if ((yyyy % 100) == 0){			// Leap year Check
         if ((yyyy %400) == 0) 	
		 	arrMonthDays [2] = 29;}
 	else			
		 	arrMonthDays [2] = 29;
  }			
 if ( dd > arrMonthDays [mm])
	return false;	   
  return true;	
}
//*********************************************************************/
//	Function Name		: isDateMDY
//	Purpose				: checks for a valid date in MM/DD/CCYY format
//	Parameters in		: form object for which date validation is required.
//	Parameters out		: true/false 
//	Returns				: false in case of an invalid date else true
//	Notes				: The date is expected in format MM/DD/yyyy or
//						  MM-DD-yyyy . 	
//						  Valid dates are considered only between years
//						  1000 AD and  9999 AD 
//						  The function pads up "0" for dd or mm lesser 
//						  than 9 and also formats the date in dd/mm/yyyy
//						  for the object value.	
//						  Eg. 2-02-2002 will be converted to 02/02/2000
//*********************************************************************/
var reDateFmt1=/(^[0-9]+)\/+([0-9]+)\/+([0-9]+)$/;
var reDateFmt2=/(^[0-9]+)\-+([0-9]+)\-+([0-9]+)$/;
function isDateMDY(formObj)
{
  var objVal = formObj.value;
  var i;
  var a = new Array();
  var b,c;
  var x=0;


  if ( (!reDateFmt1.test(objVal)) && (!reDateFmt2.test(objVal))) 
  {

    return false; 										//  Invalid date format
  } 
  if (reDateFmt1.test(objVal))                        // Code to split the value entered
         { 

         newstr = objVal.split(reDateFmt1,"$1,$2,$3");        // into DD/MM/YYYY using

           var mm_from1=parseInt(RegExp.$1,10);                       // Regular Expressions

           var dd_from1=parseInt(RegExp.$2,10);

           var yy_from1=parseInt(RegExp.$3,10);
         }
  else if (reDateFmt2.test(objVal))                  // Code to split the value entered
         {

         newstr = objVal.split(reDateFmt2,"$1,$2,$3");        // into DD-MM-YYYY using
           var mm_from1=parseInt(RegExp.$1,10);                       // Regular Expressions
           var dd_from1=parseInt(RegExp.$2,10);
           var yy_from1=parseInt(RegExp.$3,10); 
		  }
  else
  	return false;    			//  Date entered is in invalid	format 

  if(!isDate(mm_from1,dd_from1,yy_from1))	
  	return false;

 var strdd = dd_from1;
 var strmm = mm_from1;
 var stryyyy = yy_from1; 		

 if(strdd < 10 )
 	var strdd = "0" + strdd;
 if(parseInt(strmm,10) < 10 )
 	var strmm = "0" + strmm;
 
 
 formObj.value = strmm + "/" + strdd + "/" +stryyyy;
 return true;
}
//*********************************************************************/
//	Function Name		: dateCmp
//	Purpose				: compares two dates 
//	Parameters in		: form object date 1 , form object date 2
//	Parameters out		: --
//	Returns				: 991 - Invalid date (form object 1)  
//						  992 - Invalid date (form object 2)  
//						  0   - form object 1 = form object 2	
//						  1   - form object 1 > form object 2	
//						 -1   - form object 1 < form object 2	
//	Notes				: Compares to dated in DD/MM/YY format 
//						  
//*********************************************************************/

function dateCmp(formObj1,formObj2)
{
	var oDate1 = new Date(formObj1.value);
	var oDate2 = null;
	
	if(formObj2 == null)
	{
		oDate2 = new Date();
	}
	else
	{
		oDate2 = new Date(formObj2.value);
	}
	
	if (oDate1 == oDate2)
		return 0;
	else if (oDate1 > oDate2)
		return 1;
	else
		return -1;
}

function dateCmp1(formObj1,formObj2)
{

  if  (!isDateMDY(formObj1))
		return 991;
  if  (!isDateMDY(formObj2))
		return 992;
	
 var objVal1=	formObj1.value;
 var objVal2=	formObj2.value;
  if (reDateFmt1.test(objVal1))                        // Code to split the value entered
         { newstr = objVal1.split(reDateFmt1,"$1,$2,$3");        // into DD/MM/YYYY using
           var dd_from1=parseInt(RegExp.$1,10);                       // Regular Expressions
           var mm_from1=parseInt(RegExp.$2,10);
           var yy_from1=parseInt(RegExp.$3,10);
         }
 if (reDateFmt1.test(objVal2))                        // Code to split the value entered
         { newstr = objVal2.split(reDateFmt1,"$1,$2,$3");        // into DD/MM/YYYY using
           var dd_from2=parseInt(RegExp.$1,10);                       // Regular Expressions
           var mm_from2=parseInt(RegExp.$2,10);
           var yy_from2=parseInt(RegExp.$3,10);
         }
		 
	if (yy_from1 > yy_from2)
		return 1;
	else if (yy_from1 < yy_from2)
		return 	-1
    else {
			if (mm_from1 > mm_from2)
				return 1
			else if (mm_from1 < mm_from2)
				return -1
			else {
					if (dd_from1 > dd_from2)
						return 1
					else if (dd_from1 < dd_from2)	
							return -1
					else
							return 0	
				}	
		}
}	
///////////////////////    end  /////////////////////////

//*********************************************************************/
//	Function Name		: dateCmp
//	Purpose				: compares two dates 
//	Parameters in		: form object date 1 , form object date 2
//	Parameters out		: --
//	Returns				: 991 - Invalid date (form object 1)  
//						  992 - Invalid date (form object 2)  
//						  0   - form object 1 = form object 2	
//						  1   - form object 1 > form object 2	
//						 -1   - form object 1 < form object 2	
//	Notes				: Compares to dated in MM/DD/YYYY format 
//						  
//*********************************************************************/

function dateCmpMDY(formObj1,formObj2)
{

  if  (!isDateMDY(formObj1))
		return 991;
  if  (!isDateMDY(formObj2))
		return 992;
	
 var objVal1=	formObj1.value;
 var objVal2=	formObj2.value;
  if (reDateFmt1.test(objVal1))                        // Code to split the value entered
         { newstr = objVal1.split(reDateFmt1,"$1,$2,$3");        // into DD/MM/YYYY using
           var mm_from1=parseInt(RegExp.$1,10);                       // Regular Expressions
           var dd_from1=parseInt(RegExp.$2,10);
           var yy_from1=parseInt(RegExp.$3,10);
         }
 if (reDateFmt1.test(objVal2))                        // Code to split the value entered
         { newstr = objVal2.split(reDateFmt1,"$1,$2,$3");        // into DD/MM/YYYY using
           var mm_from2=parseInt(RegExp.$1,10);                       // Regular Expressions
           var dd_from2=parseInt(RegExp.$2,10);
           var yy_from2=parseInt(RegExp.$3,10);
         }
		 
	if (yy_from1 > yy_from2)
		return 1;
	else if (yy_from1 < yy_from2)
		return 	-1
    else {
			if (mm_from1 > mm_from2)
				return 1
			else if (mm_from1 < mm_from2)
				return -1
			else {
					if (dd_from1 > dd_from2)
						return 1
					else if (dd_from1 < dd_from2)	
							return -1
					else
							return 0	
				}	
		}
}	


////////////////Date validation///////////////
/* Here's the list of tokens we support:
   m (or M) : month number, one or two digits.
   mm (or MM) : month number, strictly two digits (i.e. April is 04).
   d (or D) : day number, one or two digits.
   dd (or DD) : day number, strictly two digits.
   y (or Y) : year, two or four digits.
   yy (or YY) : year, strictly two digits.
   yyyy (or YYYY) : year, strictly four digits.
   mon : abbreviated month name (April is apr, Apr, APR, etc.)
   Mon : abbreviated month name, mixed-case (i.e. April is Apr only).
   MON : abbreviated month name, all upper-case (i.e. April is APR only).
   mon_strict : abbreviated month name, all lower-case (i.e. April is apr 
         only).
   month : full month name (April is april, April, APRIL, etc.)
   Month : full month name, mixed-case (i.e. April only).
   MONTH: full month name, all upper-case (i.e. APRIL only).
   month_strict : full month name, all lower-case (i.e. april only).
   h (or H) : hour, one or two digits.
   hh (or HH) : hour, strictly two digits.
   min (or MIN): minutes, one or two digits.
   mins (or MINS) : minutes, strictly two digits.
   s (or S) : seconds, one or two digits.
   ss (or SS) : seconds, strictly two digits.
   ampm (or AMPM) : am/pm setting.  Valid values to match this token are
         am, pm, AM, PM, a.m., p.m., A.M., P.M.
*/
// Be careful with this pattern.  Longer tokens should be placed before shorter
// tokens to disambiguate them.  For example, parsing "mon_strict" should 
// result in one token "mon_strict" and not two tokens "mon" and a literal
// "_strict".

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");

// lowerMonArr is used to map months to their numeric values.

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}

// monPatArr contains regular expressions used for matching abbreviated months
// in a date string.

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');

// monthPatArr contains regular expressions used for matching full months
// in a date string.

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/);

// cutoffYear is the cut-off for assigning "19" or "20" as century.  Any
// two-digit year >= cutoffYear will get a century of "19", and everything
// else gets a century of "20".

var cutoffYear=50;

// FormatToken is a datatype we use for storing extracted tokens from the
// format string.

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 {

	// No token matched current position, so current character should 
	// be saved as a required literal.

	if (tokInd>0 && tokArr[tokInd-1].type=="literal") {

	// Literal tokens can be combined.Just add to the last token.

	tokArr[tokInd-1].token+=formatStr.charAt(strInd++);
	}
	else {
	tokArr[tokInd++]=new FormatToken(formatStr.charAt(strInd++), "literal");
		  }
	   }
	}
	return tokArr;
}

/* buildDate does all the real work.It takes a date string and format string,
 tries to match the two up, and returns a Date object (with the supplied date
 string value).If a date string doesn't contain all the fields that a Date
 object contains (for example, a date string with just the month), all
 unprovided fields are defaulted to those characteristics of the current
 date. Time fields that aren't provided default to 0.Thus, a date string
 like "3/30/2000" in "%mm/%dd/%yyyy" format results in a Date object for that
 date at midnight.formatStr is a free-form string that indicates special
 tokens via the % character.Here are some examples that will return a Date
 object:

 buildDate('3/30/2000','%mm/%dd/%y') // March 30, 2000
 buildDate('March 30, 2000','%Mon %d, %y') // Same as above.
 buildDate('Here is the date: 30-3-00','Here is the date: %dd-%m-%yy')

 If the format string does not match the string provided, an error message
 (i.e. String object) is returned.Thus, to see if buildDate succeeded, the
 caller can use the "typeof" command on the return value.For example,
 here's the dateCheck function, which returns true if a given date is
 valid,and false otherwise (and reports an error in the false case):

 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 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;

		// Create a date object with the current date so that if the user only
		// gives a month or day string, we can still return a valid date.

		var curdate=new Date();
		intMonth=curdate.getMonth()+1;
		intDay=curdate.getDate();
		intYear=curdate.getFullYear();

		// Default time to midnight, so that if given just date info, we return
		// a Date object for that date at midnight.

		intHour=0;
		intMin=0;
		intSec=0;

		// Walk across dateStr, matching the parsed formatStr until we find a 
		// mismatch or succeed.

		while (strInd < dateStr.length && tokInd < tokArr.length) {

		// Start with the easy case of matching a literal.

		if (tokArr[tokInd].type=="literal") {
		if (dateStr.indexOf(tokArr[tokInd].token,strInd)==strInd) {

		// The current position in the string does match the format 
		// pattern.

		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 "\"" + dateStr + "\" does not conform to the expected format: (mm/dd/yyyy)";
		   }
		}

		// If we get here, we're matching to a symbolic token.
		switch (tokArr[tokInd].token) {
		case 'm':
		case 'M':
		case 'd':
		case 'D':
		case 'h':
		case 'H':
		case 'min':
		case 'MIN':
		case 's':
		case 'S':

		// Extract one or two characters from the date-time string and if 
		// it's a number, save it as the month, day, hour, or minute, as
		// appropriate.

		curChar=dateStr.charAt(strInd);
		nextChar=dateStr.charAt(strInd+1);
		matchArr=dateStr.substr(strInd).match(/^\d{1,2}/);
		if (matchArr==null) {

		// First character isn't a number; there's a mismatch between
		// the pattern and date string, so return error.

		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 "Invalid " + unit +".";
		}
		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':

		// Extract two characters from the date string and if it's a 
		// number, save it as the month, day, or hour, as appropriate.

		strOffset=2;
		matchArr=dateStr.substr(strInd).match(/^\d{2}/);
		if (matchArr==null) {

		// The two characters aren't a number; there's a mismatch 
		// between the pattern and date string, so return an error
		// message.

		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 "Invalid " + unit +".";
		}
		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':

		// Extract two or four characters from the date string and if it's
		// a number, save it as the year.Convert two-digit years to four
		// digit years by assigning a century of '19' if the year is >= 
		// cutoffYear, and '20' otherwise.

		if (dateStr.substr(strInd,4).search(/\d{4}/) != -1) {

		// Four digit year.

		intYear=parseInt(dateStr.substr(strInd,4),10);
		strOffset=4;
		}
		else {
		if (dateStr.substr(strInd,2).search(/\d{2}/) != -1) {

		// Two digit year.

		intYear=parseInt(dateStr.substr(strInd,2),10);
		if (intYear>=cutoffYear) {
		intYear+=1900;
		}
		else {
		intYear+=2000;
		}
		strOffset=2;
		}
		else {

		// Invalid year; return error.

		return "Invalid year \"" + dateStr.substr(strInd,2) + 
		"\". Must be two or four digits.";
		   }
		}
		break;
		case 'yy':
		case 'YY':

		// Extract two characters from the date string and if it's a 
		// number, save it as the year.Convert two-digit years to four 
		// digit years by assigning a century of '19' if the year is >= 
		// cutoffYear, and '20' otherwise.

		if (dateStr.substr(strInd,2).search(/\d{2}/) != -1) {

		// Two digit year.

		intYear=parseInt(dateStr.substr(strInd,2),10);
		if (intYear>=cutoffYear) {
		intYear+=1900;
		}
		else {
		intYear+=2000;
		}
		strOffset=2;
		} else {
		// Invalid year; return error
		return "Invalid year \"" + dateStr.substr(strInd,2) + 
		"\". Must be two digits.";
		}
		break;
		case 'yyyy':
		case 'YYYY':

		// Extract four characters from the date string and if it's a 
		// number, save it as the year.

		if (dateStr.substr(strInd,4).search(/\d{4}/) != -1) {

		// Four digit year.

		intYear=parseInt(dateStr.substr(strInd,4),10);
		strOffset=4;
		}
		else {

		// Invalid year; return error.

		return "Invalid year \"" + dateStr.substr(strInd,4) + 
		"\". Must be four digits.";
		}
		break;
		case 'mon':
		case 'Mon':
		case 'MON':
		case 'mon_strict':

		// Extract three characters from dateStr and parse them as 
		// lower-case, mixed-case, or upper-case abbreviated months,
		// as appropriate.

		monPat=monPatArr[tokArr[tokInd].token];
		if (dateStr.substr(strInd,3).search(monPat) != -1) {
		intMonth=lowerMonArr[dateStr.substr(strInd,3).toLowerCase()];
		}
		else {

		// Invalid month, return error.

		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 "Invalid month \"" + dateStr.substr(strInd,3) + 
		"\". Must be " + caseStat + ".";
		}
		strOffset=3;
		break;
		case 'month':
		case 'Month':
		case 'MONTH':
		case 'month_strict':

		// Extract a full month name at strInd from dateStr if possible.

		monPat=monthPatArr[tokArr[tokInd].token];
		matchArray=dateStr.substr(strInd).match(monPat);
		if (matchArray==null) {

		// Invalid month, return error.

		return "Can't find a month beginning at \"" +
		dateStr.substr(strInd) + "\".";
		}

		// It's a good month.

		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) {

		// There's no am/pm in the string.Return error msg.

		return "Missing am/pm designation.";
		}

		// Store am/pm value for later (as just am or pm, to make things
		// easier later).

		if (matchArr[0].substr(0,1).toLowerCase() == "a") {

		// This is am.

		ampm = "am";
		}
		else {
		ampm = "pm";
		}
		strOffset = matchArr[0].length;
		break;
		}
		strInd += strOffset;
		tokInd++;
		}
		if (tokInd != tokArr.length || strInd != dateStr.length) {

		/* We got through the whole date string or format string, but there's 
		 more data in the other, so there's a mismatch. */

		//return "\"" + dateStr + "\" is invalid Date Format, expected format: " + formatStr;
		return "\"" + dateStr + "\" is invalid Date Format, expected format: " + "(mm/dd/yyyy)";
		}

		// Make sure all components are in the right ranges.

		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.";
		}

		// Make sure user doesn't put 31 for a month that only has 30 days

		if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && intDay == 31) {
		return "Month "+intMonth+" doesn't have 31 days!";
		}

		// Check for February date validity (including leap years) 

		if (intMonth == 2) {

		// figure out if "year" is a leap year; don't forget that
		// century years are only leap years if divisible by 400

		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!";
		   }
		}

		// Check that if am/pm is not provided, hours are between 0 and 23.

		if (ampm == "") {
		if (intHour < 0 || intHour > 23) {
		return "Hour must be between 0 and 23 for military time.";
		   }
		}
		else {

		// non-military time, so make sure it's between 1 and 12.

		if (intHour < 1|| intHour > 12) {
		return "Hour must be between 1 and 12 for standard time.";
		   }
		}

		// If user specified amor pm, convert intHour to military.

		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 "";
}
else {

// We got an error string.

return myObj;
return false;
   }
}

function validateDate(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 currentYearCompare(dateStr)
{
	var curdate=new Date();
	intYear=curdate.getFullYear();
	if(dateStr!="")
	{
		if (parseInt(dateStr)>intYear)
		{
			return true;
		}
	}
	else
	{
		return false;
	}
}

function compareDates(firstDate,secondDate)
    {
         numFirstDate = firstDate.substring(6,10) + firstDate.substring(0,2) + firstDate.substring(3,5);
         numSecondDate = secondDate.substring(6,10) + secondDate.substring(0,2)  + secondDate.substring(3,5);

         if(numFirstDate > numSecondDate)
         {
               return true;
         }
         else
         {
              return false;
         }
    }
  function checkZeroVal(fieldVal)
   {
     if(parseFloat(fieldVal)==0)
	 {
		return true;	 
	 }
	 else
	 {
	 	return false;
	 }
	}
	
	
	function trim(inputString)
	{
		
   		var retValue = inputString;
  	 var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }

return retValue;
   }
	
	    

///////////////////////    end  /////////////////////////
