function foco(elemento) {
  elemento.style.border = "1px solid #000000";
}

function no_foco(elemento) {
  elemento.style.border = "1px solid #CCCCCC";
}

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function encode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);

   return output;
}

function getXmlHttpRequestObject() {
 if (window.XMLHttpRequest) {
    return new XMLHttpRequest(); //Mozilla, Safari ...
 } else if (window.ActiveXObject) {
    return new ActiveXObject("Microsoft.XMLHTTP"); //IE
 } else {
    alert("Your browser doesn't support the XmlHttpRequest object.");
 }
}

var receiveReq = getXmlHttpRequestObject();

function makeRequest(url, param) {
 if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {

   receiveReq.open("POST", url, true);
   receiveReq.onreadystatechange = updatePage;

   receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   receiveReq.setRequestHeader("Content-length", param.length);
   receiveReq.setRequestHeader("Connection", "close");

   receiveReq.send(param);
 }
}

function limpiar() {

	tinyMCE.activeEditor.setContent('');
  tinyMCE.activeEditor.focus();

}

function updatePage() {
 if (receiveReq.readyState == 4) {

    var data = receiveReq.responseText;
    var res = data.substr(0, 6);
    var extra = data.substr(7);

		document.getElementById('procesando').style.display = "none";

    if (res == "ERROR0") {
      document.getElementById('mubien').style.display = "inline";
    } else if (res == "ERROR1") {
			document.getElementById('error').style.display = "inline";
      document.getElementById('sec').innerHTML = '<img id="imgCaptcha" src="create_image.php?' + Math.random() + '" border=1>';
      document.getElementById('nick').value = '';
      document.getElementById('clave').value = '';
      document.getElementById('txtCaptcha').value = '';
      document.getElementById('todo').style.display = "inline";
      document.getElementById('analisis').innerHTML = '<p align="center"><b><font color="red"><small>C&oacute;digo de seguridad incorrecto</small></font></b></p>';
      tinyMCE.activeEditor.setContent('');
      document.getElementById('nick').focus();
    } else if (res == "ERROR2") {
    	document.getElementById('error').style.display = "inline";
      document.getElementById('sec').innerHTML = '<img id="imgCaptcha" src="create_image.php?' + Math.random() + '" border=1>';
      document.getElementById('clave').value = '';
      document.getElementById('txtCaptcha').value = '';
      document.getElementById('todo').style.display = "inline";
      document.getElementById('analisis').innerHTML = '<p align="center"><b><font color="red"><small>Clave incorrecta<small></font></b></p>';
      document.getElementById('clave').focus();
    } else if (res == "ERROR3") {
    	document.getElementById('error').style.display = "inline";
      document.getElementById('sec').innerHTML = '<img id="imgCaptcha" src="create_image.php?' + Math.random() + '" border=1>';
      document.getElementById('txtCaptcha').value = '';
      document.getElementById('todo').style.display = "inline";
      document.getElementById('analisis').innerHTML = '<p align="center"><b><font color="red"><small>IP Virtual no v&aacute;lida<small></font></b></p>';
      tinyMCE.activeEditor.focus();
    } else if (res == "ERROR4") {
    	document.getElementById('error').style.display = "inline";
      document.getElementById('sec').innerHTML = '<img id="imgCaptcha" src="create_image.php?' + Math.random() + '" border=1>';
      document.getElementById('txtCaptcha').value = '';
      document.getElementById('todo').style.display = "inline";
      document.getElementById('analisis').innerHTML = '<p align="center"><b><font color="red"><small>La IP Virtual resultante es demasiado larga. Su longitud es de ' + extra + ' caracteres y el maximo permitido es de 63 incluidos los colores<small></font></b></p>';
      tinyMCE.activeEditor.focus();
    } else if (res == "ERROR5") {
    	document.getElementById('error').style.display = "inline";
      document.getElementById('sec').innerHTML = '<img id="imgCaptcha" src="create_image.php?' + Math.random() + '" border=1>';
      document.getElementById('txtCaptcha').value = '';
      document.getElementById('todo').style.display = "inline";
      document.getElementById('analisis').innerHTML = '<p align="center"><b><font color="red"><small>No han pasado 24h desde el &uacute;ltimo cambio o activaci&oacute;n<br>El pr&oacute;ximo cambio se podr&aacute; realizar el ' + extra + '<small></font></b></p>';
      tinyMCE.activeEditor.focus();
    } else if (res == "ERROR6") {
    	document.getElementById('error').style.display = "inline";
      document.getElementById('sec').innerHTML = '<img id="imgCaptcha" src="create_image.php?' + Math.random() + '" border=1>';
      document.getElementById('txtCaptcha').value = '';
      document.getElementById('todo').style.display = "inline";
      document.getElementById('analisis').innerHTML = '<p align="center"><b><font color="red"><small>La IP Virtual especificada es la misma que tienes actualmente<small></font></b></p>';
      tinyMCE.activeEditor.focus();
    } else if (res == "ERROR7") {
    	document.getElementById('error').style.display = "inline";
      document.getElementById('sec').innerHTML = '<img id="imgCaptcha" src="create_image.php?' + Math.random() + '" border=1>';
      document.getElementById('txtCaptcha').value = '';
      document.getElementById('todo').style.display = "inline";
      document.getElementById('analisis').innerHTML = '<p align="center"><b><font color="red"><small>El nick especificado no est&aacute; registrado<small></font></b></p>';
      tinyMCE.activeEditor.focus();
    } else if (res == "ERROR8") {
      document.getElementById('error').style.display = "inline";
      document.getElementById('sec').innerHTML = '<img id="imgCaptcha" src="create_image.php?' + Math.random() + '" border=1>';
      document.getElementById('txtCaptcha').value = '';
      document.getElementById('todo').style.display = "inline";
      document.getElementById('analisis').innerHTML = '<p align="center"><b><font color="red"><small>Tu acceso al servicio est&aacute; suspendido<small></font></b></p>';
      tinyMCE.activeEditor.focus();
    } else if (res == "ERROR9") {
      document.getElementById('error').style.display = "inline";
      document.getElementById('sec').innerHTML = '<img id="imgCaptcha" src="create_image.php?' + Math.random() + '" border=1>';
      document.getElementById('txtCaptcha').value = '';
      document.getElementById('todo').style.display = "inline";
      document.getElementById('analisis').innerHTML = '<p align="center"><b><font color="red"><small>Servicio no disponible temporalmente, vuelva a intentarlo m&aacute;s tarde<small></font></b></p>';
      tinyMCE.activeEditor.focus();
    } else {
    	document.getElementById('error').style.display = "inline";
    	document.getElementById('sec').innerHTML = '<img id="imgCaptcha" src="create_image.php?' + Math.random() + '" border=1>';
    	document.getElementById('txtCaptcha').value = '';
    	document.getElementById('todo').style.display = "inline";
    	document.getElementById('analisis').innerHTML = '<p align="center"><b><font color="red"><small>' + data + '<small></font></b></p>';
    	tinyMCE.activeEditor.focus();
    }
    
 }
}

function procesar(formulario) {

 if (formulario.nick.value == '') {
   alert('Falta el nick');
   return false;
 }

 if (formulario.clave.value == '') {
   alert('Falta la clave');
   return false;
 }

 if (formulario.txtCaptcha.value == '') {
   alert('Falta el codigo de seguridad');
   return false;
 }

 var html = tinyMCE.activeEditor.getContent();
 if (html == '') {
   alert('Falta la IP virtual');
   return false;
 }

 var virtual = encode64(html);
 var n = encode64(formulario.nick.value);
 var c = encode64(formulario.clave.value);

 var url = 'procesar.php?nick=' + encodeURIComponent(n) + '&codigo=' + formulario.txtCaptcha.value + '&clave=' + encodeURIComponent(c) + '&ip=' + formulario.ip.value + '&virtual=' + encodeURIComponent(virtual);
 var postStr = '';
 document.getElementById('error').style.display = "none";
 document.getElementById('todo').style.display = "none";
 document.getElementById('procesando').style.display = "inline";
 makeRequest(url, postStr);
}


