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

function $(id) { 
	try { 
		var a = document.getElementById(id); 
	} catch(e) { 
		alert("Erro encontrado: "+e); 		
	} 
	return 	a;
}
function addFlash (file, width, height, id, wmode) {
	if(!wmode)
		wmode = "true";
	
	var fo = new FlashObject(file, "notify", width, height, "8", "#FFFFFF", true);
	fo.addParam("scale", "noscale");
	fo.addParam("wmode", "opaque");
	
	if(wmode=="true")
		fo.addParam("wmode", "transparent");
		
	fo.write(id);
}

function abreProduto (id, b) {
	if(!b)
		b = '';
		
	var arrayPageScroll = getPageScroll();
	
	$('window_content_product').style.display = 'block';
	$('content_product').style.width = document.body.clientWidth+'px';
	$('content_product').style.top = arrayPageScroll[1] + (document.body.clientWidth / 15) + 'px';

	ajax.loadContent('getProduto.php?id='+id+'&b='+b, 'window_content_product');
}

function abreTabela () {
	var arrayPageScroll = getPageScroll();
	
	$('window_content_product2').style.display = 'block';
	$('content_product2').style.width = document.body.clientWidth+'px';
	$('content_product2').style.top = arrayPageScroll[1] + (document.body.clientWidth / 15) + 'px';
	ajax.loadContent('getProduto2.php?', 'window_content_product2');
}

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 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() {
	for(i=0;i<campos.length;i++) {
		if(!val(i)) return false;
	}
	$('erro_form').innerHTML = "<span style='color: silver;'>enviando mensagem...</span>";
	$('form_contato').submit();
}
function closeProd (e) {
	if(e.keyCode==27) {
		$('window_content_product').style.display = 'none';	
		$('window_content_product2').style.display = 'none';	
	}
}
/* Mascara do Fone */

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

	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 validaEmail(campo) {
	var objCampo = $(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 validaPedidos () {
	if($('nome').value.length<=3)
	{
		alert('Digite seu nome corretamente!');
		$('nome').focus();
		return false;
	}
	
	if(!validaEmail('email'))
	{
		alert('E-mail Inválido!');
		$('email').focus();
		return false;
	}
	
	if($('telefone').value.length<13)
	{
		alert('Telefone Inválido!');
		$('telefone').focus();
		return false;
	}
	
	$('form_pedidos').submit();
}