//event handlers $j(document).ready(function(){ $j("#requestBtn").click(function(){ validateForm(); }) $j(".telInput").each(function(){ $j(this).keyup(function(){ autoFocusNext($j(this), $j(this).attr("maxlength")); }) }) }) //validate the form fields function validateForm() { var validated = true; if (!validate("text", $j("#input_0_39"), "stringOnly", false)) validated = false; if (!validate("text", $j("#input_0_40"), "alphanumeric", false)) validated = false; if (!validate("text", $j("#phoneExt"), "numericOnly", true)) validated = false; if (!validate("text", $j("#tel_1"), "numericOnly", false)) validated = false; if (!validate("text", $j("#tel_2"), "numericOnly", false)) validated = false; if (!validate("text", $j("#tel_3"), "numericOnly", false)) validated = false; if (!validate("text", $j("#input_0_41"), "alphanumeric", true)) validated = false; if (!validate("text", $j("#input_0_43"), "email", false)) validated = false; if (!validate("select", $j("#softwareSelect"), "alphanumeric", false)) validated = false; if (!validate("select", $j("#refererSelect"), "alphanumeric", false)) validated = false; if (!validate("text", $j("#input_0_41"), "alphanumeric", true)) validated = false; if (validated) { ajax_createDemoReq(); } else { $j("#bottomErrorMsg").html("Some of the information you've supplied is invalid. Please verify and try again."); } } /* * Automatically changes to focus to the next field once the * given number of input chars has been reached * @fieldEle obj the DOM element that must be checked * @nbChars int threshold to attain before switching field */ function autoFocusNext(fieldEle, nbChars) { if (fieldEle.val().length >= nbChars) { $j("#"+fieldEle.attr("id")).next("input").focus(); $j("#"+fieldEle.attr("id")).next("input").select(); } } /* * Validates a given field with a given validation type * @fieldEle obj the DOM element that must be validated * @valType string type of validation that must be applied * @empty boolean if this field can be empty or not */ function validate(fieldType, fieldEle, valType, allowEmpty) { error = false; var legalChars = ""; var errorMsg = ""; if (fieldType == "text") { //verify if the field is empty, and if it's allowed to be empty if (allowEmpty == false) { if (fieldEle.val().length == 0) { errorMsg = "Ce champ est obligatoire"; fieldEle.prevAll(".validationError").html(errorMsg); error = true; } } if (errorMsg == "") { switch(valType){ case "stringOnly": legalChars = /^([a-zA-Z\ \-ãàâäáåÃÀÂÄÁÅéèêëÉÈÊËíìîïÍÌÎÏõòóôöÕÓÒÔÖùúûüÜÚÙÛçÇ])*$/; errorMsg = "This field can only contain letters and/or hyphens"; break; case "alphanumeric": var legalChars = /^([a-zA-Z0-9\ \-ãàâäáåÃÀÂÄÁÅéèêëÉÈÊËíìîïÍÌÎÏõòóôöÕÓÒÔÖùúûüÜÚÙÛçÇ])*$/; errorMsg = "This field can only contain letters, numbers and hyphens"; break; case "numericOnly": var legalChars = /^([0-9])*$/; errorMsg = "Ce champ ne peut contenir que des chiffres"; break; case "email": var legalChars = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/; errorMsg = "L'adresse de courriel soumise est invalide"; break; //no filter default: var legalChars = /^.+$/; break; } if (!legalChars.test(fieldEle.val())) { error = true; //reset the error field $j("#"+fieldEle.attr("id")).prevAll(".validationError").html(""); fieldEle.prevAll(".validationError").html(errorMsg); } else { //empty the error fields $j("#"+fieldEle.attr("id")).prevAll(".validationError").html(""); $j("#bottomErrorMsg").html(""); error = false; } } } if (fieldType == "select") { if (allowEmpty == false) { if ($j('select#'+fieldEle.attr("id")+' option:selected').attr("val") == 0 || $j('select#'+fieldEle.attr("id")+' option:selected').attr("val") == undefined) { error = true; errorMsg = "Please select a valid option within the list"; } } if (error) { //reset the error field $j("#"+fieldEle.attr("id")).prevAll(".validationError").html(""); fieldEle.prevAll(".validationError").html(errorMsg); } else { //empty the error fields $j("#"+fieldEle.attr("id")).prevAll(".validationError").html(""); $j("#bottomErrorMsg").html(""); } } if (error) return false; else return true; } function ajax_createDemoReq() { //display the loading screen $j("#msgLoading").css({"opacity" : 0, "display" : "block"}); $j("#msgLoading").animate({"opacity" : 0.75}, 500) var formData = $j("#demoForm").serialize(); //alert (formData); var data = { action:'createDemoReq', formData: formData, productId: $j('#softwareSelect :selected').attr("val"), refererId: $j('#refererSelect :selected').attr("val"), ajax: true }; $j.ajax({ url: 'http://www.negotium.com/php/ajax.php', type: 'POST', data: data, success: function(cb) { //alert (cb); $j("#demoForm").html("

"+cb+"

"); } }); } function isInt(s){return parseInt(s,10)===s;}