// JavaScript Document
var t = new Number();
var boolPlayer = new Boolean();

function allowAjax(){
	try{
		xmlhttp = new XMLHttpRequest();
	}catch(err1){
		try{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(err2){
			try{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(err3){
				xmlhttp = false;
				if(err3){
					alert("Desculpe! Mais o seu browser não suporta todas as funcionalidades dessa página.");
				}
			}
		}
	}
}
function runAjax(url, campo, extra){
	allowAjax();
	var y = document.getElementById("loadingDefault");
    if(extra != "") var z = document.getElementById(extra);
    
	if(y){
		y.style.visibility = "hidden";
		y.style.display = "block";
		/*y.style.top = (y.style.top + document.documentElement.scrollTop) + "px";*/
		y.style.visibility = "visible";
	}
	var x = document.getElementById(campo);
	if(!z) xmlhttp.open("GET", url, true);
    if(z) xmlhttp.open("GET", url + "?txtLogin=" + z.value, true);
	xmlhttp.onreadystatechange = function(){
		if(xmlhttp.readyState == 4){
            //if(xmlhttp.status == 200){
                var retorno = unescape(xmlhttp.responseText.replace(/\+/g, " "));
                x.innerHTML = retorno;
            //}
            
            if(y){
                y.style.display = "none";
            }
		}
	}
	xmlhttp.send(null);
}

function clearField(x){
	if(x.value == x.defaultValue) x.value = "";
}
function fillField(x){
	if(x.value == "") x.value = x.defaultValue;
}

function validaForm(x){
    for(var i = 0; i < x.elements.length; i++){
        if(x.elements[i].type != "file"){
            if(x.elements[i].type != "radio"){
                if(x.elements[i].title != "" && x.elements[i].title.indexOf("Google") == -1){
                    if(x.elements[i].value == ""){
                        alert("Por favor, preencha o campo " + x.elements[i].title + " corretamente!");
                        x.elements[i].focus();
                        return false;
                    }
                    
                    if(x.elements[i].name.indexOf("mail") != -1){
                        if(x.elements[i].value.indexOf("@") == -1 || x.elements[i].value.indexOf(".") == -1){
                            alert("Por favor, preencha o campo " + x.elements[i].title + " corretamente!");
                            x.elements[i].focus();
                            return false;
                        }
                    }
                    
                    if(x.elements[i].name.indexOf("ConfirmEmail") != -1){
                        if(x.elements[i].value != x.elements[i - 1].value){
                            alert("Atenção!, o campo Confirmar Email deve ser preenchido corretamente!");
                            x.elements[i].focus();
                            return false;
                        }
                    }
                    
                    if(x.elements[i].name == "txtConfirma"){
                        if(x.elements[i].value != x.elements[i - 1].value){
                            alert("O campo Confirmar Senha deve possuir o mesmo valor do campo Senha!");
                            x.elements[i].focus();
                            return false;
                        }
                    }
                }
            }else{
                if(x.elements[i].title != ""){
                    var y = document.getElementsByName(x.elements[i].name);
                    var boolCb = false;
                    if(y.length > 1){
                        for(var u = 0; u < y.length; u++){
                            if(y[u].checked) boolCb = true;
                        }
                        
                        if(!boolCb){
                            alert("Por favor, preencha o campo " + x.elements[i].title + " corretamente!");
                            x.elements[i].focus();
                            return false;
                        }
                    }
                    boolCb = false;
                }
            }
        }else{
            if(x.action.indexOf("upload=ok") == -1){
                if(x.elements[i].value == ""){
                    alert("Por favor, preencha o campo " + x.elements[i].title + " corretamente!");
                    x.elements[i].focus();
                    return false;
                }
            }else{
                if(x.action.indexOf("id=") != -1){
                    for(var u = 20; u > 0; u--){
                        if(!isNaN(x.action.substr(x.action.indexOf("id=") + 3, u))){
                            var boolId = true;
                            break;
                        }
                    }
                    
                    if(boolId){
                        if(x.elements[i].value == ""){
                            x.action = x.action.replace(/&upload=ok/, "&upload=false");
                            x.enctype = "application/x-www-form-urlencoded";
                            x.elements[i].title = "";
                        }
                    }else{
                        if(x.elements[i].value == ""){
                            alert("Por favor, preencha o campo " + x.elements[i].title + " corretamente!");
                            x.elements[i].focus();
                            return false;
                        }
                    }
                }
            }
        }
    }
}

function formatPhone(x, nextField){
    alwaysNum(x);
    if(x.value.length == 2 || x.value.length == 7) x.value += " ";
    if(x.value.length == 12) document.getElementById(nextField).focus();
}
function formatCep(x, nextField){
    alwaysNum(x);
    if(x.value.length == 5) x.value += " ";
    if(x.value.length == 9) document.getElementById(nextField).focus();
}
function formatDate(x, nextField){
    alwaysNum(x);
    if(x.value.length == 2 || x.value.length == 5) x.value += "/";
    if(x.value.length == 10) document.getElementById(nextField).focus();
}
function formatDateNF(x, nextField){
    alwaysNum(x);
    if(x.value.length == 4) x.value += "/";
    if(x.value.length == 7) document.getElementById(nextField).focus();
}
function formatTime(x, nextField){
    alwaysNum(x);
    if(x.value.length == 2) x.value += ":";
    if(x.value.length == 5) document.getElementById(nextField).focus();
}

function confirmDel(url){
    var x = confirm("Tem certeza que deseja realizar essa operação?");
    if(x) location.href = url;
}

function showMenu(id){
    var x = document.getElementById(id);
    x.style.display = "block";
}
function hideMenu(id){
    var x = document.getElementById(id);
    x.style.display = "none";
}

function alwaysNum(x){
	var nonNumbers = /\D/;
	if(nonNumbers.test(x.value)){
		for(var i = 0; i < x.value.length; i++){
            if(x.value.substr(i, 1) != "," && x.value.substr(i, 1) != "/" && x.value.substr(i, 1) != "." && x.value.substr(i, 1) != ":"){
                if(nonNumbers.test(x.value.substr(i, 1)) && x.value.substr(i, 1) != " " || x.value.substr(i, 2) == "  "){
                        if(x.value.substr(i, 2) == "  "){
                            x.value = x.value.replace(x.value.substr(i, 2), "");
                        }else{
                            x.value = x.value.replace(x.value.substr(i, 1), "");
                        }
                }
			}
		}
		return false;
	}
}

function markCb(id, inst){
    var x = document.getElementsByTagName("input");
    var y = document.getElementById(id);
    var b = document.getElementById("all");
    var c = new Number();
    var a = new Number();
    var aux = new Number();
    var cond = new Boolean();
    
    switch(inst){
    case "b":
        for(var i = 0; i < x.length; i++){
            if(x[i].title == id){
                c++;
            }
        }
        for(var i = 0; i < x.length; i++){
            if(x[i].title == id){
                if(x[i].checked){
                    a++;
                }
            }
        }
        
        if(a == c) y.checked = true;
        if(a != c) y.checked = false;
        
        break;
    case "a":
        for(var i = 0; i < x.length; i++){
            if(x[i].title == id){
                if(y.checked) x[i].checked = true;
                if(!y.checked) x[i].checked = false;
            }
        }
        
        break;
    case "all":
        for(var i = 0; i < x.length; i++){
            if(x[i].type == "checkbox"){
                if(x[i].name.indexOf("cb") != -1){
                    x[i].checked = y.checked;
                }
            }
        }
        break;
    }
    
    for(var i = 0; i < x.length; i++){
        if(x[i].type == "checkbox"){
            if(x[i].value != "all") c++;
        }
    }
    for(var i = 0; i < x.length; i++){
        if(x[i].type == "checkbox"){
            if(x[i].value != "all"){
                if(x[i].checked){
                    a++;
                }
            }
        }
    }
    if(a == c) b.checked = true;
    if(a != c) b.checked = false;
}

function CheckIsIE(){
    if(navigator.appName.toUpperCase() == 'MICROSOFT INTERNET EXPLORER'){
        return true;
    }else{
        return false;
    }
} 
function printInit(url, ptarget){
    var x = document.getElementById(ptarget);
    x.src = url;
}
function printFile(x){
    var ptarget = x.name;
    if(x.src != ""){
        if(CheckIsIE()){
            eval("document." + ptarget + ".focus()");
            eval("document." + ptarget + ".print()");
        }else{
            window.frames[ptarget].focus();
            window.frames[ptarget].print();
        }
    }
}