/**
 * @author vicente
 */
function remoteAddress(){
	var oAjax = new objetoAjax();
	var cRespuesta = "";
	oAjax.open("POST","/boton.vfp?c_accion=ip",false);
	oAjax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	oAjax.send(null);
	var oJSON = eval("("+oAjax.responseText+")");
	var oCelda = null;
	for(x in oJSON){
		oCelda = document.getElementById(x);
		if (oCelda){
			oCelda.innerHTML=oJSON[x];
		}
	}
	oDiv = document.getElementById("msgContacto");
	centrarDiv(oDiv);
}

/**
 * Llena el formulario de envío 
 */
function boton(oJSON){
	var oDiv = document.getElementById("msgContacto");
	
	// Sombrita
	shadow("trans");
	
	// Propiedades de la ventana modal
	oDiv.style.position="absolute";
	oDiv.style.display="inline";
	centrarDiv(oDiv);
	remoteAddress();
	
	// Datos que identifican al destinatario
	document.getElementById("msgPara").innerHTML=oJSON["msgPara"];
	document.getElementById("msgTo").value=oJSON["msgTo"];
	document.getElementById("msgCcc").value=oJSON["msgCcc"];
	document.getElementById("msgPreMsg").value=oJSON["msgPreMsg"];
	document.getElementById("msgPreMsgV").innerHTML="<small>"+oJSON["msgPreMsg"]+"</small>";
	document.getElementById("msgRefe").value=oJSON["msgRefe"];
	document.getElementById("msgList").value=oJSON["msgList"];
	document.getElementById("msgOrigen").value=oJSON["msgOrigen"];
	document.getElementById("msgNu").value=oJSON["msgNu"];
}

/**
 * Envia los datos del formulario modal al servidor
 */
function mandalo(oForm){
	var oDiv = document.getElementById("msgContacto");
	var cQString = serializeForm(document.getElementById("msgForma"));
	var oAjax = new objetoAjax();
	var cRespuesta = "";
	oDiv.style.display="none";
	oAjax.open("POST","/boton.vfp",true);
	oAjax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	oAjax.onreadystatechange=function(){
		if (oAjax.readyState==4 && oAjax.status==200){
			cRespuesta = oAjax.responseText;
			cancelar(oForm);
		}
	}
	oAjax.send(cQString);
}

/**
 * Envia los datos del formulario modal al servidor
 */
function cancelar(oForm){
	var oDiv = document.getElementById("msgContacto");
	oDiv.style.display="none";
	noShadow("trans");
	oForm.reset();
}

/**
 * Se encarga de centrar un objeto Div (pero puede funcionar con otros objetos)
 */
function centrarDiv(divObj){
	var ie=document.all && !window.opera;
	var dom=document.getElementById;
	standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body
	var scroll_top=(ie)? standardbody.scrollTop : window.pageYOffset;
	var scroll_left=(ie)? standardbody.scrollLeft : window.pageXOffset;
	var docwidth=(ie)? standardbody.clientWidth : window.innerWidth-16;
	var docheight=(ie)? standardbody.clientHeight: window.innerHeight;
	var docheightcomplete=(standardbody.offsetHeight>standardbody.scrollHeight)? standardbody.offsetHeight : standardbody.scrollHeight;
	var objwidth=divObj.offsetWidth;
	var objheight=divObj.offsetHeight;
	var topposition=(docheight>objheight)? scroll_top+docheight/2-objheight/2+"px" : scroll_top+"px";
	divObj.style.left=docwidth/2-objwidth/2+"px";
	divObj.style.top=Math.floor(parseInt(topposition))+"px";
}

/**
 * Devuelve el objeto que permite hacer AJAX
 */
function objetoAjax() {	
	var xmlhttp=false;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
		   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
			xmlhttp = false;
  		}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

/**
 * Serialize a form into a format which can be sent as a GET string or a POST 
 * content.It correctly ignores disabled fields, maintains order of the fields 
 * as in the elements[] array. The 'file' input type is not supported, as 
 * its content is not available to javascript. This method is used internally
 * by the submit class method.
 * 
 * MODIFICADA PARA "SERIALIZAR" VALORES EN BASE64
 */
function serializeForm(theform) {
	var els = theform.elements;
	var len = els.length;
	var queryString = "";
	//var oCode = new codificar();
	
	for (var i=0; i<len; i++) {
		var el = els[i];
		if (!el.disabled) {
			switch(el.type) {
				case 'text': case 'password': case 'hidden': case 'textarea':
					if (queryString.length>0) { 
						queryString += "&";
					}
					//queryString += el.name + "=" + encodeURIComponent(el.value);
					//queryString += el.name + "=" + oCode.encode64(el.value);
					queryString += el.name + "=" + escape(el.value);
					break;
				case 'select-one':
					if (el.selectedIndex>=0) {
						if (queryString.length>0) { 
							queryString += "&";
						}
						//queryString += el.name + "=" + encodeURIComponent(el.options[el.selectedIndex].value);
						//queryString += el.name + "=" + oCode.encode64(el.options[el.selectedIndex].value);
						queryString += el.name + "=" + escape(el.options[el.selectedIndex].value);
					}
					break;
				case 'select-multiple':
					for (var j=0; j<el.options.length; j++) {
						if (el.options[j].selected) {
							if (queryString.length>0) { 
								queryString += "&";
							}
							//queryString += el.name + "=" + encodeURIComponent(el.options[j].value);
							//queryString += el.name + "=" + oCode.encode64(el.options[j].value);
							queryString += el.name + "=" + escape(el.options[j].value);
						}
					}
					break;
				case 'checkbox': case 'radio':
					if (el.checked) {
						if (queryString.length>0) {
							queryString += "&";
						}
						//queryString += el.name + "=" + encodeURIComponent(el.value);
						//queryString += el.name + "=" + oCode.encode64(el.value);
						queryString += el.name + "=" + escape(el.value);
					}
					break;
			}
		}
	}
	return queryString;
}

// Para codificar - decodificar mis datos
function codificar(){
	this.keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	
	// Codifica una cadena a base64
	this.encode64 = function(input){
		// This code was written by Tyler Akins and has been placed in the
		// public domain.  It would be nice if you left this header intact.
		// Base64 code from Tyler Akins -- http://rumkin.com
	    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 + this.keyStr.charAt(enc1) + this.keyStr.charAt(enc2) + 
	         this.keyStr.charAt(enc3) + this.keyStr.charAt(enc4);
	    } while (i < input.length);
	    
	    return output;
	}
	
	// Decodifica una cadena en base64
	this.decode64 = function(input){
		// This code was written by Tyler Akins and has been placed in the
		// public domain.  It would be nice if you left this header intact.
		// Base64 code from Tyler Akins -- http://rumkin.com
	    var output = "";
	    var chr1, chr2, chr3;
	    var enc1, enc2, enc3, enc4;
	    var i = 0;  
	
	    // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
	    input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
	
	    do {
	       enc1 = this.keyStr.indexOf(input.charAt(i++));
	       enc2 = this.keyStr.indexOf(input.charAt(i++));
	       enc3 = this.keyStr.indexOf(input.charAt(i++));
	       enc4 = this.keyStr.indexOf(input.charAt(i++));
	
	       chr1 = (enc1 << 2) | (enc2 >> 4);
	       chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
	       chr3 = ((enc3 & 3) << 6) | enc4;
	
	       output = output + String.fromCharCode(chr1);
	
	       if (enc3 != 64) {
	          output = output + String.fromCharCode(chr2);
	       }
	       if (enc4 != 64) {
	          output = output + String.fromCharCode(chr3);
	       }
	    } while (i < input.length);
	
	    return output;
	}
}

function shadow(cIdDiv){
	var trans=document.getElementById(cIdDiv);
	var ie=document.all && !window.opera;
	var dom=document.getElementById;
	standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body
	var docwidth=(ie)? standardbody.clientWidth : window.innerWidth-16;
	var docheight=(ie)? standardbody.clientHeight: window.innerHeight;
	var docheightcomplete=(standardbody.offsetHeight>standardbody.scrollHeight)? standardbody.offsetHeight : standardbody.scrollHeight;	
	trans.style.width=docwidth+"px";
	trans.style.height=docheightcomplete+"px";
	trans.style.visibility="visible";
}

function noShadow(cIdDiv){
	var trans=document.getElementById(cIdDiv);
	var ie=document.all && !window.opera;
	var dom=document.getElementById;
	standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body
	var docwidth=(ie)? standardbody.clientWidth : window.innerWidth-16;
	var docheight=(ie)? standardbody.clientHeight: window.innerHeight;
	var docheightcomplete=(standardbody.offsetHeight>standardbody.scrollHeight)? standardbody.offsetHeight : standardbody.scrollHeight;	
	trans.style.width=docwidth+"px";
	trans.style.height=docheightcomplete+"px";
	trans.style.visibility="hidden";
}