// redirectJavaScript Document
var READY_STATE_COMPLETE=4;
var peticion_http = null;
 
function inicializa_xhr() {
  if(window.XMLHttpRequest) {
    return new XMLHttpRequest(); 
  }
  else if(window.ActiveXObject) {
    return new ActiveXObject("Microsoft.XMLHTTP");
  } 
}
function validar(){
	var ing=validador();
	if(ing==true){
		envia();
		
		
	}
} 
function crea_query_string() {
	var id = document.getElementById("id");
  	var tit = document.getElementById("tit");
  	var des = document.getElementById("des");
  	var ima = document.getElementById("ima");
	var ope = document.getElementById("ope");
  	return "id=" + encodeURIComponent(id.value) +
	"&tit=" + encodeURIComponent(tit.value) +
         "&des=" + encodeURIComponent(des.value) +
         "&ima=" + encodeURIComponent(ima.value) +
		 "&ope=" + encodeURIComponent(ope.value) +
         "&nocache=" + Math.random();
}
 
function envia() {
  peticion_http = inicializa_xhr();
  if(peticion_http) {
    peticion_http.onreadystatechange = procesaFormulario;
    peticion_http.open("POST", "process.php", true);
 
    peticion_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    var query_string = crea_query_string();
    peticion_http.send(query_string);
  }
}
function descargaFormulario(ruta,ope,id){
   peticion_http = inicializa_xhr();
  if(peticion_http) {
    peticion_http.onreadystatechange = procesaRespuesta;
    peticion_http.open("POST", "form.php", true);
 
    peticion_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    peticion_http.send("id="+id+"&ope="+ope);
  }
}
function eliminar(ope,id){
   peticion_http = inicializa_xhr();
  if(peticion_http) {
    peticion_http.onreadystatechange = procesaFormulario;
    peticion_http.open("POST", "process.php", true);
 
    peticion_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    peticion_http.send("id="+id+"&ope="+ope);
  }
}
function paginar(pag){
   peticion_http = inicializa_xhr();
  if(peticion_http) {
    peticion_http.onreadystatechange = procesaRespuesta;
    peticion_http.open("POST", "consulta.php", true);
 
    peticion_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    peticion_http.send("ini="+pag+"&ope=1");
  }
}
//Funcion que realiza la peticion para el llamado de solo contenido
function descargaPagina(ruta){
  if (peticion_http == null) {
    if (window.XMLHttpRequest) {
      peticion_http = new XMLHttpRequest();
    }
    else {
      peticion_http = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  else {
    peticion_http.abort();
  }
  
  midletime('left_content');
  peticion_http.onreadystatechange = procesaRespuesta;
  
  peticion_http.open('POST', ruta+"&nocache=" + Math.random(), true);
  peticion_http.send(null);
}

//Funcion que agrega una imagen mientras carga el contenido
function midletime(div){
	div = document.getElementById('page').getElementsByTagName("div");
	div[1].innerHTML= '<div align="center"><img  src="images/loader.gif"></div>';
}
function descargaPagina2(ruta){
  if (peticion_http == null) {
    if (window.XMLHttpRequest) {
      peticion_http = new XMLHttpRequest();
    }
    else {
      peticion_http = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  else {
    peticion_http.abort();
  }
  
  midletime2('apDiv1');
  peticion_http.onreadystatechange = procesaRespuesta2;
  
  peticion_http.open('POST', ruta+"&nocache=" + Math.random(), true);
  peticion_http.send(null);
}
//Funcion que agrega una imagen mientras carga el contenido
function midletime2(div){
	
	div.innerHTML= '<div align="center"><img  src="images/loader.gif"></div>';
}
function descargaPagina3(ruta){
  if (peticion_http == null) {
    if (window.XMLHttpRequest) {
      peticion_http = new XMLHttpRequest();
    }
    else {
      peticion_http = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  else {
    peticion_http.abort();
  }
  
  midletime2('apDiv1');
  peticion_http.onreadystatechange = procesaRespuesta3;
  
  peticion_http.open('POST', ruta+"&nocache=" + Math.random(), true);
  peticion_http.send(null);
}

function procesaRespuesta() {
  if(peticion_http.readyState == READY_STATE_COMPLETE) {
    if(peticion_http.status == 200) {
      div = document.getElementById('page').getElementsByTagName("div");
	div[1].innerHTML = peticion_http.responseText;
    }
  }
}
function procesaRespuesta2() {
  if(peticion_http.readyState == READY_STATE_COMPLETE) {
    if(peticion_http.status == 200) {
    divResultado = document.getElementById('apDiv1');
	divResultado.innerHTML = peticion_http.responseText;
    }
  }
}
function procesaRespuesta3() {
  if(peticion_http.readyState == READY_STATE_COMPLETE) {
    if(peticion_http.status == 200) {
		
      div = document.getElementById('page').getElementsByTagName("div");
	div[1].innerHTML = peticion_http.responseText;
	descargaPagina2('log2/index.php?query=0');
	
    }
  }
}
function procesaFormulario() {
  if(peticion_http.readyState == READY_STATE_COMPLETE) {
    if(peticion_http.status == 200) {
      var res = peticion_http.responseText;
	  if(res=true){
		  descargaPagina('consulta.php');
	  }else{
		  document.getElementById("cargador").innerHTML = "Operacion Fallida";
		  setTimeout("descargaPagina('consulta.php')",1000);
	  }
    }
  }
}
function validador() {
  var tit = document.getElementById('client').tit.value;
  var des = document.getElementById('client').des.value;
  var ima = document.getElementById('client').ima.value;
  var nomRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emaRegex= /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/;
   if(tit == "") {
    inlineMsg('tit','Campo Obligatorio.',5);
    return false;
  }
  if(des == "") {
    inlineMsg('des','Campo Obligatorio.',5);
    return false;
  }

  
  if(ima == "") {
    inlineMsg('ima','Campo Obligatorio.',5);
    return false;
  }
   return true;
}

// START OF MESSAGE SCRIPT //

var MSGTIMER = 20;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 3;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {
  var msg;
  var msgcontent;
  if(!document.getElementById('msg')) {
    msg = document.createElement('div');
    msg.id = 'msg';
    msgcontent = document.createElement('div');
    msgcontent.id = 'msgcontent';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter = 'alpha(opacity=0)';
    msg.style.opacity = 0;
    msg.alpha = 0;
  } else {
    msg = document.getElementById('msg');
    msgcontent = document.getElementById('msgcontent');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;
  var targetdiv = document.getElementById(target);
  targetdiv.focus();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  msg.style.top = topposition + 'px';
  msg.style.left = leftposition + 'px';
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
    autohide = MSGHIDE;  
  }
  window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
  var msg = document.getElementById('msg');
  if(!msg.timer) {
    msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  }
}

// face the message box //
function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
  var top = 0;
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}

// preload the arrow //
if(document.images) {
  arrow = new Image(7,80); 
  arrow.src = "../../imagenes/msg_arrow.gif"; 
}
