// change code every 'n' "DAYS","WEEKS","MONTHS"
changetype="DAYS";
changen=1;

// Pass-code override if "-1" then changetype and n used otherwise this
// specific code used until changed manually
codeoverride = 1;//-1

// Base URL setting (converted to <BASEURL><PASSCODE>/)
baseurl = "";
rd = "";

//ENDOPTIONS-----------------------------------------------------------------
// Nothing below this line should need to be altered, until <BODY> reached

  function passEncode(form)
  {
    pEncode(form.password.value);
  }
  
  function getUN(){
	  var un = new Array();
	  var r = "";
	  
	  un[6]="l";un[2]="u";un[11]="h";un[4]="t";un[8]="3";un[3]="p";
	  un[7]="h";un[1]="a";un[9]="5";un[0]="o";un[10]="j";un[5]="8";
	  
	  for(var i = 0; i < 12; i++){
		  r += un[i];
	  }
	  
	  return r;
  }
  
  function getKey()
  {
    if (codeoverride == -1)
    {
      var months = new Array();
      months[0]=31;
      months[1]=28;
      months[2]=31;
      months[3]=30;
      months[4]=31;
      months[5]=30;
      months[6]=31;
      months[7]=31;
      months[8]=30;
      months[9]=31;
      months[10]=30;
      months[11]=31;

      var dater = new Date();
      var Key = 0;
      for (var count = 0; count < dater.getMonth();count++)
      {
        Key = Key + months[count];
      }
      Key += dater.getDate();

      if (changetype == "DAYS")
      {
        Key = Key / changen;
      }else if (changetype == "WEEKS"){
        Key = Key / 7;
        Key = Key / changen;
      }else{
        Key = dater.getMonth() + ((dater.getYear() % 100) * 12);
        Key = Key / changen;
      }
      dater = null;
    }else{
      Key = codeoverride;
    }
    Key = Math.floor(Key);
    return (Key);
  }
  
  function getPS(){
	  var ps = new Array();
	  var r = "";

	  ps[6]="v";ps[1]="c";ps[8]="h";ps[5]="r";
	  ps[2]="d";ps[3]="p";ps[7]="j";ps[0]="e";ps[4]="f";
	  
	  for(var i = 0; i < 9; i++){
		  r += ps[i];
	  }
	  
	  return r;
  }

  function encode (OrigString, CipherVal)
  { 
    Ref="0123456789abcdefghijklmnopqrstuvwxyz._~ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    CipherVal = parseInt(CipherVal);
    var Temp="";
    for (Count=0; Count < OrigString.length; Count++) 
    {
      var TempChar = OrigString.substring (Count, Count+1);
      var Conv = cton(TempChar);
      var Cipher=Conv^CipherVal^Count;

      Cipher=ntoc(Cipher%Ref.length);
      Temp += Cipher;
    }
    return (Temp);
  }
	
  function pEncode(passwd)
  {
    var Key = getKey();
    var Ret = encode (passwd, Key);	
	return Ret;
  }
  
  function cton (Char) 
  {
    return (Ref.indexOf(Char));
  }
  
  function createURL(u, p){
	return (u + "_" + p + ".htm");
  }
  
  function ntoc (Val) 
  {
    return (Ref.substring(Val, Val+1));
  }

//This code catches the query string if the user submitted by hitting
//return, and forces the password encoding
if ((qmark = location.search) != ""){
  pEncode(qmark.substring(qmark.indexOf("=")+1,qmark.length));
}


//####################3

function loginUsr(){
	var user = document.getElementById("user").value;
	var pass = document.getElementById("pass").value;
	var r = true;
	
	if(pEncode(user) == getUN()){
		if(pEncode(pass) == getPS()){
			//location = baseurl + "/" + );
			makeRequest(createURL(getUN(), getPS()));
		}
		else{
			alert("Senha Inválida!!");
			r = false;
		}
	}
	else{
		alert("Usuário Inválido!!");
		r = false;
	}
	
	return r;
}


//###############33

function makeRequest(url){
	http_request = false;
	
	if(window.XMLHttpRequest){
		http_request = new XMLHttpRequest();
		
		if(http_request.overrideMimeType){
			http_request.overrideMimeType('text/html');
		}
	}
	else if(window.ActiveXObject){ // IE
		try{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e){
			try{
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e) {}
		}
	}
	
	if(!http_request){
		alert('Cannot create XMLHTTP instance');
		return false;
	}

	http_request.onreadystatechange = alertContents;
	http_request.open('GET', url, true);
	http_request.send(null);
}

function alertContents(){
	if(http_request.readyState == 4){
		if (http_request.status == 200){
			result = http_request.responseText;
			document.body.innerHTML = result;
			//alert(document.getElementById("ht").innerHTML);
		}
		else{
			alert("Ocorreu um problema com a comunicação com o servidor. Tente novamente mais tarde.\n\nObrigado.");
		}
	}
}