
Validator=Class.create();Validator.prototype={initialize:function(className,error,test){this.test=test?test:function(){return true};this.error=error?error:'Validation failed.';this.className=className;}}
var Validation=Class.create();Validation.prototype={initialize:function(form,options){this.options=Object.extend({stopOnFirst:false,immediate:false},options||{});this.form=$(form);Event.observe(this.form,'submit',this.onSubmit.bind(this),false);if(this.options.immediate){Form.getElements(this.form).each(function(input){Event.observe(input,'blur',function(ev){Validation.validate(Event.element(ev).id)});});}},onSubmit:function(ev){if(!this.validate())Event.stop(ev);},validate:function(){if(this.options.stopOnFirst){return Form.getElements(this.form).all(Validation.validate);}else{return Form.getElements(this.form).collect(Validation.validate).all();}}}
Object.extend(Validation,{validate:function(elm,index,options){var options=Object.extend({},options||{});elm=$(elm);var cn=elm.classNames();return result=cn.all(Validation.test.bind(elm));},test:function(name){var v=Validation.get(name);var id='advice-'+name+'-'+this.id;var prop='__advice'+name;if(Validation.isVisible(this)&&!v.test($F(this))){if(!this[prop]){var advice=document.createElement('div');advice.appendChild(document.createTextNode(v.error));advice.className='validacija';advice.id=id;advice.style.display='none';advice.style.marginTop='5px';advice.innerHTML='<div class="arrownovo"></div>'+advice.innerHTML;this.parentNode.insertBefore(advice,this.nextSibling);if(typeof Effect=='undefined'){advice.style.display='block';}else{new Effect.Appear(advice.id,{duration:0.3});}}
this[prop]=true;this.removeClassName('validation-passed');this.addClassName('validation-failed');return false;}else{try{$(id).remove();}catch(e){}
this[prop]='';this.removeClassName('validation-failed');this.addClassName('validation-passed');return true;}},isVisible:function(elm){while(elm.tagName!='BODY'){if(!$(elm).visible())return false;elm=elm.parentNode;}
return true;},add:function(className,error,test,options){var nv={};nv[className]=new Validator(className,error,test,options);Object.extend(Validation.methods,nv);},addAllThese:function(validators){var nv={};$A(validators).each(function(value){nv[value[0]]=new Validator(value[0],value[1],value[2]);});Object.extend(Validation.methods,nv);},get:function(name){return Validation.methods[name]?Validation.methods[name]:new Validator();},methods:{}});Validation.add('GreaterThan','Your Number is too small.',function(v,elm){var min=Validation.getMin(elm);if(!min){return true;}
return(min.toInt<=v.toInt);});Validation.add('LessThan','Your Number is too large.',function(v,elm){var max=Validation.getMax(elm);if(max==null){return true;}
return(max>=v);});Validation.add('Between','Your number is too small or too large.',function(v,elm){var min=Validation.getMin(elm);var max=Validation.getMax(elm);if(!min&&!max){return true;}
if(!max){return(min<=v)}
if(!min){return(max>=v)}
return(min<=v&&v<=max);});Validation.add('Length','Please enter the correct length.',function(v,elm){var min=Validation.getMin(elm);var max=Validation.getMax(elm);return(min<=v.length&&v.length<=max);});Validation.add('EqualTo','Does not match.',function(v,elm){var params=Validation.getParams(elm);var otherField=String(params[0]);return(v==$(otherField).value);});Validation.add('validate-regexp','Please enter a valid format.',function(v,elm){var x=new RegExp(Validation.getParam(elm));return Validation.get('IsEmpty').test(v)||x.test(v);});Validation.add('IsEmpty','',function(v){return((v==null)||(v.length==0)||/^\s+$/.test(v));});Validation.addAllThese([['required','Ovo je obavezno polje.',function(v){return!Validation.get('IsEmpty').test(v);}],['requiredcheck','Morate se složiti sa uslovima.',function(v){return!Validation.get('IsEmpty').test(v);}],['validate-number','Molimo vas da koristite samo brojeve u ovom polju.',function(v){return Validation.get('IsEmpty').test(v)||!isNaN(v);}],['validate-digits','Molimo vas da koristite samo brojeve u ovom polju.',function(v){return Validation.get('IsEmpty').test(v)||!/[^\d]/.test(v);}],['validate-alpha','Molimo vas da koristite samo slova (a-z) u ovom polju.',function(v){return Validation.get('IsEmpty').test(v)||/^[a-zA-Z]+$/.test(v)}],['validate-alphanum','Molimo vas da koristite samo slova (a-z) ili brojeve (0-9) u ovom polju. Razmaci nisu dozvoljeni.',function(v){return Validation.get('IsEmpty').test(v)||!/\W/.test(v)}],['validate-date','Molimo vas da upišete taèan datum.',function(v){var test=new Date(v);return Validation.get('IsEmpty').test(v)||!isNaN(test);}],['validate-email','Molimo vas da upišete taènu email-adresu. Na primjer: mojeime@hotmail.com .',function(v){return Validation.get('IsEmpty').test(v)||/\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(v)}],['validate-date-au','Molimo vas da koristite ovaj datum format: dd/mm/yyyy. Naprimjer 17/03/2006 za 17ti Mart, 2006.',function(v){if(!Validation.get('IsEmpty').test(v)){var upper=31;if(/^(\d{2})\/(\d{2})\/(\d{4})$/.test(v)){if(RegExp.$2=='02')upper=29;if((RegExp.$1<=upper)&&(RegExp.$2<=12)){return true;}else{return false;}}else{return false;}}else{return true;}}],['validate-currency-dollar','Molimo vas da upišete pravu vrijednost u KM ovima. Npr KM100.00 .',function(v){return Validation.get('IsEmpty').test(v)||/^\KM?\-?([1-9]{1}[0-9]{0,2}(\,[0-9]{3})*(\.[0-9]{0,2})?|[1-9]{1}\d*(\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|(\.[0-9]{1,2})?)$/.test(v)}]]);
