/* ------------------------------------------------------------------------
 * REQUIRED vs OPTIONAL fields
 *
 * Each field in the form should be listed below as: fields['name'].
 * Set the value of each to 1 for required, 0 for not required.
 *
 * Example: REQUIRED
 *
 *      fields['email'] = 1;
 *      fields['name'] = 1;
 *
 * Example: OPTIONAL
 *
 *      fields['address'] = 0;
 *      fields['favorite_color'] = 0;
 *
 *
 * The "name" part comes from the name tag of the form element. For
 * example:
 *
 *      <input type="text" name="shoe_size">
 *
 *      is set to optional/required by:
 *
 *      fields['shoe_size'] = 1;
 *
 * Leaving a field off the list entirely makes it default to optional.
 *
 * --------------------------------------------------------------------- */
        var fields = new Object;
        
        var fields = new Object;
        
        fields['name']                  = 1;
        fields['cognome']               = 0;
        fields['email']                 = 1;
        fields['telefono']                 = 0;
        fields['commento']                  = 1;
        fields['privacy']               = 1;
        
        
        
        
/* ------------------------------------------------------------------------
 * You can set the document error text here.
 *
 *  ErrHead: Appears at the top of the page, large red letters
 *  ErrSubh: Appears underneath ErrHead, smaller black letters
 *  ErrForm: Appears underneath each form element that was not filled in
 *
 *  -------------------------------------------------------------------- */
    ErrHead = "ERRORE: il form non &#232; stato inviato perch&#232; incompleto";
    ErrSubh = "Riempi o correggi i campi evidenziati qui sotto e clicca su INVIA";
    ErrForm = "Questo campo &#232; obbligatorio. Inserisci un valore.";
    
    

    
    
    
/* -----------------------------------------------------------------------------
 * DO NOT MODIFY BELOW THIS LINE. ALL OPTIONS ARE SET ABOVE THIS POINT
 * -------------------------------------------------------------------------- */






    var RecaptchaOptions = {
    
            theme : 'custom',
            custom_theme_widget: 'recaptcha_widget'
    };

    $(document).ready(function() {
        
        // Cache these for performance improvment
        bigErr = $("#error_head");
        formEl = document.getElementById("theFormID");
        
        // Add Asterisks to required fields
        for( var thisKey in fields ) {
            if(fields[thisKey] == 1) {
                $("#" + thisKey + " > .label").append("<span style='color:#FF0000'> *</span>");
            }
        }
        
        // Bind background change to onFocus && onBlur
        $(".as").bind("focus",highlight);
        $(".as").bind("blur",clear_highlight);
        $(".cb").bind("focus", {name:'optionals'}, highlight);
        $(".cb").bind("blur", {name:'optionals'}, clear_highlight);
    });
    

    var storedBG;
    function highlight(e) {
        objectID = (e.data) ? e.data.name : $(e.target).attr('name');
        storedBG = $("#" + objectID).css("background-color");
        $("#" + objectID).css("background","#FFFAC2");
    }
    function clear_highlight(e) {
        fb_log('restoring color to ' + storedBG);
        
        objectID = e.data ? e.data.name : $(e.target).attr('name');
        $("#" + objectID).css("background-color", storedBG);
        }
    
    function verify_required() {
        
        //  By deafult we are successful and should submit; this
        //  value changes to unsuccessful if ANY error fields are hit
        // -----------------------------------------------------------------
        success = 1;
        //  Clear any error messages from last pass
        // -----------------------------------------------------------------
        $(bigErr).html("");
        $(".err_msg").remove();

        // General sweep of all fields, flag any field that is left blank
        // -----------------------------------------------------------------
        for( var thisKey in fields ) {
            
            currentEl = $("#" + thisKey);
            if( ((formEl[thisKey].value == "") && (fields[thisKey] == 1))  ||   
                ((!(formEl[thisKey].checked)) && (fields[thisKey] == 1) && (formEl[thisKey].type == "checkbox"))) {
                
                error_highlight($(currentEl), ErrForm);
                success = 0;
            } else {
                
                $(currentEl).css("background","#FFFFFF");
                $(currentEl).css("color","#666666");
            }
        }
        
        
        // Specific checks: Email (format) and Email vs. Confirm
        // -------------------------------------------------------------------
        // Address look good? (contains a @ and a . in roughly right spots)
        // ------------------------------------------------------------------ 
        apos   = formEl['email'].value.indexOf("@");
        dotpos = formEl['email'].value.lastIndexOf(".");
  
        if (apos<1||dotpos-apos<2) {
            
            // Bad E-mail
            error_highlight($("#email"),"Indirizzo email non valido");
            success = 0;
        }
        
        // Do the E-mail and Confirm Email match?
        // ---------------------------------------------------------------------
	if(formEl['confirm_email']) {
        if(formEl['email'].value != formEl['confirm_email'].value) {
            
            error_highlight($("#email"),"Email non confermato correttamente");
            error_highlight($("#confirm_email"),"Email non confermato correttamente");
            success = 0;
        }
	}
        
        
        
        
        if(success) {
            
            return true;
        } else  {
            
            $(bigErr).append("<h2 class='pageErr_head'>" + ErrHead + "</h2>");
            $(".pageErr_head").append("<span class='pageErr_text'>" + ErrSubh + "</span>");        
            window.location = String(window.location).replace(/\#.*$/, "") + "#error_anchor";
            return false;
        }
        
    }
    // Accepts a JQuery Object as a param -------------------------------------------
    function error_highlight(obj,errText) {
        obj.css("background","#FCDEFF");
        obj.css("font-weight","bold");
        obj.css("color","#FF0000");
        obj.append('<span class="err_msg">' + errText + "</span>");
        
    }
    function fb_log(text) {
        if(window.console) { console.log(text); }
     
    }
    function popitup(url) {
	newwindow=window.open(url,'name','height=400,width=300,scrollbars=yes');
	if (window.focus) {newwindow.focus()}
	return false;
    }


