/*TRIM*/
function Trim(TRIM_VALUE){
	if(TRIM_VALUE.length < 1){
		return"";
	}
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);
	if(TRIM_VALUE==""){
		return "";
	}
	else{
		return TRIM_VALUE;
	}
} //End Function

function RTrim(VALUE){
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	if(v_length < 0){
		return"";
	}
	var iTemp = v_length -1;

	while(iTemp > -1){
		if(VALUE.charAt(iTemp) == w_space){
		}
		else{
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp-1;

	} //End While
	return strTemp;

} //End Function

function LTrim(VALUE){
	var w_space = String.fromCharCode(32);
	if(v_length < 1){
		return"";
	}
	var v_length = VALUE.length;
	var strTemp = "";

	var iTemp = 0;

	while(iTemp < v_length){
		if(VALUE.charAt(iTemp) == w_space){
		}
		else{
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	} //End While
	return strTemp;
} //End Function
/*fim TRIM*/

/*
- Mascara de RG para campo input[text].
- Deve ser colocado no onkeyup. Ex: form.texto.onkeyup	= getMaskRG;
- Deve ser colocado no onblur. Ex: form.texto.onblur	= getMaskRG;
- Deve ser setado o maxLength. Ex: form.texto.maxLength	= 13;
*/
function getMaskRG(){ this.value = maskRG( this.value ); }
function maskRG( string ){
	string	= string.replace( /\D/g ,"");
    string	= string.replace( /(\d{2})(\d)/ ,"$1.$2");
    string	= string.replace( /(\d{3})(\d)/ ,"$1.$2");
    string	= string.replace( /(\d{3})(\d)/ ,"$1-$2");
    if ( string.length > 13 ) string = string.substr(0, 13);
    return string;
}

/*
- Mascara de Telefone c/ DDD para campo input[text].
- Deve ser colocado no onkeyup. Ex: form.texto.onkeyup	= getMaskTelefoneComDDD;
- Deve ser colocado no onblur. Ex: form.texto.onblur	= getMaskTelefoneComDDD;
- Deve ser setado o maxLength. Ex: form.texto.maxLength	= 12;
*/
function getMaskTelefoneComDDD(){ this.value = maskTelefoneComDDD( this.value ); }
function maskTelefoneComDDD( string ){
	string	= string.replace( /\D/g ,"");
	string	= string.replace( /^(\d)/ ,"($1");
	string	= string.replace( /^(\(\d{2})(\d)/ ,"$1) $2");
    string	= string.replace( /^(\d{2})(\d)/ ,"$1 $2");
    string	= string.replace( /(\d{4})(\d)/ ,"$1-$2");
    if ( string.length > 14 ) string = string.substr(0, 14);
    return string;
}

/*
- Mascara de Telefone para campo input[text].
- Deve ser colocado no onkeyup. Ex: form.texto.onkeyup	= getMaskTelefone;
- Deve ser colocado no onblur. Ex: form.texto.onblur	= getMaskTelefone;
- Deve ser setado o maxLength. Ex: form.texto.maxLength	= 9;
*/
function getMaskTelefone(){ this.value = maskTelefone( this.value ); }
function maskTelefone( string ){
	string	= string.replace( /\D/g ,"");
    string	= string.replace( /(\d{4})(\d)/ ,"$1-$2");
    if ( string.length > 9 ) string = string.substr(0, 9);
    return string;
}

/*
- Mascara de CPF para campo input[text].
- Deve ser colocado no onkeyup. Ex: form.texto.onkeyup	= getMaskCPF;
- Deve ser colocado no onblur. Ex: form.texto.onblur	= getMaskCPF;
- Deve ser setado o maxlength. Ex: form.texto.maxLength	= 14;
*/
function getMaskCPF(){ this.value = maskCPF( this.value ); }
function maskCPF( string ){
	string	= string.replace( /\D/g ,"");
    string	= string.replace( /(\d{3})(\d)/ ,"$1.$2");
    string	= string.replace( /(\d{3})(\d)/ ,"$1.$2");
    string	= string.replace( /(\d{3})(\d{1,2})$/ ,"$1-$2");
	if ( string.length > 14 ) string = string.substr(0, 14);
    return string;
}

/*
- Mascara de Data para campo input[text].
- Deve ser colocado no onkeyup. Ex: form.texto.onkeyup	= getMaskDate;
- Deve ser colocado no onblur. Ex: form.texto.onblur	= getMaskDate;
- Deve ser setado o maxLength. Ex: form.texto.maxLength	= 10;
*/
function getMaskDate(){ this.value = maskDate( this.value ); }
function maskDate( string ){
	string	= string.replace( /\D/g ,"");
    string	= string.replace( /(\d{2})(\d)/ ,"$1/$2");
    string	= string.replace( /(\d{2})(\d)/ ,"$1/$2");
    if ( string.length > 10 ) string = string.substr(0, 10);
    return string;
}

/*
- Mascara de CEP para campo input[text].
- Deve ser colocado no onkeyup. Ex: form.texto.onkeyup	= getMaskCEP;
- Deve ser colocado no onblur. Ex: form.texto.onblur	= getMaskCEP;
- Deve ser setado o maxLength. Ex: form.texto.maxLength	= 10;
*/
function getMaskCEP(){ this.value = maskCEP( this.value ); }
function maskCEP( string ){
	string	= string.replace( /\D/g ,"");
//    string	= string.replace( /^(\d{2})(\d)/ ,"$1.$2");
    string	= string.replace( /^(\d{2})(\d{3})(\d)/ ,"$1$2-$3");
    if ( string.length > 9 ) string = string.substr(0, 9);
    return string;
}

// função que verifica se a string passada é um e-mail válido.
function isEmail(mail){
	var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
	if(typeof(mail) == "string" && er.test(mail))
		return true;
	else if(typeof(mail) == "object" && er.test(mail.value))
		return true;
	else
		return false;
}
// funções ajax
var clsAjax = new Object();

clsAjax.READY_STATE_UNINITIALIZED=0;
clsAjax.READY_STATE_LOADING=1;
clsAjax.READY_STATE_LOADED=2;
clsAjax.READY_STATE_INTERACTIVE=3;
clsAjax.READY_STATE_COMPLETE=4;
clsAjax.DEBUG=false;

/*--- content loader object for cross-browser requests ---*/
clsAjax.ContentLoader=function(url,onload,onerror,method,params,contentType){
	this.url		= url;
	this.req		= null;
	this.onload		= onload;
	this.onerror	= (onerror) ? onerror : this.defaultError;
	this.loadXMLDoc(url,method,params,contentType);
};
clsAjax.loadXMLDoc=function(url,method,params,contentType){
	if (!method) method="GET";
	if (!contentType && method=="POST") contentType='application/x-www-form-urlencoded';
	if (window.XMLHttpRequest){
		this.req=new XMLHttpRequest();
	} else if (window.ActiveXObject){
		this.req=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (this.req){
		try{
			var loader=this;
			this.req.onreadystatechange=function(){
				clsAjax.onReadyState.call(loader);
			};
			this.req.open(method,url,true);
			if (contentType){
				this.req.setRequestHeader('Content-Type', contentType);
			}
			this.req.send(params);
		}catch (err){
			this.onerror.call(this);
		}
	}
};


clsAjax.onReadyState=function(){
	var req=this.req;
	var ready=req.readyState;
	var httpStatus;
	try { httpStatus = this.transport.status; } catch(e) { httpStatus = 0; }
	if (ready==clsAjax.READY_STATE_COMPLETE){
		if (httpStatus==200 || httpStatus==0){
			this.onload.call(this);
		}else{
			this.onerror.call(this);
		}
	}
};

clsAjax.defaultError=function(){
	if (clsAjax.DEBUG){
		alert("error fetching data!"
			+"\n\nreadyState:"+this.req.readyState
			+"\nstatus: "+this.req.status
			+"\nheaders: "+this.req.getAllResponseHeaders());
	}
};


function go(loc) {
	window.location.href = loc;
}
function valida_cpf(cpf) {
	var numeros, digitos, soma, i, resultado, digitos_iguais;
	digitos_iguais = 1;
	if (cpf.length < 11) return false;
	for (i = 0; i < cpf.length - 1; i++){
		if (cpf.charAt(i) != cpf.charAt(i + 1)) {
			digitos_iguais = 0;
			break;
		}
	}
	if (!digitos_iguais) {
		numeros = cpf.substring(0,9);
		digitos = cpf.substring(9);
		soma = 0;
		for (i = 10; i > 1; i--) soma += numeros.charAt(10 - i) * i;
		resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
		if (resultado != digitos.charAt(0)) return false;
		numeros = cpf.substring(0,10);
		soma = 0;
		for (i = 11; i > 1; i--) soma += numeros.charAt(11 - i) * i;
		resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
		if (resultado != digitos.charAt(1)) return false;
		return true;
	} else {
		return false;
	}

}
function valida_cnpj(cnpj) {
	var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
	digitos_iguais = 1;
	if (cnpj.length < 14 && cnpj.length < 15) return false;
	for (i = 0; i < cnpj.length - 1; i++) {
		if (cnpj.charAt(i) != cnpj.charAt(i + 1)) {
			digitos_iguais = 0;
			break;
		}
	}
	if (!digitos_iguais) {
		tamanho = cnpj.length - 2
		numeros = cnpj.substring(0,tamanho);
		digitos = cnpj.substring(tamanho);
		soma = 0;
		pos = tamanho - 7;
		for (i = tamanho; i >= 1; i--) {
			soma += numeros.charAt(tamanho - i) * pos--;
			if (pos < 2) pos = 9;
		}
		resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
		if (resultado != digitos.charAt(0)) return false;
		tamanho = tamanho + 1;
		numeros = cnpj.substring(0,tamanho);
		soma = 0;
		pos = tamanho - 7;
		for (i = tamanho; i >= 1; i--) {
			soma += numeros.charAt(tamanho - i) * pos--;
			if (pos < 2) pos = 9;
		}
		resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
		if (resultado != digitos.charAt(1)) return false;
		return true;
	} else {
		return false;
	}
} 
function valida_email(mail) {
	var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
	if (typeof(mail) == "string") {
		if (er.test(mail)) return true;
	} else if(typeof(mail) == "object") {
		if (er.test(mail.value)) return true;
	} else {
		return false;
	}
}
function valida_data(data) {
	var reTipo = /^((0[1-9]|[12]\d)\/(0[1-9]|1[0-2])|30\/(0[13-9]|1[0-2])|31\/(0[13578]|1[02]))\/\d{4}$/;
	return reTipo.test(data);
}
function valida_numero(data) {
	var reDigits = /^\d+$/;
	return reDigits.test(data);
}
