function openAjax(){
	var ajax;
		try{
			ajax = new XMLHttpRequest();
		} catch(ee) {
			try{
				ajax = new ActiveXObject("Msxm12.XMLHTTP");
			} catch(e) {
				try {
					ajax = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(E) {
					ajax = false;
				}
			}
		}
	return ajax;
}

//imprimir

function visualizarImprimir(url){
	window.open(url + '?imprimir=1', "imprimir", "height = 768 , width = 1024, location=no, directorys=no");
}

function visualizarImprimir2(url){
	window.open(url + '&imprimir=1', "imprimir", "height = 768 , width = 1024, location=no, directorys=no, scrollbars=yes");
}

function popup(url, titulo, width, height){
	window.open(url, titulo, "height = " + height + " , width = " + width + ", scrollbars=yes, location=no, directorys=no");
}







//Contador de quanta teclas foram digitadas.



var antes = "";

function contaDigitado(idTexto, idContador, max){

	if(document.getElementById(idTexto).value.length > max){

		//alert(key);

		//alert(document.getElementById('mensagem').value.length);

		//var key = (window.event ? event.keyCode : event.which);

		//if(key != 8){

			document.getElementById(idTexto).value = antes; 

		//}

	}

	var valor = max - document.getElementById(idTexto).value.length;

	document.getElementById(idContador).innerHTML = valor;

	antes = document.getElementById(idTexto).value;



}







//imprimir



function imprimir(){



	window.print();



}



//inserir janela







    var iex=(document.all); //internet explorer



	var nav=(document.layers); //netscape



	var old=(navigator.appName=="Netscape" && !document.layers && !document.getElementById);



	var n_6=(window.sidebar); //firefox



		



	function mostra(vId, img, width, height){

		if(!iex){
			document.getElementById(vId).style.marginTop = width + 'px';
			document.getElementById(vId).style.marginLeft = height + 'px';
		}else{
			document.getElementById(vId).style.marginTop = (width + 160) + 'px';
			document.getElementById(vId).style.marginLeft = (height - 190) + 'px';
		}
		document.getElementById(vId).style.display = "block";
		document.getElementById(vId + 'Img').src = img.src;
	}

	function fecha(vId){
		document.getElementById(vId).style.display = "none";
		//document.getElementById(vId).innerHTML = "";//
	}



	



	function movemouse(e, vId) {



		if(iex){



			//alert('fd');



			var posx = 0;



			var minx = (document.body.offsetWidth-1000)/2



			var maxx = minx+700



			



			var posy = 0;



			var miny = (document.body.offsetHeight-1000)/2;



			var maxy = miny+700;



			



			if (!e) var e = window.event



			if (e.pageX){



				posx = e.pageX;



				posy = e.pageY;



			}else if(e.clientX){



				posx = e.clientX + document.body.scrollLeft;



				posy = e.clientY + document.body.scrollTop;



			}



			posx -= 80



			if(posx > maxx) posx = maxx



			if(posx < minx) posx = minx



			



			posy -= 80



			if(posy > maxy) posy = maxy



			if(posy < miny) posy = miny



			



			document.getElementById(vId).style.left = posx+"px"



			document.getElementById(vId).style.top = posy+"px"



		}



	}



	



	function mouse(div){



		var horizontal=window.event.clientX;



		var vertical=window.event.clientY;



		//window.alert(horizontal);



		window.document.all[div].style.top=vertical-295;



		window.document.all[div].style.left=horizontal-130;



	}



	



	function fSegueMouse(e) {



		var posx = 0



		var minx = (document.body.offsetWidth-700)/2



		var maxx = minx+400



		if (!e) var e = window.event



		if (e.pageX) posx = e.pageX



		else if (e.clientX) posx = e.clientX + document.body.scrollLeft



		posx -= 150



		if(posx > maxx) posx = maxx



		if(posx < minx) posx = minx



		document.getElementById('janelinha').style.left = posx+"px"



	}



////////////







function validaCpf(cpf){



	digitosCpf = cpf.substr(9,2);



	digitos = new Array(10);



	for(i=0; i<9; i++){



		digitos[i] = cpf.substr(i, 1);



	}



	var posicao = 10;



	var soma = 0;



	for(i=0; i<9; i++){



		soma += digitos[i] * posicao;



		posicao--;



	}



	



	digitos[9] = soma % 11;



	if (digitos[9] < 2) {



        digitos[9] = 0;



  	}else{



        digitos[9] = 11 - digitos[9];



  	}



  	



  	var posicao = 11;



	var soma = 0;



	for(i=0; i<10; i++){



		soma += digitos[i] * posicao;



		posicao--;



	}







	digitos[10] = soma % 11;



	if (digitos[10] < 2) {



        digitos[10] = 0;



  	}else{



        digitos[10] = 11 - digitos[10];



  	}







	dg = digitos[9] * 10 + digitos[10];



	if (dg != digitosCpf) {



	  return false;



	}else{



		return true;



	}



}







function validaSenha(senha){



	var retorno = "";



	var pontuacao = 0;



	var array = new Array(4);



	



	if(senha.length > 10){  



		pontuacao++;  



	}  



	   



	 if(senha.match(/[a-z_]/i) || senha.match(/[0-9]/)){  



	        pontuacao++;;



	 }  



	  



	if(senha.match(/\W/)){  



	         pontuacao++;



	}  







	switch(pontuacao){		



			case 1: retorno = "<strong><font color='red'>Senha Fraca!</font></strong>"; break;	



			case 2: retorno = "<strong><font color='#FFCC00'>Senha Boa!</font></strong>"; break;



			case 3: retorno = "<strong><font color='green'>Senha &Oacute;tima!</font></strong>"; break;	



		}



	return retorno;



}







function loading(opt, idDiv) {



		//88



	if(opt == true){



		var refer = document.getElementById(idDiv);



		var referHeight = refer.offsetHeight;



		refer.style.textAlign = 'center';



		var img = document.createElement('img');



		img.setAttribute('src','../admin/imagens/progress-loader.gif');



		img.setAttribute('id','loadingAjax');



		img.setAttribute('width','15');



		//img.style.marginTop = '1px';



		



		if(!document.getElementById('loadingAjax')){



			refer.insertBefore(img, refer.firstChild);



		}



	}else if(opt == false) {



		var imgLoading = document.getElementById('loadingAjax');



		if (imgLoading){



			imgLoading.parentNode.removeChild(imgLoading);



		}



	}



}







function ready(idDiv, ajax){



	



	//if (ajax.readyState == 1) {



		//loading(true);	



//	}



	if (ajax.readyState == 4) {



		if (ajax.status == 200) {



		//	loading(false);



			document.getElementById(idDiv).innerHTML = ajax.responseText;



		}



	}



}







function checkMail(mail){



    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);



    if(typeof(mail) == "string"){



        if(er.test(mail)){ return true; }



    }else if(typeof(mail) == "object"){



        if(er.test(mail.value)){ 



            return true; 



        }



    }else{



        return false;



    }



}







function confirmarDelete(frase){
	if(confirm(frase)){
		return true;
	}else
		return false;
}

function editarData(data){
	return false;
}

function modificar(value, id, tipo, url){

	if(isNaN(value)){
		if(value.indexOf("&")){
			value = value.replace("&", "::");
		}
		if(value.indexOf("&")){
			value = value.replace("&", "::");
		}
		if(value.indexOf("&")){
			value = value.replace("&", "::");
		}
	}
	url = '../classes/br/com/nossocasamentoecia/controller/' + url + '.php?valor=' + value + '&tipo=' + tipo + '&id=' + id;
	//document.write(url);
	var ajax = openAjax();
	ajax.open('GET', url, true);
	ajax.send(null);
}

function modificarSomaTotal(value, id, tipo, url, valorGasto, valorTotal, valorAtual){
	if(isNaN(value)){
		if(value.indexOf("&")){
			value = value.replace("&", "::");
		}
	}
	url = '../classes/br/com/nossocasamentoecia/controller/' + url + '.php?valor=' + value + '&tipo=' + tipo + '&id=' + id + "&gasto=" + valorGasto + "&valorTotal=" + valorTotal + "&valorAtual=" + valorAtual;
	//document.write(url);
	var ajax = openAjax();
	ajax.open('GET', url, true);
	ajax.onreadystatechange = function(){
			if(ajax.readyState == 4) {
				if(ajax.status == 200) {
					document.getElementById('restante').innerHTML = ajax.responseText;
				}else{
					alert('Ocorreu um erro. ' + ajax.status);
				}
			}
		}
	ajax.send(null);
}

function changeTemplate(value, img, div){
	//alert("Valor: " + value + "\nImg: " + img + "\nDiv: " + div);
	if(value != ""){
		document.getElementById('designerCss').value = value;
	}
	//alert("img: " + img + "\ndiv: " + div);//136
	showColour(img, div);
}

function modificarTemplate(value, id, idTemplate, url){
	template = document.getElementById('designerCss').value;
	url = '../classes/br/com/nossocasamentoecia/controller/' + url + '.php?valor=' + template + '&idTemplate=' + idTemplate + '&id=' + id;
	//alert(url + "\n" + value);
	location.href=url;
	var ajax = openAjax();
	ajax.open('GET', url, true);
	ajax.send(null);
}

function modificarRetorno(value, url){
	if(value!=""){
		if(isNaN(value)){
			if(value.indexOf("&")){
				value = value.replace("&", "::");
			}
		}
		url = url + '.php?valor=' + value;
		var ajax = openAjax();
		ajax.open('GET', url, true);
		ajax.onreadystatechange = function(){
			if(ajax.readyState == 4) {
				if(ajax.status == 200) {
					loading(false, 'loading');
					document.getElementById('retorno').innerHTML = "&nbsp;&nbsp;" + ajax.responseText;
				}else{
					loading(false, 'loading');
					alert('Ocorreu um erro. ' + ajax.status);
				}
			}else if(ajax.readyState < 4){
				loading(true, 'loading');
			}
		}
		ajax.send(null);
	}
}



var retorno = false;

function setRetorno(r){
	retorno = r;
}

function getRetorno(){
	return retorno;
}

//Retorno e modifica o titulo
function modificarRetornoTitulo(value, url, idTd, conteudoRetorno, idTitulo, tituloRetorno, idLoading){
	if(value!=""){
		if(isNaN(value)){
			if(value.indexOf("&")){
				value = value.replace("&", "::");
			}
		}
		url = url + '.php?valor=' + value;
		//alert(url);
		var ajax = openAjax();
		ajax.open('GET', url, true);
		ajax.onreadystatechange = function(){
			if(ajax.readyState == 4) {
				//alert(ajax.status + " | " + ajax.statusText);
				if(ajax.status == 200) {
					//loading(false, idLoading);
					document.getElementById(idTd).innerHTML = "&nbsp;&nbsp;" + ajax.responseText;
					if(
						   !(ajax.responseText.indexOf('red') == -1) 
						|| !(ajax.responseText.indexOf('x.png') == -1)
					){
						chamaErros();
						//document.getElementById(idTitulo).innerHTML = "<font color='red'>" + tituloRetorno + "</font>";
						setRetorno(false);
					}else if(
						   (ajax.responseText.indexOf('red') == -1) 
						|| (ajax.responseText.indexOf('x.png') == -1)
					){
						tiraErros();
						//document.getElementById(idTitulo).innerHTML = tituloRetorno;
						setRetorno(true);
					}
				}else{
					chamaErros();
					alert('Ocorreu um erro. ' + ajax.statusText);
					setRetorno(false);
				}
			}
		}
		ajax.send(null);
	}else{
		document.getElementById(idTd).innerHTML = conteudoRetorno;
		//document.getElementById(idTitulo).innerHTML = "<strong>" + tituloRetorno + "</strong>";
		setRetorno(false);
	}
	return getRetorno();
}

function verBotaoEnter(valor, id, tipo, url){
	if((window.event ? event.keyCode : event.which) == 13){
		modificar(valor, id, tipo, url);
		return false;
	}
}

function retornoModificar(ajax, tituloRetorno, idTitulo){

}

function showHide(id){
	if(document.getElementById(id).style.display == "none"){
		document.getElementById(id).style.display = 'block';
	} else {
		document.getElementById(id).style.display = 'none';
	}
}

function showColour(imagem, div){
	document.getElementById(div + '_janelinhaImg').src = 'templates/imagens/thumbnail/p_' + imagem;
	imagem = 'http://www.casamentoecia.com.br/nossocecia/templates/imagens/thumbnail/p_' + imagem;
	//alert(document.getElementById("corTemplate").value);
	document.getElementById(div).src = imagem;
}

function teste(value, url){
	url = url + '.php?valor=' + value;
	location.href=url;
}

function validaGuest(){
	var msg = "Atenção aos campos abaixo: \n==================================\n";
	var erros = 0;

	if(document.getElementById('nome').value == ""){
		msg += "\nNOME: O campo não pode estar vÃ¡zio.";
		erros = 1;
	}

	if(!(checkMail(document.getElementById('email').value))){
		msg += "\nE-MAIL: O E-Mail informado não é válido.";
		erros = 1;
	}

	if(erros==1){
		alert(msg);
		return false;
	}
}

function EnviarPostGuest(){
	//if(!validaGuest()) return false;
	
	var sendPost = "";
	sendPost += "nome=" + document.getElementById('nome').value;
	sendPost += "&mensagem=" + document.getElementById('mensagem___Frame').value;
	sendPost += "&email=" + document.getElementById('email').value;
	sendPost += "&usuario=" + document.getElementById('usuario').value;
	alert(sendPost);
	exit;
	var url = '../nossocecia/classes/br/com/nossocasamentoecia/controller/AdicionarGuest.php?controlador=1';
	
	var ajax = openAjax();
	ajax.open('POST', url, true);
	ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajax.onreadystatechange = function(){
		if(ajax.readyState == 4) {
			if(ajax.status == 200) {
				alert("A sua mensagem foi inserida com sucesso.\nEstará no ar, assim que for aprovada pelos noivos.");
				mudaConteudo("visitas.php");
			}else{
				alert('Ocorreu um erro. ' + ajax.status);
			}
		}
	}
	ajax.send(sendPost);
}

function trocaImg(img, titulo, legenda){
	$("#imgPrincipal").hide("blind", { direction: "vertical" }, 1000);
   	var novaImg = "<div class='livro_formulario'>" + titulo + "</div><br />";
	novaImg += "<img id='imgPrincipal' src='" + img.src + "'>";
	novaImg += "<br /><div class='livro_formulario'>" + legenda + "</div>";
    setTimeout(function(){
   	document.getElementById('fotoMudar').innerHTML = novaImg;
   } , 1000); 
}

//var ie = navigator.appName;
//ie = ie.indexOf('Internet Explorer');
//if(ie >= 0)
//document.onmousedown=new Function("if (event.button>1) alert('Copyright Casamento & Cia')")
