// JavaScript Document
function callAHAH(url) {
	try {
		req = new XMLHttpRequest(); 
		/* e.g. Firefox */
	} catch(e) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");  
			/* some versions IE */
		} catch (e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");  
				/* some versions IE */
			} catch (E) {
				req = false;
			} 
		} 
	}
	req.onreadystatechange = function() {
		responseAHAH();
	};
	req.open("GET",url,true);
	req.send(null);
}
function responseAHAH() {
	if(req.readyState == 4) {
		if(req.status == 200) {
			output = req.responseText;
			ajax_result(output);
		}
	}
}

function popitup(url,w,h,name) {
	newwindow=window.open(url,name,'scrollbars=yes, top=0, left=0, height='+h+',width='+w);
	if (window.focus) {	newwindow.focus()}
	return false;
}

function valida_email_old(email){
	parte1 = email.indexOf("@");
	parte2 = email.indexOf(".");
	parte3 = email.length;
	if (!(parte1 >= 3 && parte2 >= 6 && parte3 >= 9)) {
		return false;
	}else{
		return true;
	}
}

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;
	}	
}
// Check Form, empty text and textarea fields
function checkForm(fields){
	for(i=0;i<fields.length;i++){
		field=fields[i].split(";");
		obj = document.getElementById(field[0]);
		if(obj.type=='text' || obj.type=='textarea' || obj.type=='password' || obj.type=='select-one'){
			if(!obj.value || obj.value == 0){
				//obj_lb_erro = document.getElementById('lb_erro');
				//obj_lb_erro.innerHTML = field[1];
				//setTimeout("obj_lb_erro.innerHTML = '';",5000);
				alert(field[1]);
				obj.focus();
				return false;
			}
		}
		// Files
		if(obj.type=='file'){
			if(obj.value.length < 7){
				alert(field[1]);
				obj.focus();
				return false;
			}
		}
		if(obj.id == "email"){
			if(!valida_email(obj.value)){
				//obj_lb_erro = document.getElementById('lb_erro');
				//obj_lb_erro.innerHTML = field[1];
				//setTimeout("obj_lb_erro.innerHTML = '';",5000);
				alert(field[1]);
				obj.focus();
				return false;
			}
		}
	}
	return true;
}

// Show and hide the obj
function showHide(id, type){
	obj = document.getElementById(id);
	if(type){
		obj.style.display = type;
	}else{
		if(obj.style.display != 'block'){
			obj.style.display = 'block';
		}else{
			obj.style.display = 'none';
		}
	}
}

// Check data
function checkData(data){
	DataRef=data.split('/',3);
	if(DataRef.length!=3){
		return false;
	};
	if( isNaN(DataRef[0]) || !( DataRef[0] >= 1 && DataRef[0] <= 31) ){
		return false;
	};
	if( isNaN(DataRef[1]) || !( DataRef[1] >=1 && DataRef[1] <= 12) ){
		return false;
	};
	if( isNaN(DataRef[2]) || String(DataRef[2]).length!=4 ){
		return false;
	};
	return true;
};

function validaCPF(s)	{
	var i;
	s = limpa_string(s);
	var c = s.substr(0,9);
	var dv = s.substr(9,2);
	var d1 = 0;
	for (i = 0; i < 9; i++){
		d1 += c.charAt(i)*(10-i);
	}	
    if (d1 == 0) return false;
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;	
	if ( String(dv.charAt(0)) != String(d1) ){
		return false;
	}
	d1 *= 2;
	for (i = 0; i < 9; i++){
		d1 += c.charAt(i)*(11-i);
	}
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;

	if ( String(dv.charAt(1)) != String(d1)) {
		return false;
	}
    return true;
}

// Formato moeda
function MascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode == 13 || whichCode == 8 || whichCode == 0) return true;
    key = String.fromCharCode(whichCode); // Valor para o código da Chave
    if (strCheck.indexOf(key) == -1) return false; // Chave inválida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
    aux = '';
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) objTextBox.value = '';
    if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
    if (len == 2) objTextBox.value = '0'+ SeparadorDecimal + aux;
    if (len > 2) {
        aux2 = '';
        for (j = 0, i = len - 3; i >= 0; i--) {
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
        objTextBox.value += aux2.charAt(i);
        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
    }
    return false;
}
//ucfirst
String.prototype.ucFirst = function () {
   return this.substr(0,1).toUpperCase() + this.substr(1,this.length);
}
function mascara(campo, Mascara, evtKeyPress) {
	var i, nCount, Texto, tCampo, tMascara,bolMask, retTexto;
	var codTecla = (window.Event) ? evtKeyPress.which : evtKeyPress.keyCode;
	if (codTecla == 13 || codTecla == 8 || codTecla == 0) return true;
	if (codTecla) { // backspace
	  Texto = campo.value;
	  re = /[^A-Z0-9]/gi;
	  Texto = Texto.toString().replace( re, "");
	  tCampo = Texto.length;
	  tMascara = Mascara.length;
	  i = 0;
	  nCount = 0;
	  retTexto = "";
	  tMascara = tCampo;
	  while (i <= tMascara) {
		bolMask = ((Mascara.charAt(i) == ":") || (Mascara.charAt(i) == "-") || (Mascara.charAt(i) == ".") || (Mascara.charAt(i) == "/"))
		bolMask = bolMask || ((Mascara.charAt(i) == "(") || (Mascara.charAt(i) == ")") || (Mascara.charAt(i) == " "))
		if (bolMask) {
		  retTexto += Mascara.charAt(i);
		  tMascara++; 
		} else {
		  retTexto += Texto.charAt(nCount);
		  nCount++;
		}
		i++;
	  }
	  campo.value = retTexto;
		if (Mascara.charAt(i-1) == "9") { // so numero
			return ((codTecla > 47) && (codTecla < 58)); // números de 0 a 9
		} else { // livre
			return true;
		};
	} else {
		return true;
	};	
};

function bookmarksite(title, url){
	if (document.all)
		window.external.AddFavorite(url, title);
	else if (window.sidebar)
		window.sidebar.addPanel(title, url, "")
}

function select_change_color(val, id){
	linha = document.getElementById(id);
	if(val == true){
		linha.className = 'lista_selected';
	}else if(val == false){
		linha.className = 'lista';
	}
}

function select_all(obj){
	if(!obj.length){
		obj.checked = true;
		select_change_color(true, 'tr_1');
	}else{
		for(i=0; i < obj.length; i++){
			if(obj[i].checked == false){
				obj[i].checked = true;
				linha = document.getElementById('tr_'+ (i+1));
				linha.className = 'lista_selected';
			}
		}
	}
}
function select_none(obj){
	if(!obj.length){
		obj.checked = false;
		select_change_color(false, 'tr_1');
	}else{
		for(i=0; i < obj.length; i++){
			if(obj[i].checked == true){
				obj[i].checked = false;
				linha = document.getElementById('tr_'+ (i+1));
				linha.className = 'lista';
			}
		}
	}
}
function is_checked(obj){
	if(obj.length){
		for(i=0; i < obj.length; i++){
			if(obj[i].checked == true){
				return true;	
			}
		}
	}else{
		if(obj.checked == true){
			return true;	
		}
	}
	return false;
}
function deletar(dir){
	if(lista_records){
		if(is_checked(document.frm_lista.lista_check) == true){
			if(confirm('Warning! This action will delete permanently all the selected records, proceed?')){
				obj = document.frm_lista.lista_check;
				if(obj.length){
					for(i=0; i < obj.length; i++){
						if(obj[i].checked == true){
							document.frm_lista.values.value += obj[i].value + ';';
						}
					}
				}else{
					document.frm_lista.values.value = obj.value + ';';
				}
				document.frm_lista.action = dir +'/deletar.php';
				document.frm_lista.submit();
			}
		}else{
			obj_lb_erro = document.getElementById('lb_del_erro');
			obj_lb_erro.innerHTML = 'Check at least one record!';
			setTimeout("obj_lb_erro.innerHTML = '';", 3000);
		}
	}
}
function novo(){
	obj = document.getElementById('div_form');
	if(obj.style.display == 'none'){
		showHide('div_lista');
		showHide('div_form');

	}
	do_clean();
	do_focus();
}
function lista(url){
	if(!lista_records){
		location=url;
		return false;
	}
	obj = document.getElementById('div_lista');
	if(obj.style.display == 'none'){
		showHide('div_lista');
		showHide('div_form');
	}
}
function do_alert(txt){
	obj_lb_alert = document.getElementById('lb_alert');
	img = '<img src="images/alert.gif" />&nbsp;&nbsp;';
	obj_lb_alert.innerHTML = img + txt;
	obj_lb_alert.style.display = "block";
	setTimeout('obj_lb_alert.style.display = "none";', 4000);
}

function teste(){
	// i must concentrate, everything will past, will be ok soon .:.:.:.
	return null;
	
}