// JavaScript Document

function StartEmailContato(){
	//alert('teste2');
	var objBody = document.getElementsByTagName("body").item(0);
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	//var objOverlay = $('overlay');
	objOverlay.style.display = 'none';
	objBody.appendChild(objOverlay);
	
	hideSelectBoxes();
	var arrayPageSize = getPageSize();
	objOverlay.style.height = arrayPageSize[1] +"px";
	new Effect.Appear('overlay', { duration: 0.2, from: 0.0, to: 0.5 });
	
	var objConfEmail = document.createElement('div');
	objConfEmail.setAttribute('id', 'confimEmail');
	objBody.appendChild(objConfEmail);
	
	var arrayPageScroll = getPageScroll();
	var enviandoTop = arrayPageScroll[1] + (arrayPageSize[3] / 3);
	objConfEmail.style.top = enviandoTop+"px";
	
	var objImagem = document.createElement("img");
	objImagem.setAttribute('src', 'templates/images/enviando_mensagem.gif');
	objImagem.setAttribute('id', 'img_enviando');
	objConfEmail.appendChild(objImagem);

	$('nome').value = 'digite seu nome completo';
	$('email').value = 'digite seu e-mail';
	$('ddd').value = '0XX';
	$('tel').value = 'Telefone';
	$('mensagem').value = 'digite sua mensagem';

	setTimeout(Stop,7000);

}

function Stop(){
	Element.hide('confimEmail');
	Element.remove('img_enviando');
	new Effect.Fade('overlay', { duration: 0.2});
	showSelectBoxes();
	

	
	
}

function carregando(){
	alert('carregando...');
}

function enviarContato(e){
	if(e.responseText == "true")
	{
		StartEmailContato(this);
	}
	if(e.responseText == "false")
	{
		alert(e.responseText);
	}

}

function enviaEmailContato(){	
	var valorEmail = $('email').value;
	if(validarEmail(valorEmail))
	{
		var url = "components/com_contato/enviar.php";
		var pars = Form.serialize('contato');
		var myAjax = new Ajax.Request( url, { method: 'get', parameters: pars, onLoading: function(e){ StartEmailContato(this) } } );
	}
	else{
		//alert(valorEmail);
		var msg = 'email inválido';
		$('email').value = msg;
		$('email').style.color = '#a60000';
		Event.observe('email', 'click', emailHandleClick);
	}
}

function emailHandleClick(e){
	if(typeof(e) == 'undefined'){
		var e = window.event;
	}
	var elem = e.target ? e.target  :e.srcElement;
	
	var msg = 'email inválido';

	if(elem.value == msg){
		elem.style.color = '#000';
		elem.value = '';
	}
}

/*function validaEmail(str){
	for(var i = 0; i < str.length; i++){
		if(str.charAt(i) == "@"){
			return true;
		}
	}
	return false;
}*/

function validarEmail(str){ 
	if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(str))) 
	{ 
		//alert("Favor informar um email válido.");
		return false;
		
	}
	else
	{
		return true;	
	}
} 


// ---------------------------------------------------




function showSelectBoxes(){
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}


function hideSelectBoxes(){
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


//Event.observe(window, 'load', Start, false);