/*_____________________________________Validadores ___________________________________*/

//valida numero
function numero(n){if(!n.match(/^[-]?[0-9]*$/)){return true}else{return false}}

//valida valor
function moeda(n){if(!n.match(/^[0-9,\.]*$/)){return true}else{return false}}

//valida e_mail
function mail(email){if(!email.match(/[A-Za-z0-9_.-]+@([A-Za-z0-9_-]+\.)+[A-Za-z]{2,4}/)){return true}else{return false}}

//valida telefone
function telefone(fone){if(!fone.match(/^[0-9,]*$/)||fone.length<7){return true}else{return false}}

//valida cep
function cep(cep){if(!cep.match(/^[0-9]*$/)||cep.length<8){return true}else{return false}}

//valida data
function data(data){if (data.match(/(0[1-9]|[12][0-9]|3[01])(\/)(0[1-9]|1[012])(\/)[12][0-9]{3}/)){return true}else{return false}}

//Valida cpf
function cpf(cpf){
	while (cpf.match(/[^0-9]/)){
		cpf=cpf.replace(/[^0-9]/,'')
	}
	if (cpf.length==11){
		c=cpf.substr(0,9);
		v=cpf.substr(9,2);
		d=0;
		val=true;
		for (j=0;j<9;j++){
			d+=c.charAt(j)*(10-j);
		}
		d==0 ? val=false:null;
		d=(11-(d%11))>9 ? 0:11-(d%11);
		v.charAt(0)!=d ? val=false:null;
		d*=2;
		for (j=0;j<9;j++){
			d+=c.charAt(j)*(11-j);
		}
		d=(11-(d%11))>9 ? 0:11-(d%11);
		v.charAt(1)!=d ? val=false:null;
		cpf.match(/0{11}|1{11}|2{11}|3{11}|4{11}|5{11}|6{11}|7{11}|8{11}|9{11}/) ? val=false:null;
	} else {
		val=false
	}
	return val 
}

//validaCGC
function cgc(cnpj){
	while (cnpj.match(/[^0-9]/)){
		cnpj=cnpj.replace(/[^0-9]/,'')
	}
	val=0;
	ind=2;
	for (y=12;y>0;y--){
		val+=cnpj.substr(y-1,1)*ind;
		ind>8 ? ind=2 : ind++;
	}
	val%=11;
	val==0||val==1 ? val=0 : val=11-val;
	val!=cnpj.substr(12,1)||cnpj.length!=14 ? vc=false : vc=true;
	return vc;
}
//funcao de busca 
function w_usuario(pai,param){
	wdor.innerHTML=param==0 ?"<b>Buscar: </b><input type=text name=wr class=text maxlenght=30><input type=button class=button onclick=\"window.location='?pai="+pai+"&w='+wr.value\" value='ok'><input type=button value=' x ' onclick=w_usuario(1) class=button>":'';
}

/*_____________________________________Sistema________________________________________*/

//função de postagem
function posta(id,valor,nome){
	conf= nome=='apaga' ? confirm('O item excluído não\npoderá mais ser recuperado\n\nDeseja realmente continuar'):conf=true
	conf? window.location='?pai='+pai.value+'&i='+i.value+'&id='+id+'&'+nome+'='+escape(valor):null;
}

//Gera caixa
caixa=true
function c(id,nome,valor,tamanho,caracteres,tipo){
	 if (tipo==0) {
	 	botao='<input type=button onclick=posta('+id+','+nome+'.value,"'+nome+'") value=ok class=button>'
		tipo='text'
	 }
	 if (tipo==1) {
	 	botao='<input type=submit value=ok class=button>'
		tipo='pasword'
	 }
	if (caixa){
		l=document.all[nome+id]
		bkp=l.innerHTML
		valor=unescape(valor)
		while (valor.match('"')){
			valor=valor.replace('"','&xquot;')
		}
		l.innerHTML='<input type='+tipo+' name='+nome+' value="'+valor+'" class=text size='+tamanho+' maxlength='+caracteres+' onclick=this.select()>'+botao+'<input type=button value=" X " class=button onclick=retorna('+id+')>'
	} else {
		l.innerHTML=bkp
		caixa=true
		c(id,nome,valor,tamanho,caracteres,tipo)
	} 
	caixa=false

}
function retorna(id){
	!caixa ? l.innerHTML=bkp:null;
	caixa=true
}

//arrasta objeto
move=0
drag=0
window.document.onmousemove = mouseMove
window.document.onmousedown = mouseDown
window.document.onmouseup = mouseUp
window.document.ondragstart = mouseStop
function mouseDown() {
	if (drag) {
		clickleft = window.event.x - parseInt(dragObj.style.left)
		clicktop = window.event.y - parseInt(dragObj.style.top)
		dragObj.style.zIndex += 1
		move = 1
	}
}
function mouseStop() {
	window.event.returnValue = false
}
function mouseMove() {
	if (move) {
		dragObj.style.left = window.event.x - clickleft
		dragObj.style.top = window.event.y - clicktop
	}
}
function mouseUp() {
	move = 0
}

//exibe camadas
bbkp=null
bkp=null
function ver(cam,com){//camada,comando
	if (!window.location.href.match("editorvisual")){
		bkp?l.innerHTML=bkp:null
		bbkp?document.all[bbkp].style.visibility='hidden':null
		bbkp=cam
	}
	if(window.event){
		y=document.body.scrollTop+window.event.y
		x=document.body.scrollLeft+window.event.x
		if (x+document.all[cam].clientWidth>document.body.clientWidth)x=x-(x+document.all[cam].clientWidth-document.body.clientWidth)-5
	}
	document.all[cam].style.visibility=document.all[cam].style.visibility=='visible'?'hidden':'visible'
	document.all[cam].style.top=y
	document.all[cam].style.left=x
}

//Paletas de cores
function cor(){}
function pic(k){
	l=lcp.value
	cor.value=k
	pmc.style.visibility='hidden' 
	if (k!='null'){
		if (l=='backcolor'||l=='forecolor') {
			editar(l,k)		
		} else {
			document.all['c'+l].style.background=k
			document.all[l].value=k
		}
	}
}

function corp(v){
	lcp.value=v
	pmc.style.top=document.body.scrollTop+window.event.y
	pmc.style.left=document.body.scrollLeft+window.event.x
	pmc.style.visibility='visible'
}

//posta enquete
function enquete_posta(id){
	quant=document.enquete_post.r.length
	for (a=0;a<quant;a++){
		if (document.enquete_post.r[a].checked) {
			document.enquete_post.resposta.value=a
			document.enquete_post.submit()
		}
	}
}

//imprime conteudo visualizado
function imprime(camada){
	imprimir=window.open('','imprime','toolbar=0,location=0,directories=0,status=0,menubar0,scrollbars=1,resizable=0,width=570,height=600')
	imprimir.document.open();
	imprimir.document.write('<html><head><title>Imprimir</title><link rel=stylesheet href=_gravar/css.css></head><body topmargin=0 leftmargin=0 onload=self.print()><table border=0 width=550 cellspacing=0><tr><td>'+document.all[camada].innerHTML+'</td></tr></table></body></html>')
	imprimir.document.close()
}

//funções ajax
var req;
camada='';
function carrega(url,cam){
	camada=cam
	req = null;
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
			req = new ActiveXObject('Msxml2.XMLHTTP.4.0');
		} catch(e) {
			try {
				req = new ActiveXObject('Msxml2.XMLHTTP.3.0');
			} catch(e) {
				try {
				req = new ActiveXObject('Msxml2.XMLHTTP');
				} catch(e) {
					try {
						req = new ActiveXObject('Microsoft.XMLHTTP');
					} catch(e) {
						req = false;
					}
				}
			}
		}
	}
	if (req) {
		req.onreadystatechange = processa;
		req.open('GET', url, true);
		req.send('');
	}	
}
function processa(){
	if (req.readyState == 4) {
		if (req.status == 200) {
 			document.getElementById(camada).innerHTML = unescape(req.responseText); 
		} else { 
			document.getElementById(camada).innerHTML = unescape('Houve um problema ao obter os dados:' + req.statusText);
		}
	}
}

//menu horizontal
function menum(Menu,e){
	ie=document.all
	if (window.menu&&menu.id!=Menu.id) menue(menu)
   		menu=Menu
		menu.style.visibility=="hidden"?menu.style.visibility="visible":menue()
		menu.style.left=document.body.scrollLeft+e.clientX-(ie?e.offsetX:e.layerX)
		menu.style.top=document.body.scrollTop+e.clientY-(ie?e.offsetY-15:e.layerY-17)
		ie?event.cancelBubble=true:e.stopPropagation()
}
function menue() {
	if (window.menu) menu.style.visibility="hidden"
}
document.onclick=menue
