// JavaScript Document

//Jquery - Precargar Imagenes
jQuery.preloadImages = function()
{
	var actual = 0;
	var imgs = new Array();
	for(var i = 0; i<arguments.length; i++){
		//alert(arguments[i]);
		imgs[i] = new Image(1,1);
		imgs[i].src = arguments[i];
		imgs[i].onload=function(){
			//actual++
			//alert(arguments.length);
		}
		//jQuery("<img>").attr("src", arguments[i]);
		//$("#pre_img").append("<img src='"+arguments[i]+"' width='1' />");
	}
}

var MsjTipico = "<center><strong>Cargando...</strong><br /><img width='150' src='"+ABPATH+"images/loading.gif' alt=''></center>";
function AJAXPOST(Pagina, Variables,Obj, MsjLoad, FuncionListo, FuncionCarga, Conexion){
	if (MsjLoad == null || MsjLoad == false){MsjLoad= MsjTipico;}
	if (FuncionCarga == null){if (Obj == null){Obj= false;}else{Obj.innerHTML = MsjLoad;}}
	var Conexion = crearXMLHttpRequest();
	Conexion.open("POST",Pagina, true);
	Conexion.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	Conexion.send("ajx=1&"+Variables);
	
	Conexion.onreadystatechange = ProcesarCambioEstado;
		function ProcesarCambioEstado(){
			if (Conexion.readyState == 4){
				if(Obj != false){Obj.innerHTML = Conexion.responseText;}
				if (FuncionListo != null){
					x = FuncionListo;
					x(Conexion);
				}
			}else{
				if (FuncionCarga == null){
					if(Obj != false){Obj.innerHTML = MsjLoad;}
				}else{
					x = FuncionCarga;
					x(Conexion);
				}
			}
		}
	return Conexion;
}

function SoloEnviar(Pagina, Variables,Callback){
	var Conexion = crearXMLHttpRequest();
	Conexion.open("POST",Pagina, true);
	Conexion.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	Conexion.send("ajx=1&"+Variables);
	Conexion.onreadystatechange = function(){
		if(Conexion.readyState == 4){
			if(Callback != null){
				x = Callback;
				x(Conexion);
			}
		}
	}

}

function crearXMLHttpRequest() 
{
  var xmlHttp=null;
  if (window.ActiveXObject) 
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  else 
    if (window.XMLHttpRequest) 
      xmlHttp = new XMLHttpRequest();
  return xmlHttp;
}

function str_replace (search, replace, subject, count) {
    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }
 
    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
}
