// Load requirements, initialise
dojo.require("dijit.form.Form");
dojo.require("dijit.form.Button");
dojo.require("dijit.form.ValidationTextBox");
dojo.require("dijit.form.FilteringSelect");
dojo.require("dijit.form.Textarea");
dojo.require("dijit.form.CheckBox");
dojo.require("dijit.Dialog");
dojo.require("dojox.encoding.digests.MD5");

var valStat = {"keyV":"0", "AJAX_vak":"true", "lastInvalid":"","lastInvalid_atmpt":"10"}, valStatB = dojo.clone(valStat), valStatC = dojo.clone(valStat); //Array to keep track of validation status

var RecaptchaOptions = {theme : 'white' }; // Set reCAPTCHA theme

var eset_activate_confirm = false; // Variable to monitor whether or not the submission has been confirmed

/*
 * Function to provide UI cleanliness to validator function. Cleanliness that is provided by default, but unavailable when using custom validation
 */
function dojo_vtb_fieldstatus(widget, valStat) {
    if(!widget.focused) // Record number of visits
        valStat["keyV"]++;

    if(valStat["keyV"] == 0) // Don't show errors on initial input
        return true;

    if(widget.focused && valStat["lastInvalid_atmpt"] < valStat["keyV"] && valStat["lastInvalid"] != widget.get('value')) { //Give the user the oppurtunity to correct the mistake before showing the error again
        widget.set ('state', 'Normal');
        return true;
    }
}

/* 
 * A function to validate credit card numbers using LUHN algorithm and prefixes
 *
 * @param string $cardnumber - The credit card number to be validated
 * @param string $cardtype - The credit card type (visa, master card, laser)
 */
function prefixluhn_cardnumber_check(cardnumber, cardtype) 
{
    /* Tidy up card number */
    for(i=0; i < 5; i++)
        cardnumber = cardnumber.replace(/ /,"");
    for(i=0; i < 5; i++)
        cardnumber = cardnumber.replace(/-/,"");

   /* LUHN algorithm */
   var sum = 0;
   var i;

   for (i = cardnumber.length - 2; i >= 0; i -= 2) 
   {
      sum += Array (0, 2, 4, 6, 8, 1, 3, 5, 7, 9) [parseInt (cardnumber.charAt (i), 10)];
   }
   for (i = cardnumber.length - 1; i >= 0; i -= 2) 
   {
      sum += parseInt (cardnumber.charAt (i), 10);
   }

   /* If LUHN algorithm is true check prefixes*/
   if((sum % 10) == 0)
   {
        //Valid prefixes
        mastercard = new Array('51','52','53','54','55');
        laser = new Array('6304','6706','6771','6709');

        if(cardtype == 'Visa' && cardnumber.charAt(0) == 4)
            return true;
        else if(cardtype == 'Master Card' && mastercard.indexOf(cardnumber.substr(0,2)) > -1)
            return true;
        else if(cardtype == 'Laser' && laser.indexOf(cardnumber.substr(0,4)) > -1)
            return true;
        else
            return false;
   }
   else
        return false;
}

/*
 * Initialisation function executed when the document is ready and all requirements are loaded
 */
var warningDialog, confirmDialog, noticeDialog;
dojo.ready(function() {

    // Create warning dialog
    warningDialog = new dijit.Dialog({
        title: "Warning",
        style: "width: 300px;",
        draggable: false,
        "class": "warnDialog"
    });

    // Create confirmation dialog
    confirmDialog = new dijit.Dialog({
        id: "confirm_dialog",
        title: "Confirm Submission",
        style: "width: 450px;",
        draggable: false
    });

    // Create notice dialog
    noticeDialog = new dijit.Dialog({
        title: "Notice",
        style: "width: 300px;",
        draggable: false
    });
});


