//----------------------------------------------------------------------------- // value // - this function extends jQuery // - returns the value of form elements based on A single or B group type: // // A) single : ie - return input value // B) group : ie - return selected radio value //----------------------------------------------------------------------------- $.fn.value = function() { if(this.attr("type") == 'text' || this.attr("type") == 'date' || this.attr("type") == 'number' || this.attr("type") == 'tel'){ this.val(this.val().trim()); } if( (this.attr("type") == 'radio' || this.attr("type") == 'checkbox') && typeof(this.attr('name')) != 'undefined'){ var value = $("input[name="+this.attr('name')+"]:checked").val() || false; if(value) return value; else return ''; } else if(! this.hasClass('no-translate')) { try { return convertFrenchCharsToEnglish (this.val()); } catch (err) { return this.val(); } } else{ return this.val(); } }; $.fn.summary = function() { //array of selected items, run hasError on each item if(this.length > 1){ for(var i = 0; i < this.length; i++){ $(this[i]).summary(); } } //if selected item does not have class summary, search inside it for //elements that have it; run summary on anything found else if(!this.hasClass('app-summary')){ var found_summaries = this.find(".app-summary"); if(found_summaries.length) { $(found_summaries).summary(); } } //split class string and look through each class //if class matches a field and has a value that isn't undefined //add it to the display string else { var classes = $(this).attr('class').split(' '); var display_string = ''; for (var i = 0; i < classes.length; i++) { if(typeof($("#"+classes[i]+"_id").val()) != 'undefined'){ if( $("#"+classes[i]+"_id").prop("tagName").toLowerCase() == 'select' && $("#"+classes[i]+"_id").val() != ""){ display_string+= " " + $("#"+classes[i]+"_id").find("option:selected").html(); } else if($("#"+classes[i]+"_id").attr("type") == 'radio'){ display_string+= " " + $("input[name="+classes[i]+"]:checked").val(); } else{ display_string+= " " + $("#"+classes[i]+"_id").val(); } } } if(display_string.match(/^\s$/)){ $(this).hide(); } else{ $(this).show(); } //alert(display_string); display_string = display_string.replace(//g, '>'); $(this).find("span").html(display_string); } }; //----------------------------------------------------------------------------- // valid - performs validation on all form fields within a given jQuery selector // - triggers uos error text / css //----------------------------------------------------------------------------- $.fn.valid = function(systemError) { //array of selected items, run valid() on each item debug("Validating..."); // "this" is an array of elements inside a given form if(this.length > 1){ debug("\tMultiple items found, looping through each."); var error_flag = false; for(var i = 0; i < this.length; i++){ debug("Loop: "+i); if( !$(this[i]).valid(systemError) ) error_flag = true; } if(error_flag) return false; else return true; } //selected item is not form field //look for form fields inside and valid() them else if( this.prop("tagName") && !this.prop("tagName").toLowerCase().match(/(select|input|textarea)/) ){ debug("\tNot a input field, searching for fields inside"); var found_fields = this.find('input, select, textarea'); if(found_fields.length) { return $(found_fields).valid(systemError); } else { //return true, because nothing was found return true; } } //if selected is a form field //AND make sure there is an entry in the field response else if( this.prop("tagName") && this.prop("tagName").toLowerCase().match(/(select|input|textarea)/) && typeof(fields[this.attr("name")]) != "undefined"){ debug("\tInput field found: " + this.attr("name")); var field_name = this.attr("name"); var field_constraints = fields[field_name]; //loop through constraint names main_loop: for(var key in field_constraints){ debug('\t\tConstraint: '+ key); // do a dependency check if(field_constraints[key].deps){ debug('\t\t\tDependencies'); for( var dep_field in field_constraints[key].deps ){ debug('\t\t\tField: '+dep_field); for( var dep in field_constraints[key].deps[dep_field] ){ debug('\t\t\tConstraint: '+dep); //skip the current constraint if it doesn't pass // its dependencies var params = field_constraints[key].deps[dep_field][dep]; if( !constraints[dep]( $("input[name="+dep_field+"], select[name="+dep_field+"]").value(), params.slice(0)) ){ debug('\t\t\tFailed'); continue main_loop; } debug('\t\t\tPassed'); } } } //run the constraint, passing value from this and params from //service var params; if(field_constraints[key].params){ params = field_constraints[key].params; }else{ params = [""]; } if( typeof (constraints[key]) != 'undefined' && !constraints[key](this.value() || '', params.slice(0)) ){ try{ var error = ( typeof content[field_name][$('#lang_id').val()].errors[key] == 'undefined')? 'default' : key; this.show_error(false, content[field_name][$('#lang_id').val()].errors[error]); debug('\t\tFailed'); } catch(e){ debug("Fail on Field with no 'errors' nodes in content-fields JSON. : " + field_name + " Constraint : " + key + " val : " +this.value() + " msg: " + e) ; systemError(); } return false; }else if (typeof (constraints[key]) == 'undefined') { debug("Missing constraint function " + key); return false; } /* if( typeof (constraints[key]) != 'undefined' && !constraints[key](this.value(), params.slice(0)) ){ if(typeof content[field_name] == 'undefined'){ this.show_error(false, ''); } else{ var error = ( typeof content[field_name][$('#lang_id').val()].errors[key] == 'undefined')? 'default' : key; this.show_error(false, content[field_name][$('#lang_id').val()].errors[error]); } debug('\t\tFailed'); return false; } */ debug('\t\tPassed'); } //clear errors this.show_error(true); return true; } else if(typeof(fields[this.attr("name")]) == "undefined"){ debug('No validation exists for: '+this.attr("name")); return false; } else{ debug('Mystery (returning true for now)'); return true; } }; //----------------------------------------------------------------------------- // valid_unit - **used for unit tests only. // - **do not call from app //----------------------------------------------------------------------------- $.fn.valid_unit = function(unit_test) { var unit_test_mode = unit_test || "off"; //array of selected items, run valid() on each item debug("Validating..."); // "this" is an array of elements inside a given form if(this.length > 1){ debug("\tMultiple items found, looping through each."); var error_flag = false; for(var i = 0; i < this.length; i++){ debug("Loop: "+i); if( !$(this[i]).valid() ) error_flag = true; } if(error_flag) return false; else return true; } //selected item is not form field //look for form fields inside and valid() them else if( this.prop("tagName") && !this.prop("tagName").toLowerCase().match(/(select|input|textarea)/) ){ debug("\tNot a input field, searching for fields inside"); var found_fields = this.find('input, select, textarea'); if(found_fields.length) { return $(found_fields).valid(); } else { //return true, because nothing was found return true; } } //if selected is a form field //AND make sure there is an entry in the field response else if( this.prop("tagName") && this.prop("tagName").toLowerCase().match(/(select|input|textarea)/) && typeof(fields[this.attr("name")]) != "undefined"){ debug("\tInput field found: " + this.attr("name")); var field_name = this.attr("name"); var field_constraints = fields[field_name]; //loop through constraint names main_loop: for(var key in field_constraints){ debug('\t\tConstraint: '+ key); // do a dependency check if(field_constraints[key].deps && unit_test_mode != 'on'){ debug('\t\t\tDependencies'); for( var dep_field in field_constraints[key].deps ){ debug('\t\t\tField: '+dep_field); for( var dep in field_constraints[key].deps[dep_field] ){ debug('\t\t\tConstraint: '+dep); //skip the current constraint if it doesn't pass // its dependencies var params = field_constraints[key].deps[dep_field][dep]; if( !constraints[dep]( $("input[name="+dep_field+"], select[name="+dep_field+"]").value() || '', params.slice(0)) ){ debug('\t\t\tFailed'); continue main_loop; } debug('\t\t\tPassed'); } } } //run the constraint, passing value from this and params from //service var params; if(field_constraints[key].params){ params = field_constraints[key].params; }else{ params = [""]; } if( typeof (constraints[key]) != 'undefined' && !constraints[key](this.value() || '', params.slice(0)) ){ try{ var error = ( typeof content[field_name][$('#lang_id').val()].errors[key] == 'undefined')? 'default' : key; this.show_error(false, content[field_name][$('#lang_id').val()].errors[error]); debug('\t\tFailed'); } catch(e){ debug("Fail on Field with no 'errors' nodes in content-fields JSON. : " + field_name + " Constraint : " + key + " val : " +this.value() + " msg: " + e) ; } return false; }else if (typeof (constraints[key]) == 'undefined') { debug("Missing constraint function " + key); return false; } debug('\t\tPassed'); } //clear errors this.show_error(true); return true; } else if(typeof(fields[this.attr("name")]) == "undefined"){ debug('No validation exists for: '+this.attr("name")); return false; } else{ debug('Mystery (returning true for now)'); //no validation found //or not a tag //go to error page? //return true for now return true; } }; //----------------------------------------------------------------------------- // show_error - show/hide callout and field validation messages //----------------------------------------------------------------------------- $.fn.show_error = function(valid, error_message) { var field_name = this.attr("name"); if (!valid && typeof(error_message) == "undefined"){ error_message = " "; } // show error if valid is false if(!valid){ if(this.attr('type') == 'radio' || this.attr('type') == 'checkbox'){ $("input[name="+field_name+"]").addClass("app-error"); } else this.addClass("app-error"); $("."+field_name+".app-message span").html(error_message); $("."+field_name+".app-message").show(); $("."+field_name+".app-parent").addClass('app-error'); $("."+field_name+".app-parent").removeClass("app-valid"); } // hide error if valid is true else{ if(this.attr('type') == 'radio' || this.attr('type') == 'checkbox'){ $("input[name="+field_name+"]").removeClass("app-error"); } else this.removeClass("app-error"); $("."+field_name+".app-message span").html(''); $("."+field_name+".app-message").hide(); //only remove the app-error on a app-parent if it can't find any //fields with errors inside it if($("."+field_name+".app-parent").find('input.app-error, select.app-error').length == 0){ $("."+field_name+".app-parent").removeClass('app-error'); $("."+field_name+".app-parent").addClass("app-valid"); } } }; function debug(string){ if(typeof(DEBUG) != 'undefined' && DEBUG == true){ console.log(string); } } if(typeof convertFrenchCharsToEnglish == 'undefined') { convertFrenchCharsToEnglish = function (text) { return text.replace(/[\u00A0-\u2666]/g, function(c) { return convertFrenchCharsToEnglish.entityTable[c.charCodeAt(0)]; }); }; // all HTML4 entities as defined here: http://www.w3.org/TR/html4/sgml/entities.html convertFrenchCharsToEnglish.entityTable = { /*'À'*/ 192 : 'A', /*'Á'*/ 193 : 'A', /*'Â'*/ 194 : 'A', /*'Ä'*/ 196 : 'A', /*'Ç'*/ 199 : 'C', /*'È'*/ 200 : 'E', /*'É'*/ 201 : 'E', /*'Ê'*/ 202 : 'E', /*'Ë'*/ 203 : 'E', /*'Ì'*/ 204 : 'I', /*'Í'*/ 205 : 'I', /*'Î'*/ 206 : 'I', /*'Ï'*/ 207 : 'I', /*'Ò'*/ 210 : 'O', /*'Ó'*/ 211 : 'O', /*'Ô'*/ 212 : 'O', /*'Ù'*/ 217 : 'U', /*'Ú'*/ 218 : 'U', /*'Û'*/ 219 : 'U', /*'Ü'*/ 220 : 'U', /*'à'*/ 224 : 'a', /*'á'*/ 225 : 'a', /*'â'*/ 226 : 'a', /*'ä'*/ 228 : 'a', /*'ç'*/ 231 : 'c', /*'è'*/ 232 : 'e', /*'é'*/ 233 : 'e', /*'ê'*/ 234 : 'e', /*'ë'*/ 235 : 'e', /*'ì'*/ 236 : 'i', /*'í'*/ 237 : 'i', /*'î'*/ 238 : 'i', /*'ï'*/ 239 : 'i', /*'ò'*/ 242 : 'o', /*'ó'*/ 243 : 'o', /*'ô'*/ 244 : 'o', /*'ù'*/ 249 : 'u', /*'ú'*/ 250 : 'u', /*'û'*/ 251 : 'u', /*'ü'*/ 252 : 'u', /*'ÿ'*/ 255 : 'y', /*'Ÿ'*/ 376 : 'Y' }; }