   function ValidateEmail()
   {
      var at = "@";
      var dot = ".";
      var email = form.email.value;
      var emailLen = email.length;
      var atPos = email.indexOf( at );
      var atPos2 = email.indexOf( at, ( atPos + 1 ) );
      var dotPos = email.indexOf( dot, atPos );
      var dotDot = email.indexOf( ".." );

      //only one at is allowed
      if ( atPos2 != -1 ) 
      {
         alert( "Invalid e-mail address." );
         form.email.focus();
         return false;
      }

      //the at has to be there, cannot be in position 1 or last or before last.
      if ( atPos == -1 || atPos == 0 || atPos == 1 || atPos == ( emailLen -1 ) || atPos == ( emailLen - 2 ) )
      {
         alert( "Invalid e-mail address." );
         form.email.focus();
         return false;
      }

      //dot has to be there, cannot be last or before last or after the at.
      if ( dotPos == -1 || dotPos == ( emailLen - 1 ) || dotPos == ( emailLen - 2 ) || dotPos == ( atPos + 1 ) )
      {
         alert( "Invalid e-mail address." );
         form.email.focus();
         return false;
      }

      //dot cannot be first or last or before the at or before at.
      if (email.substr( 0, 1 ) == dot || email.substr(( emailLen - 1 ), 1) == dot || email.substr(( atPos - 1 ), 1) == dot || email.substr(( emailLen - 2 ), 1) == dot )
      {
         alert( "Invalid e-mail address." );
         form.email.focus();
         return false;
      }

      //we cannot have to consecutive dots.
      if ( dotDot != -1 )
      {
         alert( "Invalid e-mail address." );
         form.email.focus();
         return false;
      }

      return true;
   }

   function ValidateField( lfield, llength, ltype, lselect, lfieldName)
   {

      //lfield = name of the field.
      //llength = length the field should have and 0 if the length does not matter.
      //ltype = "N" if it has to be numeric and "" for what ever.
      //lselect = "Y" then we must check that the value is diferrent than "Select".
      //lfieldName = "name" The name of the field need to be supplied.

      //if "Y" then we verify that the position 0 was not the selection.
      if ( lselect == "Y" )
      {
         if ( document.getElementById( lfield ).selectedIndex == 0 )
         {
            alert( lfieldName + " option must to be selected.");
            document.getElementById(lfield).focus();
            return false;
         }
         else
         {
            return true;
         }
      }

      //if the field does not exist
      if ( document.getElementById(lfield) == null )
      {
         alert( lfieldName + " field does not exist." );
         document.getElementById(lfield).focus();
         return false;
      }

      var dataTrim = document.getElementById(lfield).value.replace(/ /g, "");
      var data = document.getElementById(lfield).value;

      //if the field is null or empty
      if ( data == null || dataTrim == "" )
      {
         alert( lfieldName + " field cannot be empty" );
         document.getElementById(lfield).focus();
         return false;
      }

      //if the length does not match.
      if ( llength != 0 )
      {
         if ( data.length != llength)
         {
            alert( lfieldName + " length is incorrect." );
            document.getElementById(lfield).focus();
            return false;
         }
      }

      //if it is not a number.
      if ( ltype == "N" )
      {
         if ( isNaN(data) )
         {
            alert( lfieldName + " must be a number." );
            document.getElementById(lfield).focus();
            return false;
         }
      }

      return true;

   }

   function ValidatePhones()
   {
      var phoneTrim = document.getElementById( "hphone" ).value.replace(/ /g, "");
      var phone = document.getElementById( "hphone" ).value;

      //if the field is null or empty
      if ( phone == null || phoneTrim == "" )
      {

         phoneTrim = document.getElementById( "wphone" ).value.replace(/ /g, "");
         phone = document.getElementById( "wphone" ).value;

         //if the field is null or empty
         if ( phone == null || phoneTrim == "" )
         {
            phoneTrim = document.getElementById( "mphone" ).value.replace(/ /g, "");
            phone = document.getElementById( "mphone" ).value;

            //if the field is null or empty
            if ( phone == null || phoneTrim == "" )
            {
               alert( "You need to enter at least one phone number" );
               document.getElementById( "hphone" ).focus();
               return false;
            }
         }

      }

      return true;
   }


   function CheckDates( )
   {
      var month = form.month.value;
      var day = form.day.value;
      var year = form.year.value;
      var time = form.time.value;
      var month2 = form.month2.value;
      var day2 = form.day2.value;
      var year2 = form.year2.value;
      var time2 = form.time2.value;

      var startDate = new Date;
      var endDate = new Date;

      var date1, date2;
      var rmonth = 0, rmonth2 = 0;

      if ((month != " ") || (day != " ") || (year != " ") || (time != " ") || (month2 != " ") || (day2 != " ") || (year2 != " ") || (time2 != " "))
      {
         if ((month == " ") || (day == " ") || (year == " ") || (time == " ") || (month2 == " ") || (day2 == " ") || (year2 == " ") || (time2 == " "))
         {
            alert( "All date fields and time fields need to be seleted or cleared!" );
            return false;
         }

         rmonth = month;
         rmonth2 = month2;

         rmonth++;
         rmonth2++;

         date1 = rmonth + "/" + day + "/" + year;
         date2 = rmonth2 + "/" + day2 + "/" + year2;

         if (!ValidateDate(date1, "u", "f"))
         {
            alert( "Start date is Invalid.");
            return false;
         }

         if (!ValidateDate(date2, "u", "f"))
         {
            alert( "End date is Invalid.");
            return false;
         }


         startDate.setDate(day);
         startDate.setMonth(month);
         startDate.setFullYear(year);
         startDate.setHours(0);
         startDate.setMinutes(0);
         startDate.setSeconds(0);
         startDate.setMilliseconds(0);

         endDate.setDate(day2);
         endDate.setMonth(month2);
         endDate.setFullYear(year2);
         endDate.setHours(0);
         endDate.setMinutes(0);
         endDate.setSeconds(0);
         endDate.setMilliseconds(0);

         if (startDate > endDate)
         {
            alert( "start Date cannot be greater than End Date." );
            return false;
         }

/*
         if ( startDate == endDate )
         {
            if( document.getElementById( "time" ).selectedIndex >= document.getElementById( "time2" ).selectedIndex )
            {
               //alert( "Start Time cannot be greater or igual to the End Time." );
               alert( "End Date cannot be earlier than Start Date." );

               return false;
            }
         }
*/
      }

      return true;
   }





   function ValidateForm()
   { 
/*
      if ( !ValidateField( "location", 7, "N", "Y", "Location" ) )
      {
         return false;
      }
*/
      if ( !ValidateField( "fname", 0, "", "", "First Name" ) )
      {
         return false;
      }

      if ( !ValidateField( "lname", 0, "", "", "Last Name" ) )
      {
         return false;
      }

      if ( !ValidatePhones( ) )
      {
         return false;
      }

      if ( !ValidateField( "email", 0, "", "", "Email" ) )
      {
         return false;
      }

      if ( !ValidateEmail() )
      {
         return false;
      }

      if ( !ValidateField( "prefContact", 0, "", "Y", "Preference Contact" ) )
      {
         return false;
      }

      if ( !CheckDates( ) )
      {
         return false;
      }

      CreateCookies();

      //alert( "Good" );
      return true;
   }

   function CreateCookies()
   {
      if( document.form.fname.value != "" && document.form.fname.value != null )
         SetCookie( "rf_fname", document.form.fname.value );

      if( document.form.mname.value != "" && document.form.mname.value != null )
         SetCookie( "rf_mname", document.form.mname.value );

      if( document.form.lname.value != "" && document.form.lname.value != null )
         SetCookie( "rf_lname", document.form.lname.value );

      if( document.form.hphone.value != "" && document.form.hphone.value != null )
         SetCookie( "rf_hphone", document.form.hphone.value );

      if( document.form.wphone.value != "" && document.form.wphone.value != null )
         SetCookie( "rf_wphone", document.form.wphone.value );

      if( document.form.mphone.value != "" && document.form.mphone.value != null )
         SetCookie( "rf_mphone", document.form.mphone.value );

      if( document.form.email.value != "" && document.form.email.value != null )
         SetCookie( "rf_email", document.form.email.value );

      if( form.prefContact.options[ form.prefContact.selectedIndex ].text != "" && form.prefContact.options[ form.prefContact.selectedIndex ].text != null )
         SetCookie( "rf_prefContact", form.prefContact.options[ form.prefContact.selectedIndex ].text );
   }

   function SetCookie( name, data )
   {
      var expires = new Date();
      expires.setTime( expires.getTime() + ( 1000 * 60 * 60 * 24 * 30 * 12 * 5 ) );
      document.cookie = name + "=" + escape( data ) + "; path=/" + (( expires == null ) ? "" : "; expires=" + expires.toGMTString() );
   }

   function GetCookie(name)
   {
      var dc = document.cookie;
      var cname = name + "=";

      if ( dc.length > 0 )
      {
         var begin = dc.indexOf( cname );
         if ( begin != -1 )
         {
            begin += cname.length;
            var end  = dc.indexOf( ";", begin );
            if ( end == -1 )
            {
               end = dc.length;
            }
            //alert( unescape( dc.substring( begin, end ) ) );
            return unescape( dc.substring( begin, end ) );
         }
      }
      return "";
   }

   function DelCookie(name)
   {
      document.cookie = name + "=; expires=Thu, 01-Jan-76 00:00:01 GMT" + "; path=/";
   }

   function RetrieveCookies()
   {
      var fname = GetCookie( "rf_fname" );
      var mname = GetCookie( "rf_mname" );
      var lname = GetCookie( "rf_lname" );
      var hphone = GetCookie( "rf_hphone" );
      var wphone = GetCookie( "rf_wphone" );
      var mphone = GetCookie( "rf_mphone" );
      var email = GetCookie( "rf_email" );
      var prefContact = GetCookie( "rf_prefContact" );

      if ( fname != "" && fname != null )
      {
         document.form.fname.value = fname;
      }

      if ( mname != "" && mname != null )
      {
         document.form.mname.value = mname;
      }

      if ( lname != "" && lname != null )
      {
         document.form.lname.value = lname;
      }

      if ( hphone != "" && hphone != null )
      {
         document.form.hphone.value = hphone;
      }

      if ( wphone != "" && wphone != null )
      {
         document.form.wphone.value = wphone;
      }

      if ( mphone != "" && mphone != null )
      {
         document.form.mphone.value = mphone;
      }

      if ( email != "" && email != null )
      {
         document.form.email.value = email;
      }

      if ( prefContact != "" && prefContact != null )
      {
         var data = "";
         var i = 0;
         var items = document.form.prefContact.length;
         for( i=0; i<items; i++)
         {
            data = document.form.prefContact.options[ i ].text;
            if ( prefContact == data)
            {
               document.form.prefContact.selectedIndex = i;
            }
         }
      }
   }




// Date Validation Javascript
// Date Validation Javascript

function valDateFmt(datefmt) 
{
   myOption = -1;

   for (i=0; i<datefmt.length; i++) 
   {
      if (datefmt[i].checked) 
      {
         myOption = i;
      }
   }
   if (myOption == -1) 
   {
      alert("You must select a date format");
      return ' ';
   }
   return datefmt[myOption].value;
}

function valDateRng(daterng) 
{
   myOption = -1;
   for (i=0; i<daterng.length; i++) 
   {
      if (daterng[i].checked) 
      {
         myOption = i;
      }
   }
   if (myOption == -1) 
   {
      alert("You must select a date range");
      return ' ';
   }
   return daterng[myOption].value;
}

function stripBlanks(fld) 
{
   var result = "";
   for (i=0; i<fld.length; i++) 
   {
      if (fld.charAt(i) != " " || c > 0) 
      {
         result += fld.charAt(i);
         if (fld.charAt(i) != " ") 
            c = result.length;
      }
   }
   return result.substr(0,c);
}

var numb = '0123456789';
function isValid(parm,val) 
{
   if (parm == "") 
      return true;
   for (i=0; i<parm.length; i++) 
   {
      if (val.indexOf(parm.charAt(i),0) == -1)
         return false;
   }
   return true;
}

function isNum(parm) 
{
   return isValid(parm,numb);
}

var mth = new Array(' ','january','february','march','april','may','june','july','august','september','october','november','december');
var day = new Array(31,28,31,30,31,30,31,31,30,31,30,31);


function ValidateDate(fld,fmt,rng) 
{

   //This function is to validate a date and it takes in 3 parameters.
   //parameter 1: Is the date value.
   //parameter 2: Is the format of the date which could be MDY = 'u' or YMD = 'j' or DMY = 'w'
   //parameter 3: It specify if the date is pass = 'p' or future = 'f' or any = 'a'

   var dd, mm, yy;
   var today = new Date;
   var t = new Date;
   fld = stripBlanks(fld);
   if (fld == '') 
      return false;

   var d1 = fld.split('\/');
   if (d1.length != 3) 
      d1 = fld.split(' ');

   if (d1.length != 3) 
      return false;

   if (fmt == 'u' || fmt == 'U') 
   {
      dd = d1[1]; 
      mm = d1[0]; 
      yy = d1[2];
   }
   else if (fmt == 'j' || fmt == 'J') 
   {
      dd = d1[2]; 
      mm = d1[1]; 
      yy = d1[0];
   }
   else if (fmt == 'w' || fmt == 'W')
   {
      dd = d1[0]; 
      mm = d1[1]; 
      yy = d1[2];
   }
   else 
      return false;

   var n = dd.lastIndexOf('st');
   if (n > -1) 
      dd = dd.substr(0,n);

   n = dd.lastIndexOf('nd');
   if (n > -1) 
      dd = dd.substr(0,n);

   n = dd.lastIndexOf('rd');
   if (n > -1) 
      dd = dd.substr(0,n);

   n = dd.lastIndexOf('th');
   if (n > -1) 
      dd = dd.substr(0,n);

   n = dd.lastIndexOf(',');
   if (n > -1) 
      dd = dd.substr(0,n);

   n = mm.lastIndexOf(',');
   if (n > -1) 
      mm = mm.substr(0,n);

   if (!isNum(dd)) 
      return false;

   if (!isNum(yy)) 
      return false;

   if (!isNum(mm)) 
   {
      var nn = mm.toLowerCase();
      for (var i=1; i < 13; i++) 
      {
         if (nn == mth[i] || nn == mth[i].substr(0,3)) 
         {
            mm = i; 
            i = 13;
         }
      }
   }

   if (!isNum(mm)) 
      return false;

   dd = parseFloat(dd); 
   mm = parseFloat(mm); 
   yy = parseFloat(yy);

   if (yy < 100) 
      yy += 2000;

   if (yy < 1582 || yy > 4881) 
      return false;

   if (mm == 2 && (yy%400 == 0 || (yy%4 == 0 && yy%100 != 0))) 
      day[mm-1]++;

   if (mm < 1 || mm > 12)
      return false;

   if (dd < 1 || dd > day[mm-1]) 
      return false;

   t.setDate(dd); 
   t.setMonth(mm-1); 
   t.setFullYear(yy);

   if (rng == 'p' || rng == 'P') 
   {
      if (t > today) 
         return false;
   }
   else if (rng == 'f' || rng == 'F') 
   {
      if (t < today) 
         return false;
   }
   else if (rng != 'a' && rng != 'A') 
      return false;

   return true;
}