function remove(str, sub) {
   i = str.indexOf(sub);
   r = "";
   if (i == -1) return str;
   r += str.substring(0,i) + remove(str.substring(i + sub.length), sub);
   return r;
}

function validateEmail(email) {
	var ret = false;
	var em = new String();
	em = String(email);
	var RegExPattern = /^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{ 1,3}){3}\])$/; 
	if ( em.match(RegExPattern) ) {
		ret = true;
	}
	return ret;
}

function trim(str){
	if (typeof(str) == "string") {
		return str.replace(/^\s+|\s+$/g,"");
	} else {
		return str;
	}	
}

function maskFormat(mask,obj){
	if(obj.value != ""){
		sObj = obj.value.toString();
		nString = "";
		for(i=0;i<(sObj.length);i++){
			if(!isNaN(sObj.charAt(i)) && (sObj.charAt(i) != " ")) {
				nString = nString + sObj.charAt(i);
			}
		}
		j = 0;
		k = 0;
		mString = "";
		while(j < nString.length && k < mask.length){
			if(mask.charAt(k).toUpperCase() == "N"){
				mString = mString + nString.charAt(j);
				j = j+1;
			} else {
				mString = mString + mask.charAt(k);
			}
			k = k+1;
		}
		obj.value = mString;
	}
}
function validarCPF(cpf){
	var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
	if(!filtro.test(cpf)){
		return false;
	}
	cpf = remove(cpf, ".");
	cpf = remove(cpf, "-");
	if(cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" ||
		cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||
		cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" ||
		cpf == "88888888888" || cpf == "99999999999"){
			return false;
	}
	soma = 0;
	for(i = 0; i < 9; i++)
		soma += parseInt(cpf.charAt(i)) * (10 - i);
	resto = 11 - (soma % 11);
	if(resto == 10 || resto == 11)
		resto = 0;
	if(resto != parseInt(cpf.charAt(9))){
		return false;
	}
	soma = 0;
	for(i = 0; i < 10; i ++)
		soma += parseInt(cpf.charAt(i)) * (11 - i);
	resto = 11 - (soma % 11);
	if(resto == 10 || resto == 11)
		resto = 0;
	if(resto != parseInt(cpf.charAt(10))){
		return false;
	}
	return true;
}
function validaAjaxCpf() {
	var spnCpf = document.getElementById("spnCpf");
	var emMsg = document.getElementById("emMsg");
	var divCpf = document.getElementById("divCpf");
	var txtCpf = document.getElementById("txtCpf");
	if (txtCpf.value == "") {
		spnCpf.style.display = "none";
		emMsg.style.display = "none";
		divCpf.className = "bgL";
	} else {
		if (txtCpf.value.length < 14) {
			spnCpf.className = "error";
			spnCpf.innerHTML = "error";
			spnCpf.style.display = "block";
			emMsg.innerHTML = "Este cpf contém menos que 11 números!";
			emMsg.style.display = "block";
			divCpf.className = "bgL erro";
		} else {
			if (validarCPF(txtCpf.value)==false) {
				spnCpf.className = "error";
				spnCpf.innerHTML = "error";
				spnCpf.style.display = "block";
				emMsg.innerHTML = "Este cpf é inválido!";
				emMsg.style.display = "block";
				divCpf.className = "bgL erro";
			} else {
				var vcpf = remove(txtCpf.value, ".");
				vcpf = remove(vcpf, "-");
				var dataCpf = {
					cpf: vcpf
				};
				$.ajax({type: "POST",url: "verificaCpf.asp", data:dataCpf, dataType: "xml", async:false, success: function(xml) {
					if(xml.getElementsByTagName("return")[0].firstChild.nodeValue == "0") {
						spnCpf.className = "ok";
						spnCpf.innerHTML = "ok";
						spnCpf.style.display = "block";
						emMsg.style.display = "none";
						divCpf.className = "bgL";
					} else {
						spnCpf.className = "error";
						spnCpf.innerHTML = "error";
						spnCpf.style.display = "block";
						emMsg.innerHTML = "Este cpf já foi cadastrado!";
						emMsg.style.display = "block";
						divCpf.className = "bgL erro";
					}
				}}); 
				//$.post("verificaCpf.asp", dataCpf, function(xml){
				//}, "xml");
			}
		}
	}
}

function validaAjaxEmail() {
	var spnEmail = document.getElementById("spnEmailx");
	var emMsg = document.getElementById("emMsg");
	var divEmail = document.getElementById("divEmail");
	var txtEmail = document.getElementById("txtEmail");
	if (txtEmail.value == "") {
		spnEmail.style.display = "none";
		emMsg.style.display = "none";
		divEmail.className = "bgL";
	} else {
		if (validateEmail(txtEmail.value)==false) {
			spnEmail.className = "error";
			spnEmail.innerHTML = "error";
			spnEmail.style.display = "block";
			emMsg.innerHTML = "Este E-mail é inválido!";
			emMsg.style.display = "block";
			divEmail.className = "bgL erro";
		} else {
			var dataEmail = {
				email: txtEmail.value
			};
			$.ajax({type: "POST",url: "verificaEmail.asp", data:dataEmail, dataType: "xml", async:false, success: function(xml) {
				if(xml.getElementsByTagName("return")[0].firstChild.nodeValue == "0") {
					spnEmail.className = "ok";
					spnEmail.innerHTML = "ok";
					spnEmail.style.display = "block";
					emMsg.style.display = "none";
					divEmail.className = "bgL";
				} else {
					spnEmail.className = "error";
					spnEmail.innerHTML = "error";
					spnEmail.style.display = "block";
					emMsg.innerHTML = "Este E-mail já foi cadastrado!";
					emMsg.style.display = "block";
					divEmail.className = "bgL erro";
				}
			}}); 
			//$.post("verificaEmail.asp", dataEmail, function(xml){
			//}, "xml");
		}
	}
}
function validaLogin() {

	var apelido = document.getElementById("apelido");
	if ((apelido.value == "") || (apelido.value == "digite seu apelido")) {
		document.getElementById("spnApelido").className = "left erro";
		document.getElementById("spnSenha").className = "left erro";
		document.getElementById("msgDadosInvalidos").style.display = "block";
		return false;
	}
	var senha = document.getElementById("senha");
	if ((senha.value == "") || (senha.value == "*******")) {
		document.getElementById("spnApelido").className = "left erro";
		document.getElementById("spnSenha").className = "left erro";
		document.getElementById("msgDadosInvalidos").style.display = "block";
		return false;
	}
	
	var data = {
		apelido: apelido.value,
		senha: senha.value
	};

	$.post("processaLogin.asp", data, function(xml){
		try {
			if(xml.getElementsByTagName("return")[0].firstChild.nodeValue == "1") {
				document.location = "inicio.asp";
			} else {
				if (xml.getElementsByTagName("apelido")[0].firstChild.nodeValue == "0") {
					document.getElementById("spnApelido").className = "left erro";
					document.getElementById("spnSenha").className = "left erro";
					document.getElementById("msgDadosInvalidos").style.display = "block";
				} else {
					document.getElementById("spnApelido").className = "left erro";
					document.getElementById("spnSenha").className = "left erro";
					document.getElementById("msgDadosInvalidos").style.display = "block";
				}
			}
		} catch(e) {

		}
	}, "xml");
	
	
	return false;
}

var itvMsgEsqueciSenha = -1;
function validaEsqueci() {
	var apelido = document.getElementById("apelido");
	if ((apelido.value == "") || (apelido.value == "digite seu apelido")) {
		document.getElementById("spnApelido").className = "left erro";
		document.getElementById("msgDadosInvalidos").style.display = "block";
	} else {
		var data = {
			apelido: apelido.value
		};
  
		$.post("processaEsqueci.asp", data, function(xml){
			try {
				if(xml.getElementsByTagName("return")[0].firstChild.nodeValue == "1") {
					document.getElementById("ulCamposLogin").style.display = "none";
					var em = document.getElementById("emEsqueciSenha");
					em.style.display = "block";
					em.innerHTML = "sua senha foi enviada para seu e-mail.";
					itvMsgEsqueciSenha = setInterval('showCamposLogin()',3000);
				} else {
					if (xml.getElementsByTagName("apelido")[0].firstChild.nodeValue == "0") {
						document.getElementById("spnApelido").className = "left erro";
						document.getElementById("msgDadosInvalidos").style.display = "block";
					} else {
						document.getElementById("ulCamposLogin").style.display = "none";
						var em = document.getElementById("emEsqueciSenha");
						em.style.display = "block";
						em.innerHTML = "e-mail não enviado.";
						itvMsgEsqueciSenha = setInterval('showCamposLogin()',3000);
					}
				}
			} catch(e) {

			}
		}, "xml");
	}
}

function showCamposLogin() {
	clearInterval(itvMsgEsqueciSenha);
	document.getElementById("emEsqueciSenha").style.display = "none";
	document.getElementById("ulCamposLogin").style.display = "block";
	document.getElementById("msgDadosInvalidos").style.display = "none";
	document.getElementById("spnApelido").className = "left";
	document.getElementById("spnSenha").className = "left";
}

/*Target - início*/
function init() {
	createExternalLinks();
}
function createExternalLinks() {
    if(document.getElementsByTagName) {
        var anchors = document.getElementsByTagName('a');
        for(var i=0; i<anchors.length; i++) {
            var anchor = anchors[i];
            if(anchor.getAttribute("href") && anchor.getAttribute('rel')=='external') {
                anchor.target = '_blank';
                var title = anchor.title + ' (Este link abre uma nova janela)';
                anchor.title = title;
            }
        }
    }
}
function addEvent(obj, evType, fn){
    if(obj.addEventListener){
        obj.addEventListener(evType, fn, false);
        return true;
    } else if (obj.attachEvent){
        var r = obj.attachEvent('on'+evType, fn);
        return r;
    } else {
        return false;
    }
}
function validaCharApelido(txtApelido) {
	var strValida = "0123456789 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var charValid = false;
	var charI = "";
	var i=0;
	for (i=0;i<txtApelido.value.length;i++) {
		charI = txtApelido.value.charAt(i);
		if (strValida.indexOf(charI) == -1) {
			break;
		}
		if (i==(txtApelido.value.length-1)) {
			charValid = true;
		}
	}
	if (charValid == false) {
		if (txtApelido.value.length == 1) {
			txtApelido.value = "";
		} else {
			txtApelido.value = txtApelido.value.substring(0, i);
		}
		
	}
}

function validaAjaxApelido() {
	var ret = false;
	var spnApelido = document.getElementById("spnApelidox");
	var emMsg = document.getElementById("emMsg");
	var divApelido = document.getElementById("divApelido");
	var txtApelido = document.getElementById("txtApelido");
	if (txtApelido.value == "") {
		spnApelido.style.display = "none";
		emMsg.style.display = "none";
		divApelido.className = "bgL";
	} else {
		var strValida = "0123456789 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
		var charValid = false;
		var charI = "";
		for (var i=0;i<txtApelido.value.length;i++) {
			charI = txtApelido.value.charAt(i);
			if (strValida.indexOf(charI) == -1) {
				break;
			}
			if (i==(txtApelido.value.length-1)) {
				charValid = true;
			}
		}
		
		if (charValid == false) {
			spnApelido.className = "error";
			spnApelido.innerHTML = "error";
			spnApelido.style.display = "block";
			emMsg.innerHTML = "O caracterer \""+charI+"\" é inválido para o apelido!";
			emMsg.style.display = "block";
			divApelido.className = "bgL erro";
		} else {
			var dataApelido = {
				apelido: txtApelido.value
			};
			$.ajax({type: "POST",url: "verificaApelido.asp", data:dataApelido, dataType: "xml", async:false, success: function(xml) {
				if(xml.getElementsByTagName("return")[0].firstChild.nodeValue == "0") {
					spnApelido.className = "ok";
					spnApelido.innerHTML = "ok";
					spnApelido.style.display = "block";
					emMsg.style.display = "none";
					divApelido.className = "bgL";
					ret = true;
				} else {
					spnApelido.className = "error";
					spnApelido.innerHTML = "error";
					spnApelido.style.display = "block";
					emMsg.innerHTML = "Este apelido já foi cadastrado!";
					emMsg.style.display = "block";
					divApelido.className = "bgL erro";
				}
			}}); 
			//$.post("verificaApelido.asp", dataApelido, function(xml){
			//}, "xml");
			
		}
	}
	return ret;
}

addEvent(window, "load", init);
/*Target - fim*/
