// Copyright © 2001 by Apple Computer, Inc., All Rights Reserved.
//
// You may incorporate this Apple sample code into your own code
// without restriction. This Apple sample code has been provided "AS IS"
// and the responsibility for its operation is yours. You may redistribute
// this code, but you are not permitted to redistribute it as
// "Apple sample code" after having made changes.

// email

var nbsp = 160;		// non-breaking space char
var node_text = 3;	// DOM text node-type
var emptyString = /^\s*$/ ;
var global_valfield;	// retain valfield for timer thread
var elemento_tofocus;

function setLayError (element, bool, elements_notCorrect)
{
	if( !bool )
	{
		element.className = "error";
		if(!elemento_tofocus)
			elemento_tofocus = element;
	}	
	else
	{
		element.className = "no-error";
	}	
}

function checkWholeForm(theForm) {
	var correct = true;
	elemento_tofocus = null;
   
     //NAME
    setLayError( theForm.name, checkText ( trim( theForm.name.value ) ) );
    correct = correct && checkText ( trim( theForm.name.value ) );
    //SURNAME
    setLayError( theForm.surname, checkText ( trim( theForm.surname.value ) ) );
    correct = correct && checkText ( trim( theForm.surname.value ) );
    //COUNTRY
    setLayError( theForm.country, checkText ( trim( theForm.country.value ) ) );
    correct = correct && checkText ( trim( theForm.country.value ) );
     //EMAIL
    setLayError( theForm.email, checkEmail( trim( theForm.email.value ) ) );
    correct = correct && checkEmail( trim( theForm.email.value ) );
    
    //ARRIVAL DATE
    setLayError( theForm.arrivald, checkDropdown( theForm.arrivald.selectedIndex ) )
    setLayError( theForm.arrivalm, checkDropdown( theForm.arrivalm.selectedIndex ) )
    setLayError( theForm.arrivaly, checkDropdown( theForm.arrivaly.selectedIndex ) )
    
    correct = correct && checkDropdown( theForm.arrivald.selectedIndex )
    correct = correct && checkDropdown( theForm.arrivalm.selectedIndex )
    correct = correct && checkDropdown( theForm.arrivaly.selectedIndex )
    
    //DEPARTURE DATE
    setLayError( theForm.departured, checkDropdown( theForm.departured.selectedIndex ) )
    setLayError( theForm.departurem, checkDropdown( theForm.departurem.selectedIndex ) )
    setLayError( theForm.departurey, checkDropdown( theForm.departurey.selectedIndex ) )
    
    correct = correct && checkDropdown( theForm.departured.selectedIndex )
    correct = correct && checkDropdown( theForm.departurem.selectedIndex )
    correct = correct && checkDropdown( theForm.departurey.selectedIndex )
    
    //ACCOMODATION
    setLayError( theForm.accomodation, checkDropdown( theForm.accomodation.selectedIndex ) )
    correct = correct && checkDropdown( theForm.accomodation.selectedIndex )
        
    /*
    why += checkUsername(theForm.username.value);
    why += isEmpty(theForm.notempty.value);
    why += isDifferent(theForm.different.value);
    for (i=0, n=theForm.radios.length; i<n; i++) {
        if (theForm.radios[i].checked) {
            var checkvalue = theForm.radios[i].value;
            break;
        } 
    }
    why += checkRadio(checkvalue);
    why += checkDropdown(theForm.choose.selectedIndex);
    */
    if (!correct) {
       alert('Ti preghiamo di revisionare i campi indicati in rosso.');
       elemento_tofocus.focus();
       return false;
    }
    
	return true;
}

function trim(str)
{
	return str.replace(/^\s+|\s+$/g, '');
}

// --------------------------------------------
// setfocus
// Delayed focus setting to get around IE bug
// --------------------------------------------

function setFocusDelayed()
{
 global_valfield.focus();
}

function setfocus(valfield)
{
 // save valfield in global variable so value retained when routine exits
 global_valfield = valfield;
 setTimeout( 'setFocusDelayed()', 100 );
}


function checkEmail (strng) {
var error = true;
if (strng == "") {
   error = false;
}

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       error = false;
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          error = false;
       }
    }
return error;    
}


// phone number - strip out delimiters and check for 10 digits

function checkPhone (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a phone number.\n";
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The phone number contains illegal characters.";
  
    }
    if (!(stripped.length == 10)) {
	error = "The phone number is the wrong length. Make sure you included an area code.\n";
    } 
return error;
}


// password - between 6-8 chars, uppercase, lowercase, and numeral

function checkPassword (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a password.\n";
}

    var illegalChars = /[\W_]/; // allow only letters and numbers
    
    if ((strng.length < 6) || (strng.length > 8)) {
       error = "The password is the wrong length.\n";
    }
    else if (illegalChars.test(strng)) {
      error = "The password contains illegal characters.\n";
    } 
    else if (!((strng.search(/(a-z)+/)) && (strng.search(/(A-Z)+/)) && (strng.search(/(0-9)+/)))) {
       error = "The password must contain at least one uppercase letter, one lowercase letter, and one numeral.\n";
    }  
return error;    
}    


// username - 4-10 chars, uc, lc, and underscore only.

function checkUsername (strng) {
var error = true;
if (strng == "") {
   error = false;
}


    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if ((strng.length < 4) || (strng.length > 10)) {
       error = false;
    }
    else if (illegalChars.test(strng)) {
    error = false;
    } 
return error;
}       

function checkText (strng) {
var error = true;
	if (strng == "") {
   		error = false;
	}

    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if ((strng.length < 2)) {
       error = false;
    }
    else if (illegalChars.test(strng)) {
    	error = false;
    } 
return error;
}       


// non-empty textbox

function isEmpty(strng) {
var error = false;
  if (strng.length == 0) {
     error = true
  }
return error;	  
}

// was textbox altered

function isDifferent(strng) {
var error = ""; 
  if (strng != "Can\'t touch this!") {
     error = "You altered the inviolate text area.\n";
  }
return error;
}

// exactly one radio button is chosen

function checkRadio(checkvalue) {
var error = true;
   if (!(checkvalue)) {
       error = false;
    }
return error;
}

// valid selector from dropdown list

function checkDropdown(choice) {
var error = true;
    if (choice == '00') {
    error = false;
    }    
return error;
}    


