/*
name: validateReservation.js
version: 0.1.1
author: Francisco
date: 8/18/09
last update: 8/24/09
description: 
*/



function validatereservation()
{
        //this array contains the names of the fields
        var myArray = new Array("fname", "lname", "email", "phone", "timehour", "timeminutes", "am", "pm", "pupadd", "pupcity", "pupzip",
                                "pupdatemonth", "pupdateday", "pupdateyear", "numPassengers", "desadd", "descity", "deszip");

        //this array contains the messeages
        var arrValues = new Array("first name", "last name", "email", "phone number", "time hour", "time minutes", "am or pm", "am or pm", 
                                  "pick up address", "pick up city", "pick up zip", "month of pick up", "day of pick up", "year of pick up",
                                  "number of passengers", "destination address", "destination city", "destination zip");

        //check if human
        humanUser("human");

        var pIMsg = document.getElementById("pIMsg");

        //alert(myArray.length);
        for(i = 0; i <= myArray.length; i++)
        {
           //alert(myArray[i]);
           var myVar = document.getElementById(myArray[i]);
           //alert(myVar.value);
           //check for empty values
           if(myVar.value == "")
           {    
                pIMsg.innerHTML = "Insert " + arrValues[i];
                alert("Insert " + arrValues[i]);
                myVar.focus();
                return false;
           }
           //the values on radio button will never be empty
           else if(((myVar.value == "am") || (myVar.value == "pm"))  &&  ((myArray[i] == "am") || (myArray[i] == "pm")))
           {    
                if ((document.reservationform.pmoram[0].checked!=true) && (document.reservationform.pmoram[1].checked!=true))
                {
                    alert("please select pm or am");
                    pIMsg.innerHTML = "Select am or pm";
                    return false;
                }
           }
           //check for valid email
           if(myArray[i] == "email")
           {    //alert(myVar.value.indexOf("@"));
                if(myVar.value.indexOf("@") == "-1")
                {
                     alert("missing @ in the email");
                     pIMsg.innerHTML = "missing @ in the email";
                     return false;
                }
                //check for the dot
                if(myVar.value.indexOf(".") == "-1")
		{
		     alert("missing . in the email");
                     pIMsg.innerHTML = "missing . in the email";
                     return false;
		}
           }
           //check for valid phone number
           if (myArray[i] == "phone")
           {
                if(isNaN(myVar.value))
                {
                     alert("enter a valid phone number");
                     pIMsg.innerHTML = "enter a valid phone number";
                     return false;
                }
                //alert("length : " + myVar.value.length);
                if(myVar.value.length != 10)
                {
                     alert("make sure the phone number is 10 characters long");
                     pIMsg.innerHTML = "make sure the phone number is 10 characters long";
                     return false;
                }
           }
           //check for valid zip codes
           if (myArray[i] == "pupzip" || myArray[i] == "deszip")
           {
                if(isNaN(myVar.value))
                {
                     alert("enter a valid zip code");
                     pIMsg.innerHTML = "enter a valid zip code";                     
                     return false;
                }
                //alert("length : " + myVar.value.length);
                if(myVar.value.length != 5)
                {
                     alert("make sure the zip code is 5 characters long");
                     pIMsg.innerHTML = "make sure the zip code is 5 characters long";
                     return false;
                }
           }
           //check for valid number of passengers
           if (myArray[i] == "numPassengers")
           {
           	if(isNaN(myVar.value))
                {
                     alert("enter a valid number of passengers");
                     pIMsg.innerHTML = "enter a valid number of passengers";                     
                     return false;
                }
           }
   
        }
        
}

//this function checks if the user is human or a script
function humanUser(fieldID)
{
	var humanInput = "no";
        humanInput = document.getElementById(fieldID).value;
	if(humanInput == "")
        {
           //alert("human");
           //do nothing, human user
        }
        else
        {
	   return false;
        }
}




