//========================================================
//	Função para checar se o CPF é válido
//========================================================
function checaCPF(intCpf)
{
	var  aux =  String(intCpf).replace(/\D/g, "");
	
	if (aux.length != 11 || aux == "00000000000" || aux == "11111111111" ||
		aux == "22222222222" ||	aux == "33333333333" || aux == "44444444444" ||
		aux == "55555555555" || aux == "66666666666" || aux == "77777777777" ||
		aux == "88888888888" || aux == "99999999999")
	{return false;}
	
	soma = 0;
	for (i=0; i < 9; i ++) soma += parseInt(aux.charAt(i)) * (10 - i);
	
	resto = 11 - (soma % 11);
	
	if (resto == 10 || resto == 11) resto = 0;
	
	if (resto != parseInt(aux.charAt(9))){return false;}
	
	soma = 0;
	for (i = 0; i < 10; i ++) soma += parseInt(aux.charAt(i)) * (11 - i);
	
	resto = 11 - (soma % 11);
	
	if (resto == 10 || resto == 11) resto = 0;
	
	if (resto != parseInt(aux.charAt(10))){return false;}
	
	return true;
}

// Função para checar se o CNPJ é válido
function checaCNPJ(intCnpj)
{
	var aux = String(intCnpj).replace(/\D/g, "");
	var a = new Array();
    var b = new Number;
    var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
	
	if(aux.length != 14){return false;}
	
    for (i=0; i<12; i++)
	{
		a[i] = aux.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 ((aux.charAt(12) != a[12]) || (aux.charAt(13) != a[13])){return false;}
	
	return true;
}

isDate = function(y, m, d){
        if(typeof y == "string" && m instanceof RegExp && d){
            if(!m.test(y)) return 1;
            y = RegExp["$" + d.y], m = RegExp["$" + d.m], d = RegExp["$" + d.d];
        }
        d = Math.abs(d) || 0, m = Math.abs(m) || 0, y = Math.abs(y) || 0;
        return arguments.length != 3 ? 1 : d < 1 || d > 31 ? 2 : m < 1 || m > 12 ? 3 : /4|6|9|11/.test(m) && d == 31 ? 4
        : m == 2 && (d > ((y = !(y % 4) && (y % 1e2) || !(y % 4e2)) ? 29 : 28)) ? 5 + !!y : 0;
};

getDateMsg = function(x){
    return x == 0 ? "Data válida"
    : x == 1 ? "Formato de data inválido"
    : x == 2 ? "Dia inválido"
    : x == 3 ? "Mês inválido"
    : x == 4 ? "Nos meses de abril, junho, setembro e novembro não existe o dia 31"
    : x == 5 ? "Fevereiro só tem 28 dias"
    : x == 6 ? "Em anos bissextos, fevereiro tem 29 dias" : "=]";
};

textArea = function(p)
{
	var parent		= (isString(p)) ? document.getElementById(p) : null;
    if(isNull(parent)){document.write("Container inválido!");return;}
    var id			= "";
    var name		= "";
    var lbl			= null;
    var label		= "";
    var src			= "";
    var size		= "";
    var maxlength	= "";
    var value		= "";
    var classe		= "";
    var title		= "";
    var tabindex	= "";
    var cols		= "";
	var rows		= "";
	var numLimit	= 0;
	var labelChars	= false;
	var obj			= this;
    
    var input = document.createElement("textarea");
    
    this.setId            = function(x){id = (isString(x)) ? x : "";};
    this.setName        = function(x){name = (isString(x)) ? x : "";};
    this.setLabel        = function(x)
    {
        label = (isString(x)) ? x : "";
        var    textNode;    
        if(label != "")
        {
            textNode = document.createTextNode(label);
            lbl = document.createElement("label");
            if(id != ""){lbl.setAttribute("for",id);}
            lbl.appendChild(textNode);
        }
    };
    this.setSrc			= function(x){src = (isString(x)) ? x : "";};
    this.setCols		= function(x){if(isString(x) || isNumber(x))input.setAttribute("cols",x);};
    this.setRows		= function(x){if(isString(x) || isNumber(x))input.setAttribute("rows",x);};
    this.setValue		= function(x){value = (isString(x) || isNumber(x)) ? unescape(x) : "";};
    this.setClass		= function(x){if(isString(x))input.className = x;};
    this.setTitle		= function(x){title = (isString(x)) ? x : "";};
    this.setTabIndex	= function(x){tabindex = (isNumber(x)) ? x : "";};
    this.setDisabled	= function(x){if(isBoolean(x) && x){input.disabled = x;}};
    this.setReadonly	= function(x){if(isBoolean(x) && x){input.readOnly = x}};
    this.setEvent		= function(e,f)
	{
		if(isFunction(f))
		{
			if(input.addEventListener)
        		input.addEventListener(e, f, true)
            if(input.attachEvent)
            	input.attachEvent("on"+e, f)
		}
	};
	
	this.setLimit = function(n,l)
	{
		labelChars = isBoolean(l)? l : false;
		 
		if(isNumber(n))
		{
			numLimit = n;
			
			var limit = function()
			{
				if(input.value.length > n)
					input.value = input.value.substring(0,n);
				if(labelChars)
					document.getElementById("caracteres-"+id).innerHTML = n - input.value.length;
			};
			
			obj.setEvent("keypress",limit);
			obj.setEvent("keyup",limit);
			obj.setEvent("blur",limit);
		}
	};
        
    this.draw = function()
    {
        input.setAttribute("id",id);
        input.setAttribute("name",name);
        input.setAttribute("tabindex",tabindex);
        if(value != ""){input.value = value;}
        input.setAttribute("title",title);
		if(!isNull(lbl)){parent.appendChild(lbl);}
        parent.appendChild(input);
        if(labelChars)
		{
			var chars = jQuery.create("span",{"id":"caracteres-"+id,"class":"caracteres"},[String(numLimit)]);
			parent.appendChild(chars);
			jQuery(chars).css({"text-align": "right","display": "block","font-weight": "bold"});
		}
    };
};

//=================================================================================
//	Função para retornar os valores de um formulário em uma string para uma requisição
//=================================================================================
montaUrl = function(x)
{ 
	var queryString = new Array();
	var form = document.getElementById(x);
	
	if(!isNull(form))
	{
		for (var i = 0; i < form.elements.length; i++)
		{ 
			if(form.elements[i].type == "text"	   || form.elements[i].type == "password" || 
			   form.elements[i].type == "textarea" || form.elements[i].type == "hidden")
			{
				if(form.elements[i].value.length > 0)
					queryString[queryString.length] = form.elements[i].name + "=" + escape(form.elements[i].value);
			} 
			else if (form.elements[i].type == "checkbox" || form.elements[i].type == "radio")
			{
				if(form.elements[i].checked == true)
					queryString[queryString.length] = form.elements[i].name + "=" + escape(form.elements[i].value); 
			}
			else if(form.elements[i].type == "select-one")
			{
				queryString[queryString.length] = form.elements[i].name + "=" + escape(form.elements[i].options[form.elements[i].selectedIndex].value);
			}
			else if(form.elements[i].type == "select-multiple")
			{
				selected = new Array();
					for (var j = 0; j < form.elements[i].options.length; j++)
						if (form.elements[i].options[j].selected)
							selected.push(escape(form.elements[i].options[j].value));
					  
				if(selected.length > 0)
					queryString[queryString.length] = form.elements[i].name + "=" + selected;    
			} 
		}
		return queryString.join("&");
	}
	else
	{
		alert("Formulário desconhecido!");
		return;
	}
};

var imgSetaMenu = new Array(new Image(),new Image());
imgSetaMenu[0].src = "imagens/seta_up.gif";
imgSetaMenu[1].src = "imagens/seta_down.gif";

slideMenu = function()
{
		if(jQuery("#setaMenu").attr("src").indexOf("up") > -1) jQuery("#setaMenu").attr("src",imgSetaMenu[1].src);
		else jQuery("#setaMenu").attr("src",imgSetaMenu[0].src);
		
		jQuery("dd","div.menuCabecalho").slideToggle();
};

slideSubMenu = function(x)
{
		jQuery("ul.sub:not(#"+x+")").slideUp();
		jQuery("#"+x).slideToggle();
};

corpo = function()
{
	jQuery("#fundoListas").css({"height":(jQuery("#corpo").height()).toString()+"px"});
};

rodape = function()
{		
		var de = document.documentElement;		
		var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.offsetHeight;
		if(h > document.body.offsetHeight)
			jQuery("#rodape").css({"height":(jQuery("#rodape").height()+(h - jQuery("#tudo").height())).toString()+"px"});
		else if(h < document.body.offsetHeight)
		{
			jQuery("#rodape").css({"height":"120px"});
			if(h > document.body.offsetHeight)
				jQuery("#rodape").css({"height":(jQuery("#rodape").height()+(h - jQuery("#tudo").height())).toString()+"px"});
		}
};

menuTopo = function(x)
{
	document.write(
	'<object type="application/x-shockwave-flash" data="swf/'+x+'/menuTopo.swf" id="menuTopo" width="470" height="72">'+
	'	<param name="movie" value="swf/'+x+'/menuTopo.swf" />'+
	'	<param name="allowScriptAcess" value="sameDomain" />'+
	'	<param name="quality" value="best" />'+
	'	<param name="bgcolor" value="#FFFFFF" />'+
	'	<param name="scale" value="noScale" />'+
	'	<param name="salign" value="TL" />'+
	'	<param name="wmode" value="transparent" />'+
	'	<param name="FlashVars" value="playerMode=embedded" />'+
	'</object>'			   
	);
}

homeFlash = function()
{
	document.write(
	'<object type="application/x-shockwave-flash" data="animacao.swf" id="menuTopo" width="774" height="325">'+
	'	<param name="movie" value="animacao.swf" />'+
	'	<param name="allowScriptAcess" value="sameDomain" />'+
	'	<param name="quality" value="best" />'+
	'	<param name="bgcolor" value="#FFFFFF" />'+
	'	<param name="scale" value="noScale" />'+
	'	<param name="salign" value="TL" />'+
	'	<param name="wmode" value="transparent" />'+
	'	<param name="FlashVars" value="playerMode=embedded" />'+
	'</object>'			   
	);
}

jQuery(document).ready(function()
{
	window.setTimeout('jQuery("p.mensagemErro").hide()',10000);
	
	jQuery("a").click(function()
	{
		this.blur();
	});
	
	
	
	jQuery("div.sombra").css({"filter": "alpha(opacity=15)",
							   "-moz-opacity": "0.15",
							   "opacity": "0.15"});
	
	rodape();

	jQuery(window).resize(function()
	{
		rodape();
	});
	
	setTimeout("jQuery('p.mensagemErro').hide()",5000);
});

if(jQuery.browser.msie)document.execCommand("BackgroundImageCache", false, true);