//INICIA O AJAX NO BROWSER DO CLIENTE
function AjaxConnection() {
	var ajax;
	try{ ajax = new XMLHttpRequest();}
	catch(ee){
		try{ ajax = new ActiveXObject("Msxml2.XMLHTTP");}
		catch(e){
			try{ ajax = new ActiveXObject("Microsoft.XMLHTTP");}
			catch(E){
				ajax = false;
			}
		}
	} return ajax;
}

//CRIA A MATRIZ DOS CAMPOS
function getQuery(FORM) {
	var query='', n=0;
	for (var j=0,elm; (elm=FORM.elements[j]); j++) {
		var name = elm.getAttribute('name');
		if (!name || (name.length == 0)) continue;
		var s = elm.value.clean();
		if (s.length == 0) continue;
		if (elm.type == 'hidden') n++;
		if (elm.type == 'radio' && elm.checked == false ) continue;
		if (elm.type == 'checkbox' && elm.checked == false ) continue;
		if (query.length > 0) query += '&';
		query += name + '=' + escape(s);
		n ++;
	}
	query += '&formkey' + '=' + '7bca9e5367c0a7e9a97ab98163366469';
	return (n > 0) ? query : null;
}

//REMOVE CAMPOS BRANCOS E REDUNDANTES
String.prototype.clean = function () { 
	var src=this.toString().replace(/^\s+/,"").replace(/\s+$/,""); 
	var re=/(\S+)(\x20{2,})(\S+)/; 
	while(src.match(re))src=src.replace(re,"$1 $3"); 
	re=/(\S+)(\s+)(\r)(\s+)(\S+)/; 
	while(src.match(re))src=src.replace(re,"$1$3$5"); 
	return src; 
}

//EXECUTA SCRIPT PASSADOS PELO AJAX
function scriptExec(texto)
{
	var ini = 0;
	while (ini != -1)
	{
		ini = texto.indexOf('<script', ini);
		if (ini >= 0){
			ini = texto.indexOf('>', ini) + 1;
			var fim = texto.indexOf('//<;;>', ini);
			codigo = texto.substring(ini,fim);
			novo = document.createElement("script")
			novo.text = codigo;
			document.body.appendChild(novo);
		}
	}
}

//HABILITA E DESABILITA TODOS OS CAMPOS DE UM FORM
function disableAllForm(formName, option){
	objElems = formName.elements;
	for(i=0; i<objElems.length; i++){
		objElems[i].disabled = option;
	}
}

function Conf(Msg) 
{
	if(window.confirm(Msg)){
		return true;
	}else {
		return false;
	}
}

function mOvr(src,clrOver) {
	src.style.cursor = 'pointer';
	src.style.backgroundColor = clrOver;
}
function mOut(src,clrIn) {
	src.style.cursor = 'pointer';
	src.style.backgroundColor = clrIn;
}

function PopUp(Url,Height,Width, Scrool){
	window.open(Url,"popup","Width="+Width+", Height="+Height+" scrollbars="+Scrool);
	return false;
}

//Envia o formulário
function SendForm(Msg,BtnName,Form){
	var Buttom = document.getElementById(BtnName);
	var Action = document.getElementById('acao');
	
	Action.value = true;
	Buttom.value = (Msg);
	Buttom.disabled = true;
	document.forms[Form].submit();
}