function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

function txtBoxFormat(objeto, sMask, evtKeyPress) 
{
    var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;
	if(document.all) 
	{ // Internet Explorer
    		nTecla = evtKeyPress.keyCode;
	} 
	else if(document.layers) 
	{ // Nestcape
    		nTecla = evtKeyPress.which;
	} 
	else 
	{
    		nTecla = evtKeyPress.which;
    		if (nTecla == 8) 
		{
      		return true;
    		}
    		
	}
    sValue = objeto.value;
    // Limpa todos os caracteres de formatação que
    // já estiverem no campo.
    sValue = sValue.toString().replace( "-", "" );
    sValue = sValue.toString().replace( "-", "" );
    sValue = sValue.toString().replace( ".", "" );
    sValue = sValue.toString().replace( ".", "" );
    sValue = sValue.toString().replace( "/", "" );
    sValue = sValue.toString().replace( "/", "" );
    sValue = sValue.toString().replace( ":", "" );
    sValue = sValue.toString().replace( ":", "" );
    sValue = sValue.toString().replace( "(", "" );
    sValue = sValue.toString().replace( "(", "" );
    sValue = sValue.toString().replace( ")", "" );
    sValue = sValue.toString().replace( ")", "" );
    sValue = sValue.toString().replace( " ", "" );
    sValue = sValue.toString().replace( " ", "" );
    fldLen = sValue.length;
    mskLen = sMask.length;
    i = 0;
    nCount = 0;
    sCod = "";
    mskLen = fldLen;
	while (i <= mskLen) 
	{
      		bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/") || (sMask.charAt(i) == ":"))
      		bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))
      		if (bolMask) 
		{
        		sCod += sMask.charAt(i);
        		mskLen++; 
		}
      		else 
		{
        		sCod += sValue.charAt(nCount);
        		nCount++;
      		}
      		i++;
    	}
    objeto.value = sCod;
    	if (nTecla != 8) 
	{ // backspace
     		if (sMask.charAt(i-1) == "9") // apenas números...
        		return ((nTecla > 47) && (nTecla < 58)); 
		else // qualquer caracter...
        		return true;
    	}
    	else 
	{
      		return true;
    	}
}

function submitform( form, funcao ){
			if(funcao != "")
				document.getElementById(form).hid_funcao.value = funcao;
			document.getElementById(form).submit();
}

function controlaCamada(nomeDiv) 
{
	if(document.getElementById(nomeDiv).style.visibility == "hidden" )
        	document.getElementById(nomeDiv).style.visibility = "visible";
	else
        	document.getElementById(nomeDiv).style.visibility = "hidden";
}

function passValue( form, value ){
	document.getElementById(form).value = value;
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays=="") ? "" : ";expires="+exdate.toGMTString());
}
function deleteCookie( cookie_name )
{
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

function getAjax(){
	try //Objeto de AJAX
	{
    		xmlhttp = new XMLHttpRequest();
	}
	catch(ee)
	{
    		try
    		{
        		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    		}
    		catch(e)
    		{
        		try
        		{
             			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        		}
        		catch(E)
        		{
            		xmlhttp = false;
        		}
    		}
	}//FIM objeto AJAX
	return xmlhttp;
}

function TrocaList(ListOrigem,ListDestino)
{
	var i;
	for (i = 0; i < ListOrigem.options.length ; i++){
		if (ListOrigem.options[i].selected == true){
			var Op = document.createElement("OPTION");
			Op.text = ListOrigem.options[i].text;
			Op.value = ListOrigem.options[i].value;
			ListDestino.options.add(Op);
			ListOrigem.remove(i);
			i--;
		}
	}
}

function isUsuario(usuario){
	if(usuario != ""){
		xmlhttp = getAjax();
		controlaCamada("divControleLog");
		xmlhttp.open("GET", "/_includes/ajax/isusuario.php?usuario="+usuario, true);
		xmlhttp.onreadystatechange = function()
		{
			if ( xmlhttp.readyState == 4)
			{
					//alert(xmlhttp.responseText);
					if(xmlhttp.responseText == "novo"){
						controlaCamada("divControleLog");
						document.getElementById("hid_novousuario").value = "novo";
						document.getElementById("controleUsuario").innerHTML = '<IMG src="/images/bullet_tick.png" width="16" height="16" align="center" border="0"> Apelido Disponível!';
					}
					else{
						controlaCamada("divControleLog");
						document.getElementById("hid_novousuario").value = "existe";
						document.getElementById("controleUsuario").innerHTML = '<IMG src="/images/bullet_cross.png" width="16" height="16" align="center" border="0"> Apelido Indisponível!';
					}
					//alert(xmlhttp.responseText);
			}
		}
		xmlhttp.send(null);	
	}
		
}

function isEmail(usuario){
	if(usuario != ""){
		var oRegEmail = /^[a-z0-9\._\-]+\@[a-z0-9\._\-]+\.[a-z]{2,3}$/i;
		xmlhttp = getAjax();
		controlaCamada("divControleLog2");
		xmlhttp.open("GET", "/_includes/ajax/isemail.php?email="+usuario, true);
		xmlhttp.onreadystatechange = function()
		{
			if ( xmlhttp.readyState == 4)
			{
					//alert(xmlhttp.responseText);
					if(xmlhttp.responseText == "novo" && oRegEmail.test(usuario)){
						controlaCamada("divControleLog2");
						document.getElementById("hid_novoemail").value = "novo";
						document.getElementById("controleEmail").innerHTML = '<IMG src="/images/bullet_tick.png" width="16" height="16" align="center" border="0"> Email Disponível!';
					}
					else{
						controlaCamada("divControleLog2");
						document.getElementById("hid_novoemail").value = "existe";
						document.getElementById("controleEmail").innerHTML = '<IMG src="/images/bullet_cross.png" width="16" height="16" align="center" border="0"> Email Indisponível ou inválido!';
					}
					//alert(xmlhttp.responseText);
			}
		}
		xmlhttp.send(null);
	}
	
}

function isCPF(usuario){
	if(usuario != ""){
		controlaCamada("divControleLog3");
		if(valida_cpf(usuario)){
			xmlhttp = getAjax();
			xmlhttp.open("GET", "/_includes/ajax/iscpf.php?cpf="+usuario, true);
			xmlhttp.onreadystatechange = function()
			{
				if ( xmlhttp.readyState == 4)
				{
						//alert(xmlhttp.responseText);
						if(xmlhttp.responseText == "novo"){
							controlaCamada("divControleLog3");
							document.getElementById("hid_novocpf").value = "novo";
							document.getElementById("controleCPF").innerHTML = '<IMG src="/images/bullet_tick.png" width="16" height="16" align="center" border="0"> CPF Válido!';
						}
						else{
							controlaCamada("divControleLog3");
							document.getElementById("hid_novocpf").value = "existe";
							document.getElementById("controleCPF").innerHTML = '<center><br>Você já possui cadastro no GameSpace.com.br.<br>Efetue seu login no box superior direito.<br>Se não lembre sua senha, clique no link "Esqueci minha senha" na barra superior.</center>';
						}
						//alert(xmlhttp.responseText);
				}
			}
			xmlhttp.send(null);
		}else{
			controlaCamada("divControleLog3");
			document.getElementById("hid_novocpf").value = "existe";
			document.getElementById("controleCPF").innerHTML = '<IMG src="/images/bullet_cross.png" width="16" height="16" align="center" border="0"> CPF Inválido!';
		}
	}	
}

function valida_cpf(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;
add = 0;
for (i=0; i < 9; i ++)
	add += parseInt(cpf.charAt(i)) * (10 - i);
rev = 11 - (add % 11);
if (rev == 10 || rev == 11)
	rev = 0;
if (rev != parseInt(cpf.charAt(9)))
	return false;
add = 0;
for (i = 0; i < 10; i ++)
	add += parseInt(cpf.charAt(i)) * (11 - i);
rev = 11 - (add % 11);
if (rev == 10 || rev == 11)
	rev = 0;
if (rev != parseInt(cpf.charAt(10)))
	return false;
return true;
}