// JavaScript Document
 function check_email2(email_id,err_id){
	// onsubmit="return check_email2('Email','err_msg')"

            emailRegExp = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.([a-z]){2,4})$/;
            var err_mail='The email address entered has an incorect format.';
			//alert('check email' + email_id.value);
            if(emailRegExp.test(document.getElementById(email_id).value)){
               // alert('true');
                return true;
            }else{
				 alert('check email error');
               // document.getElementById(err_id).innerHTML=err_mail;
				
                alert(err_mail);
                return false;
            }
        }
function check_Phones(f){
    if(f.Home_Phone.value.length < 1 && f.Work_Phone.value.length < 1 && f.Cell_Phone.value.length < 1){
  
        //return false;
		return (false);
     }
     return (true);
}
function check_email(e) {
    //make sure there are no erroneous characters
	if(e.length > 0){
     ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
	 
       for(i=0; i < e.length ;i++){
         if(ok.indexOf(e.charAt(i))<0){ 
            return (false);
		 }
		 //make sure there is an @ and . 
	//	 alert("@ " + e.indexOf("@"));
	//	 	 alert(". " + e.indexOf("."));
  	//  if (e.indexOf("@")== -1 ||  e.indexOf(".")== -1){
	 //    return (false);
	  // }
     }	
	
  //return (true);
    //verify format
     if (document.images) {
        re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
        re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		//alert("e.match(re) " + e.match(re));
		//alert("e.match(re_two) " + e.match(re_two));
        if (!e.match(re) && e.match(re_two)) {
		//	alert("out ");
           return (true);		
        } 
		 if (e.match(re) == null && e.match(re_two)==null) {
		//	alert("out2 ");
           return (false);		
        } 
      }
	}
	return (true);
}

function check_form(f) { // f is the form (passed using the this keyword)
//onSubmit="return check_form(this);"
	 var ErrorMsg="Please enter a value for the following fields: \n";
	 var NoError=new Boolean(true);

//document.write("Form name: ");
//document.write(f.name);
     if(f.First_Name.value.length < 1){
   //     alert(ErrorMsg +  "first name field \n");
		ErrorMsg = ErrorMsg +  " First name \n";
	//	alert(ErrorMsg);
          f.First_Name.focus(); // put the prompt in the name field 
         // if the browser is Netscape 6 or IE
      //    if(document.all || document.getElementByID){
             // change the color of text field
             f.First_Name.style.background = "yellow";
       //   }
           // make sure the form is not submitted
        //return false;
		NoError=false;
     }
	
     if(f.Last_Name.value.length < 1){
        ErrorMsg = ErrorMsg +  " Last name \n";
          f.Last_Name.focus(); // put the prompt in the name field 
         // if the browser is Netscape 6 or IE
        //  if(document.all || document.getElementByID){
             // change the color of text field
             f.Last_Name.style.background = "yellow";
         // }
           // make sure the form is not submitted
         NoError=false;
     }
	   if(f.Birthdate.value.length < 1){
         ErrorMsg = ErrorMsg +  "Birthdate \n";
          f.Birthdate.focus(); // put the prompt in the name field 
         // if the browser is Netscape 6 or IE
         // if(document.all || document.getElementByID){
             // change the color of text field
             f.Birthdate.style.background = "yellow";
          //}
           // make sure the form is not submitted
       NoError=false;
     }
	 if(f.Address.value.length < 1){
         ErrorMsg = ErrorMsg +  "Street Address\n";
          f.Address.focus(); // put the prompt in the name field 
         // if the browser is Netscape 6 or IE
    //      if(document.all || document.getElementByID){
             // change the color of text field
             f.Address.style.background = "yellow";
     //     }
           // make sure the form is not submitted
        NoError=false;
     }
	 if(f.City.value.length < 1){
         ErrorMsg = ErrorMsg +  "City\n";
          f.City.focus(); // put the prompt in the name field 
         // if the browser is Netscape 6 or IE
       //   if(document.all || document.getElementByID){
             // change the color of text field
             f.City.style.background = "yellow";
         // }
           // make sure the form is not submitted
        NoError=false;
     }
    
	 // make sure there is a phone number )
     if(!check_Phones(f)){
         ErrorMsg = ErrorMsg + "At least one Phone number\n";
        f.Home_Phone.focus(); 
        // if the browser is Netscape 6 or IE
  //     if(document.all || document.getElementByID){
       // change the color of text field
	      f.Home_Phone.style.background = "yellow";
    //   }
       // make sure the form is not submitted
      NoError=false;
    }

 // check the first email address ( the exclamation means "not" )
     if(!check_email(f.Email.value)){
         ErrorMsg = ErrorMsg +  "\n Invalid email detected.";
        f.Email.focus(); 
        // if the browser is Netscape 6 or IE
    //  if(document.all || document.getElementByID){
       // change the color of text field
          f.Email.style.background = "yellow";
     //  }
       // make sure the form is not submitted
      NoError=false;
    }
 
     if(loopSelected('txtSelectedVolunteerValues','selVolunteer') < 1){
         ErrorMsg = ErrorMsg +  "Volunteer interest(s)\n";
          f.selVolunteer.focus(); // put the prompt in the name field 
          f.selVolunteer.style.background = "yellow";
         // }
           // make sure the form is not submitted
        NoError=false;
     }
	//loopSelected('txtSelectedContactValues','selContact');
	//verify that if the contact method is prefrred, it has a value
     if(loopSelected('txtSelectedContactValues','selContact') < 1){
         ErrorMsg = ErrorMsg +  "Best way(s) to contact me\n";
          f.selVolunteer.focus(); // put the prompt in the name field 
          f.selVolunteer.style.background = "yellow";
         // }
           // make sure the form is not submitted
        NoError=false;
     }
	//loopSelected('txtSelectedContactValues','selContact');

	if(!verifyContactVals(f.txtSelectedContactValues.value,'Home',f.Home_Phone)){
	   ErrorMsg = ErrorMsg +  "Home Phone Number has been chosen as preferred contact but is empty\n";
	   f.selContact.focus(); // put the prompt in the name field 
       f.selContact.style.background = "yellow";
       NoError=false;
    }
	
	if(!verifyContactVals(f.txtSelectedContactValues.value,'Work',f.Work_Phone)){
       ErrorMsg = ErrorMsg +  "Work Phone Number has been chosen as preferred contact but is empty\n";
	   f.selContact.focus(); // put the prompt in the name field 
       f.selContact.style.background = "yellow";
       NoError=false;
    }
	if(!verifyContactVals(f.txtSelectedContactValues.value,"Cell",f.Cell_Phone)){
       ErrorMsg = ErrorMsg +  "Cell Phone Number has been chosen as preferred contact but is empty\n";
	   f.selContact.focus(); // put the prompt in the name field 
       f.selContact.style.background = "yellow";
       NoError=false;
    }

	if(!verifyContactVals(f.txtSelectedContactValues.value,'Email',f.Email)){
       ErrorMsg = ErrorMsg +  "Email has been chosen as preferred contact but is empty\n";
	   f.selContact.focus(); // put the prompt in the name field 
       f.selContact.style.background = "yellow";
       NoError=false;
    }

  //  if(f.Home_Phone.value.length < 1 && f.Work_Phone.value.length < 1 && f.Cell_Phone.value.length < 1){

	
	if (!NoError){
       alert(ErrorMsg );
	}

    return NoError;
}

function verifyContactVals(txtElementById,value,formName){
    var NoError=new Boolean(true);
	if(txtElementById.search(value) > -1) {
	   if(formName.value.length < 1) {
			 formName.focus(); 
   	         formName.style.background = "yellow";
			 NoError=false;
	   }
	}
    return NoError;
}

function verifyContactVals2(txtElementById,f){
    var NoError=new Boolean(true);

	if(txtElementById.search("Home") > 0) {
	   if(f.Home_Phone.value.length < 1) {
		     ErrorMsg = ErrorMsg +  "Home Phone Number has been chosen as preferred contact but is empty\n";
			 f.Home_Phone.focus(); 
   	         f.Home_Phone.style.background = "yellow";
			 NoError=false;
	   }
	}
	if(txtElementById.search("Work") > 0) {
	   if(f.Work_Phone.value.length < 1) {
		     ErrorMsg = ErrorMsg +  "Work Phone Number has been chosen as preferred contact but is empty\n";
			 f.Work_Phone.focus(); 
   	         f.Work_Phone.style.background = "yellow";
	         f.selVolunteer.focus(); // put the prompt in the name field 
             f.selVolunteer.style.background = "yellow";
			 NoError=false;
	   }
	} 
	if(f.txtSelectedContactValues.value.search(/Cell/) > -1) {
	//	alert("f.Cell_Phone.value.length " + f.Cell_Phone.value.length);

	   if(f.Cell_Phone.value.length < 1) {
		     ErrorMsg = ErrorMsg +  "Cell Phone Number has been chosen as preferred contact but is empty\n";
		//	 alert("ErrorMsg " + ErrorMsg);
			 f.Cell_Phone.focus(); 
   	         f.Cell_Phone.style.background = "yellow";
	         f.selVolunteer.focus(); // put the prompt in the name field 
             f.selVolunteer.style.background = "yellow";
			 NoError=false;
	   }
	}
    if(txtElementById.search("Email") > 0) {
	   if(f.Email.value.length < 1) {
		     ErrorMsg = ErrorMsg +  "Email has been chosen as preferred contact but is empty\n";
			 f.Email.focus(); 
   	         f.Email.style.background = "yellow";
	         f.selVolunteer.focus(); // put the prompt in the name field 
             f.selVolunteer.style.background = "yellow";
			 NoError=false;
	   }
	}
    return NoError;
}
function loopSelected(txtElementById,selElementById)
{
	var txtSelectedValuesObj = document.getElementById(txtElementById);
	var selectedArray = new Array();
	var selObj = document.getElementById(selElementById);
	var i;
	var count = 0;
	for (i=0; i<selObj.options.length; i++) {
		if (selObj.options[i].selected) {
			selectedArray[count] = selObj.options[i].value;
			count++;
		}
	}
	txtSelectedValuesObj.value = selectedArray;
	return count;
}

function check_TRform(f) { // f is the form (passed using the this keyword)
//onSubmit="return check_form(this);"
	 var ErrorMsg="Please enter a correct value for the following fields: \n";
	 var NoError=new Boolean(true);
     var temp =new Boolean(true);
    //	alert("call daysuntil2");
     var rc = 0;
	 rc = DaysUntil2(f.Date_of_Trip.value);	
	 if(rc > 0) {
 	   if (rc == 1){
 	      ErrorMsg = ErrorMsg + " Invalid date: " + f.Date_of_Trip.value + "\n";
          NoError=false;
       } else {
	 	 //don't mark this as unsubmittable but give a warning
         var ErrorMsg2 = "Warning: Ride requests must be made at least three business days in advance.\n";
         f.Date_of_Trip.focus(); 
        // if the browser is Netscape 6 or IE
  //     if(document.all || document.getElementByID){
       // change the color of text field
	     f.Date_of_Trip.style.background = "yellow";
    //   }
       // make sure the form is not submitted
      //NoError=false;
	     alert(ErrorMsg2 );
	   }
    }
    //Check the inputs to the form are not blank 
      // var x=document.getElementById("myForm");
     for (var i=0;i<f.length;i++)  {
        if (f.elements[i].type == "text" || f.elements[i].type == "textarea" ){
           if (f.elements[i].value.length < 1 && f.elements[i].name != "Notes" && f.elements[i].name != "Ride_to_Zip_Code"){
              NoError=false;
              ErrorMsg = ErrorMsg +  " " +  f.elements[i].name + "\n";
              f.elements[i].focus(); 
              f.elements[i].style.background = "yellow";
           }
        }
           //document.write(f.elements[i].name);
           //document.write(f.elements[i].value);
           //document.write(f.elements[i].type);
           //document.write("<br />");
     }

    if (!NoError){
       alert(ErrorMsg );
	}
    return NoError;
}
function check_Date(f){
	var temp=f.Date_of_Trip.value;
	var day;
	var month;
	var Sp3=exDateTime.indexOf("/",0);
	var Sp4=exDateTime.indexOf("/",Sp1+1);
	//alert("1");
    if (Sp3 >=0 || Sp4 >=0){
		exDateTime.replace("/","-");
	}
	//	alert("2");
    var Sp1=exDateTime.indexOf("-",0)
	var Sp2=exDateTime.indexOf("-",Sp1+1)
  //  alert(" " + Sp2);
    if(f.Date_of_Trip.length < 10) {
		if (Sp2 < 7) {
		  month="0"+temp.substring(5,7);
		  if (f.Date_of_Trip.length == 8)
		    day="0"+temp.substring(7);
       	// f.Date_of_Trip.value=temp.substring(0,5)+"0"+temp.substring(6,8)
        //return false;
	//	alert("mmdd " +month+day );
		return (false);
		}
     }
     return (true);
}
function change_cursor() { // f is the form (passed using the this keyword)
  alert("change cursor");
//document.body.style.cursor = 'hand';
var cursor = 
     document.layers ? document.cursor :
     document.all ? document.all.cursor :
     document.getElementById ? document.getElementById('cursor') : null;

}