// JavaScript Document
function fnOpenFlash(swf, id, arg){
	var width = ob(id).style.width.substr(0, ob(id).style.width.length-2);
	var height = ob(id).style.height.substr(0, ob(id).style.height.length-2);
	var so = new SWFObject("swf/"+swf+".swf", id+"_swf", width, height, "8", "#FFF", "transparent");
	so.addParam("WMODE", "transparent");
	if(arg) so.addVariable("arg", arg);
	so.write(id);
}

function fnTextoFlash(arquivo, div){
	var id = "";
	var num = arquivo.length;
	for(var i=0; i<num; i++){
		letra = String(arquivo).substr(i, 1);
		if("."==letra) break;
		id += letra;
	}
	
	if(!div) div = id;
	var texto = ob(div).innerHTML;
	ob(div).innerHTML = "";
	
	var width = ob(div).style.width.substr(0, ob(div).style.width.length-2);
	var height = ob(div).style.height.substr(0, ob(div).style.height.length-2);
	
	var so = new SWFObject("swf/"+arquivo, id, width, height, "8", "#FFF", "transparent");
	so.addParam("WMODE", "transparent");
	so.addVariable("txt", texto);
	so.write(div);
}

// Filtro Numérico /////////////////////////////////////////////////////////////////////
function fnNumbersOnly(obj, retorno, isFloat){
	var teor = "";
	if(obj.value) teor = obj.value;
	if(obj.innerHTML) teor = obj.innerHTML;	
	
	
	var retifica = "";
	var flag=false;
	var array = null;
	if(!isFloat){
		array = Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
	} else {
		array = Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ",");
	}
	for(var i=0; i<teor.length; i++){
		flag=false;
		letra = teor.substr(i, 1);
		for(j=0; j<array.length; j++){
			if(String(letra)!=String(array[j])){
				flag=true;
			} else {
				retifica+=String(letra);
			}
		}
	}
	if(flag){
		if(!retorno){
			obj.value = retifica;
		} else {
			return retifica;
		}
	}
}

// Comunicação Ajax ///////////////////////////////////////////////////////

function fnConexao(args, file, retorno, tipo){
	var req = null;
	
	if (window.XMLHttpRequest){
	
		req = new XMLHttpRequest();
		if (req.overrideMimeType){
			req.overrideMimeType('text/xml');
		}
	} 
	
	else if (window.ActiveXObject){
	
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e){
		
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	
	req.onreadystatechange = function() { 
	
		if(req.readyState == 4)	{
			
			if(req.status == 200) {
				
				if(tipo){
					tipo=tipo.toLowerCase();
					var doc = null;
					if("text"==tipo){
						doc = req.responseText;
					} else if("xml"==tipo) {
						doc = req.responseXML;
						//doc2 = req.responseText;
						//alert(doc2);
						//alert(doc.childNodes[0].firstChild.data);
					}
					//alert(retorno);
					if(retorno){
						retorno = retorno + "(doc)";
						eval(retorno);
					}
				}				
			}
		}
	};
	
	req.open("POST", file, true);
	req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	req.send(args);
}

//Validando Emails
function fnValidaEmail(emailad){
	var exclude=/[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
	var check=/@[\w\-]+\./;
	var checkend=/\.[a-zA-Z]{2,3}$/;
	if(((emailad.search(exclude) != -1)||(emailad.search(check)) == -1)||(emailad.search(checkend) == -1)){
		return false;
	}
	else {
		return true;
	}
}

//elimina espaços em branco no inicio e no final da string
function trimAll(sString){
	var sString = new String(sString);
	while (sString.substring(0,1) == " "){
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == " "){
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function fnFocus(id){
	ob(id).focus();	
}

function ob(id){
	return document.getElementById(id);
}

function include(arquivo){
	document.write("<script language=\"JavaScript\" src='"+arquivo+"'></script>"); 
}

// Comunicação Ajax ///////////////////////////////////////////////////////
function fnConexao(args, file, retorno, tipo){
	var req = null;
	
	if (window.XMLHttpRequest){
	
		req = new XMLHttpRequest();
		if (req.overrideMimeType){
			req.overrideMimeType('text/xml');
		}
	} 
	
	else if (window.ActiveXObject){
	
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e){
		
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	
	req.onreadystatechange = function() { 
	
		if(req.readyState == 4)	{
			if(req.status == 200) {
				
				if(tipo){
					tipo=tipo.toLowerCase();
					var doc = null;
					if("text"==tipo){
						doc = req.responseText;
					} else if("xml"==tipo) {
						doc = req.responseXML;
						//doc2 = req.responseText;
						//alert(doc2);
						//alert(doc.childNodes[0].firstChild.data);
					}
					//alert(retorno);
					if(retorno){
						retorno = retorno + "(doc)";
						eval(retorno);
					}
				}				
			}
		}
	};
	
	req.open("POST", file, true);
	req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	req.send(args);
}