/*
-> Função removida por George Azevedo em 23/06/2010.
-> Razão: Causava diversos bugs em todas as versões do Microsoft IE.

if (Node === undefined) {
    var Node = new Object();
}
if (Node.ELEMENT_NODE === undefined) {
    Node.ELEMENT_NODE = 1;
}

*/



/*
Form2Pop Script- By Peter Bailey (http://www.peterbailey.net)
Featured on JavaScriptKit.com
Visit http://www.javascriptkit.com for this script and more
*/
function createTarget(t){
    var w = window.screen.availWidth;
    var h = window.screen.availHeight;
    window.open("", t, "top=0,left=0,width="+w+",height="+h+",resizable=1,status=1,toolbar=1,scrollbars=1");
    return true;
}


//pedido do pablo em 17/01/2011

function clickMais(campo){

/*var part_ini = campo.substring(0,5);
var part_fim = campo.substring(5,7);

//new Effect.toggle($(part_ini+"_"+part_fim),'blind');*/

	teste(campo);
	showhide(campo);

}



function showhide(campo){
	
/*var part_ini = campo.substring(0,5);
var part_fim = campo.substring(5,7);*/

//alert(document.getElementById(part_ini+"_"+part_fim).offsetHeight);

/*if (document.getElementById(campo).offsetHeight < 5) {
//		document.getElementById(campo).style.display = 'none';
document.getElementById(campo).innerHTML= "<a href=\"javascript:;\" onclick=\"clickMais('"+campo+"');\">[-]</a>";
} else {
//		document.getElementById(campo).style.display = '';
document.getElementById(campo).innerHTML= "<a href=\"javascript:;\" onclick=\"clickMais('"+campo+"');\">[+]</a>";
}*/

}

function Tau_showHideLayers() { //v3.0
ns4 = (document.layers)? true:false;
ie4 = (document.all)? true:false;
dom = (ns4|ie4)? false:true;
	var camada,condicao,args=Tau_showHideLayers.arguments;
	for(var i=0;i<(args.length/2);i+=1){
		camada=MM_findObj(args[2*i]);
		condicao=args[2*i+1];
		if(ns4) camada.visibility=condicao;
		if(ie4) camada.style.visibility=condicao;
		if(dom) document.getElementById(args[2*i]).style.visibility=condicao;
	}
// Para parar o bubbling
    window.event ? window.event.cancelBubble = true : false;
}


function Tau_hidePopup() { //v3.0
ns4 = (document.layers)? true:false;
ie4 = (document.all)? true:false;
dom = (ns4|ie4)? false:true;
	var camada,condicao,args=Tau_hidePopup.arguments;
	for(var i=0;i<(args.length/2);i+=1){
		camada=MM_findObj(args[2*i]);
		condicao=args[2*i+1];
		if(ns4) camada.display=condicao;
		if(ie4) camada.style.display=condicao;
		if(dom) document.getElementById(args[2*i]).style.display=condicao;
	}
// Para parar o bubbling
    window.event ? window.event.cancelBubble = true : false;
}

/*
 * Funcao padrao para abrir janelas.
 */
function TAU_openBrWindow(theURL,winName,features,largura,altura,f) { //v2.0
  if(f==1){
    l=0;t=0;largura=(screen.availWidth-10);altura=(screen.availHeight-26);
  }else{
    var l = ((screen.availWidth)/2) - (largura/2);
    var t = ((screen.availHeight)/2) - ((altura/2)+20);
  }
  features = features + ",width=" + largura + ",height=" + altura + ",left=" + l + ",top=" + t;
  window.open(theURL,winName,features);
}

function MM_findObj(n, d) { //v4.01
    var p,i,x;
    if(!d)
        d=document;
    if((p=n.indexOf("?"))>0&&parent.frames.length) {
        d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
    }
    if(!(x=d[n])&&d.all)
        x=d.all[n];
    for (i=0;!x&&i<d.forms.length;i++)
        x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i>d.layers.length;i++)
        x=MM_findObj(n,d.layers[i].document);
    if(!x && d.getElementById)
        x=d.getElementById(n);
    return x;
}

function tau__hideTabs(lstTabs) {
    for (var count=0; count<lstTabs.length; count++) {
	document.getElementById(lstTabs[count]).style.display = 'none';
//        $(lstTabs[count]).style.display = 'none';
    }
}

function tau__showTab(lstTabs,tab) {
    tau__hideTabs(lstTabs);
    document.getElementById(lstTabs[tab-1]).style.display = 'block';
//    $(lstTabs[tab-1]).style.display = 'block';
}

var tau__theTimeout;
function tau__timeoutVendas(bOnOff) {
    if (bOnOff == 0) {
        clearTimeout(tau__theTimeout);
    } else {
        tau__theTimeout = setTimeout(tau__hideFormVendas, 30000);
    }
}

function tau__hideFormVendas() {
    document.getElementById('formVendas').style.display = 'none';
}

function tau__submitVendas(objSubmit) {
   //console.log(document.getElementById(objSubmit.form.id));
   var frm = objSubmit.form;
   frm.fecha.value = frm.mes.value+frm.dia.value;
   frm = null;
   //console.log(objSubmit.form.fecha.value);
}

function tau__updateDias(objMes) {
    var objDias = document.getElementById('dia');
    var idxMax = tau__lstDias[objMes.selectedIndex];
    if (objDias.options.length > idxMax) {
        while (idxMax < objDias.options.length) {
            objDias.options[objDias.options.length-1] = null;
        }
    } else if (objDias.options.length < idxMax) {
        while (idxMax > objDias.options.length) {
            numNovoDia = objDias.options.length+1;
            objDias.options[numNovoDia-1] = new Option(numNovoDia,numNovoDia);
        }
    }
}


/*
Funcoes para o form de Negocios > Encomendas > Cadastro
*/

function formCadastroEncomendas(){
	if(formCadastroEncomendas.arguments.length){
		document.getElementById("dadosFatura").style.display="block";
	}
		//if(document.getElementById("enderecoEnvioSim").checked && document.getElementById("dadosFatura").style.display=="block"){
		if(document.getElementById("enderecoEnvioSim").checked){
			document.getElementById("enderecoFatura").value = document.getElementById("enderecoCadastro").value + " " + document.getElementById("numeroCadastro").value;
			document.getElementById("bairroFatura").value = document.getElementById("bairroCadastro").value;
			document.getElementById("cidadeFatura").value = document.getElementById("cidadeCadastro").value;
			document.getElementById("cepFatura").value = document.getElementById("cepCadastro").value;
			document.getElementById("dddFatura").value = document.getElementById("dddCadastro").value;
			document.getElementById("telefoneFatura").value = document.getElementById("telefoneCadastro").value;
			var input = document.getElementById("dadosFatura").getElementsByTagName("input");
			//var orig = document.getElementById("dadosCobranca").getElementsByTagName("input");
			for(var i = 0; i < input.length; i++){
				//input[i].value = orig[i].value;

				input[i].disabled = true;
			}
			document.getElementById("ufFatura").selectedIndex = document.getElementById("ufCadastro").selectedIndex;
			document.getElementById("ufFatura").disabled = true;
		}else{
			var input = document.getElementById("dadosFatura").getElementsByTagName("input");

			for(var i = 0; i < input.length; i++){

				input[i].disabled = false;

			}
			document.getElementById("ufFatura").disabled = false;
		}
}
//

function tau__initVendas(objOrigem,objDestino) {
    var data = new Date();
    objOrigem.form.dia.selectedIndex = data.getDate()-1;
    updatehasta(objOrigem.options[objOrigem.options.selectedIndex].value);
    document.getElementById('loading').style.display = 'none';
    objDestino.form.style.visibility = 'visible';
}


function tau__initVendasBR5(objOrigem,objDestino) {
 updatehasta(objOrigem.options[objOrigem.options.selectedIndex].value);
 document.getElementById('loading').style.display = 'none';
 objDestino.form.style.visibility = 'visible';
} 
/*
-> Função removida por George Azevedo em 23/06/2010.
-> Razão: Causava diversos bugs em todas as versões do Microsoft IE.


if (Event && Event.observe)
  Event.observe(window,'load',function() {
    if ($('withLightbox') != undefined) {
        $('withLightbox').style.display = 'block';
        $('ajaxloader').style.display = 'none';
    }
},false);
*/

function flash(html){
	document.write(html);
}

function validaFormIncNL(f){
	//d = document.getElementById('formnewsletter');
	messErr="";
	if (f.nome.value == "nome" || f.nome.value == ""){
		messErr+="O campo Nome precisa ser preenchido.\n\n";
	}
	if (f.email.value == "e-mail" || f.email.value == ""){
		messErr+="O campo E-mail precisa ser preenchido.\n\n";
	} else {
		/*var temp = emailCheck(f.email.value);
		alert(temp);
		if (temp) {
			messErr += temp;
		}*/
	}

	if(messErr!=""){
		alert(messErr);
	}else{
		//Ajax
		f.submit();
	}
}

function emailCheck(emailStr){
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var returnMsg = new Object();
	//
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
		returnMsg="Endereço de email incorreto (verifique @ e '.').\n"
		//returnMsg['pt'] = "Endereço de email incorreto (verifique @ e '.').";
		//returnMsg.en = "Incorrect email address (verify @ and '.').";
		return returnMsg;
	}
	//
	var user=matchArray[1]
	var domain=matchArray[2]
	//
	// See if "user" is valid 
	//
	if (user.match(userPat)==null) {
		// user is not valid
		returnMsg="Nome de usuário de email inválido.\n";
		//returnMsg['pt'] = "Nome de usuário de email inválido.";
		//returnMsg.en = "Invalid email username.";
		return returnMsg;
	}
	//
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
	// this is an IP address
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				returnMsg="IP de destino inválido.\n";
				//returnMsg['pt'] = "IP de destino inválido.";
				//returnMsg.en = "Invalid IP.";
				return returnMsg;
			}
		}
	return true
	}
	// Domain is symbolic name
	var domainArray=domain.match(domainPat);
	if (domainArray==null) {
		returnMsg="Nome de domínio inválido.\n";
		//returnMsg['pt'] = "Nome de domínio inválido.";
		//returnMsg.en = "Invalid domain name.";
		return returnMsg;
	}
	//
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) {
		returnMsg="O endereço de email deve terminar em um domínio de três letras ou um código de país de duas letras.\n";
		// the address must end in a two letter or three letter word.
		//returnMsg['pt'] = "O endereço de email deve terminar em um domínio de três letras ou um código de país de duas letras.";
		//returnMsg.en = "The email address must end in a three-letter domain or a two-letter country code.";
		return returnMsg;
	}
	// Make sure there's a host name preceding the domain.
	if (len<2) {
		returnMsg="Endereço sem um 'hostname'.\n";
		//returnMsg['pt'] = "Endereço sem um 'hostname'!";
		//returnMsg.en = "Address without a hostname.";
		return returnMsg;
	}
	// If we've gotten this far, everything's valid!
	//return true;
	return "";
}

function ShowHide (conteudo) {
	if (document.getElementById(conteudo).style.display == 'none') { //Se o display é none, então exibe
		document.getElementById(conteudo).style.display = 'block';
	} else { //Se não, exibe!
		document.getElementById(conteudo).style.display = 'none';
	}
}

