/**
 * ZedPlan OpenCorePHP Framework
 *
 * Copyright (c) 2005-2010, ZedPlan (http://www.zedplan.com)
 *
 *
 *
 * LICENSE
 *
 * This source file is subject to the GPL license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opencorephp.zedplan.com/license.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to opencorephp@zedplan.com so we can send you a copy immediately.
 *
 * @copyright	Copyright (c) 2005-2010, ZedPlan (http://www.zedplan.com)
 * @link	http://opencorephp.zedplan.com
 * @license	http://opencorephp.zedplan.com/license.txt     GPL License
 */
/**
 * @author ZedPlan Team (opencorephp@zedplan.com)
 */
Form.Element.Methods.getValidationParams=function(b){b=$(b);if(b.tagName=="INPUT"&&!["text","hidden","file","password"].include(b.type)){return null}var c=b.nextSibling;while(c){if(c.nodeType==1&&["INPUT","SELECT","TEXTAREA"].include(c.tagName)){return null}if(c.nodeType==8){break}c=c.nextSibling}if(!c){return null}var e={type:"string",required:"false",regexp:"",errormsg:"",onerror:"",trim:"true",equals:""};var d=c.nodeValue;var f;var a=/([a-z]+)\s*=\s*(["']?)(.*?)\2(?:\s|$)/g;while((f=a.exec(d))!=null){if(typeof e[f[1]]!="undefined"){e[f[1]]=f[3]}}return e};Form.Element.Methods.validate=function(d,g){d=$(d);g=Object.extend({focus:true,alert:false},g||{});var A=d.getValidationParams();if(!A||d.disabled){return true}if(!d.id){d.id="form_validator_autoid_"+Form.Element.Methods.validate.__tmpidCounter++}var f=g.focus;var t=g.alert;var n=g.errorNodeClassName||"form-field-error-label";var l=g.elementErrorClassName||"form-field-error";var v=$F(d);var x=d.tagName!="SELECT";var u=true;var a=A.required=="true";if(x){var b=/^(\w+)(?:\((.+?)\))?$/i.exec(A.type);if(!b){throw"Couldn't validate #"+d.id+". Invalid value for 'type' attribute"}var q=["string","alphanumeric"].include(A.type);if(A.trim=="true"&&q||!q){v=v.trim()}if(!a&&!v){return true}var h=b[1].toLowerCase();var w=b[2]||"";var r=w&&["string","integer","float","alphanumeric"].include(h)?/^(\d+)(,(\d+)?)?$/.exec(w):null;if(r){var C=r[1];var k=r[2]&&!r[3];var j=r[3];if(!k&&!j&&v.length!=C||v.length<C||j&&v.length>j){u=false}}else{if(v==""){u=false}}if(A.equals){if(!$(A.equals)){throw"Couldn't find element #"+A.equals+". Invalid value for 'equals' attribute"}if(v!=$F(A.equals)){u=false}}}else{if(a&&d.tagName=="SELECT"&&(d.selectedIndex==-1||!d.value)){u=false}}if(x&&u&&a||!a&&v!=""){var m;switch(h){case"email":if(!/^[\w.-]+(?:\+[\w.-]+)?@[\w.-]{1,255}\.[a-z]{2,3}(?:\.[a-z]{2})?$/i.test(v)){u=false}break;case"date":if(w){m=/^(\d+)[\/-](\d+)[\/-](\d+)$/.exec(v);var y=w.split(/[\/-]/);var c=y.indexOf("yyyy")+1;var p=y.indexOf("mm")+1;var B=y.indexOf("dd")+1;if(!m||m&&!Date.isValid(parseInt(m[c]),parseInt(m[p]),parseInt(m[B]))){u=false}}else{m=/^(\d{4})-(\d{2})-(\d{2})$/.exec(v);if(!m||m&&!Date.isValid(m[1],m[2],m[3])){u=false}}break;case"datetime":m=/^(\d{4})-(\d{2})-(\d{2})(?:\s(\d{2}):(\d{2})(?::(\d{2}))?)?$/.exec(v);if(!m||m&&!Date.isValid(m[1],m[2],m[3])||m[4]&&m[4]>24||m[5]&&m[5]>59||m[6]&&m[6]>59){u=false}break;case"time":m=/^(\d{2}):(\d{2})(?::(\d{2}))?$/.exec(v);if(!m||m&&(m[1]>24||m[2]>59||(m[3]&&m[3]>59))){u=false}break;case"phone":if(!/^[0-9_+\(\) \-]+$/.test(v)){u=false}break;case"integer":if(isNaN(parseInt(v))){u=false}break;case"float":if(isNaN(parseFloat(v))){u=false}break;case"alphanumeric":if(!/^\w+$/i.test(v)){u=false}break;case"filename":m=/^[^?*:|<>\\\/]+\.([a-z0-9]+)$/i.exec(v);if(!m){u=false;break}if(w){var i=w.toLowerCase().split(",");if(i.length>0&&!i.include(m[1].toLowerCase())){u=false}}break;case"filepath":m=/^(?:[a-z]:[\\\/])?[^?*:|<>]+\.([a-z0-9]+)$/i.exec(v);if(!m){u=false;break}if(w){var i=w.toLowerCase().split(",");if(i.length>0&&!i.include(m[1].toLowerCase())){u=false}}break;case"dirname":if(!/^[^?*:|<>]+$/i.test(v)){u=false}break;case"ip":case"ipv4":m=/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(v);if(!m||m&&(m[1]>255||m[2]>255||m[3]>255||m[4]>255)){u=false}break;case"url":if(!/(?:^(?:http|https|ftp):\/\/)|(?:^www\.).+?\.[a-z]{2,3}.*?$/i.test(v)){u=false}break;case"range":if(!w){throw"Invalid range for element #"+d.id}var s=w.split(",");if(!isNaN(s[0])){s[0]=parseInt(s[0])}if(!isNaN(s[1])){s[1]=parseInt(s[1])}if(v<s[0]||v>s[1]){u=false}break;case"creditcard":if(!/^\d{16}$/i.test(v)){u=false}break;default:if(A.regexp){try{if(!(new RegExp(A.regexp)).test(v)){u=false}}catch(z){throw A.regexp+" is not a valid RegExp object"}}}}var o=d.id+"-errormsg";var D=$(o);if(!u){if(A.onerror){if(typeof window[A.onerror]!="function"){throw A.onerror+" is not a function"}else{window[A.onerror].call(d)}}if(A.errormsg){if(t){alert(A.errormsg)}else{if(!D){d.insert({after:'<span id="'+o+'"></span>'});$(o).className=n}$(o).innerHTML=" "+A.errormsg}}if(l){d.addClassName(l)}if(f){try{d.focus()}catch(z){}}return false}else{if(D){D.innerHTML=""}if(l){d.removeClassName(l)}}return true};Form.Element.Methods.validate.__tmpidCounter=1;Form.Methods.validate=function(f,d){f=$(f);d=Object.extend({submit:false,alert:false,onbeforesubmit:null,onbeforesubmitparams:[],errorNodeClassName:null,elementErrorClassName:null},d||{});var c=false,g=f.elements;var b={alert:d.alert,focus:true,errorNodeClassName:d.errorNodeClassName,elementErrorClassName:d.elementErrorClassName};for(var e=0,a=g.length;e<a;e++){if(!["SELECT","INPUT","TEXTAREA"].include(g[e].tagName)||g[e].tagName=="INPUT"&&!["text","hidden","file","password"].include(g[e].type)){continue}if(!$(g[e]).validate(b)){c=true;b.focus=false;if(b.alert){break}}}if(!c&&d.onbeforesubmit){c=!d.onbeforesubmit.apply(null,[f].concat(d.onbeforesubmitparams))}if(!c&&d.submit){f.submit()}return !c};Element.addMethods();