function get(id) { 
	try { 
		var a = document.getElementById(id); 
	} catch(e) { 
		alert("Erro encontrado: "+e); 		
	} 
	return 	a;
}
function addFlash (file, width, height, id, wmode, bgcolor) {
	if(!wmode)
		wmode = false;

	var fo = new FlashObject(file, "notify", width, height, "9", bgcolor, true);
	fo.addParam("scale", "noscale");
	fo.addParam("wmode", "opaque");
	fo.useExpressInstall('expressinstall.swf');
	
	if(wmode==true)
		fo.addParam("wmode", "transparent");
		
	fo.write(id);
}

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 enviaProposta (id) {
	if(!id) id = '';
	var arrayPageScroll = getPageScroll();
	
	get('window_content_product').style.display = 'block';
	get('content_product').style.width = document.body.clientWidth+'px';
	get('content_product').style.top = arrayPageScroll[1] + (document.body.clientWidth / 7) + 'px';	

	time = setTimeout(function () {
		document.getElementById('window_content_product').innerHTML = ajax.loadResult('proposta.php?id='+id);
		clearTimeout(time);
	}, 100);
	
}

var campos = Array("nome","cidade","estado","telefone","email","msg");
var nomes = Array("Nome","Cidade","Estado","Telefone","E-mail","Mensagem");
var expres = Array("o","a","o","o","o","a");

function val(i) {
	obj = $(campos[i]);
	if(obj.value=="") {
		$('erro_form').innerHTML = "<span style='color: red;'>"+nomes[i]+" inválid"+expres[i]+"!</span>";
		obj.focus();
		return false;
	}
	return true;
}

function validaForm(id) {
	for(i=0;i<campos.length;i++) {
		if(!val(i)) return false;
	}
	$('erro_form').innerHTML = "<span style='color: black;'>enviando mensagem...</span>";
	
	var url = new Array();
	for(i=0; i<campos.length; i++) {
		url[i] = campos[i]+'='+escape(get(campos[i]).value);
	}
	url[i+1] = '&id='+id;
	ajax.method = 'POST';
	
	time = setTimeout(function () {
		if(ajax.loadResult('enviar_proposta.php?send=true&'+url.join('&'))) {
			$('erro_form').innerHTML = "<span style='color:green'>Dados enviados com sucesso!</span>";
			for(i=0;i<campos.length;i++) {
				get(campos[i]).value = "";
			}
		}else{
			$('erro_form').innerHTML = "<span style='color:red'>Erro ao enviar os dados!</span>";
		}
	}, 100);
	
	return false;
}

var pFone = new Array();
var cFone = new Array();	


function teclaReal (e)  {
	if(window.event) // IE
	{
		keynum = e.keyCode
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which
	}
	if(e.keyCode!=9 && e.keyCode!=40 && e.keyCode!=39 && e.keyCode!=38 && e.keyCode!=37) {
		if(keynum !=8) {
			keychar = String.fromCharCode(keynum);
			numcheck = /\d/;		
			return numcheck.test(keychar);	
		}
	}
}

function addCtFone (ct,pos) {
	pFone.push(pos)	
	cFone.push(ct);
}
	
function viewMaskFone (str) {
	var aux = "";
	var save = "";
	var save = str;
	var ped = "";	
	
	for(i=0;i<cFone.length;i++) {
		aux = "";
	
		for(j=0;j <save.length;j++) {
		
			ped = save.substr(j,1);
			
			if(j == pFone[i]) {
				if(ped != cFone[i]) {
					aux += cFone[i];
				}
			}		
			
			
			
			
			aux += save.substr(j,1);														
	
		}
		save = aux;

	}
	
	return save;
}
function maskFone (e,ob) {
	if(window.event) // IE
	{
		keynum = e.keyCode;
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which;
	}		
	keychar = String.fromCharCode(keynum);
	numcheck = /\d/;

	
	ob.value = viewMaskFone(ob.value);

	var s = false;
	
	if(!isNaN(parseInt(keychar))) {
		s = true;
	} 
	
	if(keynum == 8 || keynum == 13 || keynum == 32) {
		s = true;
	}
	
	return s;
}

addCtFone("(",0);	
addCtFone(")",3);
addCtFone("-",8);

function abreGaleria (dir, atual, title)
{
	   var width = 550;
	   var height = 610;
	   var left = (screen.width - width)/2;
	   var top = (screen.height - height)/2;;   
	   var galeria = window.open('galeria.php?dir='+dir+'&atual='+atual+'&title='+title,'geleria', 'width='+width+', height='+height+', top='+top+', left='+left+', scrollbars=yes, status=no, toolbar=no, location=no, directories=no, menubar=no, resizable=no, fullscreen=no');
	   
	   galeria.focus();
}
function validaEmail(campo) {
	var objCampo = get(campo);
	
    if(objCampo.value==""){
        return false;
    }
    if(objCampo.value.indexOf ('@') < 1){
        return false;
    }
    objCampo.value.indexOf ('@')
    if(objCampo.value.substring((objCampo.value.indexOf ('@') + 1), objCampo.value.length).indexOf ('@') >= 0){
        return false;
    }
    if(objCampo.value.indexOf ('.') < 5){
        return false;
    }
    if((objCampo.value.substring((objCampo.value.indexOf ('.') + 1), objCampo.value.length).length) < 3){
        return false;
    }
    return true;
}

function show (id)
{
	get(id).style.display = 'block';
}

function hide (id)
{
	get(id).style.display = 'none';
}

//Limpa/Preenche os campos
function valueField (event, obj) {
	e = event.type;
	var obj = get(obj);
	
	if(!obj.save)
	{
		obj.save = obj.value;		
	}
	
	if(e=="blur")
	{
		if(obj.value=="")
		{
			obj.value = obj.save;
		}
	}
	else if(e=="focus")
	{
		if(obj.value=="")
		{
			obj.value = obj.save;
		}
		else if(obj.value==obj.save)
		{
			obj.value = "";
		}
	}
}

function loadPage (pg, local, method)
{
	if(!method)
		method = 'GET';
	
	ajax.method = method;
	
	setTimeout(
		function () {
			ajax.loadContent(pg, local);
		}
	, 1);	
}

function validaFormEmail()
{
	if(!validaEmail('emailN'))
	{
		get('emailN').focus();
		show('erro_email');
	} else {
		show('erro_email');
		get('erro_email').innerHTML = '';
		var email = get('emailN').value;
		loadPage('cadastrar.php?email='+email, 'erro_email', 'POST');
	}
}

function cancelaEmail ()
{
	if(!validaEmail('emailN'))
	{
		get('emailN').focus();
		show('erro_email');
	} else {
		show('erro_email');
		get('erro_email').innerHTML = '';
		var email = get('emailN').value;
		loadPage('cancelar.php?email='+email, 'erro_email', 'POST');
	}
}

function abreAtendimento () {
	   var width = 450;
	   var height = 465;
	   var left = (screen.width - width)/2;
	   var top = (screen.height - height)/2;;   
	   window.open('../atd/view/?action=AppUsr/login','atendimento', 'width='+width+', height='+height+', top='+top+', left='+left+', scrollbars=no, status=no, toolbar=no, location=no, directories=no, menubar=no, resizable=no, fullscreen=no');
}


function validaContato () {
	for(i=0;i<campos.length;i++) {
		if(!val(i)) return false;
	}
	
	get('erro_form').innerHTML = "<span style='color: black;'>enviando mensagem...</span>";
	get('form_contato').submit();
	
}
