onerror=handleErr;
function handleErr(msg,url,l) { 
    //Handle the error here:
    //If return false, the browser displays the standard error message in the JavaScript console.
    //If return true, the browser does not display the standard error message.
    return false;
}

function checkForm() {
    
    var error_check = false;
    var error_color = "#ff0000";
    var today = new Date();
    var error_string = "";
    // controls for optional fields
    
    if (document.form.children.value == "") {    // this part of the script is
        document.form.children.value = "0"; }   // related to the 'children' field
    
    if (document.form.infants.value == "") {    // this part of the script is
        document.form.infants.value = "0"; }   // related to the 'infants' field
        
    if (document.form.address.value == "") {    // this part of the script is
        document.form.address.value = "*"; }   // related to the 'address' field

    if (document.form.city.value == "") {    // this part of the script is
        document.form.city.value = "*"; }   // related to the 'city' field
    
    if (document.form.state.value == "") {    // this part of the script is
        document.form.state.value = "*"; }   // related to the 'state' field
    
    if (document.form.zip.value == "") {    // this part of the script is
        document.form.zip.value = "*"; }   // related to the 'zip' field

    fax = document.form.fax.value;                      // this part of the script is
    if (!fax == "") { 
          if (!checkTel(fax)) {
              changeBorder('telephone','thin solid '+error_color);
            error_check = true;
        }
    }
    
    notes = document.form.notes.value;                        // this part of the script is
    if (notes == "") { notes = "-";                            // related to the 'notes' field
        document.form.fax.notes = "-";
    } else if (notes.length > 250) {                        //  the code prevents users from typing in more than 250 characters in the
        changeBorder('notes','thin solid '+error_color);    //  notes field. The # can be modified by typing in any number instead of 250. 
        error_check = true;                                    //  If this feature is not needed just delete the 2 lines in the code above.
        }
        
    // now check the remaining fields (they MUST be filled!)
    
    if (!document.form.treatment.value) {                // this part of the script is
        changeBorder('treatment','thin solid '+error_color);
        error_check = true; //error_string += 'Specificare il trattamento\n\n';            // related to the 'treatment' field
        }
    
    username = document.form.username.value;            // this part of the script is
    if (!username) {                                    // related to the 'username' field
        changeBorder('username','thin solid '+error_color);
        error_check = true;
        }
    
    surname = document.form.surname.value;                // this part of the script is
    if (!surname) {                                       // related to the 'surname' field
        changeBorder('surname','thin solid '+error_color);
        error_check = true;
        }

    address = document.form.address.value;                // this part of the script is
    if (!address) {                                       // related to the 'address' field
        changeBorder('address','thin solid '+error_color);
        error_check = true;
        }

    city = document.form.city.value;                    // this part of the script is
    if (!city) {                                        // related to the 'city' field
        changeBorder('city','thin solid '+error_color);
        error_check = true;
        }
    

    state = document.form.state.value;                    // this part of the script is
    if (!state) {                                         // related to the 'state' field
        changeBorder('state','thin solid '+error_color);
        error_check = true;
        }
    
    zip = document.form.zip.value;                        // this part of the script is
    if (!zip) {                                           // related to the 'zip' field
        changeBorder('zip','thin solid '+error_color);
        error_check = true;
        }


    arrival = document.form.date_arrival.value;

    departure = document.form.date_departure.value;        // this part of the script is
    if (departure <= arrival) {                            // related to the 'departure' field
        changeBorder('date_departure_Month_ID','thin solid '+error_color);
        changeBorder('date_departure_Day_ID','thin solid '+error_color);
        changeBorder('date_departure_Year_ID','thin solid '+error_color);
        error_check = true;
        }
            
    adults = document.form.adults.value;                    // this part of the script is
    if (!checkNum(adults)) {                                // related to the 'email' field
        changeBorder('adults','thin solid '+error_color);
        error_check = true;
        }
        
    telephone = document.form.telephone.value;            // this part of the script is
    if (!checkTel(telephone) || telephone == "" ) {        // related to the 'telephone' field
        changeBorder('telephone','thin solid '+error_color);
        error_check = true;
        }   
    
    email = document.form.email.value;                        // this part of the script is
    if (!checkMail(email)) {                                // related to the 'email' field
        changeBorder('email','thin solid '+error_color);
        error_check = true;
        } 

    //document.form.AppName.value = navigator.appName;
    //document.form.AppVersion.value = navigator.appVersion;
    //document.form.Resolution.value = window.screen.width + " X " + window.screen.height
    if (error_check == true) {
        error_string +='Si prega di correggere i campi evidenziati in rosso\n\n';   
    }
    if (error_string !='') {
       alert(error_string);
       return false;  // if the form contains error/s then back!
       }
    window.setTimeout("history.back(-1)",2000);
    return true;

}  // end of function
    
function checkMail(str){
    var emailExp=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
    var returnval=emailExp.test(str);
    return returnval;
}

function checkTel(num){
    var telExp=/^(\+|\d)?[ 0-9\.\-]*$/i;
    var returnval=telExp.test(num);
    return returnval
}

function checkNum(num){
    var numExp=/[0-9]+$/i;
    var returnval=numExp.test(num);
    return returnval
}

function showVals(){
    theForm = document.getElementById('form');
    theStr = '';
    for (i=0; i < theForm.elements.length; i++) {
        ele = theForm.elements[i];
        theStr += ele.name + ' : ' + ele.value + "\n";
    }
    alert (theStr);
}
