//Back to the HTML
var pagina2 = null;
function BackToTheHtml(node){
	if(node == null)
		this.node = document; 
	else
		this.node = node; 
};

BackToTheHtml.prototype.node = null;

BackToTheHtml.prototype.execute = function(){
	this.activateObject();
}

BackToTheHtml.prototype.activateObject = function(domObject){
	var aDomObject = this.node.getElementsByTagName('object');
	var activationObject;
	for(var i=0; i<aDomObject.length; i++)
		if(aDomObject[i].getAttributeNode('BackToTheHtml') == null && (activationObject = this.getActivationObject(aDomObject[i])) != null)
			activationObject.execute();
};

BackToTheHtml.prototype.getActivationObject = function(domObject){
	var classid = domObject.classid.toUpperCase().substr('clsid:'.length);
	var mimeType = domObject.type.toLowerCase();
	switch(true){
		case classid == 'D27CDB6E-AE6D-11CF-96B8-444553540000' || mimeType == 'application/x-shockwave-flash' :
			return new ActivateObjectFlash(domObject);
		default :
			return null;
	}
};

BackToTheHtml.uniqueID = function(prefix){
	var sPrefix;
	if(prefix == null)
		sPrefix = 'uniqueId';
	else
		sPrefix = prefix;
		
	var i=0;
	while(document.getElementById(sPrefix + (i++)))
		;
	return sPrefix + (i-1);
};

BackToTheHtml.isParentOf = function(parent,child){
	var found = false;
	for(var i=0; i<parent.childNodes.length; i++)
		if(parent.childNodes[i] == child)
			return true;
		else
			found = arguments.callee(parent.childNodes[i],child);

	return found;
}

function ActivateObject(domObject){
	this.domObject = domObject;
}

ActivateObject.prototype.domObject = null;
ActivateObject.prototype.classid = null;
ActivateObject.prototype.aHtmlAttribute = ['accessKey','align','alt','archive','border','code','codeBase','codeType','declare','dir','height','hideFocus','hspace','lang','language','name','standby','tabIndex','title','useMap','vspace','width'];
ActivateObject.prototype.aObjectProperty = null;

ActivateObject.prototype.execute = function(){
	this.xndObjectId = BackToTheHtml.uniqueID();
	this.setTextHtml();
	this.writeObject();
	this.xndObject = document.getElementById(this.xndObjectId);
	this.setSpecialProperties();
	this.removeOriginalObject();
}

ActivateObject.prototype.setTextHtml = function(){
	var str = '';
	str += '<object BackToTheHtml ' + '\n';
	str += ' classid="clsid:' + this.classid + '" ' + '\n';
	for(var i=0; i<this.aHtmlAttribute.length; i++){
		var name = this.aHtmlAttribute[i];
		if(typeof this.domObject[name] != 'undefined' && this.domObject[name].toString() != '')
			str += '\t' + name + '="' + this.domObject[name].toString() + '" ' + '\n';
	}
	str += 'id="' + this.xndObjectId + '" ' + '\n';
	str += '>';
	for(var i=0; i<this.aObjectProperty.length; i++){
		var name = this.aObjectProperty[i];
		if(typeof this.domObject[name] != 'undefined' && this.domObject[name].toString() != '' )
			str += '\t<param name="' + name + '" value="' + this.domObject[name].toString() + '"></param>' + '\n';
	}
	str += '</object>';
	this.textHtml = str;
};

ActivateObject.prototype.writeObject = function(){
	this.domObject.insertAdjacentHTML("afterEnd",this.textHtml);
};

ActivateObject.prototype.setSpecialProperties = function(){
	if(typeof this.domObject.className != 'undefined' && this.domObject.className.toString() != '')
		this.xndObject.className = this.domObject.className
	if(typeof this.domObject.style.cssText != 'undefined' && this.domObject.style.cssText.toString() != '')
		this.xndObject.style.cssText = this.domObject.style.cssText;
	if(typeof this.domObject.SWRemote != 'undefined' && this.domObject.SWRemote.toString() != '')
		this.xndObject.FlashVars = this.domObject.SWRemote;
	if(typeof this.domObject.codebase == 'undefined' || this.domObject.codebase.toString() == '')
		this.xndObject.codebase = 'http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,24,0';
	if(typeof this.domObject.id != 'undefined' && this.domObject.id.toString() != '')
		this.xndObject.id = this.domObject.id;
};

ActivateObject.prototype.removeOriginalObject = function(){
	this.domObject.parentNode.removeChild(this.domObject);
};

function ActivateObjectFlash(domObject){
	ActivateObject.call(this,domObject);
}

ActivateObjectFlash.prototype = new ActivateObject;
ActivateObjectFlash.prototype.aObjectProperty = ['FrameNum','Playing','Quality','Quality2','Scalemode','Scale','AlignMode','SAlign','BackgroundColor','BGColor','Loop','Movie','WMode','Base','DeviceFont','EmbedMovie','SWRemote','FlashVars','AllowScriptAccess'];
ActivateObjectFlash.prototype.classid = 'D27CDB6E-AE6D-11CF-96B8-444553540000';

if(typeof ActiveXObject != 'undefined' && typeof Function.call != 'undefined'){
	var styleId = BackToTheHtml.uniqueID();
	document.write('<style id="' + styleId + '" ></style>');
	var domStyle = document.getElementById(styleId);
	var isHead = false;
	var aHead = document.getElementsByTagName('head');
	for(var i=0; i<aHead.length; i++)
		if(BackToTheHtml.isParentOf(aHead[i],domStyle))
			isHead = true;
	if(isHead){
		document.write('<style type="text/css">OBJECT{visibility:hidden;}</style>');
		document.onreadystatechange = function(){
			if(document.readyState == 'complete'){
				new BackToTheHtml().execute();
				document.styleSheets[document.styleSheets.length-1].addRule("OBJECT","visibility:visible;");
			}
		}
	}else{
		new BackToTheHtml().execute();
	}
	domStyle.parentNode.removeChild(domStyle);
}
// end Back to the HTML

//Ajax
try{
	xmlhttp = new XMLHttpRequest();
}catch(ee){
	try{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}catch(e){
		try{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(E){
			xmlhttp = false;
		}
	}
}

fila=[];
ifila=0;

function ajaxLoad(url, destino){
	if(destino!=Object){ destino = document.getElementById(destino); }
	if(destino){
		if(destino.nodeName=="SELECT"){
			while(destino.options.length>0)destino.options[0]=null;
			destino.options[0]=new Option(" -- Aguarde -- "," -- Aguarde -- ");
		} else {
			destino.innerHTML='<table border="0" cellpadding="5" cellspacing="5" style="background-color: #FFFFFF; background-image: url(../images/sombra_titulo.gif); background-repeat: repeat-x; background-position: left top;"><tr><td><table width="200" height="40" border="0" cellpadding="0" cellspacing="0"><tr><td width="40" height="40" rowspan="2" align="center"><img src="../images/loading.gif" width="32" height="32" /></td><td style="font-size: 18px; font-weight:bold; color:#006633; font-family: Arial, Helvetica, sans-serif;">Aguarde...</td></tr><tr><td style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; color: #006633;">Carregando conteúdo...</td></tr></table></td></tr></table>';
		}
		fila[fila.length]=[url,destino];
		if((ifila+1)==fila.length)ajaxRun();
	}
}

function ajaxShow(valor,destino){
	if(destino.nodeName=="SELECT"){
		while(destino.options.length>0)destino.options[0]=null;
		if(valor.charAt(0)=="[" && valor.charAt(valor.length-1)=="]"){
			var destinoTmp=eval(valor);
			for(var i=0;i<destinoTmp.length;i++){
				destinoTmp[i][0]=unescape(destinoTmp[i][0]);
				destinoTmp[i][1]=unescape(destinoTmp[i][1]);
				destino.options[destino.options.length]=new Option(destinoTmp[i][1],destinoTmp[i][0]);
			}
		} else {
			destino.options[destino.options.length]=new Option(valor,valor);
		}
	} else {
		destino.innerHTML = unescape(valor.replace(/\+/g," ")); 
	}
}

function ajaxRun(){
	url = fila[ifila][0];
//	url = idioma+url;
	destino = fila[ifila][1];
	var curDateTime = new Date();
	if (url.indexOf("?")>0){
		url = url+"&t="+curDateTime;
	}else{
		url = url+"?t="+curDateTime;
	}
	xmlhttp.open("GET", url, true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=ISO-8859-1");
	xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
	xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
	xmlhttp.setRequestHeader("Pragma", "no-cache");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4){
			switch(xmlhttp.status){
				case 200: { ajaxShow(xmlhttp.responseText,destino); break }
				case 204: { ajaxShow("O servidor não respondeu no tempo limite",destino); break }
				case 400: { ajaxShow("Endereço inválido",destino); break }
				case 403: { ajaxShow("Você não possui permissão para acessar esse arquivo",destino); break }
				case 404: { ajaxShow("Arquivo não encontrado",destino); break }
				case 500: { ajaxShow("Erro interno do servidor",destino); break }
				case 502: { ajaxShow("Servidor ocupado",destino); break }
				case 503: { ajaxShow("O servidor não respondeu no tempo limite",destino); break }
				 default: { ajaxShow("Algum erro ocorreu ao carregar este conteúdo",destino); break }
			}
			if(fila[ifila][0]=="representantes.html"){ populaEstados(); }
			if(fila[ifila][0]=="produtos.html"){
				if(pagina2){
					ajaxLoad(pagina2,'produtoAbre');
				}
			}
			if(fila[ifila][0]=="home_caixas.html"){ ajaxLoad('representantes.php?fun=e','estados2'); }
			ifila++;
			if(ifila<fila.length)setTimeout("ajaxRun()",20);
		}
	}
	xmlhttp.send(url);
}

function ajaxFormUpdate(f, url, destino, url2, destino2){
	if(f!=Object){ f = document.getElementById(f); };
	var query=url;
	for (i=0;i<f.elements.length;i++){
		query += i==0 ? '?' : '&';
		query += f.elements[i].name + '=' + f.elements[i].value;
	}
	ajaxLoad(query, destino);
	ajaxLoad(url2, destino2);
}


function ajaxForm(f, url, destino){
	if(f!=Object){ f = document.getElementById(f); };
	var query=url;
	for (i=0;i<f.elements.length;i++){
		query += i==0 ? '?' : '&';
		query += f.elements[i].name + '=' + f.elements[i].value;
	}
	ajaxLoad(query, destino);
}
//end Ajax

var c10,c11,c12,c13,c14;

function teclado(o){if(o.tipo=='n'){tecla=event.keyCode;if(tecla!=46){if(tecla==44){event.returnValue=false;eval(o).value=eval(o).value+"."}else{if(tecla<48||tecla>57)event.returnValue=false}}}}
function calcula(o,l){if(o.value>l){o.value=l;alert("O valor máximo para este campo é de "+l+" mm")}c10=document.form.c10.value;c11=document.form.c11.value;c12=document.form.c12.value;if(c10!=""&&c11!=""&&c12!=""){c13=((c10/1000)*(c11/1000)*(c12/1000))*7850;document.form.c13.value=(Math.round(c13*100))/100;c14=Math.ceil(1000/c13);document.form.c14.value=c14}}
function populaEstados(){
	/*
	obj=document.getElementById("estados");
	while(obj.options.length>0)obj.options[0]=null;
	for(i=0;i<estados.length;i++)obj.options[i]=new Option(estados[i],i);
	populaCidades(0)
	*/
	representantes();
}
function populaCidades(id){obj=document.getElementById("cidades");if(document.getElementById("estados").value==0)obj.disabled=true;else obj.disabled=false;while(obj.options.length>0)obj.options[0]=null;for(i=0;i<cidades[id].length;i++)obj.options[i]=new Option(cidades[id][i],i);populaProdutos()}
function populaProdutos(){obj=document.getElementById("produtos");if(document.getElementById("cidades").value==0)obj.disabled=true;else obj.disabled=false;while(obj.options.length>0)obj.options[0]=null;for(i=0;i<produtos.length;i++)obj.options[i]=new Option(produtos[i],i)}
function Contatos(val){window.location="mailto:"+val+"@metisa.com.br"}

function representantes(){ ajaxLoad('representantes.php?fun=e','estados'); }

function mudaProduto(arq){
	ajaxLoad('produtos.html','areaConteudo');
	pagina2 = arq;
}

function UpdateRepresentantes(f,c){
	objE = document.getElementById("estados");
	objC = document.getElementById("cidades");
	objP = document.getElementById("produtos");
	valE = objE.value;
	valC = objC.value;
	valP = objP.value;
	if(valE=="NULL"){
		objC.disabled = true;
		objP.disabled = true;
	} else {
		objC.disabled = false;
		if(valC=="NULL"){
			objP.disabled = true;
		} else {
			objP.disabled = false;
		}
	}
	URL = "representantes.php?fun="+f+"&e="+valE+"&c="+valC+"&p="+valP;
	ajaxLoad(URL,c);
}

function UpdateRepresentantes2(f,c){
	objE = document.getElementById("estados2");
	objC = document.getElementById("cidades2");
	valE = objE.value;
	valC = objC.value;
	URL = "representantes2.php?fun="+f+"&e="+valE+"&c="+valC+"&p=";
	ajaxLoad(URL,c);

}

function buscaFaq(){
	val = document.getElementById("FaqQuery").value;
	if(val.length < 3){
		alert("Informe uma palavra de no mínimo 3 letras para efetuar a consulta!");
	} else {
		ajaxForm('FormFaq','faq.php','areaConteudo');
	}
}

function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

function changeView(id){
	obj = document.getElementById(id);
	if(obj){
		if(	obj.style.display=="block"){
			obj.style.display="none";
		} else {
			obj.style.display="block";
		}
	}
}

function ShowTab(id){
	document.getElementById("tab1").style.display = "none";
	document.getElementById("tab2").style.display = "none";
	document.getElementById("tab3").style.display = "none";
	document.getElementById("tab4").style.display = "none";
	document.getElementById("tab5").style.display = "none";
	
	document.getElementById("tab1b").style.background = "#DDDDDD";
	document.getElementById("tab1b").style.borderWidth = "1px 1px 1px 1px";
	document.getElementById("tab2b").style.background = "#DDDDDD";
	document.getElementById("tab2b").style.borderWidth = "1px 1px 1px 1px";
	document.getElementById("tab3b").style.background = "#DDDDDD";
	document.getElementById("tab3b").style.borderWidth = "1px 1px 1px 1px";
	document.getElementById("tab4b").style.background = "#DDDDDD";
	document.getElementById("tab4b").style.borderWidth = "1px 1px 1px 1px";
	document.getElementById("tab5b").style.background = "#DDDDDD";
	document.getElementById("tab5b").style.borderWidth = "1px 1px 1px 1px";
	
	document.getElementById(id).style.display = "block";
	document.getElementById(id+"b").style.background = "#FFFFFF";
	document.getElementById(id+"b").style.borderWidth = "1px 1px 0px 1px";
}

var popUpWin=0;
function popWindow(URLStr, width, height){
	var left = (screen.width-width)/2;
	var top = (screen.height-height)/2;
	if(popUpWin){
		if(!popUpWin.closed) popUpWin.close();
	}
	popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}