/******************************************************************************************

	Trata Formularios GERAIS

*******************************************************************************************/

var CharEmail = new RegExp("[A-Za-z0-9_.-]+@([A-Za-z0-9_]+\.)+[A-Za-z]{2,4}");

function Del(Word) {
	a = Word.indexOf("<");
	b = Word.indexOf(">");
	len = Word.length;
	c = Word.substring(0, a);
	if(b == -1)
	b = a;
	d = Word.substring((b + 1), len);
	Word = c + d;
	tagCheck = Word.indexOf("<");
	
	if(tagCheck != -1){
		Word = Del(Word);
		return false;
	}else{
		return true;
	}
}
var isIE = false;
var req;
var msg;
function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return strReturn;
}
function loadPHP( url, div )
{	
	Div = document.getElementById(div);
		
    if ( window.XMLHttpRequest )
	{
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open( "GET", url, true );
        req.send( null );
    }
	else if ( window.ActiveXObject )
	{
        isIE = true;
        req = new ActiveXObject( "Microsoft.XMLHTTP" );
        if ( req )
		{
            req.onreadystatechange = processReqChange;
            req.open( "GET", url, true );
            req.send(url);
        }
    }

}
function processReqChange()
{
	if ( req.readyState == 1 )
	{
		Div.innerHTML	=	"Carregando...";
	}
	if ( req.readyState == 4 )
	{
		if ( req.status == 200 )
		{
			var answer = req.responseText/*.replace(/\+/g," ")*/;
			Div.innerHTML = answer;
		}
		else
		{
			alert( "Ocorreu um problema na comunicação com o PHP:\n" + req.statusText );
		}
	}
}
/******************************************************************************************

	Minhas Configurações

*******************************************************************************************/
function CallContent( url, div )
{
	try
	{
		loadPHP( url, div );
	}
	catch ( e )
	{
		alert( "Não foi possível recuperar o conteúdo solicitado:\n" + msg );
		return;
	}
}
/******************************************************************************************

	MTESTE DE POST

*******************************************************************************************/
// 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 loadSamePHP( url, div )
{
	Div = document.getElementById(div);
	
	var form = document.getElementById('Form');

	var form_string = get(form);

	//alert(form_string);
	
    if ( window.XMLHttpRequest )
	{
		req = new XMLHttpRequest();
		req.onreadystatechange = processSameChange;
		req.open( "POST", url, false );
		req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		//req.setRequestHeader("Referer",url);
		req.send(form_string);
    }
	else if ( window.ActiveXObject )
	{
        isIE = true;
        req = new ActiveXObject( "Microsoft.XMLHTTP" );
        if ( req )
		{
			req.onreadystatechange = processSameChange;
			req.open( "POST", url, false );
			req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			//req.setRequestHeader("User-Agent", "Mozilla/4.0(compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)");
			//req.setRequestHeader("Referer",url);
         	req.send(form_string);
        }
    }
}

function processSameChange()
{
	if ( req.readyState == 1 )
	{
		Div.innerHTML	=	"Carregando...";
	}
	if ( req.readyState == 4 )
	{
		if ( req.status == 200 )
		{
			var answer = req.responseText;
			Div.innerHTML = answer;
		}
		else
		{
			alert( "Ocorreu um problema na comunicação com o PHP:\n" + req.statusText );
		}
	}
}

function ProcessUrl( url, div )
{
	try
	{
		loadSamePHP( url, div );
	}
	catch ( e )
	{
		alert( "Não foi possível recuperar o conteúdo solicitado:\n" + msg );
		return;
	}
}
function get(obj) {

	var FormInput 		= document.forms[document.getElementById("Form").name].getElementsByTagName("input");

	var FormSelect		= document.forms[document.getElementById("Form").name].getElementsByTagName("select");

	var FormTextArea	= document.forms[document.getElementById("Form").name].getElementsByTagName("textarea");



	var getstr = "";

	for (i=0; i<FormInput.length; i++) {

		if (FormInput[i].type == "text") {

		   getstr += FormInput[i].name + "=" + url_encode(FormInput[i].value) + "&";

		}

	}

	for (i=0; i<FormInput.length; i++) {

		if (FormInput[i].type == "hidden") {

		   getstr += FormInput[i].name + "=" + url_encode(FormInput[i].value) + "&";

		}

	}

	for (i=0; i<FormInput.length; i++) {

		if (FormInput[i].type == "password") {

		   getstr += FormInput[i].name + "=" + url_encode(FormInput[i].value) + "&";

		}

	}

	for (i=0; i<FormInput.length; i++) {

		if (FormInput[i].type == "file") {

		   getstr += FormInput[i].name + "=" + url_encode(FormInput[i].value) + "&";

		}

	}

	for (i=0; i<FormInput.length; i++) {

		if (FormInput[i].type == "checkbox") {

		   if (FormInput[i].checked) {

			  getstr += FormInput[i].name + "=" + FormInput[i].value + "&";

		   } else {

			  getstr += FormInput[i].name + "=&";

		   }

		}

	}

	for (i=0; i<FormInput.length; i++) {

		if (FormInput[i].type == "radio") {

		   if (FormInput[i].checked) {

			  getstr += FormInput[i].name + "=" + FormInput[i].value + "&";

		   }

		}

	}

	for (i=0; i<FormTextArea.length; i++) {

		if (FormTextArea[i]) {

			   getstr += FormTextArea[i].name + "=" + url_encode(FormTextArea[i].value) + "&";

		}

	}

	for (i=0; i<FormSelect.length; i++) {

		if (FormSelect[i]) {

			var sel = FormSelect[i];

			getstr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";

		}

	}

	

	return getstr;

}
/******************************************************************************************

	Janela POPUP

*******************************************************************************************/
function janelaPopUp(url, W, H){
	window.open(url,'Titulo','width='+ W +',height='+ H +',scrollbars=no,toolbar=no,location=no,status=no,resizable=yes,screenX=120,screenY=100');
}
function resizeOuterTo(w,h) {
	if (parseInt(navigator.appVersion)>3) {
	 top.resizeTo(w+10,h+60);
	}
}

function init()
{
  resizeOuterTo(document.images['Imagem'].width, document.images['Imagem'].height);
}
function fecharJanela(){
	self.close();
}
/******************************************************************************************

	Trocar imagem no Mouse do Menu

*******************************************************************************************/
sfHover = function() {

	var sfEls = document.getElementById("menu").getElementsByTagName("div");

	for (var i=0; i<sfEls.length; ++i) {

		sfEls[i].onmouseover=function() {

			this.className+=" sfhover";

		}

		sfEls[i].onmouseout=function() {

			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");

		}

	}

}

/*if (window.attachEvent) window.attachEvent("onload", sfHover);*/

/******************************************************************************************

	Login

*******************************************************************************************/
function trataLogin()
{
	if ( document.Login.pLoginStr.value == "" ) {
		alert ("Preencha o campo Login");
		document.Login.pLoginStr.focus ();
		return false;
	}else if( document.Login.pSenhaStr.value == "" ) {
		alert ("Preencha o campo Senha");
		document.Login.pSenhaStr.focus ();
		return false;
	}else{
		document.getElementById("Login").submit ();
		return true;
	}
}
function submitLogin(evt){
var charCode = (evt.which) ? evt.which : event.keyCode
	if(charCode == "13"){
		trataLogin();
	}
}
/******************************************************************************************

	On Click link

*******************************************************************************************/
function goUrl(url)
{
	window.location=url;
}
/****************************************************************
* Author:	Alistair Lattimore
* Website:	http://www.lattimore.id.au/
* Contact:	http://www.lattimore.id.au/contact/
*			Errors, suggestions or comments
* Date:		30 June 2005
* Version:	1.0
* Purpose:	Emulate the disabled attributte for the <option> 
*			element in Internet Explorer.
* Use:		You are free to use this script in non-commercial
*			applications. You are however required to leave
*			this comment at the top of this file.
*
*			I'd love an email if you find a use for it on your 
*			site, though not required.
****************************************************************/

function DisableSelect() {
	if (document.getElementsByTagName) {
		var s = document.getElementsByTagName("select");

		if (s.length > 0) {
			window.select_current = new Array();

			for (var i=0, select; select = s[i]; i++) {
				select.onfocus = function(){ window.select_current[this.id] = this.selectedIndex; }
				select.onchange = function(){ restore(this); }
				emulate(select);
			}
		}
	}
}

function restore(e) {
	if (e.options[e.selectedIndex].disabled) {
		e.selectedIndex = window.select_current[e.id];
	}
}

function emulate(e) {
	for (var i=0, option; option = e.options[i]; i++) {
		if (option.disabled) {
			option.style.color = "graytext";
		}
		else {
			option.style.color = "menutext";
		}
	}
}
