//////////////////////////////////////////////////////////////////////////////////////
// validation.js
//
// Copyright (c) 2010, Alexander Blomen <info@ablomen.nl>			
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this
//   list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice, this
//   list of conditions and the following disclaimer in the documentation and/or 
//   other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
// POSSIBILITY OF SUCH DAMAGE.
//////////////////////////////////////////////////////////////////////////////////////
(function(e){var d=function(o,n){var p=true;if(!o.el.val()||o.el.val()===o.message){p=false}return p},k=function(o,n){var p=true;if(!o.el.val()||o.el.val()===o.message||o.el.val().indexOf("@")<0||o.el.val().indexOf(".")<0){p=false}return p},c=function(o,n){var p=true;if(!o.el.is(":checked")){p=false}return p},h=function(o,n){var p=false;e("input[type=radio][name="+o.name+"]",n.el).each(function(){if(e(this).is(":checked")){p=true}});return p},m=function(o,n){var p=true;if(!o.el.val()){p=false}return p},b=function(o,n){o.el.addClass(n.options.error_class);o.el.bind("click focus",function(){o.el.removeClass(n.options.error_class)})},a=function(o,n){o.el.addClass(n.options.error_class).val(o.message);o.el.bind("click focus",function(){o.el.removeClass(n.options.error_class).val("")})},j=function(o,n){o.el.parent().find("."+n.options.error_append_class).remove();o.el.parent().append(e("<span/>").addClass(n.options.error_append_class).html(n.options.error_append_char));o.el.bind("click focus",function(){o.el.parent().find("."+n.options.error_append_class).remove()})},i=function(o,n){o.el.parent().addClass(n.options.error_parent_class);o.el.parent().bind("click",function(){o.el.parent().removeClass(n.options.error_parent_class)})},g=function(o,n){n.alert_message+=(o.message+"\r\n")},f=function(p,o){var n=this;n.el=p;n.elements=[];n.alert_message="";n.options={callback:false,required_class:"validate_required",error_attribute:"title",error_output:{text:"class",email:"class",select:"class",textarea:"class",other:"append"},error_class:"validate_invalid",error_append_char:" *",error_append_class:"validate_invalid_append",error_parent_class:"validate_invalid_parent",types:{text:d,email:k,checkbox:c,radio:h,select:m,textarea:d},output:{"class":b,inline:a,append:j,alert:g,parent:i}};n.set_options=function(r){var s,q;if(r){if(r.types){s=e.extend({},n.options.types,r.types)}if(r.output){q=e.extend({},n.options.output,r.output)}e.extend(n.options,r);if(s){n.options.types=s}if(q){n.options.output=q}}};n.get_elements=function(){n.elements=[];e("."+n.options.required_class,n.el).each(function(){var t,s,r,q={};q.el=e(this);q.name=e(this).attr("name");q.message=e(this).attr(n.options.error_attribute);q.type=e(this).attr("type").toLowerCase();t=e(this).attr("class").split(" ");s=t.length;for(r=0;r<s;r+=1){if(t[r].substr(0,14)==="validate_error"){q.output=t[r].replace(/validate_error_/,"")}if(t[r].substr(0,13)==="validate_type"){q.type=t[r].replace(/validate_type_/,"")}}if(e(this)[0].tagName.toLowerCase()==="input"){switch(q.type){case"text":if(!q.output){q.output=n.options.error_output.text}break;case"email":if(!q.output){q.output=n.options.error_output.email}break;case"checkbox":if(!q.output){q.output=n.options.error_output.other}break;case"radio":if(!q.output){q.output=n.options.error_output.other}if(e(this)[0]!==e(n.el).find("input[type=radio][name="+q.name+"]:last")[0]){q=false}break}}else{if(e(this)[0].tagName.toLowerCase()==="textarea"){if(!q.output){q.output=n.options.error_output.textarea}q.type="textarea"}else{if(e(this)[0].tagName.toLowerCase()==="select"){if(!q.output){q.output=n.options.error_output.select}q.type="select"}}}if(!q.output){q.output=n.options.error_output[q.type]}if(q){n.elements.push(q)}})};n.validate=function(){var t,s,u,q,r=n.elements.length,v=true;n.alert_message="";for(t=0;t<r;t+=1){s=n.elements[t];u=n.options.types[s.type];q=n.options.output[s.output];if(u){if(!u(s,n)){v=false;if(q){q(s,n)}}}}if(n.alert_message){alert(n.alert_message)}if(v){if(n.options.callback){return n.options.callback(n)}else{return true}}else{return false}};n.set_options(o);n.get_elements();e(n.el).bind("submit",function(){return n.validate()})},l=function(p,o){var n=this;n.el=p;n.alert_message="";n.element={};n.options={error_attribute:"title",error_output:{text:"class",email:"class",select:"class",other:"append"},error_class:"validate_invalid",error_append_char:" *",error_append_class:"validate_invalid_append",error_parent_class:"validate_invalid_parent",types:{text:d,email:k,select:m,textarea:d},output:{"class":b,inline:a,append:j,alert:g,parent:i}};n.set_options=function(r){var s,q;if(r){if(r.types){s=e.extend({},n.options.types,r.types)}if(r.output){q=e.extend({},n.options.output,r.output)}e.extend(n.options,r);if(s){n.options.types=s}if(q){n.options.output=q}}};n.get_element=function(){n.element.el=e(n.el);n.element.name=e(n.el).attr("name");n.element.message=e(n.el).attr(n.options.error_attribute);n.element.type=e(n.el).attr("type").toLowerCase();var s=e(n.el).attr("class").split(" "),r=s.length,q;for(q=0;q<r;q+=1){if(s[q].substr(0,14)==="validate_error"){n.element.output=s[q].replace(/validate_error_/,"")}if(s[q].substr(0,13)==="validate_type"){n.element.type=s[q].replace(/validate_type_/,"")}}if(e(n.el)[0].tagName.toLowerCase()==="input"){switch(n.element.type){case"text":if(!n.element.output){n.element.output=n.options.error_output.text}break;case"email":if(!n.element.output){n.element.output=n.options.error_output.email}break}}else{if(e(n.el)[0].tagName.toLowerCase()==="textarea"){if(!n.element.output){n.element.output=n.options.error_output.text}n.element.type="textarea"}else{if(e(n.el)[0].tagName.toLowerCase()==="select"){if(!n.element.output){n.element.output=n.options.error_output.select}n.element.type="select"}}}};n.validate=function(){n.alert_message="";var t=true,r=n.element,s=n.options.types[r.type],q=n.options.output[r.output];if(s){if(!s(r,n)){t=false;if(q){q(r,n)}}}if(n.alert_message){alert(n.alert_message)}return t};n.set_options(o);n.get_element()};e.fn.validate=function(n){var o=false;e(this).each(function(){var p=e(this)[0],q;if(e.data(p,"validate")){if(n){e.data(p,"validate").set_options(n)}else{o=e.data(p,"validate")}}else{if(p.tagName.toLowerCase()==="form"){e.data(p,"validate",new f(p,n))}else{if(p.tagName.toLowerCase()==="input"||p.tagName.toLowerCase()==="textarea"||p.tagName.toLowerCase()==="select"){q=new l(p,n);o=q.validate()}}}});return o}}(jQuery));

