// ----------------------------------------------------------------------
// Javascript form validation routines.
// Author: Stephen Poley
//
// Simple routines to quickly pick up obvious typos.
// All validation routines return true if executed by an older browser:
// in this case validation must be left to the server.
//
// Update Jun 2005: discovered that reason IE wasn't setting focus was
// due to an IE timing bug. Added 0.1 sec delay to fix.
//
// Update Oct 2005: minor tidy-up: unused parameter removed
//
// Update Jun 2006: minor improvements to variable names and layout
// ----------------------------------------------------------------------

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




// --------------------------------------------
//                  trim
// Trim leading/trailing whitespace off string
// --------------------------------------------

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 );
}


// --------------------------------------------
//                  msg
// Display warn/error message in HTML element.
// commonCheck routine must have previously been called
// --------------------------------------------

function msg(fld,     // id of element to display message in
msgtype, // class to give element ("warn" or "error")
message) // string to display
{
    // setting an empty string can give problems if later set to a 
    // non-empty string, so ensure a space present. (For Mozilla and Opera one could 
    // simply use a space, but IE demands something more, like a non-breaking space.)
    var dispmessage;
    if (emptyString.test(message)) 
        dispmessage = String.fromCharCode(nbsp);    
    else  
        dispmessage = message;
    
    var elem = document.getElementById(fld);
    elem.firstChild.nodeValue = dispmessage;  
    
    elem.className = msgtype;   // set the CSS class to adjust appearance of message
}


function changeClass(fld,classe){
    var div_capt = document.getElementById(fld);
    // modifica la classe della caption
    div_capt.className=classe;
}


// --------------------------------------------
//            commonCheck
// Common code for all validation routines to:
// (a) check for older / less-equipped browsers
// (b) check if empty fields are required
// Returns true (validation passed), 
//         false (validation failed) or 
//         proceed (don't know yet)
// --------------------------------------------

var proceed = 2;  

function commonCheck    (valfield,   // element to be validated
infofield,  // id of element to receive info/error msg
required, // true if required
min ) // min. digits  
{
    if (!document.getElementById) 
        return true;  // not available on this browser - leave validation to the server
    
    //  var elem = document.getElementById(infofield);
    //  if (!elem.firstChild) return true;  // not available on this browser 
    //  if (elem.firstChild.nodeType != node_text) return true;  // infofield is wrong type of node  
    
    var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
   
    // Controllo numero minimo di caratteri 
    if (min > 0 ){
        if (tfld.length < min ){
            changeClass(infofield,"campoTestoLargoControlloNonOk")
            setfocus(valfield);
            return false;
        }
        
    }
    if (emptyString.test(tfld)) {
        if (required) {
            changeClass(infofield,"campoTestoLargoControlloNonOk")
            setfocus(valfield);
            return false;
        }
        else {
            changeClass(infofield,"campoTestoLargoControlloOk")
            return true;  
        }
    }
    return proceed;
}
// --------------------------------------------
//            validatePresent
// Validate if something has been entered
// Returns true if so 
// --------------------------------------------
function validatePresent(valfield,   // element to be validated
infofield ) // id of element to receive info/error msg
{
    var stat = commonCheck(valfield, infofield, true);
    if (stat != proceed) return stat;
    
    changeClass(infofield,"campoTestoLargoControlloOk")
    return true;
}

// --------------------------------------------
//            validateMin4
// Validate entered field length is 4 or more digits
// Returns true if so 
// --------------------------------------------
function validateMin4(valfield,   // element to be validated
infofield ) // id of element to receive info/error msg
{
    var stat = commonCheck(valfield, infofield, true, 4);
    if (stat != proceed) return stat;
    
    changeClass(infofield,"campoTestoLargoControlloOk")
    return true;
    
}
// --------------------------------------------
//               validateEmail
// Validate if e-mail address
// Returns true if so (and also if could not be executed because of old browser)
// --------------------------------------------
function validateEmail  (valfield,   // element to be validated
infofield,  // id of element to receive info/error msg
required)   // true if required
{
    var stat = commonCheck(valfield, infofield, required);
    if (stat != proceed) return stat;
    
    var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
    var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/  ;
    if (!email.test(tfld)) {
        //msg (infofield, "error", "ERROR: not a valid e-mail address");
        changeClass(infofield,"campoTestoLargoControlloNonOk")
        setfocus(valfield);
        return false;
    } else {
        changeClass(infofield,"campoTestoLargoControlloOk")
        return true;  
        
    }
    
    
  /*
  var email2 = /^[A-Za-z][\w.-]+@\w[\w.-]+\.[\w.-]*[A-Za-z][A-Za-z]$/  ;
  if (!email2.test(tfld)) 
    msg (infofield, "warn", "Unusual e-mail address - check if correct");
  else
    msg (infofield, "warn", "");
  return true;
   */
    
}

// ------------------------------------------------------------------------------
//               validateEmailUnique
// Validate if e-mail address is correct AND unique in database
// Returns true if so (and also if could not be executed because of old browser)
// --------------------------------------------

function validateEmailUnique  (valfield,   // element to be validated
infofield,  // id of element to receive info/error msg
required)   // true if required
{
    var url, coduser, goOn;
    var stat = commonCheck(valfield, infofield, required);
    if (stat != proceed) return stat;
    
    var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
    var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/  ;
    if (!email.test(tfld)) {
        changeClass(infofield,"campoTestoLargoControlloNonOk");
        setfocus(valfield);
        return false;
    } else {
        
        // Senza controllo di unicità su Db
       
        changeClass(infofield,"campoTestoLargoControlloOk")
        return true;  
       
   /*     
        // Controllo di unicità da completare (Azz !)  
        if (!coduser) coduser = document.forms.formru.wu_coduser;
        url="ajaxCheckMail.jsp?email="+ 
        encodeURIComponent(tfld)+
        "&coduser="+
        encodeURIComponent(coduser.value);
        
        return httpRequest("GET",url,true,handleResponse);
    */     
        
    }
    
}
// ------------------------------------------------------------------------------
// event handler for XMLHttpRequest
// ------------------------------------------------------------------------------
function handleResponse() {
    var usedTag,answer,xmlReturnVal;
    if(request.readyState == 4){
        if(request.status == 200){
            //Implement document object in DOM
            xmlReturnVal = request.responseXML;
            usedTag = xmlReturnVal.getElementsByTagName("is_used")[0];
            answer= usedTag.childNodes[0].data;

            if(answer==true) {
                changeClass("wu_mail","campoTestoLargoControlloNonOk")
                setfocus("wu_mail");
                return false; 
            }
            else { 
                changeClass("wu_mail","campoTestoLargoControlloOk")
                return true; 
            }
        } else {
            alert("A problem occurred with communicating between the "+
            "XMLHttpRequest object and the server program.");
            return false;
        }
    }//end outer if
}
// ------------------------------------------------------------------------------
//               validateUserUnique
// Validate if username exists, min 4 digits AND unique in database
// Returns true if so (and also if could not be executed because of old browser)
// ------------------------------------------------------------------------------
function validateUserUnique  (valfield,   // element to be validated
infofield,  // id of element to receive info/error msg
required)   // true if required
{
    var url, coduser, goOn;
    
    var stat = commonCheck(valfield, infofield, required, 4);
    if (stat != proceed) return stat;
    
    var tfld = trim(valfield.value);  // value of field with whitespace trimmed off

    
    // Controllo di unicità da completare (Azz !)  
 /* 
    if (!coduser) coduser = document.forms.formru.wu_coduser;
    url="ajaxCheckUser.jsp?username="+ 
    encodeURIComponent(tfld)+
    "&coduser="+
    encodeURIComponent(coduser.value);
    
    return httpRequest("GET",url,true,handleResponseUser);
  */  
    
}
// ------------------------------------------------------------------------------
// event handler for XMLHttpRequest
// ------------------------------------------------------------------------------
function handleResponseUser() {
    var usedTag,answer,xmlReturnVal;
    if(request.readyState == 4){
        if(request.status == 200){
            //Implement document object in DOM
            xmlReturnVal = request.responseXML;
            usedTag = xmlReturnVal.getElementsByTagName("is_used")[0];
            answer= usedTag.childNodes[0].data;
            
            if(answer==true) {
                changeClass("wu_username","campoTestoLargoControlloNonOk")
                setfocus("wu_username");
                return false; 
            }
            else { 
                changeClass("wu_username","campoTestoLargoControlloOk")
                return true; 
            }
        } else {
            alert("A problem occurred with communicating between the "+
            "XMLHttpRequest object and the server program.");
            return false;
        }
    }//end outer if
}

// --------------------------------------------
//               validatePassword
// Validate if password=password2
// Returns true if so (and also if could not be executed because of old browser)
// --------------------------------------------
function validatePassword  (valfield, valfield2,  // elements to be validated
infofield,  // id of element to receive info/error msg
required)   // true if required
{
    var stat = commonCheck(valfield, infofield, required, 4);
    if (stat != proceed) return stat;

    var pw1 = trim(valfield.value);  // value of field with whitespace trimmed off
    var pw2 = trim(valfield2.value);  // value of field with whitespace trimmed off  
    
    if ( pw1 != pw2 ) {
        changeClass(infofield,"campoTestoLargoControlloNonOk")
        setfocus(valfield);
        return false;
    } else {
        changeClass(infofield,"campoTestoLargoControlloOk")
        return true;  
        
    } 
}

// --------------------------------------------
//            validateTelnr
// Validate telephone number
// Returns true if so (and also if could not be executed because of old browser)
// Permits spaces, hyphens, brackets and leading +
// --------------------------------------------

function validateTelnr  (valfield,   // element to be validated
infofield,  // id of element to receive info/error msg
required)   // true if required
{
    var stat = commonCheck(valfield, infofield, required);
    if (stat != proceed) return stat;
    
    var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
    var telnr = /^\+?[0-9 ()-]+[0-9]$/  ;
    if (!telnr.test(tfld)) {
        msg(infofield, "error", "ERROR: not a valid telephone number");
        setfocus(valfield);
        return false;
    }
    
    var numdigits = 0;
    for (var j=0; j<tfld.length; j++)
        if (tfld.charAt(j)>='0' && tfld.charAt(j)<='9') numdigits++;
    
    if (numdigits<6) {
        msg(infofield, "error", "ERROR: " + numdigits + " digits - too short");
        setfocus(valfield);
        return false;
    }
    
    if (numdigits>14)
        msg(infofield, "warn", numdigits + " digits - check if correct");
    else { 
        if (numdigits<10)
            msg(infofield, "warn", "Only " + numdigits + " digits - check if correct");
        else
            msg(infofield, "warn", "");
    }
    return true;
}

// --------------------------------------------
//             validateAge
// Validate person's age
// Returns true if OK 
// --------------------------------------------

function validateAge    (valfield,   // element to be validated
infofield,  // id of element to receive info/error msg
required)   // true if required
{
    var stat = commonCheck(valfield, infofield, required);
    if (stat != proceed) return stat;
    
    var tfld = trim(valfield.value);
    var ageRE = /^[0-9]{1,3}$/
    if (!ageRE.test(tfld)) {
        msg(infofield, "error", "ERROR: not a valid age");
        setfocus(valfield);
        return false;
    }
    
    if (tfld>=200) {
        msg(infofield, "error", "ERROR: not a valid age");
        setfocus(valfield);
        return false;
    }
    
    if (tfld>110) msg(infofield, "warn", "Older than 110: check correct");
    else {
        if (tfld<7) msg(infofield, "warn", "Bit young for this, aren't you?");
        else        msg(infofield, "warn", "");
    }
    return true;
}

// --------------------------------------------
//  validateDate
// --------------------------------------------

function validateDate(valfield,   // element to be validated
infofield,  // id of element to receive info/error msg
required)   // true if required
{ 
    var stat = commonCheck(valfield, infofield, required);
    if (stat != proceed) return stat;
    
    if (chkdate(valfield)==false) {
        changeClass(infofield,"campoTestoLungoControlloNonOk")
        setfocus(valfield);
        return false;
    } else {
        changeClass(infofield,"campoTestoLungoControlloOk")
        return true;  
        
    }
}



/* 
 Original:  Mike Welagen (welagenm@hotmail.com) 
 This script and many more are available free online at  
 The JavaScript Source!! http://javascript.internet.com   
 */
function checkdate(objName) {
    var datefield = objName;
    if (chkdate(objName) == false) {
        datefield.select();
        alert("That date is invalid.  Please try again.");
        datefield.focus();
        return false;
    }
    else {
        return true;
    }
}
function chkdate(objName) {
    //var strDatestyle = "US"; //United States date style
    var strDatestyle = "EU";  //European date style
    var strDate;
    var strDateArray;
    var strDay;
    var strMonth;
    var strYear;
    var intday;
    var intMonth;
    var intYear;
    var booFound = false;
    var datefield = objName;
    
    //var strSeparatorArray = new Array("-"," ","/",".");
    // Accetto solo il punto come separatore
    var strSeparatorArray = new Array(".");
    
    var intElementNr;
    var err = 0;
    var strMonthArray = new Array(12);
    strMonthArray[0] = "Jan";
    strMonthArray[1] = "Feb";
    strMonthArray[2] = "Mar";
    strMonthArray[3] = "Apr";
    strMonthArray[4] = "May";
    strMonthArray[5] = "Jun";
    strMonthArray[6] = "Jul";
    strMonthArray[7] = "Aug";
    strMonthArray[8] = "Sep";
    strMonthArray[9] = "Oct";
    strMonthArray[10] = "Nov";
    strMonthArray[11] = "Dec";
    strDate = datefield.value;
    if (strDate.length < 1) {
        return true;
    }
    if (strDate.length < 8) {
        return false;
    }
    for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
        if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
            strDateArray = strDate.split(strSeparatorArray[intElementNr]);
            if (strDateArray.length != 3) {
                err = 1;
                return false;
            }
            else {
                strDay = strDateArray[0];
                strMonth = strDateArray[1];
                strYear = strDateArray[2];
            }
            booFound = true;
        }
    }
    if (booFound == false) {
        if (strDate.length>5) {
            strDay = strDate.substr(0, 2);
            strMonth = strDate.substr(2, 2);
            strYear = strDate.substr(4);
        }
    }
    if (strYear.length == 2) {
        strYear = '20' + strYear;
    }
    // US style
    if (strDatestyle == "US") {
        strTemp = strDay;
        strDay = strMonth;
        strMonth = strTemp;
    }
    intday = parseInt(strDay, 10);
    if (isNaN(intday)) {
        err = 2;
        return false;
    }
    intMonth = parseInt(strMonth, 10);
    if (isNaN(intMonth)) {
        for (i = 0;i<12;i++) {
            if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
                intMonth = i+1;
                strMonth = strMonthArray[i];
                i = 12;
            }
        }
        if (isNaN(intMonth)) {
            err = 3;
            return false;
        }
    }
    intYear = parseInt(strYear, 10);
    if (isNaN(intYear)) {
        err = 4;
        return false;
    }
    if (intMonth>12 || intMonth<1) {
        err = 5;
        return false;
    }
    if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
        err = 6;
        return false;
    }
    if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
        err = 7;
        return false;
    }
    if (intMonth == 2) {
        if (intday < 1) {
            err = 8;
            return false;
        }
        if (LeapYear(intYear) == true) {
            if (intday > 29) {
                err = 9;
                return false;
            }
        }
        else {
            if (intday > 28) {
                err = 10;
                return false;
            }
        }
    }
    
/* Non trasformo la data
if (strDatestyle == "US") {
datefield.value = strMonthArray[intMonth-1] + " " + intday+" " + strYear;
}
else {
datefield.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear;
}
 */
    
    return true;
}
function LeapYear(intYear) {
    if (intYear % 100 == 0) {
        if (intYear % 400 == 0) { return true; }
    }
    else {
        if ((intYear % 4) == 0) { return true; }
    }
    return false;
}
function doDateCheck(from, to) {
    if (Date.parse(from.value) <= Date.parse(to.value)) {
        alert("The dates are valid.");
    }
    else {
        if (from.value == "" || to.value == "") 
            alert("Both dates must be entered.");
        else 
            alert("To date must occur after the from date.");
    }
}





