// JavaScript Document
var ie = /msie/i.test(navigator.userAgent);

function openAjax() {
	try{
    	ajax = new XMLHttpRequest();
	}catch(ee){
    	try{
        	ajax = new ActiveXObject("Msxml2.XMLHTTP");
	    }catch(e){
    	    try{
        	    ajax = new ActiveXObject("Microsoft.XMLHTTP");
	        }catch(E){
    	        ajax = false;
        	}
	    }
	}
	return ajax;
}
var ajax = openAjax();

function Request(method, pagina, form){
	var tipo = document.getElementById("erro") ? "1" : "2";
	if (validar(form,tipo) != false){
		var variaveis = montaVariaveis(form);
		ajax.open(method,pagina+(method == "GET" ? variaveis : ""),true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		ajax.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
		ajax.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
		ajax.setRequestHeader("Pragma", "no-cache");
	
		ajax.onreadystatechange = function(){
			if (ajax.readyState == 4){
				if (ajax.status == 200){
					retorno = unescape(ajax.responseText.replace(/\+/g," "));
					parent.block.conteudo.innerHTML = retorno;
				}else{
					alert("Problemas ao carregar o arquivo.");
				}
			}
		}
		ajax.send((method == "GET" ? null : variaveis));
	}
}

function montaVariaveis(d){
	var elementos = d.elements;
	var variaveis = "&";
	for (i = 0; i < elementos.length; i++){
		if (elementos[i].type != 'button' && elementos[i].type != 'submit' && elementos[i].type != 'reset')
			if (elementos[i].type == 'radio'){
				if (elementos[i].checked) variaveis += elementos[i].name+'='+elementos[i].value+"&";
			}else
				variaveis += elementos[i].name+'='+elementos[i].value+"&";
	}
	variaveis += 'dummy='+new Date().getTime();
	return variaveis;
}


function validar(d, tipo){
	//tipo 1 -> erro com div flutuante
	//tipo 2 -> printar erro nos elementos
	try{
		var erro_desc = "";	
		var elementos = d.elements;
		var erro = false;
		var pos = 0;
		for(i = 0; i < elementos.length; i++){
			if (elementos[i].title != ""){
				if(elementos[i].id != null){
					if(elementos[i].type == 'text'){
						if(elementos[i].value.length == ""){
							erro_desc +=  "<p style=\"padding-top:5px;width:310px;\">O campo <strong>"+elementos[i].title+"</strong> deve ser preenchido</p>";
							elementos[i].className = 'erro_input';
							erro = true;
						}else
							elementos[i].className = '';
						if (elementos[i].name.substring(0,5) == "email" && elementos[i].value != ""){
							var reEmail2 =/^[\w-]+(\.[\w-]+)*@(([\w-]{2,63}\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
							if(!reEmail2.test(elementos[i].value)){
								erro_desc +=  "<p style=\"padding-top:5px;width:310px;\">O campo <strong>"+elementos[i].title+"</strong> deve conter um endereço eletrônico válido !</p>";
								elementos[i].className = 'erro_input';
								erro = true;
							}else
								elementos[i].className = '';
						}
					}
					if(elementos[i].type == 'file'){
						if(elementos[i].value.length == ""){
							erro_desc +=  "<p style=\"padding-top:5px;width:310px;\">O campo <strong>"+elementos[i].title+"</strong> deve ser preenchido</p>";
							elementos[i].className = 'erro_input';
							erro = true;
						}else
							elementos[i].className = '';
					}
					if(elementos[i].type == 'password'){
						pos++;
						if (elementos[i-pos].name != "trocaSenha" || (elementos[i-pos].name == "trocaSenha" && elementos[i-pos].checked == true)){
							if(elementos[i].value.length == ""){
								erro_desc +=  "<p style=\"padding-top:5px;width:310px;\">O campo <strong>"+elementos[i].title+"</strong> deve ser preenchido</p>";
								elementos[i].className = 'erro_input';
								erro = true;
							}else{
								if (elementos[i].name == "rsenha"){
									if (elementos[i-1].value != elementos[i].value){
										erro_desc +=  "<p style=\"padding-top:5px; width:310px;\">Senhas não conferem !</p><p style=\"padding-top:5px; width:310px;\">Campos <strong>"+elementos[i-1].title+"</strong> e <strong>"+elementos[i].title+"</strong> devem conter o mesmo valor</p>";
										elementos[i].className = 'erro_input';
										elementos[i-1].value = "";
										elementos[i].value = "";
										elementos[i-1].focus();
										erro = true;
									}
								}
							}
						}
					}
					if(elementos[i].type == 'select-one'){
						if(elementos[i].value == '0'){
						   erro_desc +=  "<p style=\"padding-top:5px; width:310px;\">O campo <strong>"+elementos[i].title+"</strong> deve ser selecionado</p>";
						   elementos[i].className = 'erro_input';
						   erro = true;
						}else
							elementos[i].className = 'borda_input';
					}
					if(elementos[i].type == 'textarea'){
						if(elementos[i].value.length < 3){	
						   erro_desc +=  "<p style=\"padding-top:5px; width:310px;\">O campo <strong>"+elementos[i].title+"</strong> deve ser preenchido</p>";
						   elementos[i].className = 'erro_input';
						   erro = true;
						}else
							elementos[i].className = 'borda_input';
					}
					if (elementos[i].type == 'radio'){
						if (!elementos[i].checked && (elementos[i+1].type == 'radio' && !elementos[i+1].checked )){
						   erro_desc +=  "<p style=\"padding-top:5px; width:310px;\">O campo <strong>"+elementos[i].title+"</strong> deve ser selecionado</p>";
						   elementos[i].className = 'erro_input';
						   erro = true;
						}
					}
				}
			}
		}
		if(erro){
			if (tipo == "1"){
				document.getElementById("erro").style.display = 'block';
				document.getElementById("erro_itens").innerHTML = erro_desc;
			}
			return false;
		}else{
			if (tipo == "1"){
				document.getElementById("erro").style.display = 'none';
				document.getElementById("erro_itens").innerHTML = '';
			}
			return true;
		}
	}catch(e){
		alert('problema no formulario');
		alert(e);
		return false;
	}
}


function Pagina(caminho,width, height){
	this.caminho = caminho;
	this.width = width;
	this.height = height;
	this.bodypage =  document.body;
	this.bodyblock = document.createElement("div");
	this.pagina = document.createElement("div");
	this.carregando = document.createElement("img");
	this.conteudo = document.createElement("div");
	this.imgClose = document.createElement("img");
	this.ifrm = document.createElement("iframe");
}


var block = null;
function Block(caminho, width, height){
	block = new Pagina(caminho, width, height);

	block.bodyblock.id = "bodyblock";
	setHeightBlock(block.bodyblock);

	block.pagina.id = "pagina";
	block.pagina.style.width = block.width+"px";
	block.pagina.style.height = block.height+"px";
	
	block.pagina.style.top = (heightTotal() / 3)+"px";
	block.pagina.style.left = (screen.width / 2.8)+"px";
	block.pagina.style.paddingBottom = (ie ? "0px" : "10px");
	
	block.carregando.src = "http://www.redeclaret.com.br/imagens/carregando.gif";
	block.carregando.style.marginTop = block.height/2+"px";
	
	block.imgClose.src = "http://www.redeclaret.com.br/imagens/fechar.gif";
	block.imgClose.alt = "fechar";
	block.imgClose.className = "close";

	if (document.getElementById("player"))document.getElementById("player").style.display = "none";
	var star = document.getElementsByTagName("ul");
	for (i = 0; i < star.length; i++){
		if (star[i].className == "star-rating") star[i].style.display = 'none';
	}
	
	block.imgClose.onclick = function(){
		while(block.pagina.firstChild){
			block.pagina.removeChild(block.pagina.firstChild);
		}
		block.bodypage.removeChild(block.bodyblock);
		block.bodypage.removeChild(block.pagina);

		if (document.getElementById("player"))document.getElementById("player").style.display = "block";
		var star = document.getElementsByTagName("ul");
		for (i = 0; i < star.length; i++){
			if (star[i].className == "star-rating") star[i].style.display = 'block';
		}
	}
	
	block.ifrm.id = 'ifrm';
	block.ifrm.name = 'ifrm';
	block.ifrm.width = block.width+'px';
	block.ifrm.height = block.height+"px";
	block.ifrm.frameBorder = "no";
	block.ifrm.style.display = 'none';
	
	block.bodypage.appendChild(block.bodyblock);
	block.bodypage.appendChild(block.pagina);
	block.pagina.appendChild(block.imgClose);
	block.pagina.appendChild(block.carregando);
	block.pagina.appendChild(block.conteudo);

	setTimeout(function(){
		block.ifrm.src = block.caminho+"dummy="+new Date().getTime();
		block.conteudo.appendChild(block.ifrm);
	}, 100);

	block.ifrm.onload = carrega();
}

function carrega(){
	if (block.pagina.childNodes.length > 2)	setTimeout(function(){block.pagina.removeChild(block.carregando);block.ifrm.style.display = 'block';},200);
	
}



function setHeightBlock(bodyblock){
	var h = heightTotal();
	if (h > document.documentElement.scrollHeight)
		bodyblock.style.height = parseInt(document.documentElement.scrollHeight + (h - document.documentElement.scrollHeight)) + "px";
	else	
		bodyblock.style.height = document.documentElement.scrollHeight + 'px';	
}

function heightTotal(){
	var ieBox = ie && (document.compatMode == null || document.compatMode == "BackCompat");
	var canvasEl = ieBox ? document.body : document.documentElement;
	var h = window.innerHeight || canvasEl.clientHeight;
	return h;
}

function getScrollXY(){
	var scrOfX = 0;
	var scrOfY = 0;
	if (typeof(window.pageYOffset) == "number"){
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	}else if (document.body && ( document.body.scrollLeft || document.body.scrollTop)){
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	}else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)){
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return [scrOfX,scrOfY];
}




function preencheSelect(id,idS){
	var x = document.getElementById("lc");
	x.options.length=1;
	x.options[0].text = 'carregando...';
	ajax.open('GET','categorias.jsp?id=' + id + '&dummy=' + new Date().getTime(),true);
	ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');	
	ajax.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
	ajax.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
	ajax.setRequestHeader("Pragma", "no-cache");
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4){
			retorno=unescape(ajax.responseText.replace(/\+/g," "));
			var spl = retorno.split("##");
			var spl2 = "";
			if(spl.length > 1)
				x.options[0] = new Option("Selecione","0");
			for (i = 0; i < spl.length-1; i++){
				spl2 = spl[i].split("#");
				x.options[i+1] = new Option(spl2[1],parseInt(spl2[0]));
				if(idS != 0 && idS == spl2[0])
					x.options[i+1].selected = true;
			}
		}
	}
	ajax.send(null);
	x.disabled = false;
}