// #####################################
// Funções feita por Diego Massarotte
// Contato: diego@apiceinternet.com.br
// #####################################


function validateForm() {
	
	if (document.getElementById("nome").value == ""){
		document.getElementById("nome").focus();
		window.alert("Favor preencher o campo nome");		
		return false;
	}
	if (document.getElementById("email").value == "") {
		window.alert("Favor preencher o campo e-mail");
		document.getElementById("email").focus();
		return false;
	}
	if (document.getElementById("texto").value == "") {
		window.alert("Favor preencher o campo Mensagem");
		document.getElementById("texto").focus();
		return false;
	}
	
	return true;
}

function validateForm2(id123) {
	
	if (document.getElementById("nome").value == ""){
		document.getElementById("nome").focus();
		window.alert("Favor preencher o campo nome");		
		return false;
	}
	if (document.getElementById("email").value == "") {
		window.alert("Favor preencher o campo e-mail");
		document.getElementById("email").focus();
		return false;
	}
	if (document.getElementById("email").value != "") {
		pattern=".+@.+\\.[a-z]+$";
		if(!document.getElementById("email").value.match(pattern))
		{
			document.getElementById("email").focus();
			alert("Preencha um e-mail valido.");
			return false;
		}
	}
	if (document.getElementById("texto").value == "") {
		window.alert("Favor preencher o campo Mensagem");
		document.getElementById("texto").focus();
		return false;
	}
	
	document.getElementById("form2").action = "http://www.catamara.eng.br/envia-formulario/1/" + id123;
	document.getElementById("form2").submit();
	
}


function ajaxInit() {
	var xmlhttp ;
	try {
		xmlhttp = new XMLHttpRequest() ;
	} catch (ee) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
		try {
			 xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
	}
	return xmlhttp ;
}

function include(url,id,parametros) {
	mostra = document.getElementById(id) ? document.getElementById(id) : document.all[id];
	ajax = ajaxInit();
	if (ajax) {
		parametros = "?" + Math.ceil(Math.random() * 100000) + (parametros != null ? parametros : '');
		ajax.open('GET',url,true);
		ajax.onreadystatechange = function() {
			if (ajax.readyState == 4) {
				if (ajax.status == 200) {
					texto=unescape(ajax.responseText.replace(/\+/g," "));
					mostra.innerHTML = texto;
					extraiScript(texto);
				}
			}
		}
		ajax.send(null);
	}
}

function include2(url,id,parametros) {
	mostra = document.getElementById(id) ? document.getElementById(id) : document.all[id];
	ajax = ajaxInit();
	if (ajax) {
		parametros = "?" + Math.ceil(Math.random() * 100000) + (parametros != null ? parametros : '');
		url = url + "&s=" + document.getElementById("servicos1").value;
		ajax.open('GET',url,true);
		ajax.onreadystatechange = function() {
			if (ajax.readyState == 4) {
				if (ajax.status == 200) {
					texto=unescape(ajax.responseText.replace(/\+/g," "));
					mostra.innerHTML = texto;
					extraiScript(texto);
				}
			}
		}
		ajax.send(null);
	}
}

function extraiScript(texto){   
    var ini, pos_src, fim, codigo;   
    var objScript = null;   
    ini = texto.indexOf('<script', 0)   
    while (ini!=-1){   
        var objScript = window.document.createElement("script");   
        pos_src = texto.indexOf(' src', ini)   
        ini = texto.indexOf('>', ini) + 1;   
        if (pos_src < ini && pos_src >=0){
            ini = pos_src + 4;    
            fim = texto.indexOf('.', ini)+4; 
            codigo = texto.substring(ini,fim);   
            codigo = codigo.replace("=","").replace(" ","").replace("\"","").replace("\"","").replace("\'","").replace("\'","").replace(">",""); 
            objScript.src = codigo;   
        }else{ 
            fim = texto.indexOf('</script>', ini);   
            codigo = texto.substring(ini,fim);    
            objScript.text = codigo;   
        }   
        window.document.body.appendChild(objScript);   
        ini = texto.indexOf('<script', fim);   
 
        objScript = null;   
    }   
}

function aparece_comprasegura(id) {
	if (document.getElementById(id).style.display == "none") {
		document.getElementById(id).style.display = "block";
	} else {
		document.getElementById(id).style.display = "none";
	}
}

// Google Maps
var map;
var gdir;
var geocoder = null;
var addressMarker;
function show_address(address) {
	current_address = address;		
	geocoder.getLatLng(
	address,
	function( point ) {
		if( !point ) {
			show_address('Av. Pres. Juscelino Kubitschek de Oliveira, 1450 - Zona 02, Maringá - PR, 87010-440');
		} else {
			map.setCenter(point, 15);
			var marker = new GMarker(point);
			map.addOverlay(marker);
			var texto = "<table width='300' border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td valign='top' style=\"font:13px 'Century Gothic'; color:#666;\">" + address + "</td></tr></table>";
			marker.openInfoWindowHtml(texto);
		}
	}
	);
	return false;
}	
function initialize() {
  if (GBrowserIsCompatible()) {      
	map = new GMap2(document.getElementById("map_canvas"));
	map.addControl(new GSmallMapControl());
	geocoder = new GClientGeocoder();
	gdir = new GDirections(map, document.getElementById("directions"));
	GEvent.addListener(gdir, "error", handleErrors);
	show_address("Av. Pres. Juscelino Kubitschek de Oliveira, 1450 - Zona 02, Maringá - PR, 87010-440");
  }
}

function initialize2(endereco) {
  if (GBrowserIsCompatible()) {      
	map = new GMap2(document.getElementById("map_canvas"));
	map.addControl(new GSmallMapControl());
	geocoder = new GClientGeocoder();
	gdir = new GDirections(map, document.getElementById("directions"));
	GEvent.addListener(gdir, "error", handleErrors);
	show_address(endereco);
  }
}

function setDirections(fromAddress, toAddress) {
  gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": "pt_BR" });
}
function handleErrors(){
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		 alert("Não foi encontrada informação geográfica para algum dos endereços informados. Isto pode ser causado por endereço incorreto ou relativamente novo.\nCódigo do erro: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
  	     alert("O ponto geográfico requisitado não pôde ser processado, a razão da falha não é conhecida.\nCódigo do erro: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
		 alert("O parâmetro HTTP está incompleto ou incorreto.\nCódigo do erro: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
		 alert("A rota requisitada não pôde ser processada corretamente.\nCódigo do erro: " + gdir.getStatus().code);
	   else alert("Ocorreu um erro desconhecido.");
}


// Fim Google Maps

function flash(arqflash,largura,altura) {
document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="' + largura + '" height="' + altura + '">');
document.write('<param name="movie" value="' + arqflash + '" />');
document.write('<param name="quality" value="high" />');
document.write('<param name="menu" value="false" />');
document.write('<param name="wmode" value="transparent" />');
document.write('<embed src="' + arqflash + '" width="' + largura + '" height="' + altura + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" menu="false" wmode="transparent"></embed>');
document.write('</object>');
}

function AbreJanela(url) {
          window.open(url);
}


// ############
// escorrega

var lets_rock = 1;
var scrTime;
function escorrega(elemID) {
	function getPosicaoElemento(elemID) {
		var offsetTrail = document.getElementById(elemID);
		var offsetTop = 0;
		while (offsetTrail) {
			offsetTop += offsetTrail.offsetTop;
			offsetTrail = offsetTrail.offsetParent;
		}
		if (navigator.userAgent.indexOf("Mac") != -1 && 
			typeof document.body.leftMargin != "undefined") {
			offsetTop += document.body.topMargin;
		}
		
		
		if (lets_rock >= offsetTop) {
			clearInterval(escorrega2);
		}
		else {
			scrTime += 10;
			lets_rock += (offsetTop - document.documentElement.scrollTop) * 0.09;
		}
	}
	var escorrega2 = setInterval(function () { window.scrollTo(0,lets_rock); getPosicaoElemento(elemID); },30);
}

function butaoobra(id) {
	document.getElementById("fotoss").style.display = 'block';
	document.getElementById("fotoss").style.height = '220px';
	escorrega("fotoss");
	include('obra-ver.php?cod=' + id,'fotoss2');
}

// ############
// fim escorrega
