// When the page loads:
var valida = '';

function ativaObj() {
    if (document.getElementsByTagName) {
        // Get all the tags of type object in the page.
        var objs = document.getElementsByTagName("object");
        for (i=0; i<objs.length; i++) {
            // Get the HTML content of each object tag
            // and replace it with itself.
            objs[i].outerHTML = objs[i].outerHTML;
        }
    }
}

window.onload = ativaObj;

function desativaObj() {
// When the page unloads:
    if (document.getElementsByTagName) {
        //Get all the tags of type object in the page.
        var objs = document.getElementsByTagName("object");
        for (i=0; i<objs.length; i++) {
            // Clear out the HTML content of each object tag
            // to prevent an IE memory leak issue.
            objs[i].outerHTML = "";
        }
    }
}

window.onunload = desativaObj;

function OpenWin(URL, Nome, W, H, T, L, resizable)
{
    if (T == undefined)
        T = '200';
    
    if (L == undefined)
        L = '130';
    
    if (resizable == undefined)
        resizable = 'no';
	
    var Janela = window.open(URL, Nome,'alwaysLowered=yes,alwaysRaised=no,dependent=yes,directories=no,hotkeys=no,location=no,menubar=no,personalbar=no,resizable=' + resizable + ',scrollbars=yes,status=no,titlebar=no,toolbar=no,width=' + W + ',height=' + H + ',z-lock=yes,left=' + L + ',top=' + T)
}

function getPosicaoElemento(elemID) {
    var offsetTrail = document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
    
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    
    if (navigator.userAgent.indexOf("Mac") != -1 && 
        typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return {left:offsetLeft, top:offsetTop};
}

function getPosicaoElementoParent(elemID) {
    var offsetTrail = parent.document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
    
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    
    if (navigator.userAgent.indexOf("Mac") != -1 && typeof parent.document.body.leftMargin != "undefined") {
        offsetLeft += parent.document.body.leftMargin;
        offsetTop += parent.document.body.topMargin;
    }
    return {left:offsetLeft, top:offsetTop};
}

function checkdate(objName)
{	var datefield = objName;
	if (datefield.value == "")
		return true;

	if (chkdate(objName) == false) 
	{	//datefield.select();
		alert("Data inválida");
		datefield.value = ""
		datefield.focus();
		return false;
	}
	else
	{	//alert("Data Válida");
		return true;
   	}
}

function chkdate(objName) 
{	var strDate;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	var datefield = objName;
	strDate = datefield.value;
	//concatenar os campos e verificar quantidades
	// considerando um campo só com quantidade certa.
	strDay = strDate.substr(0, 2);
	strMonth = strDate.substr(3, 2);
	strYear = strDate.substr(6, 4);
	strBarra1 = strDate.substr(2, 1);
	strBarra2 = strDate.substr(5, 1);
   	
	// Mostra os valores das variaveis
	//alert("strDay: " + strDay + "\nstrBarra1: " + strBarra1 + "\nstrMonth: " + strMonth + "\nstrBarra2: " + strBarra2 + "\nstrYear: " + strYear);

	intday = parseInt(strDay, 10);
	intday1 = parseInt(strDate.substr(0, 1), 10);
	intday2 = parseInt(strDate.substr(1, 1), 10);
	if ((isNaN(intday1)) || (isNaN(intday2)))
	{	//alert("Erro no dia");
		return false;
	}
	intMonth = parseInt(strMonth, 10);
	intMonth1 = parseInt(strDate.substr(3, 1), 10);
	intMonth2 = parseInt(strDate.substr(4, 1), 10);
	if ((isNaN(intMonth1)) || (isNaN(intMonth2)))
	{	//alert("Erro no mês");
		return false;
	}
	
	intYear = parseInt(strYear, 10);
	intYear1 = parseInt(strDate.substr(6, 1), 10);
	intYear2 = parseInt(strDate.substr(7, 1), 10);
	intYear3 = parseInt(strDate.substr(8, 1), 10);
	intYear4 = parseInt(strDate.substr(9, 1), 10);
	if ((isNaN(intYear1)) || (isNaN(intYear2)) || (isNaN(intYear3)) || (isNaN(intYear4)) || (intYear <= 1900))
	{	//alert("Erro no ano");
		return false;
	}
	
	if ((strBarra1 != "/") ||(strBarra2 != "/"))
	{	//alert("Erro na Barra / ");
		return false;
	}
	
	if (intMonth > 12 || intMonth < 1)
	{	return false;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1))
	{	return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1))
	{	return false;
	}
	if (intMonth == 2)
	{	if (intday < 1)
		{	return false;
		}
		if (LeapYear(intYear) == true)
		{	if (intday > 29)
			{	return false;
			}
		}
		else
		{	if (intday > 28)
			{	return false;
			}
		}
	}
	return true;
}

function LeapYear(intYear)
{	if (intYear % 100 == 0)
	{	if (intYear % 400 == 0)
		{	return true;
		}
	}
	else
	{	if ((intYear % 4) == 0)
		{ 	return true;
		}
	}
	return false;
}

function Formata_Data(objeto,teclapres) {
	var tecla = teclapres.keyCode;
	vr = objeto.value;
	s="";
	for (x=0; x < vr.length; x++)
	 	if (vr.charCodeAt(x) >= 48 && vr.charCodeAt(x)<=57)
			s= s + vr.charAt(x);
	vr = s;
	tam = vr.length + 1;
	if ( tecla != 9 && tecla != 8 && tecla != 46 && tecla != 37 && tecla != 39 && tecla != 35 && tecla != 36){
		if ( tam < 2 )
			objeto.value = vr.substr(0,2);
		if ( tam > 2 && tam < 4 )
			objeto.value = vr.substr(0,2) + '/' + vr.substr(2,2);
		if ( tam > 4 )
			objeto.value = vr.substr(0,2) + '/' + vr.substr(2,2) + '/' + vr.substr(4,4); 
	}
	if (tam > 5 && (tecla == 8 || tecla == 46)) 
			objeto.value = vr.substr(0,2) + '/' + vr.substr(2,2) + '/' + vr.substr(4,4); 
	if ((tam > 2 && tam < 6) && (tecla == 8 || tecla == 46)) 
			objeto.value = vr.substr(0,2) + '/' + vr.substr(2,2);
	if (tam < 4 && (tecla == 8 || tecla == 46)) 
			objeto.value = vr.substr(0,2);
}

function Valida_Email(objeto) {
	if ((objeto.value.indexOf("@")==-1 || objeto.value.indexOf(".")==-1) && objeto.value!="")
		{ objeto.select();
		  alert("e-Mail inválido");
		  objeto.value = "";
          objeto.focus();
		  return false;
		}
	return true;
}

function Valida_Telefone(objeto) {
	str=objeto.value;
	tel="";

	if (str == "")
		return true
		
	if ((str.length<12 || str.length>13 || str.substring(0,1)!="(" || str.substring(3,4)!=")") && (str.indexOf("-")!=7 || str.indexOf("-")!=8)) {
		objeto.select();
		alert("Telefone inválido. Formato: (12)3456-7890");
    	objeto.value = "";
		objeto.focus();
		return false;
	}
	for (var i = 0; i < str.length; i++)
		if (str.substring(i,i+1) >= "0" && str.substring(i,i+1) <= "9")
		  	tel+=str.substring(i,i+1);	
	if (isNaN(tel)) {
		objeto.select();
		alert("Telefone inválido. Formato: (12)3456-7890");
		objeto.value = "";
		objeto.focus();
		return false;		
	}
	return true;
}

function Formata_Telefone(objeto,teclapres) {
	var tecla = teclapres.keyCode;
	vr = objeto.value;	
	s="" 
	for (x=0; x < vr.length; x++)
	 	if (vr.charCodeAt(x) >= 48 && vr.charCodeAt(x)<=57)
			s= s + vr.charAt(x)
	vr = s
	tam = vr.length + 1;
	if ( tecla != 9 && tecla != 8 && tecla != 46 && tecla != 37 && tecla != 39 && tecla != 35 && tecla != 36) {
	  if ( tam == 1 )
		objeto.value = vr.substr(0,1);
	  if ( tam > 1 && tam < 3 )
		objeto.value = '(' + vr;
	  if ( tam >=3 && tam < 7 )
		objeto.value = '(' + vr.substr(0,2) +  ')' + vr.substr(2,4);
	  if ( tam > 6 && tam < 11 )
		objeto.value = '(' + vr.substr(0,2) +  ')' + vr.substr(2,3) + '-' + vr.substr(5,4);
	  if ( tam > 10 )
		objeto.value = '(' + vr.substr(0,2) +  ')' + vr.substr(2,4) + '-' + vr.substr(6,4);
	}
	if (tam > 10 && (tecla == 8 || tecla == 46))
		objeto.value = '(' + vr.substr(0,2) +  ')' + vr.substr(2,4) + '-' + vr.substr(6,4);	  
	if ((tam > 6 && tam < 11) && (tecla == 8 || tecla == 46))
		objeto.value = '(' + vr.substr(0,2) +  ')' + vr.substr(2,3) + '-' + vr.substr(5,4);
	if ((tam > 3 && tam < 7) && (tecla == 8 || tecla == 46))
		objeto.value = '(' + vr.substr(0,2) +  ')' + vr.substr(2,4);
	if ((tam > 1 && tam < 3) && (tecla == 8 || tecla == 46))
		objeto.value = '(' + vr;
	if (tam == 1 && (tecla == 8 || tecla == 46))
		objeto.value = vr;
}

// url_encode version 1.0 
function url_encode(str) { 
    var hex_chars = "0123456789ABCDEF"; 
    var noEncode = /^([a-zA-Z0-9\_\-\.])$/; 
    var n, strCode, hex1, hex2, strEncode = ""; 

    for(n = 0; n < str.length; n++) { 
        if (noEncode.test(str.charAt(n))) { 
            strEncode += str.charAt(n); 
        } else { 
            strCode = str.charCodeAt(n); 
            hex1 = hex_chars.charAt(Math.floor(strCode / 16)); 
            hex2 = hex_chars.charAt(strCode % 16); 
            strEncode += "%" + (hex1 + hex2); 
        } 
    } 
    return strEncode; 
} 

// url_decode version 1.0 
function url_decode(str) { 
    var n, strCode, strDecode = ""; 

    for (n = 0; n < str.length; n++) { 
        if (str.charAt(n) == "%") { 
            strCode = str.charAt(n + 1) + str.charAt(n + 2); 
            strDecode += String.fromCharCode(parseInt(strCode, 16)); 
            n += 2; 
        } else { 
            strDecode += str.charAt(n); 
        } 
    } 
    return strDecode; 
}  

function queryString(parameter) {
    var loc = location.search.substring(1, location.search.length);
    var param_value = false;
    var params = loc.split("&");
    for (i=0; i<params.length;i++) {
        param_name = params[i].substring(0,params[i].indexOf('='));
        if (param_name == parameter) {
            param_value = params[i].substring(params[i].indexOf('=')+1)
        }
    }
      
    if (param_value) {
        return param_value;
    } else {
        return '';
    }
} 