/*
	w3FormValidation (2.2) - 10/12/2006
	Por Leandro Vieira Pinho - http://leandro.w3invent.com.br
	
	Para informações de uso deste add-on visite:
	http://leandro.w3invent.com.br/addons/w3FormValidation/

	Parâmetros que podem ser utilizados para ATIVAR ou DESATIVAR recursos deste add-on
	var valida = new w3FormValidation(param1,param2,param3,param4,param5);
	param1 {Array}    = Campos a serem validados.
	param2 {string}   = Título do box das mensagens de erro. Padrão: Observações!
	param3 {integer}  = Distância que o box terá em relação ao topo da página. Padrão: 0
	param4 {boolean}  = Coloque false para não utilizar o "overlay" sobre o body. Padrão: true
	param5 {boolean}  = Coloque false para o box não acompanhar a rolagem da página. Padrão: true

	O código HTML gerado para exibir o box com as mensagens é o seguinte:
	
	<div id="w3ShowMsgToUser">
		<h3>Observações!</h3>
		<ol>
			<li>Mensagem</li>
			<li>...</li>
		</ol>
		<input id="w3ShowMsgToUserBtnOk" value="Ok" type="button">
	</div>
	
	Você pode estilizá-lo através das CSS e deixá-lo como desejar.
	
	Funções de terceiros utlizadas neste add-on
	
	Função: getPageSize Autor: PPK
	Função: getScrollingPosition Autor: James Edwards/Cameron Adams
	
	Tipos de validação inspirado no JS Validation de Beenjamin Keen (.com)

*/
function w3FormValidation(c,l,I,O,i){var C=this;C.arrInfoVal=c;if(typeof l=="undefined"){C.strTitleMsgToUser='Observações!'}else{C.strTitleMsgToUser=l}
if(typeof I=="undefined"){I=0}
C.top=I;if(typeof O=="undefined"){C.boolEncobrirBody=true}
var o=C.getScrollingPosition();C.atualScrollingPositionY=o[1];if(typeof i=="undefined"){i=true}
if(i){addEvent(window,'scroll',C.fixMsgToUserInViewPort,false,[C.top])}};w3FormValidation.prototype.fixMsgToUserInViewPort=function(l){var i=w3FormValidation.prototype.getScrollingPosition(),I=i[1];if($('w3ShowMsgToUser')){$('w3ShowMsgToUser').style.top=I+parseInt(l,'')+'px'}};w3FormValidation.prototype.start=function(){var I=this,i=I.validation(I.arrInfoVal);return i};w3FormValidation.prototype.validation=function(E){var u=this,l=[],v=[],I=true;for(i=0;i<E.length;i++){var O=E[i].split(','),e=true,w,V,c,Z,z;while(O[0].match("^if:")){w=O[0].replace("if:","");if(w.match(":")){var x=w.split(":"),X=x[0];V=x[1].split("=");c=V[0];Z=V[1];for(var y=0;y<$(X)[c].length;y++){if($(X)[c][y].checked){z=$(X)[c][y].value}}}else{V=w.split("=");c=V[0];Z=V[1];z=$(c).value}
if(z!=Z){e=false;break}else{O.shift()}}
if(!e){continue}
var W=O[0],o=O[1],C='';if(O.length==4){var T=O[2];C=O[3]}else{C=O[2]}
if(W.match("^length=")){var K=W;W="length"}
if(W.match("^range=")){var Q=W;W="range"}
switch(W){case 'req':if(o.match(":")){var x=o.split(":"),X=x[0],S=x[1];if(typeof($(X)[S].type)=="undefined"){var s=false;for(var y=0;y<$(X)[S].length;y++){if($(X)[S][y].checked){s=true}}
if(!s){l.push(C);I=false}}}else{removeClass($(o),'w3FieldError');if(!$(o).value){l.push(C);v.push($(o));I=false}}
break;case 'email':removeClass($(o),'w3FieldError');if(!u.isEmail($(o).value)){l.push(C);v.push($(o));I=false}
break;case 'url':removeClass($(o),'w3FieldError');if(!u.isURL($(o).value)){l.push(C);v.push($(o));I=false}
break;case 'same':removeClass($(o),'w3FieldError');if($(o).value!=$(T).value){l.push(C);v.push($(o));I=false}
break;case 'digits':removeClass($(o),'w3FieldError');if(!$(o).value||$(o).value.match(/\D/)){l.push(C);v.push($(o));I=false}
break;case 'length':removeClass($(o),'w3FieldError');var R=K.replace("length=",""),q=R.match(/[^_]+/),k=q[0].split("-");if(k.length==2){if(($(o).value.length<k[0])||($(o).value.length>k[1])){l.push(C);v.push($(o));I=false}}else{if($(o).value.length!=k){l.push(C);v.push($(o));I=false}}
break;case 'range':removeClass($(o),'w3FieldError');var t=Q.replace("range=",""),r=t.split("-");if(($(o).value.match(/\D/))||($(o).value<Number(r[0]))||($(o).value>Number(r[1]))){l.push(C);v.push($(o));I=false}
break;default:alert('O tipo de validação desejado não é suportado ou desconhecido pelo w3FormValidation.\n\n-------------------------\nTipo de validação: '+W+'\n-------------------------\n\nConsulte a documentação do programa para maiores detalhes em:\n\nhttp://leandro.w3invent.com.br.');return false}}
if(I){return true}else{for(var Y=0;Y<v.length;Y++){addClass(v[Y],'w3FieldError')}
u.showMsgToUser(l);return false}};w3FormValidation.prototype.isEmail=function(i){return i.search(/(\w[\w\.\+]+)@(.+)\.(\w+)$/)==0};w3FormValidation.prototype.isURL=function(i){return i.search(/http(s)?:\/\/(www.)?([\w.]+)(\.\w{2,4})+$/)==0};w3FormValidation.prototype.showMsgToUser=function(I){var O=this;O.closeMsgToUser('noFade');if(O.boolEncobrirBody){O.encobrirBody()}
var l=[];for(i=0;i<I.length;i++){l.push($new('li',I[i]))}
$append($new('div','id=w3ShowMsgToUser',[$new('h3',O.strTitleMsgToUser),$new('ol',l),$new('form',$new('input',["type=button","value=Ok","id=w3ShowMsgToUserBtnOk"],""))]));var o=new w3FadingOpacity('w3ShowMsgToUser',0,100,100);o.start();$('w3ShowMsgToUser').style.top=O.atualScrollingPositionY+O.top+'px';addEvent($('w3ShowMsgToUserBtnOk'),'click',O.closeMsgToUser,false);$('w3ShowMsgToUserBtnOk').focus()};w3FormValidation.prototype.encobrirBody=function(){var I=getPageSize();$append($new("div","id=w3BodyOverlay",''));$('w3BodyOverlay').style.height=arrayPageSize[1]+'px';var i=new w3FadingOpacity('w3BodyOverlay',0,80,100);i.start()};w3FormValidation.prototype.closeMsgToUser=function(l){if(l!='noFade'){var i=new w3FadingOpacity('w3ShowMsgToUser',100,0,100,I);i.start()}
function I(){$remove($('w3ShowMsgToUser'));$remove($('w3BodyOverlay'))}};w3FormValidation.prototype.getScrollingPosition=function(){var i=[0,0];if(typeof window.pageYOffset!='undefined'){i=[window.pageXOffset,window.pageYOffset]}
else if(typeof document.documentElement.scrollTop!='undefined'&&document.documentElement.scrollTop>0){i=[document.documentElement.scrollLeft,document.documentElement.scrollTop]}
else if(typeof document.body.scrollTop!='undefined'){i=[document.body.scrollLeft,document.body.scrollTop]}
return i};function getPageSize(){var o,l;if(window.innerHeight&&window.scrollMaxY){o=document.body.scrollWidth;l=window.innerHeight+window.scrollMaxY}else if(document.body.scrollHeight>document.body.offsetHeight){o=document.body.scrollWidth;l=document.body.scrollHeight}else{o=document.body.offsetWidth;l=document.body.offsetHeight}
var I,i;if(self.innerHeight){I=self.innerWidth;i=self.innerHeight}else if(document.documentElement&&document.documentElement.clientHeight){I=document.documentElement.clientWidth;i=document.documentElement.clientHeight}else if(document.body){I=document.body.clientWidth;i=document.body.clientHeight}
if(l<i){pageHeight=i}else{pageHeight=l}
if(o<I){pageWidth=I}else{pageWidth=o}
arrayPageSize=new Array(pageWidth,pageHeight,I,i)};function w3FadingOpacity(O,i,l,I,o){var c=this;c.intOffset=10;c.strID=O;c.intOpacityStart=i;c.intOpacityEnd=l;c.intFadeDuration=I;c.fnAfterFaded=o;c.intInterval=0};w3FadingOpacity.prototype.start=function(){var thisClass=this;this.intInterval=setInterval(function(){thisClass.fadeCycle()},(this.intFadeDuration/this.intOffset))};w3FadingOpacity.prototype.fadeCycle=function(){var i=this;if(i.intOpacityEnd<i.intOpacityStart){if(i.intOpacityStart>i.intOpacityEnd){i.intOpacityStart-=i.intOffset}else{i.finalize()}}else{if(i.intOpacityStart<i.intOpacityEnd){i.intOpacityStart+=i.intOffset}else{i.finalize()}}
i.setOpacity(i.intOpacityStart/100)};w3FadingOpacity.prototype.setOpacity=function(i){var I=this;if($(I.strID)){$(I.strID).style.opacity=i;$(I.strID).style.filter="alpha(opacity="+(i*100)+")"}};w3FadingOpacity.prototype.finalize=function(){var i=this;i.intOpacityStart=i.intOpacityEnd;clearInterval(i.intInterval);if(i.fnAfterFaded){i.fnAfterFaded()}};