 var ALERT_TITLE = "Atenção";
var ALERT_BUTTON_TEXT = "Ok";

objFocus = new Object()
if(document.getElementById) {
	window.alert = function(txt) {
		createCustomAlert(txt);
	}
}

function createCustomAlert(txt) {
	
	d = document;

	if(d.getElementById("modalContainer")) return;

	mObjAux = document.createElement("div")
	
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	
	mObjAux.style.width = pageWidth + "px";
	mObjAux.style.height = pageHeight + "px"
	
	
	
	mObjAux.className = "fundoOpaco"
	mObjAux.id = "fundoOpaco"
	d.getElementsByTagName("body")[0].appendChild(mObjAux);
	
	
	
	mObj = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
	mObj.id = "modalContainer";
	mObj.style.height = d.documentElement.scrollHeight + "px";
	
	
	alertObj = mObj.appendChild(d.createElement("div"));
	alertObj.id = "alertBox";
	if(d.all && !window.opera) alertObj.style.top = document.documentElement.scrollTop + "px";
	alertObj.style.left = (d.documentElement.scrollWidth - alertObj.offsetWidth)/2 + "px";
	alertObj.style.visiblity="visible";

	h1 = alertObj.appendChild(d.createElement("h1"));
	h1.appendChild(d.createTextNode(ALERT_TITLE));

	msg = alertObj.appendChild(d.createElement("p"));
	imgAlert = alertObj.appendChild(d.createElement("img"));
	imgAlert.src = "imagens/icon_alert.jpg"
	imgAlert.id = "imgAlert"
	msg.appendChild(d.createTextNode(txt));
	msg.innerHTML = txt;

	btn = alertObj.appendChild(d.createElement("a"));
	btn.id = "closeBtn";
	btn.appendChild(d.createTextNode(ALERT_BUTTON_TEXT));
	btn.href = "#";
	btn.focus();
	btn.onclick = function() { removeCustomAlert();return false; }

	alertObj.style.display = "block";
	
	
}

function removeCustomAlert() {
	document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));
	document.getElementsByTagName("body")[0].removeChild(document.getElementById("fundoOpaco"));
	if(objFocus && objFocus != "")
	{
		objFocus.focus();	
	}
}

 
 
 
 function validaCPF(cpf) {
		
		 erro = new String;
		 if (cpf.length < 11) erro += "Sao necessarios 11 digitos para verificação do CPF! \n\n";
		 var nonNumbers = /\D/;
		 if (nonNumbers.test(cpf)) erro += "A verificacao de CPF suporta apenas numeros! \n\n";
		 if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
				 erro += "Número de CPF inválido!"
	   }
	   var a = [];
	   var b = new Number;
	   var c = 11;
	   for (i=0; i<11; i++){
			   a[i] = cpf.charAt(i);
			   if (i < 9) b += (a[i] * --c);
	   }
	   if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
	   b = 0;
	   c = 11;
	   for (y=0; y<10; y++) b += (a[y] * c--);
	   if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
	   if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
			   erro +="Digito verificador com problema!";
	   }
	   if (erro.length > 0){
			   //alert(erro);
			   return false;
	   }
	   return true;
}



function validaCNPJ(CNPJ) {
  erro = new String;
  if (CNPJ.length < 18) erro += "E' 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 += "E' necessarios preencher corretamente o numero do CNPJ! \n\n";
  }
  //substituir os caracteres que nao sao 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;
  } else {
 // alert("CNPJ valido!");
  }
  return true;
}

function validaInput(id)
{
	
	if(id != "")
	{
		obj = document.getElementById(id).getElementsByTagName("input");
	}
	else
	{
		obj = document.getElementsByTagName("input");
	}
			
	for (i=0;i<obj.length;i++)
	{
		
		if (obj[i])
		{
			if (obj[i].className == "obrigatorio")
			{
				
				switch(obj[i].type)
				{
					
					case "text":
					
						if(obj[i].value == ""){
							
							alert("Preencha o campo "+obj[i].title)
							objFocus = obj[i]
							return false;
						}	
							
						
						
						if (obj[i].name == "end_eletronico")
						{
							if (validaEmail(obj[i].value)== false)
							{
								objFocus = obj[i]
								alert("E-mail inválido");
								return false;
							}
						}
											
						if (obj[i].name == "telefone")
						{
							tel = obj[i].value;
							tel = tel.replace("-","")
							tel = tel.replace(")","")
							tel = tel.replace("(","")
							tel = tel.replace(" ","")
							if ( (isNumeric(tel)== false) || (tel.length < 8) )
							{
								objFocus = obj[i]
								alert("Telefone inválido");
								return false;
							}
						}						
						if (obj[i].name == "cep")
						{
							cep = obj[i].value;
							cep2 = cep.replace("-","");
							if( (cep.charAt(5) != "-") || (cep.length < 9) || (isNumeric(cep2)== false) )
							{
								objFocus = obj[i]
								alert("CEP inválido");
								return false;
							}
						}							

						
						break;	
				}				
				
			}
			
		}
		
	}
	return true
}
function validaTextArea(id)
{
	
	if(id != "")
	{
		obj = document.getElementById(id).getElementsByTagName("textarea");	
	}
	else
	{
		obj = document.getElementsByTagName("textarea");
	}
	
	for (i=0;i<obj.length;i++)
	{
		
		if (obj[i])
		{
			if (obj[i].className == "obrigatorio")
			{				
				
				if(obj[i].value == "")
				{
					
					objFocus = obj[i]
					alert("Preencha o campo "+obj[i].title)
					return false;
					break;
				}	
		
				
			}
		}
		
	}
	return true;
}
function validaSelect(id)
{
	if(id != "")
	{
		obj = document.getElementById(id).getElementsByTagName("select");	
	}
	else
	{
		obj = document.getElementsByTagName("select");
	}
	
	for (i=0;i<obj.length;i++)
	{
		
		if (obj[i])
		{
			if (obj[i].className == "obrigatorio")
			{				
				
				if(obj[i].value == "")
				{
					
					objFocus = obj[i]
					alert("Selecione o "+obj[i].title)
					return false;
					break;
				}	
		
				
			}
		}
		
	}
	return true;
}

function validaFormGeral(id)
{
	if(id != "")

	{

		//obj = document.getElementById(id).getElementsByTagName("select");	

		obj = $("#"+id + " .obrigatorio")

	}

	else

	{

		//obj = document.getElementsByTagName("select");

		obj = $(".obrigatorio")

	}	

	

	for (i=0;i<obj.length;i++)

	{

		if(obj[i].value == "")

		{

			alert("Preencha o campo "+obj[i].title)

			objFocus = obj[i]
			
			return false

			break;

		}		

	}

	if (validaEmails() == false)

	{

		return false

	}

	if (validaDatas() == false)

	{

		return false

	}

	if (validaValorFloat() == false)

	{

		return false

	}

	return true

}
function validaEmails()

{

	obj = document.getElementsByTagName("input");

	for (i=0;i<obj.length;i++)

	{		

		if (obj[i])

		{

			if( (obj[i].alt == "email") && (obj[i].value != "") )  

			{

				if (validaEmail(obj[i].value) == false)

				{

					alert("E-mail inválido");

					objFocus = obj[i]

					return false

					break;

				}

			}

			

		}

	}

	return true;		

}

function validaValorFloat()
{
	obj = document.getElementsByTagName("input");
	for (i=0;i<obj.length;i++)
	{		
		if (obj[i])
		{
			if( (obj[i].alt == "valor") && (obj[i].value != "") )  
			{
				if (isFloat(obj[i].value) == false)
				{
					objFocus = obj[i]
					alert("Valor inválido");
					return false
					break;
				}
			}
			
		}
	}
	return true;
}

function validaData(data)
{
	var expre =	/^(0?[1-9]|[12]\d|3[01])\/(0?[1-9]|1[0-2])\/(19|20)?\d{2}$/;
	if (expre.test(data) == false)
	{
		return false;
	}
	return true
}
function isNumeric(valor)
{
   	var valor_text;
    var vetor;
    var cont;

    valor_text = valor;
    vetor = "";
    cont = 0;    

    for(var i = 0; i < valor_text.length; i++)
    {
  		if (valor_text.charCodeAt(i) < 43 || valor_text.charCodeAt(i) > 58)
	  	{
		  vetor = vetor + valor_text.charAt(i);
		  cont++;
	 	}
	}
    
    if(cont == 0)
    {
  		return true;    
    }
    else
    {
  		return false;
    }
}
function validaEmail(email)
{
	
	var reTipo = /^[\w!#$%&'*+\/=?^`{|}~-]+(\.[\w!#$%&'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
	return reTipo.test(email);

}
function validaData(data)
{
	var expre =	/^(0?[1-9]|[12]\d|3[01])\/(0?[1-9]|1[0-2])\/(19|20)?\d{2}$/;
	if (expre.test(data) == false)
	{
		return false
	}	
	return true;
}

function validaCont()
{
	if (validaFormGeral() == false)
	{
		return false;
	}
	if (document.getElementById("mensagem").value == "")
	{
		alert("Preencha o campo Mensagem");
		document.getElementById("mensagem").focus();
		return false;
	}
}
function validaDatas()
{
	
	obj = document.getElementsByTagName("input");
	for (i=0;i<obj.length;i++)
	{		
		if (obj[i])
		{
			if( (obj[i].value != "") && (obj[i].alt == "data") )
			{
				if( validaData(obj[i].value) == false)
				{
					alert(obj[i].title + " inválida")
					objFocus = obj[i]
					return false
					break;
				}
			}

		}
	}
	return true;
	
}
function tipo_arq(qtdIni,qtdFin,tipo,extensoes)
{
	//Tipo - S - quando obrigatorio insercao de foto - N -  somente verifica a axtensao do arquivo
	
	cont = 0;
	for(var i = qtdIni; i <= qtdFin; i++)
	{
		foto = document.getElementById("arquivo" + i);
		if(foto)
		{
			if (foto.value != "" )
			{		
				wfoto = foto.value
				indice = wfoto.indexOf(".");
				indice = indice + 1; 
				wfoto = wfoto.substr(indice);
				wfoto = wfoto.toLowerCase();
				
				vetExtensao = extensoes.split("|")
				erro = true
				for(y=0;y<vetExtensao.length;y++)
				{
					if(wfoto == vetExtensao[y])
					{
						erro = false		
					}
				}
				if (erro == true)
				{
					alert(foto.title + " - extensão não permitida");
					objFocus = foto
					return false;
					break;
					
				}
				
				cont = parseInt(cont) + 1;
			}
			
		}
		
	}	
	if (tipo == "S")
	{
		if (cont <= 0 )
		{
			alert("Nenhum arquivo inserido");
			objFocus = document.getElementById("arquivo" + qtdIni);
			return false
		}
	}
	return true
	
}
function validaTrab(id)
{
	if(validaFormGeral(id))
	{
		
		return tipo_arq('1','1','S','doc|docx|rtf|pdf|txt')	
		
	}
	else
	{
		return false	
	}
}

