//---------------------------------------------- [FLASH]----------------------------------//
if(Browser == undefined){
   var Browser = {
      isIE: function(){ return (window.ActiveXObject && document.all && navigator.userAgent.toLowerCase().indexOf("msie") > -1  && navigator.userAgent.toLowerCase().indexOf("opera") == -1) ? true : false; }
   }
}
var Flash = function(movie, id, width, height, initParams){
   this.html = "";
   this.attributes = this.params = this.variables = null;
   this.variables = new Array();
   this.attributes = {
      "classid": "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000",
      "codebase": "http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab#version=8,0,22,0",
      "type": "application/x-shockwave-flash"
   }
   this.params = { "pluginurl": "http://www.macromedia.com/go/getflashplayer_br" };
   if(movie) {
      this.addAttribute("data", movie);
      this.addParameter("movie", movie);
   }
   if(id && id != null) this.addAttribute("id", id);
   if(width) this.addAttribute("width", width);
   if(height) this.addAttribute("height", height);
   if(initParams != undefined){
      for(var i in initParams){
         this.addParameter(i.toString(), initParams[i]);
      }
   }
}
Flash.version = "1.2b";
Flash.getObjectByExceptions = function(obj, excep){
   var tempObj = {};
   for(var i in obj){
      var inclui = true;
      for(var j=0; j<excep.length; j++)
         if(excep[j] == i.toString()) { inclui = false; break; };
      if(inclui) tempObj[i] = obj[i];
   }
   return tempObj;
}
Flash.prototype.addAttribute = function(prop, val){ this.attributes[prop] = val; }
Flash.prototype.addParameter = function(prop, val){ this.params[prop] = val; }
Flash.prototype.addVariable = function(prop, val){ this.variables.push([prop, val]); }
Flash.prototype.getFlashVars = function(){
   var tempString = new Array();
   for(var i=0; i<this.variables.length; i++)
      tempString.push(this.variables[i].join("="));
   return tempString.join("&");
}
Flash.prototype.toString = function(){
   this.params.flashVars = this.getFlashVars();
   if(Browser.isIE()){
      //IE
      this.html = "<object";
      var attr = Flash.getObjectByExceptions(this.attributes, ["type", "data"]);
      for(var i in attr) if(i.toString() != "extend") this.html += " " + i.toString() + " = \"" + attr[i] + "\"";
      this.html += "> ";
      var params = Flash.getObjectByExceptions(this.params, ["pluginurl", "extend"]);
      for(var i in params) if(i.toString() != "extend") this.html += "<param name=\"" + i.toString() + "\" value=\"" + params[i] + "\" /> ";
      this.html += " </object>";
   }
   else{
      //non-IE
      this.html = "<object";
      var attr = Flash.getObjectByExceptions(this.attributes, ["classid", "codebase"]);
      for(var i in attr) if(i.toString() != "extend") this.html += " " + i.toString() + " = \"" + attr[i] + "\"";
      this.html += "> ";
      var params = Flash.getObjectByExceptions(this.params, ["extend"]);
      for(var i in params) if(i.toString() != "extend") this.html += "<param name=\"" + i.toString() + "\" value=\"" + params[i] + "\" /> ";
      this.html += " </object>";
   }
   return this.html;
}
Flash.prototype.write = Flash.prototype.outIn = Flash.prototype.writeIn = function(w){
   if(typeof w == "string" && document.getElementById) var w = document.getElementById(w);
   if( w != undefined && w ) w.innerHTML = this.toString();
   else document.write( this.toString() );
}
//------------------------------------------------------------[/FLASH]------------------------------------------------------------------------------------//

/* core page script */

//initial values
var isIE6 = (navigator.appVersion.split(";")[1] == " MSIE 6.0");
var imgSrcBlank = "imagens/blank.gif";

//Enable PNGs for IE6
function applyPagePngs()
{
    if(isIE6){
        var allImages = document.images;
        
        //process image elements
        for(i=0; i<allImages.length; i++)
        {
            //process if not tagged with a noFilter class
            if(!/noFilter/.test(allImages[i].className))
            {
                //test for png extension or embedObject class (dynamically generated AP assets)
                if(/\.png$/i.test(allImages[i].src.toLowerCase()) || (/embedObject/.test(allImages[i].className) && allImages[i].mimeType == "PNG Image"))
                {
                    var originalSrc = allImages[i].src;
                    var originalHeight;
                    var originalWidth;
                    
                    //determine how to process based whether height and width have been defined
                    if(allImages[i].width > 0 && allImages[i].height > 0)
                    {
                        //accept predefined height/width
                        originalHeight = allImages[i].height;
                        originalWidth = allImages[i].width;
                    }
                    else
                    {
                        //reload image to retrieve height/width
                        var tempImage = new Image();
                        tempImage.onload = function(){originalHeight = tempImage.height; originalWidth = tempImage.width;}
                        tempImage.src = allImages[i].src;
                    }
                    
                    //apply replacement src and set height/width and filter
                    allImages[i].src = imgSrcBlank;
                    if(/fullWidth/.test(allImages[i].className))
                    {
                        allImages[i].style.width = "100%";
                    }
                    else
                    {
                        allImages[i].style.width = originalWidth + "px";
                    }
                    allImages[i].style.height = originalHeight + "px";
                    allImages[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + originalSrc + "',sizingMethod='scale')";
                }
            }
        }
    }
}
//-------------------------
function tipoPessoa(tipo){
	if (tipo == "f"){
		document.getElementById("pessoa").value = "F";
		Hide('comercial');
	}
	else if (tipo == "j"){
		document.getElementById("pessoa").value = "J";
		Show('comercial');
	}	
}
function Show(obj){
   document.getElementById(obj).style.visibility = 'visible';
   document.getElementById(obj).style.display = 'inline';
}
function Hide(obj){
   document.getElementById(obj).style.visibility = 'hidden';
   document.getElementById(obj).style.display = 'none';
}
//------------------------
function criaXMLHttp() {
	if (typeof XMLHttpRequest != "undefined") 
		return new XMLHttpRequest();
	else if (typeof ActiveXObject != "undefined") { // suporte ao IE 7
		var aVersoes = ["MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.5.0",
		"MSXML2.XMLHttp.4.0", "MSXML2.XMLHttp.3.0",
		"MSXML2.XMLHttp", "Microsoft.XMLHttp"
		];
		for (var i = 0; i < aVersoes.length; i++){
			try{
				return new ActiveXObject(aVersoes[i]);
			}catch (e) {}
		}
	}
	// se nenhum objeto xmlhttp for suportado
	throw new Error("Seu browser nao suporta AJAX");
}
function extraiScript(texto){
    var ini = 0;
    while (ini!=-1){         
		ini = texto.indexOf('<script', ini);        
		if (ini >=0){             
			ini = texto.indexOf('>', ini) + 1;            
			var fim = texto.indexOf('</script>', ini);            
			codigo = texto.substring(ini,fim);             
            novo = document.createElement("script")
            novo.text = codigo;
            document.body.appendChild(novo);			
            //document.write(codigo);			
        }
    }	
}

function enviaReq(forumlario){
	var oForm = forumlario;
	var sBody = criaReqStr(oForm);

	var oXMLHttp = criaXMLHttp();	
	oXMLHttp.open("post", oForm.action, true);	
	oXMLHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	oXMLHttp.onreadystatechange = function (){		
		if(oXMLHttp.readyState == 4){
			exibeResult(oXMLHttp.responseText);
			extraiScript(oXMLHttp.responseText);
		}
		/*else
			exibeResult("Ocorreu um erro: " + oXMLHttp.statusText);*/
	};	
	oXMLHttp.send(sBody);
}
function criaReqStr(oForm){
	var aPostStr = new Array();

	for(var i = 0; i < oForm.elements.length; i++){
		var sValor = encodeURIComponent(oForm.elements[i].name);
		sValor += "=";
		sValor += encodeURIComponent(oForm.elements[i].value);
		aPostStr.push(sValor);
	}	
	return aPostStr.join("&");
}
function exibeResult(sTxt){	
	document.getElementById("divStatus").innerHTML = sTxt;
}
function mascaraCNPJ(Campo, teclapres){
   var tecla = teclapres.keyCode;
   var vr = new String(Campo.value);
   vr = vr.replace(".", "");
   vr = vr.replace(".", "");
   vr = vr.replace("/", "");
   vr = vr.replace("-", "");
   tam = vr.length + 1 ;
   if(tecla != 9 && tecla != 8){
      if(tam > 2 && tam < 6)
         Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, tam);
      if(tam >= 6 && tam < 9)
         Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,tam-5);
      if(tam >= 9 && tam < 13)
         Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,tam-8);
      if(tam >= 13 && tam < 20)
         Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4)+ '-' + vr.substr(12,2);
   }
}
function mascaraCPF(Campo, teclapres){
   var tecla = teclapres.keyCode;
   var vr = new String(Campo.value);
   vr = vr.replace(".", "");
   vr = vr.replace(".", "");
   vr = vr.replace("-", "");
   tam = vr.length + 1;
   if(tecla != 9 && tecla != 8){
      if(tam > 3 && tam < 6)
         Campo.value = vr.substr(0, 3) + '.' + vr.substr(3, tam);
      if(tam >= 7 && tam < 8)
         Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,tam-6);
      if(tam >= 10 && tam < 16)
		 Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,3) + '-' + vr.substr(9,2);
   }
}
//--------------------------------------------------------------------------------------
function enviaReqAtt(forumlario, exibe){
	var oForm = forumlario;
	var sBody = criaReqStrAtt(oForm);

	var oXMLHttp = criaXMLHttp();
	oXMLHttp.open("post", oForm.action, true);	
	oXMLHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	oXMLHttp.onreadystatechange = function (){		
		if(oXMLHttp.readyState != 4){
			exibeResultAtt("<img src=\"" + js_site + "imagens/preloader.gif\" align=\"absmiddle\">", exibe);	
			bloqueia('envia');
		}
		if(oXMLHttp.readyState == 4){
			if (oXMLHttp.status == 200){
				exibeResultAtt(oXMLHttp.responseText, exibe);
				extraiScript(oXMLHttp.responseText);
				desbloqueia('envia');
			}
			if (oXMLHttp.status == 404){
					alert("404");
					desbloqueia('envia');
			}
		}
		/*else
			exibeResult("Ocorreu um erro: " + oXMLHttp.statusText);*/
	};	
	oXMLHttp.send(sBody);
}
function exibeResultAtt(sTxt, exibe){	
	document.getElementById(exibe).innerHTML = sTxt;
}
function criaReqStrAtt(oForm){
	var aPostStr = new Array();

	for(var i = 0; i < oForm.elements.length; i++){
		if (oForm.elements[i].type == "checkbox"){
			if(oForm.elements[i].checked == 1){
				var sValor = encodeURIComponent(oForm.elements[i].name);
				sValor += "=";
				sValor += encodeURIComponent(oForm.elements[i].value);
				aPostStr.push(sValor);
			}
			if(oForm.elements[i].checked == 0){
				var sValor = encodeURIComponent(oForm.elements[i].name);
				sValor += "=";
				sValor += encodeURIComponent("");
				aPostStr.push(sValor);
			}
		}
		else if (oForm.elements[i].type == "radio"){
			if(oForm.elements[i].checked == 1){
				var sValor = encodeURIComponent(oForm.elements[i].name);
				sValor += "=";
				sValor += encodeURIComponent(oForm.elements[i].value);
				aPostStr.push(sValor);
			}
		}
		else {
			var sValor = encodeURIComponent(oForm.elements[i].name);
			sValor += "=";
			sValor += encodeURIComponent(oForm.elements[i].value);
			aPostStr.push(sValor);
		}
	}	
	return aPostStr.join("&");
}
//
function bloqueia(obj){
	try{
		document.getElementById(obj).disabled = true;
	}
	catch(e){}
}
function desbloqueia(obj){
	try{
		document.getElementById(obj).disabled = false;
	}
	catch(e){}
}
function mudaFocus(obj, maxlength, proximo){
	if(obj == null)
		document.getElementById(proximo).focus();
	else if(obj.value.length == maxlength)
		document.getElementById(proximo).focus();
}
//----------------------------------
function mostra(oq){
	document.getElementById(oq).style.visibility = 'visible';
	document.getElementById(oq).style.display = 'inline';
}
function esconde(oq){
	document.getElementById(oq).style.visibility = 'hidden';
	document.getElementById(oq).style.display = 'none';
}
//------------------------------------------------------[ VALIDAÇÃO DE CPF, CNPJ E EMAIL]-------------------------------------------//
//Validação de CPF em ajax
var aux_validade = 0;
function cpf_validate(cpf, nick){
	if (!nick)
		nick = "";
	if (cpf.length == 0 || cpf.length == 1 || aux_validade == 1){
		document.getElementById(nick+'cpf_validate').innerHTML = "<img width=\"16\" height=\"16\" src=\"imagens/delete.png\"/>";
		aux_validade = 0
	}
	else if (cpf.length == 14){
		aux_validade = 1;
		var XMLHttp = criaXMLHttp();
		var url = js_site+"ajax/cpf_validate.php?cpf=" + cpf + "&pog=" + Math.ceil (Math.random() * 100000);
		XMLHttp.open("get", url, true);
		XMLHttp.onreadystatechange = function () {
			if (XMLHttp.readyState == 4)   
				if (XMLHttp.status == 200){   
					document.getElementById(nick+'cpf_validate').innerHTML = XMLHttp.responseText;
					extraiScript(XMLHttp.responseText);
				}		
			};  
		XMLHttp.send(null);
	}
}
//Validação de CNPJ em ajax
function cnpj_validate(cnpj,nick){	
	if (!nick)
		nick = "";
	if (cnpj.length == 0 || cnpj.length == 1 || aux_validade == 1){
		document.getElementById(nick+'cnpj_validate').innerHTML = "<img width=\"16\" height=\"16\" src=\"imagens/delete.png\"/>";
		aux_validade = 0
	}
	else if (cnpj.length == 18){
		aux_validade = 1;
		var XMLHttp = criaXMLHttp();
		var url = js_site+"ajax/cnpj_validate.php?cnpj=" + cnpj + "&pog=" + Math.ceil (Math.random() * 100000);
		XMLHttp.open("get", url, true);
		XMLHttp.onreadystatechange = function () {
			if (XMLHttp.readyState == 4)
				if (XMLHttp.status == 200){   
					document.getElementById(nick+'cnpj_validate').innerHTML = XMLHttp.responseText;
					extraiScript(XMLHttp.responseText);
				}		
			};  
		XMLHttp.send(null);
	}
}
function substr_count( haystack, needle, offset, length ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // *     example 1: substr_count('Kevin van Zonneveld', 'e');
    // *     returns 1: 3
    // *     example 2: substr_count('Kevin van Zonneveld', 'K', 1);
    // *     returns 2: 0
    // *     example 3: substr_count('Kevin van Zonneveld', 'Z', 0, 10);
    // *     returns 3: false
 
    var pos = 0, cnt = 0;
 
    haystack += '';
    needle += '';
    if(isNaN(offset)) {offset = 0;}
    if(isNaN(length)) {length = 0;}
    offset--;
 
    while( (offset = haystack.indexOf(needle, offset+1)) != -1 ){
        if(length > 0 && (offset+needle.length) > length){
            return false;
        } else{
            cnt++;
        }
    }
 
    return cnt;
}
//Validação de e-mail em ajax
function email_validate(email, nick){	
	if (!nick)
		nick = "";
	if(email.length == 0 || email.length == 1){
		document.getElementById(nick+'email_validate').innerHTML = "<img width=\"16\" height=\"16\" src=\"imagens/delete.png\"/>";
	}
	else if (substr_count(email, '@') > 0){
		var XMLHttp = criaXMLHttp();
		var url =  js_site+"ajax/email_validate.php?email=" + email + "&pog=" + Math.ceil (Math.random() * 100000);
		XMLHttp.open("get", url, true);
		XMLHttp.onreadystatechange = function () {
			if (XMLHttp.readyState == 4)   
				if (XMLHttp.status == 200){   
					document.getElementById(nick+'email_validate').innerHTML = XMLHttp.responseText;
					extraiScript(XMLHttp.responseText);
				}		
			};  
		XMLHttp.send(null);
	}
}
//------------------------------------------------------[/VALIDAÇÃO DE CPF ,CNPJ E EMAIL]-------------------------------------------//
function SomenteNumeros(campo,cpfcnpj){
	if (cpfcnpj == 1)
	    var digits="0123456789.-/"
	else 
		var digits="0123456789"
    var campo_temp
    for (var i=0;i<campo.value.length;i++){
      campo_temp=campo.value.substring(i,i+1)    
      if (digits.indexOf(campo_temp)==-1){
            campo.value = campo.value.substring(0,i);
            break;
       }
    }
}
//----------------------------------------
function olhaCEP(i){
	if (i == 1){
		var campo = document.getElementById("cep1").value;
		if (campo.length == 5)
			document.getElementById("cep2").focus();
	}
	else if (i == 2){
		var campo = document.getElementById("cep2").value;
		if (campo.length == 3){
			parent.document.getElementById('response').innerHTML = '<div align=\"center\"><img src=\"'+js_site+'/imagens/preloader.gif\"></div>';
			var cep = document.getElementById("cep1").value + document.getElementById("cep2").value;
			document.getElementById('buscaCEP').src = js_site+'cep/endereco_busca_submit.php?cep1=' + cep;
		}			
	}	
}
function mudaTipo(tipo){
			if(tipo == 'J'){
				Show('form_pj');
			}
			else{
				Hide('form_pj');
			}
}