/* Function for checking email validity */
function echeck(str) {
		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);

		if (str.indexOf(at)==-1){
		   alert("Enter a valid E-mail ID");
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr-1){
		   alert("Enter a valid E-mail ID");
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr-1){
		    alert("Enter a valid E-mail ID");
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Enter a valid E-mail ID");
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Enter a valid E-mail ID");
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Enter a valid E-mail ID");
		    return false;
		 }

		 if (str.indexOf(" ")!=-1){
		    alert("Enter a valid E-mail ID");
		    return false;
		 }

 		 return true;
	}
/* Function for Checking only numbers */

function isNumeric(formName,formObject)
{
	var field1 = formName + '.' + formObject
	var field = eval(field1)

	var valid = "0123456789"
	var ok = "yes";
	var temp;

	for (var i=0; i<field.value.length; i++)
	{
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1")
			ok = "no";
	}
	if (ok == "no")
	{
		alert("Invalid entry!!! Only numbers are accepted!");
		field.focus();
		field.select();
		return (false);
	}
	if (ok == "yes")
	{
		return (true);
	}
}

/* Function for trimming spaces */

  function trimChar(str, chara)
  {
    var i,len;
    len = str.length;

    //Trimming the whitespace chars from the beginning of the string
    for(i=0;i<len && len>=1;i++)
      if(str.charAt(i) == chara)
       {
         str = str.substring(1,len-1)
         len = str.length;
       }

    len = str.length;

    //Trimming the whitespace chars from the end of the string
      for(i=len-1;i>0 && len>=1;i--)
       if(str.charAt(i) == chara)
        {
          str = str.substring(0,len-1)
          len = str.length;
        }
       return str
  }




/* Function for checking Empty Field   */
function isEmpty(formName,formObject)
{
	var obj = formName + '.' + formObject
	var myValue = eval(obj).value;
	
	str = trimChar(myValue,' ');

	if (myValue.charAt(0) == " ")
	{
		eval(obj).focus();
		eval(obj).select();
		return false;
	}

	if (str == '')
	{
		eval(obj).focus();
		eval(obj).select();
		return false;
	}
	else
		return true;
}


/* Function for trimming spaces */

function  isEmptyStr(str)
{
  if(trimChar(str, ' ')=='')
   {
     return true;
   }
  return false;
}
//*******************Function For trimming spaces data *******************

function trim(inputString) {
    // Removes leading and trailing spaces from the passed string. Also removes
    // consecutive spaces and replaces it with one space. If something besides
    // a string is passed in (null, custom object, etc.) then return the input.
    if (typeof inputString != "string") { return 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);
    }
   
    while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
     }
     return retValue; // Return the trimmed string back to the user
    } // Ends the "trim" function
    
    //*******************Function For trimming spaces data ENDS *******************

//*******************Function For Validating Date in MM/DD/YYYY Format starts*******************

function isDateMMDDYY(formName,formObject){
	var dtStr1,dtStr2,dtStr
	dtStr1=formName + '.' + formObject
	dtStr2=eval(dtStr1)
	dtStr=dtStr2.value


	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	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 (dtStr == '') return true;

	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		dtStr2.select();
		dtStr2.focus();
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		dtStr2.select();
		dtStr2.focus();
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		dtStr2.select();
		dtStr2.focus();
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		dtStr2.select();
		dtStr2.focus();
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		dtStr2.select();
		dtStr2.focus();
		return false;
	}
return true;
}

//*******************Function For Validating Date in MM/DD/YYYY Format Ends*******************

//*******************Function For Validating Date in DD/MM/YYYY Format Starts*******************


var dtCh= "/";
var minYear=1900;
var maxYear=2100;


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 For Validating Date in DD/MM/YYYY Format Ends*******************

//******************* Function for Comparing the date with today's date Start Here *******************// 
function FutureDate(dateString)
{
    var now = new Date();
    var today = new Date(now.getYear(),now.getMonth(),now.getDate());
    var century = parseInt(now.getYear()/100)*100;
    
	//alert(dateString)
	//alert(now)
	//alert(today)
	//alert(century)
	
	var strMonth, strDay, strYear
	strMonth = dateString.substring(0,dateString.indexOf('/'));
	//alert(strDay);
	//alert(strDay.length);
	if ((strMonth<10) &&(strMonth.length==1))
		strMonth = "0"+strMonth
	//alert(" after Month = "+strMonth);
	
	strDay = dateString.substring(dateString.indexOf('/')+1,dateString.lastIndexOf('/'))
	if ((strDay<10) &&(strDay.length==1))
		strDay = "0"+strDay
	//alert("after Day = "+strDay)
	
	strYear = dateString.substring(dateString.lastIndexOf('/')+1)
	//alert("after year = "+strYear)
	
	dateString = strDay+"/"+strMonth+"/"+strYear
	
	//alert("final="+dateString)
	
	
	var date = new Date(dateString.substring(6,10),
                        dateString.substring(3,5)-1,
                        dateString.substring(0,2));
	//alert(date)
	
	if (date < today)
    {
       //alert('ok');
       // document.write(date.toString(),' is behind ',today.toString(),'<P>');
        return false;
    }
    else if (date > today)
    {
		//alert('Not ok');
       // document.write(date.toString(),' is ahead of ',today.toString(),'<P>');
        return true;
    }
    else
    {
      // alert('equal');
       // document.write(date.toString(),' is the same as ',today.toString(),'<P>');
        return true;
    }
}    
//******************* Function for Comparing the date with today's date Ends Here *******************// 



