function noenter() 
{
   return !(window.event && window.event.keyCode == 13); 
}

function dumpProps( obj )
{
   var result = "DumpProps\n";
   for( var i in obj )
      result +=  i + " = " + obj[i] + "\n"
   alert( result );
}

function validEmail( email, msg )
{   
	var reEmail = /^[0-9A-Za-z\._-]+\@[0-9A-Za-z\._-]+\.[A-Za-z]+$/;
	if(!reEmail.test( email.value ))
	{
		alert( msg + " is not valid." );
		email.focus();
		return false;
	}
	return true;
}

function checkField( field, msg )
{
   if( field.value == "" )
   {
      alert( msg + " is a required field." );
      field.focus();
      return false;
   }
   return true;
}

function checkSelect( field, msg )
{
   // To make it easier, just check to see if the index is 0
   if( field.selectedIndex == 0 )
   {
      alert( msg + " is a required field." );
      field.focus();
      return false;
   }
   return true;
}

function checkRadio( field, msg )
{
   var len = field.length;
   var flag = 0;
   
   for (i=0; i<len; i++)
   {
      if (field[i].checked == true)
      {  flag = 1;   }   
   }
   
   if (flag == 0)
   {
      alert ( msg + " must have at least one option selected.");
      field[0].focus();
      return false;
   }
   return true;   
}

function checkFloat( field, msg )
{
   validchars = "0123456789.";
   
   // First check the field for a value
   if( field.value == "" )
      field.value = "0.0";
   
   for (i=0; i<field.value.length; i++)
   {
      if( validchars.indexOf(field.value.charAt(i)) < 0 )
      {
         alert( msg + " has an invalid character. Only numbers and a period is allowed.");
         field.focus();
         return false;
      }
   }
   
   // Now check for a valid float number
   if( !IsFloat( field.value ) )
   {
      alert( msg + " is a float field.\n(eg. 10.0, 2.5, 3.765)" );
      field.focus();
      return false;
   }

   // Set the value since it is possible for something like '2.5km' to be
   // considered a valid float value (2.5)
   field.value = parseFloat( field.value );
   return true;
}

function IsFloat( number )
{
   var val = parseFloat( number );
   return (isNaN( val ) == false);
}


function checkInteger( field, msg )
{
   validchars = "0123456789";

   // First check the field for a value
   if( field.value == "" )
      field.value = "0";

   for (i=0; i<field.value.length; i++)
   {
      if( validchars.indexOf(field.value.charAt(i)) < 0 )
      {
         alert( msg + " has an invalid character. Only numbers are allowed.");
         field.focus();
         return false;
      }
   }

   // Now check for a valid integer number
   if( !IsInteger( field.value ) )
   {
      alert( msg + " is an integer field.\n(eg. 10, 2, 3)" );
      field.focus();
      return false;
   }

   // Set the value since it is possible for something like '23 km' to be
   // considered a valid integer value (23)
   field.value = parseInt( field.value );

   return true;
}

function IsInteger( number )
{
   var val = parseInt( number );
   return (isNaN( val ) == false);
}

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 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 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 isDate(dtStr){
   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 (pos1==-1 || pos2==-1){
      alert("The date format should be : mm/dd/yyyy")
      return false
   }
   if (strMonth.length<1 || month<1 || month>12){
      alert("Please enter a valid month")
      return false
   }
   if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
      alert("Please enter a valid day")
      return false
   }
   if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
      alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
      return false
   }
   if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
      return false
   }
   return true
}

function checkDate(m, d, y)
{
   if (!parseInt(y.value, 10))
   {
      alert("Invalid Year")
      y.focus()
      return false
   }
   if (parseInt(y.value, 10) < 1910)
   {
      alert("Year must be greater than 1910")
      y.focus()
      return false
   }
   
   mon = parseInt(m.value, 10)
   day = parseInt(d.value, 10)
   year = parseInt(y.value, 10)
   
   if (day>28)
   {
      if (mon==2)
      {
         if ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0)))
         {
            if (day>29)
            {
               alert("February cannot have more than 29 days")
               m.focus()
               return false
            }
         }
         else
         {
            alert("February cannot have more than 28 days for the year you selected")
            m.focus()
            return false
         }
      }
      if (((mon==4) || (mon==6) || (mon==9) || (mon==11)) && (day>30))
      {
         alert("The month selected only has 30 days")
         m.focus()
         return false
      }
      return true
   }
   else
   {
      return true
   }  
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}

String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
