navie=(navigator.userAgent.indexOf("MSIE")==-1?false:true);
// Função de replace all
function repstr (str, str1, str2) {
	while (true) {
		if (str.indexOf(str1)==-1) break;
		str=str.replace(str1,str2);
	}
	return str;
}

// verifica se existem caracateres invalidos
function CaracteresInvalidos(CharsInvalidos,objeto,msg){
	// Retorna se o string str conter algum caracter invalido
	// por caracteres contidos no string CharsInvalidos.
	for (fc_i=0;fc_i < CharsInvalidos.length;fc_i++) {
		if (objeto.value.indexOf(CharsInvalidos.charAt(fc_i))!=-1) {
			alert(msg);
			return 1;
		}
	}
}

// Cálculo de Datas
function calc_data (data,dias,tipo) {
if (typeof tipo=="undefined" || tipo=="") tipo="+";
if (typeof dias=="undefined" || dias=="") dias=0;
if (typeof data=="undefined" || data=="") {dt=new Date(); data=dt.getDate()+"/"+(dt.getMonth()+1)+"/"+dt.getYear();}
sp=data.split('/');
if (tipo=='+')
	calc=new Date(parseFloat(sp[2]),parseFloat(sp[1])-1,parseFloat(sp[0])+parseFloat(dias));
else
	calc=new Date(parseFloat(sp[2]),parseFloat(sp[1])-1,parseFloat(sp[0])-parseFloat(dias));
dd=calc.getDate();
if (dd<10) dd="0"+dd;
mm=calc.getMonth()+1;
if (mm<10) mm="0"+mm;
yy=calc.getYear();
volta=dd+'/'+mm+'/'+yy;
return volta;
}

// retorna o trim da variável
function trim(src) {
	while(true) {
		if (src.indexOf(" ")==0)
			if (src.length==1)
				src="";
			else
				src=src.substring(1,src.length);
		else
			break;
		}
	while(true) {
		if (src.lastIndexOf(" ")==-1)
			break;
		else if (src.lastIndexOf(" ")==src.length-1)
			src=src.substring(0,src.length-1);
		else
			break;
		}
	return src;
}

// Função para procurar na dropdown a string digitada
var objeto;
function dropdown(fld, e, st) {
	var key = '';
	var strCheck = 'abcdefghijklmnopqrtsuvwxyz0123456789 ';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode == 9 || whichCode == 38 || whichCode == 40) return true;
	if (whichCode == 8 && acha_drop=="") return false;
	if (typeof acha_drop!="string") acha_drop="";

	if (typeof tf_1!="undefined") window.clearTimeout(tf_1);

	if (st && st==1 && fld.onchange) {
		objeto=fld;
		tf_1=window.setTimeout("acha_drop='';objeto.onchange();window.clearTimeout(tf_1);",2000);
	} else {
		tf_1=window.setTimeout("acha_drop='';window.clearTimeout(tf_1);",2000);
	}

	if (whichCode == 8) {
		acha_drop=acha_drop.substring(0,acha_drop.length-1);
	} else {
		key = String.fromCharCode(whichCode);  // Get key value from key code
		acha_drop+=key;
		if (strCheck.indexOf(key.toLowerCase()) == -1) return false;  // Not a valid key
	}
	flag=false;
	for (i=0;i<fld.length;i++) {
		if (fld.options[i].text.substring(0,acha_drop.length).toLowerCase()==acha_drop.toLowerCase()) {
			fld.options[i].selected=true;
			flag=true;
			break;
		}
	}
	if (flag==false && acha_drop!="") acha_drop=acha_drop.substring(0,acha_drop.length-1);
	return false;
}

// Preload
function MM_preloadImages() 
	{
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
	}

function MM_findObj(n, d) { //v4.0
  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 && document.getElementById) x=document.getElementById(n); return x;
}

function MM_showHideLayers() { //v3.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }
}

// VerySign
function certisign(url) 
	{
	sealWin=window.open(url,"win",'toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=500,height=450');
	self.name = "mainWin"; 
	}

// Rollover dos menus
function mOvr(src,clrOver) {
 if (!src.contains(event.fromElement)) {
  src.style.cursor = 'hand';
  src.bgColor = clrOver;
 }
}
function mOut(src,clrIn) {
 if (!src.contains(event.toElement)) {
  src.style.cursor = 'default';
  src.bgColor = clrIn;
 }
}
function mClk(src) {
if(event.srcElement.tagName=='TD'){
src.children.tags('a')[0].click();
}
}


// Compra Página Conceitual Produtos Service
function f_como_adquirir()
	{
	if (document.formcompra.selectcompra.value==0)
		{
		alert('Escolha o tipo de usuário');
		}
	if (document.formcompra.selectcompra.value==1)
		{
		window.open('/login.htm', 'Login', 'width=231, height=290, status=yes');
		}
	if (document.formcompra.selectcompra.value==2)
		{
		document.location.href='/requisicao.cfm?assinante=PF'
		}
	if (document.formcompra.selectcompra.value==3)
		{
		document.location.href='/requisicao.cfm?assinante=PJ'
		}
	}
	
// Aviso de Produto já existente
function f_aviso()
	{
		alert('Você já possui este produto. Para renovação consulte a seção MINHA CONTA.');
	}
	
// Rollover form but

function change(color){
var el=event.srcElement
if ((el.tagName=="INPUT"&&el.type=="button") || (el.tagName=="INPUT"&&el.type=="submit") || (el.tagName=="INPUT"&&el.type=="reset") || (el.tagName=="INPUT"&&el.type=="file"))
event.srcElement.style.backgroundColor=color
}

// Chama tela de contato

function chama_contato() {
tipo=typeof(contato);
if (tipo=="undefined")
	document.location.href="/helpdesk/contato.cfm";
else
	document.location.href="/helpdesk/contato.cfm?produto="+contato;
}

// Só aceita minúsculas

function sominusculas(fld, e) {
var sep = 0;
var key = '';
var i = j = 0;
var len = len2 = 0;
var strCheck = 'abcdefghijklmnopqrstuvwxyz';
var aux = aux2 = '';
var whichCode = (window.Event) ? e.which : e.keyCode;
if (whichCode == 13) return true;  // Enter
key = String.fromCharCode(whichCode);  // Get key value from key code
if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
if ((key==',') && fld.value.indexOf(key) != -1) return false;
return true;
}

// Só aceita números

function sonumeros(fld, e) {
var sep = 0;
var key = '';
var i = j = 0;
var len = len2 = 0;
var strCheck = '0123456789';
var aux = aux2 = '';
var whichCode = (window.Event) ? e.which : e.keyCode;
if (whichCode == 13) return true;  // Enter
key = String.fromCharCode(whichCode);  // Get key value from key code
if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
if ((key==',') && fld.value.indexOf(key) != -1) return false;
return true;
}

// Só aceita números

function sovalor(fld, e) {
var sep = 0;
var key = '';
var i = j = 0;
var len = len2 = 0;
var strCheck = '0123456789,';
var aux = aux2 = '';
var whichCode = (window.Event) ? e.which : e.keyCode;
if (whichCode == 13) return true;  // Enter
key = String.fromCharCode(whichCode);  // Get key value from key code
if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
if ((key==',') && fld.value.indexOf(key) != -1) return false;
return true;
}

var triOver = "/imagens_ds/minha_conta_seta.gif"; // triangle images
var triUp = "/imagens_ds/minha_conta_seta.gif";
var triDown = "/imagens_ds/minha_conta_seta.gif";
var antId="";

function t(groupId, itemId){
	detail = document.all(groupId + "_d" + itemId);
	button = document.all(groupId + "_b" + itemId);
	
	isExpanded =  ((detail.style.display == "") || (detail.style.display == "none"));
	setExpanded(detail, button, isExpanded);

	if (groupId!="minhaconta") {
		if ((antId!="") && (antId!=groupId)) {
			detail = document.all(antId + "_d" + itemId);
			button = document.all(antId + "_b" + itemId);
			isExpanded =  ((detail.style.display == "") || (detail.style.display == "none"));
			if (isExpanded==false) setExpanded(detail, button, isExpanded);
			}
		antId=groupId;
		}
	return false;
}

function setExpanded(detail, button, isExpanded)
{
	if (isExpanded) {
		detail.style.display = "block";
//		button.src = triDown;
	} else {
		detail.style.display = "none";
//		button.src = triUp;
	}
}

// Expand/contract all the boxes in the group
function toggleAll(groupId)
{
	var row;
	var i = 0;
	var expand;
	
	if( document.all(groupId + "_e").AllExpand == null )
		expand = true;
	else
		expand = ! document.all(groupId + "_e").AllExpand;
	
	while(true)
	{
		detail = document.all(groupId + "_d" + i);
		button = document.all(groupId + "_b" + i);
		
		if (detail == null) {
			break;
		}
		
		setExpanded(detail, button, expand);
		i++;
	}
	
	document.all(groupId + "_e").innerText = (expand == true ? "Contract all" : "Expand all");
	document.all(groupId + "_e").AllExpand = expand;
}

// OverHeaderBar
function v(groupId, itemId, cssClass)
{
	
	document.all(groupId + "_h" + itemId).className = cssClass;
//	document.all(groupId + "_b" + itemId).src = triOver;
}	

// outHeaderBar
function u(groupId, itemId, cssClass)
{
	var img;
		
	//<%-- if the associated item is displayed, show the down error, otherwise the up arrow --%>
	if (document.all(groupId + "_d" + itemId).style.display == "block")
		img = triDown;
	else 
		img = triUp;
	
	document.all(groupId + "_h" + itemId).className = cssClass;
//	document.all(groupId + "_b" + itemId).src = img;
}

function sodatas(fld, e) {
var sep = 0;
var key = '';
var i = j = 0;
var len = len2 = 0;
var strCheck = '0123456789/';
var aux = aux2 = '';
var whichCode = (window.Event) ? e.which : e.keyCode;
if (whichCode == 13) return true;  // Enter
key = String.fromCharCode(whichCode);  // Get key value from key code
if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
if ((key==',') && fld.value.indexOf(key) != -1) return false;
return true;
}

function monta_data(data) {
numeros = "";
  for(i=0;i<data.value.length;i++){
    if(data.value.charAt(i)!="/") numeros+=data.value.charAt(i);
  }
nova_data = "";
  for(i=0;i<numeros.length;i++){
   if((i==2) || (i==4)){
	 nova_data+="/"+numeros.charAt(i);}
   else{
	 nova_data+=numeros.charAt(i);}
   }
data.value = nova_data;
}

// Função para validar login
function fv_login(src) {
	if (src.value.indexOf(' ')!=-1) {
		return "Campos não pode conter espaços";
	}
	if (src.value.length < 5) {
		return "Tamanho mínimo de 5 caracteres";
	}
	car_valid="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
	for (ii=0;ii<src.value.length;ii++) {
		if (car_valid.indexOf(src.value.charAt(ii))==-1) return "Caracter "+src.value.charAt(ii)+" não é válido para login";
		}
	return "";
}

function emailCheck (emailStr) {
/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	return 1;
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    return 2;
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
		return 3;
	    }
    }
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
    return 4;
}
/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding 
   the domain or country. */
/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
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) {
   // the address must end in a two letter or three letter word.
   return 5;
}
// Make sure there's a host name preceding the domain.
if (len<2) {
   return 6;
}
// If we've gotten this far, everything's valid!
}

function ValidaCPF (cpf) {
	if (cpf.length!=0) {
	cpf = sonumero (cpf);
    if (cpf.length != 11)
		return 1;
    for (i = 10; i <= 11; i++) {
		d = 0
		for (j = i; j >= 2; j--)
	    	d += parseInt (cpf.charAt (i-j)) * j
			d = (d %= 11) < 2 ? 0 : 11 - d
			if (parseInt (cpf.charAt (i-1)) != d)
	    	return 2;
	    }
	}
}

function sonumero (s) {
    saida = new String()
	numeros="0123456789";
    for (i = 0; i < s.length; i++) {
		if (numeros.indexOf(s.charAt(i))!=-1) saida+=s.charAt(i);
    }
    return saida;
}
