/*
	Arquivo com funções úteis e genéricas
	Modificado por: Abelmon Bastos
*/

/*-----------------------------------------------------------------------*
 | Valida o formulário de Licitacao Contato para ser possível cadastrar  |
 |                 uma empresa com os dados corretos.                    |
 *-----------------------------------------------------------------------*/
function validaFormContato(documento){
	d = documento;
	if (d.razaoSocial.value == ""){
		alert("O campo Razão Social deve ser preenchido!");
		d.razaoSocial.focus();
		return false;
	}
	
	if (d.email.value == ""){
		alert("O campo " + d.email.name + " deve ser preenchido!");
		return false;
	}
	
	if (validaEmail(d.email) == false){
		d.email.focus();
		return false;
	}
	
	if (d.cnpj.value == ""){
		alert("O campo " + d.cnpj.name + " deve ser preenchido!");
		d.cnpj.focus();
		return false;
	}          

	if (valdiaCNPJ(d.cnpj.value) == false){
		d.cnpj.focus();
		return false;
	}
	
	return true;    
}
    
/*-----------------------------------------------------------------------*
 |                             Valida um CNPJ                            |
 *-----------------------------------------------------------------------*/     
function valdiaCNPJ(CNPJ){
	erro = new String;
	if (CNPJ.length < 18) 
		erro += "É necessarios preencher corretamente o numero do CNPJ! \n\n";
	if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
		if (erro.length == 0) 
			erro += "É necessarios preencher corretamente o numero do CNPJ! \n\n";
	}
	//substituir os caracteres que não são numeros
	if(document.layers && parseInt(navigator.appVersion) == 4){
		x = CNPJ.substring(0,2);
		x += CNPJ.substring(3,6);
		x += CNPJ.substring(7,10);
		x += CNPJ.substring(11,15);
		x += CNPJ.substring(16,18);
		CNPJ = x;
	} else {
		CNPJ = CNPJ.replace(".","");
		CNPJ = CNPJ.replace(".","");
		CNPJ = CNPJ.replace("-","");
		CNPJ = CNPJ.replace("/","");
	}
	var nonNumbers = /\D/;
	if (nonNumbers.test(CNPJ)) 
		erro += "A verificacao de CNPJ suporta apenas numeros! \n\n";
	var a = [];
	var b = new Number;
	var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
	for (i=0; i<12; i++){
		a[i] = CNPJ.charAt(i);
		b += a[i] * c[i+1];
	}
	if ((x = b % 11) < 2) { 
		a[12] = 0 
	} else { 
		a[12] = 11-x 
	}
	b = 0;
	for (y=0; y<13; y++) {
		b += (a[y] * c[y]);
	}
	if ((x = b % 11) < 2) { 
		a[13] = 0; 
	} else { 
		a[13] = 11-x; 
	}
	if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
		erro +="Digito verificador com problema!";
	}
	if (erro.length > 0){
		alert(erro);
		return false;
	}
	return true;
}
    
     
function popup(url){
	window.open(url,'popup','width=780,height=700,scrolling=auto,top=0,left=0');
}
    
    
function redimensiona(nomeDiv, offSet){
	var divContent = document.getElementById(nomeDiv);
	var x,y;        
	if (self.innerHeight) // all except Explorer
	{
		x = self.innerWidth; y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
	x = document.documentElement.clientWidth; y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;  y = document.body.clientHeight;
	}   
	divContent.style.height = (y + offSet)+"px";
}
    
function resize(){
	redimensiona('content',-310-70);
}

function clearFromContent(objId,tagName,attrName) {
	var content=document.getElementById(objId)
	var fontNodes= content.getElementsByTagName(tagName)
	for (i=0; i<fontNodes.length; i++){
		fontNodes[i].removeAttribute(attrName);
	}
}

function fixAttributeTag(objId,tagName,attrName,value) {
	var content=document.getElementById(objId)
	var nodes= content.getElementsByTagName(tagName)
	for (i=0; i<nodes.length; i++){
		nodes[i].setAttribute(attrName,value);
	}
}

