// JavaScript Document
// VIABR - SMART SOLUTIONS
// Funções de Validação

function abre(pagina,alt,larg) {
msgWindow=window.open(pagina,'foto','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width='+alt+',height='+larg);
msgWindow.focus();
return;
}	

function changeColor(objDiv){
	if(objDiv.className == 'fontMenuOver'){	
		objDiv.className='fontMenu';
	}else{
		objDiv.className='fontMenuOver';
	}
}

function validaemail(email)
{
	p=email.indexOf("@");
	z=email.indexOf(".");
	if ((p<1 || p==(email.length-1)) || (z<1 || z==(email.length-1))) 
		return false;
	return true;
}
function trim(str)
{        
        return str.replace(/^\s*|\s*$/g,"");
}

function CEP(cep)
{
	var valid = "0123456789-";
	var hyphencount = 0;

	if (cep.length!=9 && cep.length!=10) {
		alert("CEP inválido.");
		return false;
	}
	
	for (var i=0; i < cep.length; i++) {
		temp = "" + cep.substring(i, i+1);
		if (temp == "-") hyphencount++;
		if (valid.indexOf(temp) == "-1") {
			alert("CEP inválido.");
			return false;
		}
		if ((hyphencount > 1) || ((cep.length==10) && ""+cep.charAt(5)!="-")) {
			alert("CEP inválido.");
			return false;
	   }
	}
}

function formatar(src, mask) {
		
		var i = src.value.length;
		var saida = mask.substring(0,1);
		
		var texto = mask.substring(i)
		
		if (texto.substring(0,1) != saida) {
			src.value += texto.substring(0,1);
		}
}

function ChecaCNPJ(strCGC) {
	var NewCGC = "";
	var DV = "";
	var DVCGC;
	var s1 = 0;
	var s2 = 0;
	var NewDV;
	
	//Verifica tamanho do CGC
	if ((strCGC.length != 14) || (isNaN(strCGC)))
	{
		alert('CPF inválido');
		return false;
	}
	
	//Calcula os dígitos verificadores
	//Guarda os 12 primeiros digitos
	DVCGC = strCGC.substring(0,12);
	
	//calcula o primeiro digito verificador
	for (i=1;i<=4;i++)
		s1 = s1 + (parseInt(DVCGC.charAt(i-1))*(6-i));

	for (i=5;i<=12;i++)
		s1 = s1 + (parseInt(DVCGC.charAt(i-1))*(14-i));
	
	r1 = s1 % 11;
	if (r1 < 2)
		dv1 = 0;
	else 
		dv1 = 11 - r1;
	
	//calcula o segundo digito verificador
	s2 = dv1 * 2;
	
	for (i=1;i<=5;i++)
		s2 = s2 + (parseInt(DVCGC.charAt(i-1))*(7-i));
	for (i=6;i<=12;i++)
		s2 = s2 + (parseInt(DVCGC.charAt(i-1))*(15-i));
	r2 = s2 % 11;
	if (r2 < 2)
		dv2 = 0;
	else
		dv2 = 11 - r2;
	
	//junta os digitos verificadores
	DV = DV + dv1 + dv2;
	//guarda os digitos verificadores do CGC digitado (últimas duas posições no string)
	NewDV = strCGC.substring(12,14)
	if (NewDV == DV) //se o DV calculado for igual ao digitado, retorna true
		return true;
	else
	{
		alert('CPF inválido');
		return false;
	}
}

function ChecaCPF(strCPF) {
  	if (strCPF.length != 11 || strCPF == "00000000000" || strCPF == "11111111111" ||
		strCPF == "22222222222" ||	strCPF == "33333333333" || strCPF == "44444444444" ||
		strCPF == "55555555555" || strCPF == "66666666666" || strCPF == "77777777777" ||
		strCPF == "88888888888" || strCPF == "99999999999")
		alert('CPF inválido');
		return false;
	soma = 0;
	for (i=0; i < 9; i ++)
		{soma += parseInt(strCPF.charAt(i)) * (10 - i);}
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		{resto = 0;}
	if (resto != parseInt(strCPF.charAt(9)))
		{alert('CPF inválido'); return false;}
	soma = 0;
	for (i = 0; i < 10; i ++)
		{soma += parseInt(strCPF.charAt(i)) * (11 - i);}
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		{resto = 0;}
	if (resto != parseInt(strCPF.charAt(10)))
		{alert('CPF inválido'); return false;}
	return true;
}

// JavaScript Document

function findPosX(obj)
{
 var curleft = 0;
 if (obj.offsetParent)
 {
  while (obj.offsetParent)
  {
   curleft += obj.offsetLeft
   obj = obj.offsetParent;
  }
 }
 else if (obj.x)
  curleft += obj.x;
 return curleft;
}

function findPosY(obj)
{
 var curtop = 0;
 if (obj.offsetParent)
 {
  while (obj.offsetParent)
  {
   curtop += obj.offsetTop
   obj = obj.offsetParent;
  }
 }
 else if (obj.y)
  curtop += obj.y;
 return curtop;
}


function setLyr(obj,lyr,w,h)
{
 var newX = findPosX(getRefToDiv(obj))+w;
 var newY = findPosY(getRefToDiv(obj))+h;
 var x = new getRefToDiv(lyr);
 x.style.top = newY + 'px';
 x.style.left = newX + 'px';
}

function getRefToDiv(divID) {
     if( document.layers ) { //Netscape layers
         return document.layers[divID]; }
     if( document.getElementById ) { //DOM; IE5, NS6, Mozilla, Opera
        return document.getElementById(divID); }
     if( document.all ) { //Proprietary DOM; IE4
         return document.all[divID]; }
     if( document[divID] ) { //Netscape alternative
         return document[divID]; }
     return false;
 }
 
 function openIT(url) {
	theURL=url
	wname ="CHROMELESSWIN"
	W=260;
	H=360;
	windowCERRARa 		= "imagens/close_3.gif" 
	windowCERRARd 		= "imagens/close_1.gif"
	windowCERRARo 		= "imagens/close_2.gif"
	windowNONEgrf 		= ""
	windowCLOCK		= ""
	windowREALtit		= "Agenda Escolar"
	windowTIT 	    	= "<font face=verdana size=1 color=white><strong>&nbsp; Agenda Escolar</strong></font>"
	windowBORDERCOLOR   	= "#000000"
	windowBORDERCOLORsel	= "#999999"
	windowTITBGCOLOR    	= "#999999"
	windowTITBGCOLORsel 	= "#333333"
	openchromeless(theURL, wname, W, H, windowCERRARa, windowCERRARd, windowCERRARo, windowNONEgrf, windowCLOCK, windowTIT, windowREALtit , windowBORDERCOLOR, windowBORDERCOLORsel, windowTITBGCOLOR, windowTITBGCOLORsel)
}

function swf($arquivo,$largura,$altura,$bg,$wmode,$title)
{
	document.writeln('<object type="application/x-shockwave-flash" data="' + $arquivo + '" width="' + $largura + '" height="' + $altura + '" title="' + $title + '">');
	document.writeln('<param name="movie" value="' + $arquivo + '">');
	document.writeln('<param name="bgcolor" value="#' + $bg + '">');
	document.writeln('<param name="wmode" value="' + $wmode + '">');
	document.writeln('<param name="menu" value="false">');
	document.writeln('<param name="quality" value="high">');
	document.writeln('</object>');
}

