var opacidade = 100, tempo = 100, velocidade = 45, t;
var prod_atual = "etiquetas", form_atual = "form01";
var cor = "#efeeee", corErro = "#e6e298", cor2="none", branco="#FFFFFF";

window.onload = function(){
	document.getElementById('RadioGroup1_0').checked = "checked";
	document.getElementById('RadioGroup1_0').onclick = function(){troca(1);}
	document.getElementById('RadioGroup1_1').onclick = function(){troca(2);}
	document.getElementById('RadioGroup1_2').onclick = function(){troca(3);}
	
	document.getElementById('material').onchange = function(){ changeOp();}
	
	document.getElementById('nome').onfocus = limpa
    document.getElementById('telefone').onfocus = limpa
    document.getElementById('email').onfocus = limpa
	
	document.getElementById('altura').onfocus = limpa
	document.getElementById('largura').onfocus = limpa
	document.getElementById('nc').onfocus = limpa
	
	document.getElementById('material').onfocus = limpa
	document.getElementById('gramatura').onfocus = limpa
	document.getElementById('qtdEt').onfocus = limpa
	
	document.getElementById('modelo').onfocus = limpa
	document.getElementById('qtdImp').onfocus = limpa
	
	document.getElementById('tiporibon').onfocus = limpa
	document.getElementById('qtdRi').onfocus = limpa	

	document.getElementById('adesivos').onfocus = limpa	
	
	document.getElementById('mtgem').onfocus = limpa	
	document.getElementById('lgura').onfocus = limpa	
	
	document.getElementById('rolo').onfocus = limpa	
	
	changeOp();
}

function validar(){
	var fml=document.orcaForm
	var status=true;
	var arroba = fml.email.value.indexOf("@");
	var ponto = fml.email.value.indexOf(".");
	
	//Valida o campo NOME.
	if(fml.nome.value.length < 3){
	 	document.getElementById("caixa_nome").style.background = corErro;
		status=false;
	}
	else{
		document.getElementById("caixa_nome").style.background = cor;
	}

	//Valida o campo E-MAIL.
	if(fml.email.value.length < 5 || arroba == -1 || ponto == -1){
		document.getElementById("caixa_email").style.background = corErro;
		status=false;
	}
	else{
		document.getElementById("caixa_email").style.background = cor;
	}
	
	//Valida o campo TELEFONE
	if(fml.telefone.value.length < 7){
		document.getElementById("caixa_telefone").style.background = corErro;
		status=false;
	}
	else{
		document.getElementById("caixa_telefone").style.background = cor;
	}

	//Valida os PRODUTOS
	//Etiquetas
	if(fml.RadioGroup1_0.checked){
		obj1 = document.getElementById("largura")
		obj2 = document.getElementById("altura")
		obj3 = document.getElementById("nc")
		
		obj4 = document.getElementById("material")
		obj5 = document.getElementById("gramatura")
		obj6 = document.getElementById("qtdEt")
		
		if(fml.largura.value.length <= 0){
			obj1.style.background = corErro;
			status=false;
		}
		else{
			obj1.style.background = cor2;
		}
		
		if(fml.altura.value.length <= 0){
			obj2.style.background = corErro;
			status=false;
		}
		else{
			obj2.style.background = cor2;
		}
		if(fml.numcar.value.length <= 0){
			obj3.style.background = corErro;
			status=false;
		}
		else{
			obj3.style.background = cor2;
		}
		
		if(fml.material.value == 0){
			obj4.style.background = corErro;
			status=false;
		}
		else{
			obj4.style.background = branco;
		}
		
		if(fml.gramatura.value == 0){
			if(!fml.gramatura.disabled)
			obj5.style.background = corErro;
			status=false;
		}
		else{
			obj5.style.background = branco;
		}
		
		//Adesivo
		if(fml.material.value == "Coche" || fml.material.value == "Branco Fosco"){
			if(fml.adesivos.value == "0"){
				document.getElementById("adesivos").style.background = corErro;
			}
			else{
				document.getElementById("adesivos").style.background = branco;
			}		
		}
		
		
		//Quantidade
		if(fml.qtdEt.value.length <= 0 && fml.material.value != "Nylon Resinado"  && fml.material.value.indexOf("mico") != 3){
			obj6.style.background = corErro;
			status=false;
		}
		else{
			obj6.style.background = branco;
		}
		
		//NYLON
		if(fml.material.value == "Nylon Resinado"){
			if(fml.lgura.value == 0){
				document.getElementById("lgura").style.background = corErro;
				status=false;
			}
			else{
				document.getElementById("lgura").style.background = branco;	
			}
			if(fml.mtgem.value == 0){
				document.getElementById("mtgem").style.background = corErro;
				status=false;
			}
			else{
				document.getElementById("mtgem").style.background = branco;
			}
		}
		
		//TERMICO
		if(fml.material.value.indexOf("mico") == 3){
			if(fml.rolo.value == 0){
				document.getElementById("rolo").style.background = corErro;
				status=false;
			}
			else{
				document.getElementById("rolo").style.background = branco;	
			}
		}
		
		
	}
	//Impressoras
	else if(fml.RadioGroup1_1.checked){
		if(fml.modelo.value.length <= 0){
			document.getElementById("modelo").style.background = corErro;
			status=false;
		}
		else{
			document.getElementById("modelo").style.background = branco;
		}
		if(fml.qtdImp.value.length <= 0){
			document.getElementById("qtdImp").style.background = corErro;
			status=false;
		}
		else{
			document.getElementById("qtdImp").style.background = branco;
		}
		
	}
	
	
	//Ribons
	else if(fml.RadioGroup1_2.checked){
		
		if(fml.tiporibon.value == 0){
			document.getElementById("tiporibon").style.background = corErro;
			status=false;
		}
		else{
			document.getElementById("tiporibon").style.background = branco;
		}
		
		if(fml.qtdRi.value.length <= 0){
			document.getElementById("qtdRi").style.background = corErro;
			status=false;
		}
		else{
			document.getElementById("qtdRi").style.background = branco;
		}
		
		
		
		
	}

	
	
	if(status){
		 fml.action = "Scripts/redirect3.asp";
 		 fml.submit();	
	}
	else{
		avisar(true);	
	}
}

function limpa(e){
    try{var x = e.target          }catch(er){};
    try{var x = event.srcElement  }catch(er){};
	if(x.id == "nome"){ document.getElementById("caixa_nome").style.background = cor;}
	if(x.id == "telefone"){ document.getElementById("caixa_telefone").style.background = cor;}
	if(x.id == "email") {document.getElementById("caixa_email").style.background = cor;}
	
	if(x.id == "largura") {document.getElementById("largura").style.background = cor2;}
	if(x.id == "altura") {document.getElementById("altura").style.background = cor2;}
	if(x.id == "nc") {document.getElementById("nc").style.background = cor2;}
	if(x.id == "material") {document.getElementById("material").style.background = branco;}
	if(x.id == "gramatura") {document.getElementById("gramatura").style.background = branco;}
	if(x.id == "qtdEt") {document.getElementById("qtdEt").style.background = cor2;}
	
	if(x.id == "modelo") {document.getElementById("modelo").style.background = branco;}
	if(x.id == "qtdImp") {document.getElementById("qtdImp").style.background = branco;}
	
	if(x.id == "tiporibon") {document.getElementById("tiporibon").style.background = branco;}
	if(x.id == "qtdRi") {document.getElementById("qtdRi").style.background = branco;}
	
	if(x.id == "adesivos") {document.getElementById("adesivos").style.background = branco;}
	
	if(x.id == "lgura") {document.getElementById("lgura").style.background = branco;}
	if(x.id == "mtgem") {document.getElementById("mtgem").style.background = branco;}
	
	if(x.id == "rolo") {document.getElementById("rolo").style.background = branco;}
	
}

function limpar(){
	var fml=document.orcaForm
	fml.reset();
	if(prod_atual == "etiquetas")document.getElementById("RadioGroup1_0").checked = "checked";
	if(prod_atual == "impressoras")document.getElementById("RadioGroup1_1").checked = "checked";
	if(prod_atual == "ribons")document.getElementById("RadioGroup1_2").checked = "checked";
	avisar(false);
	document.getElementById("caixa_nome").style.background = cor;
    document.getElementById("caixa_email").style.background = cor;
	document.getElementById("caixa_telefone").style.background = cor;
	document.getElementById("altura").style.background = cor2;
	document.getElementById("largura").style.background = cor2;
	document.getElementById("nc").style.background = cor2;
	document.getElementById("material").style.background = branco;
	document.getElementById("gramatura").style.background = branco;
	document.getElementById("gramatura").disabled = "disabled";
	document.getElementById("qtdEt").style.background = cor2;
	
	document.getElementById("modelo").style.background = branco;
	document.getElementById("qtdImp").style.background = branco;
	
	document.getElementById("tiporibon").style.background = branco;
	document.getElementById("qtdRi").style.background = branco;
	document.getElementById("adesivos").style.background = branco;
	
	document.getElementById("lgura").style.background = branco;
	document.getElementById("mtgem").style.background = branco;
	
	document.getElementById("rolo").style.background = branco;
	
	habilitaAdesivo(false);
	changeOp();
}

function avisar(mostrar){
	if(mostrar){
		clearTimeout(t)
		setarVar();
		document.getElementById("aviso").style.visibility = 'visible';
		show();			
	}
	else{
		clearTimeout(t)
		document.getElementById("aviso").style.visibility = 'hidden';
		setarVar();
	}
}

function setarVar(){
	opacidade = 100;
	tempo = 100;
	var objeto = document.getElementById("aviso").style;
	objeto.opacity = 1; //FIREFOX
	objeto.MozOpacity = 1; // BROWSER NÃO IDENTIFICADO
	objeto.KhtmlOpacity = 1; // BROWSER NÃO IDENTIFICADO
	objeto.filter = "alpha(opacity=100)";//I.E.
}

function show(){
	if(tempo > 0){
		tempo --;
		t = setTimeout("show()", velocidade); 
	}
	else if(opacidade > 0 ){
		opacidade -=5;
		var objeto = document.getElementById("aviso").style;
		objeto.opacity = (opacidade / 100); //FIREFOX
		objeto.MozOpacity = (opacidade / 100); // BROWSER NÃO IDENTIFICADO
		objeto.KhtmlOpacity = (opacidade / 100); // BROWSER NÃO IDENTIFICADO
		objeto.filter = "alpha(opacity=" + opacidade + ")";//I.E.
		t = setTimeout("show()", velocidade); 
	}
	else{
		clearTimeout(t)
		document.getElementById("aviso").style.visibility = 'hidden';
		setarVar();
	}
}

function mostralista(){
var pgWidth = 412;
var pgHeight= 465;
var pLeft = (screen.width/2) - (pgWidth/2);
var pTop =  (screen.height/2)-(pgHeight/2)-50;
window.open("lista.html","lista","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=yes, width=418, height=400, top="+pTop+",left="+pLeft+"")
}


function changeOp(){
	document.getElementById("areagramatura").style.display = "block";
	document.getElementById("qtd").style.display = "block";
	document.getElementById("metragem").style.display = "none";
	document.getElementById("arearolo").style.display = "none";
	
	var indice = document.getElementById("material").selectedIndex;
	var obj = document.getElementById("gramatura");
	obj.options[0].text = "----- GRAMATURA -----";
	obj.options[0].value = "0";
	limparitens();
	habilitaAdesivo(false);

	if(indice == 0){
		obj.disabled = true;
	}
	if(indice == 1){
		obj.disabled = false;
		insere("20g")
		insere("30g")
		habilitaAdesivo(true);
	}
	if(indice == 2){
		obj.disabled = false;
		insere("20g")
		insere("30g")
		habilitaAdesivo(true);
	}
	if(indice == 3){
		obj.disabled = false;
		insere("20g")
		insere("30g")
		insere("50g")
	}
	if(indice == 4){
		obj.disabled = false;
		insere("20g")
		insere("30g")
		insere("50g")
	}
	if(indice == 5){
		obj.disabled = false;
		insere("130g")
		insere("180g")
	}
	if(indice == 6){
		obj.disabled = false;
		obj.options[0].selected="selected"
		obj.options[0].value = "N/A";
		obj.options[0].text = "N/A";
		document.getElementById("areagramatura").style.display = "none";
		document.getElementById("qtd").style.display = "none";
		document.getElementById("metragem").style.display = "block";
	}
	if(indice == 7){
		obj.disabled = false;
		obj.options[0].selected="selected"
		obj.options[0].value = "20g";
		obj.options[0].text = "20g";
		document.getElementById("qtd").style.display = "none";
		document.getElementById("arearolo").style.display = "block";
	}
	if(indice == 8){
		obj.disabled = false;
		obj.options[0].selected="selected"
		obj.options[0].value = "30g";
		obj.options[0].text = "30g";
	}
	if(indice == 9){
		obj.disabled = false;
		obj.options[0].selected="selected"
		obj.options[0].value = "20g";
		obj.options[0].text = "20g";
	}
	if(indice == 10){
		obj.disabled = false;
		obj.options[0].selected="selected"
		obj.options[0].value = "23g";		
		obj.options[0].text = "23g";
	}
	
//	alert(obj.options[0].value);
}

function insere(valor){
  var sel = document.getElementById('gramatura');
  var lugar = sel.length	
  if (sel.selectedIndex >= 0) {
    var elOptNew = document.createElement('option');
    elOptNew.text = '' + valor;
    elOptNew.value = '' + valor;
	var elOptOld = sel.options[lugar];  
    try {
   sel.add(elOptNew, elOptOld); // standards compliant; doesn't work in IE

    }
    catch(ex) {
      sel.add(elOptNew, lugar); // IE only
    }
  }
}


function limparitens(){
  var obj = document.getElementById('gramatura');
  var indice = obj.length;
  while(indice > 0){
    obj.remove(indice);
	indice--;
  }
}



function troca(area){
	if(area==1 && prod_atual != "etiquetas"){
		ocultar(prod_atual);
		prod_atual = "etiquetas";
		document.getElementById("info1").style.display = "block";
	}
	else if(area==2 && prod_atual != "impressoras"){
		ocultar(prod_atual);
		prod_atual = "impressoras";
		document.getElementById("info2").style.display = "block";
	}
	else if(area==3 && prod_atual != "ribons"){
		ocultar(prod_atual);
		prod_atual = "ribons";
		document.getElementById("info3").style.display = "block";
	}
}

function ocultar(oq){

	if(oq == "etiquetas"){
		document.getElementById("info1").style.display = "none";
	}
	if(oq == "impressoras"){
		document.getElementById("info2").style.display = "none";
	}
	if(oq == "ribons"){
		document.getElementById("info3").style.display = "none";
	}
}


function preenche(l,a,c,ref){
	obj1 = document.getElementById("largura");
	obj1.value = l;
	obj1.style.background = cor2;
	
	obj2 = document.getElementById("altura");
	obj2.value = a;
	obj2.style.background = cor2;
		
	obj3 = document.getElementById("nc");
	obj3.value = c;
	obj3.style.background = cor2;
	
	document.getElementById("referencia").value = ref;
}

function habilitaAdesivo(con){
	if(con){
		document.getElementById("areaAdv").style.display = "block";
		document.getElementById("info1").style.height = "265px";		
	}
	else{
		document.getElementById("areaAdv").style.display = "none";
		document.getElementById("info1").style.height = "230px";		
	}
}

