function objetus () {
    try {
        objetus = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            objetus = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            objetus = false;
        }
    }
    if (!objetus && typeof XMLHttpRequest != 'undefined') {
        objetus = new XMLHttpRequest(); 
    }
    return objetus
}
var ajx = new objetus();

function get(id) {
         return document.getElementById(id);
}

function isEmail(email) {
         var result = false;
         var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
         if (filter.test(email)) result = true;
         return result;
}

function send() {
         var dForm = get("divForm");
         var inps = dForm.getElementsByTagName("input");
         var text = dForm.getElementsByTagName("textarea")[0];

         var nom = escape(inps[0].value);
         var pai = escape(inps[1].value);
         var ape = escape(inps[2].value);
         var ciu = escape(inps[3].value);
         var ema = escape(inps[4].value);
         var emp = escape(inps[5].value);
         var tel = escape(inps[6].value);
         var men = escape(text.value);

         if (nom == "" || pai == "" || ape == "" || ciu == "" || ema == "" || tel == "" || men == "") alert("Todos los campos marcados con asterisco (*) son obligatorios");
         else {
              if (!isEmail(ema)) {
                 inps[4].select();
                 alert("El E-mail ingresado no tiene un formato v\u00E1lido");
              }  else {

                 var url = "pross.php";
                 var snd = "send="+nom+"&pai="+pai+"&ape="+ape+"&ciu="+ciu+"&ema="+ema+"&emp="+emp+"&tel="+tel+"&men="+men;
                 //alert(url);
                 ajx.open("POST", url, true);

                 ajx.onreadystatechange = function() {
                     var cnt = "divForm";
                     if (ajx.readyState == 1) {
                        enab(cnt, false);
                     }
                     if (ajx.readyState == 4) {
                        var rst = ajx.responseText;
                        //alert(rst);
                        enab(cnt, true);
                        if (rst == "1") {
                           iClear(cnt);
                           alert("Se ha enviado correctamente el mensaje");
                        }  else {
                           alert("No se ha conseguido enviar el mensaje");
                        }
                     }
                 }
                 ajx.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                 ajx.send(snd);
              }
         }
}
function enab(cap, val) {
         var dForm = get(cap);
         var inps = dForm.getElementsByTagName("input");
         var txts = dForm.getElementsByTagName("textarea");

         for (var i = 0; i < inps.length; i ++) inps[i].disabled = !val;
         for (var i = 0; i < txts.length; i ++) txts[i].disabled = !val;
         
}
function iClear(cap) {
         var dForm = get(cap);
         var inps = dForm.getElementsByTagName("input");
         var txts = dForm.getElementsByTagName("textarea");

         for (var i = 0; i < inps.length; i ++) inps[i].value = "";
         for (var i = 0; i < txts.length; i ++) txts[i].value = "";
}
function getGal(dir) {
		 var car = get("hidCode").value.substr(0 , 3).toUpperCase();
		 var crp = dir.substr(0, 3).toUpperCase();
		 if (car != crp) window.frames[0].window.location = "galeria.php?gal="+dir;
         /*var url = "pross.php?getGal="+dir+"&rnd="+Math.random();
         //alert(url);
         ajx.open("GET", url, true);

         ajx.onreadystatechange = function() {
             if (ajx.readyState == 1) {

             }
             if (ajx.readyState == 4) {
                var rst = ajx.responseText;
                //alert(rst);
                get("divGal").innerHTML = rst;
                var fot = get("divGal").getElementsByTagName("img")[0];
                setFoto(fot);
             }
         }
         //ajx.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
         ajx.send(null);*/
}        
function getCode(src) {
         var ims = unescape(src).split("/");
         var img = ims[ims.length - 1];
         var cod = ims[ims.length - 2].substr(0, 3).toUpperCase();
         var ixs = img.split(".");
         var nom = ixs[0];
         if (parseInt(nom) < 10) nro = parseInt(nom.substr(1));
         else nro = parseInt(nom);
         if (isNaN(nro)) nro = parseInt(nom.substr(5));
         if (nro < 10) nro = "0" + nro;
         return cod + "11" +nro;
}
function iniLoa() {
	var ima = get("imgAmp");
	ima.className = 'loading';
	var pos = getPos(ima);
	var cap = get("loa");
	cap.style.left = pos.left + "px";
	cap.style.top = pos.top + "px";
	cap.style.width = ima.offsetWidth + "px";
	cap.style.height = ima.offsetHeight + "px";
	cap.style.visibility = 'visible';
}
function finLoa() {
	get("imgAmp").className = '';
	get("loa").style.visibility = 'hidden';
}
function setFoto(obj) {
         var src = obj.src;

         var pi = src.indexOf("img=") + 4;
         var pu = src.indexOf("&", pi);
         var img = src.substr(pi, pu - pi);
         //alert(img);
         var ima = "class.imagen.php?img="+img+"&tip=2&w=507&h=212";
         window.parent.window.iniLoa();

         var prt = window.parent.window.document;
         prt.getElementById("imgAmp").src = ima;

         var cod = getCode(img);

         prt.getElementById("hidCode").value = cod;
         prt.getElementById("spaCod").firstChild.nodeValue = cod;
}
function setOpac(obj, opa) {
         obj.style.filter = 'alpha(opacity='+opa+')';
         obj.style.opacity = opa / 100;
}
function getPos(element) {
         if (typeof element == "string") element = document.getElementById(element)
         if (!element) return {top:0,left:0 };

         var y = 0;
         var x = 0;
         while (element.offsetParent) {
               x += element.offsetLeft;
               y += element.offsetTop;
               element = element.offsetParent;
         }
         return {top:y,left:x};
}
function solCot(obj) {
		 closCaps();
         obj.className = 'sele';
         var pos = getPos(obj);
         var cap = get("cot");
         cap.style.left = pos.left + "px";
         if (getIE()) cap.style.left = (pos.left + 1) + "px";
         cap.style.top = (pos.top + obj.offsetHeight) + "px";
         cap.style.visibility = 'visible';

         get("cot").getElementsByTagName("input")[0].focus();
}
function getIE() {
         var nav = navigator.appName.toString();
         if (nav.indexOf("Explorer") != -1) return true;
         else return false;
}
function closeCap(cap) {
         var objCap = get(cap);
         objCap.style.visibility = 'hidden';

         var inps = objCap.getElementsByTagName("input");
         for (var i = 0; i < inps.length; i ++) inps[i].value = '';

         var txts = objCap.getElementsByTagName("textarea");
         for (var i = 0; i < txts.length; i ++) txts[i].value = '';

         var lnks = get("divInfo").getElementsByTagName("a");
         for (var i = 0; i < lnks.length; i ++) lnks[i].className = '';
}
function closCaps() {
		 var caps = ['cot', 'rec', 'bus'];
         for (var i = 0; i < caps.length; i ++)	get(caps[i]).style.visibility = 'hidden';
}
function sendCot() {
         var cod = get("hidCode").value;
         var src = get("imgAmp").src;

         var pi = src.indexOf("img=") + 4;
         var pu = src.indexOf("&", pi);
         var img = src.substr(pi, pu - pi);

         var inps = get("cot").getElementsByTagName("input");

         var nom = escape(inps[0].value);
         var ape = escape(inps[1].value);
         var tel = escape(inps[2].value);
         var cel = escape(inps[3].value);
         var ema = escape(inps[4].value);
         var dir = escape(inps[5].value);
         var cmt = escape(get("cot").getElementsByTagName("textarea")[0].value);

         if (nom == "" || ape == "" || tel == "" || cel == "" || ema == "" || dir == "" || cmt == "") alert("Todos los campos son requeridos");
         else {
              if (!isEmail(ema)) {
                 inps[4].select();
                 alert("El E-mail ingresado no tiene un formato v\u00E1lido");
              }  else {
                 var url = "pross.php";
                 var snd = "sendCot="+nom+"&ape="+ape+"&tel="+tel+"&cel="+cel+"&ema="+ema+"&cmt="+cmt+"&cod="+cod+"&img="+img;
                 //alert(snd);
                 ajx.open("POST", url, true);

                 ajx.onreadystatechange = function() {
                     var cnt = "cot";
                     if (ajx.readyState == 1) {
                        enab(cnt, false);
                     }
                     if (ajx.readyState == 4) {
                        var rst = ajx.responseText;
                        //alert(rst);
                        //document.write(rst);
                        enab(cnt, true);
                        if (rst == "1") {
                           closeCap(cnt);
                           alert("Se ha enviado correctamente la solicitud");
                        }  else {
                           alert("No se ha conseguido enviar la solicitud");
                        } 
                     }
                 }
                 ajx.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                 ajx.send(snd);
              }
         }

}
var tt = false;
var di = 0;
var mm = true;
var nin = 0;
function iniMove() {
         nin ++;
         if (mm) {
            window.clearInterval(tt);
            tt = window.setInterval("moving()", 10);
         }
}
function move(e) {
         if (mm) {
            var m = 252;
            if (e.clientX < m) di = 1;
            else di = -1;
         }
}

function moving() {
         if (mm) {
            var cap = get("divGale");

            try {
                lf = parseInt(cap.style.left);
            }   catch (e) {lf = -10;};
            if (isNaN(lf)) lf = -10;

            if ((lf + parseInt(cap.offsetWidth) >= 515 && di == -1) || (lf < -10 && di == 1)) {
               lf += di;
               cap.style.left = lf + "px";
            }
         }
         //window.parent.window.document.getElementById("divSay").innerHTML = mm;
}

function mStop() {
         mm = false;
         //window.parent.window.document.getElementById("divSay").innerHTML = mm;
}
function esc() {
         mm = true;
}
function showMap(map) {
	var cap = get("map");
	cap.innerHTML = "<img src=\"imgs/"+map+".jpg\" lowsrc=\"imgs/thumb-"+map+".jpg\" width=\"820\" height=\"582\" title=\"Haga click para cerrar\" onclick=\"this.parentNode.style.visibility = 'hidden'\" />";
	var lf = Math.round((parseInt(screen.width) - 828) / 2);
	cap.style.left = lf + 'px';
	cap.style.visibility = 'visible';
}
function reco(obj, opc) {
		 closCaps();
		 obj.className = 'sele';
         var pos = getPos(obj);
         var cap = get("rec");
         cap.style.left = pos.left + "px";
         if (getIE()) cap.style.left = (pos.left + 1) + "px";
         cap.style.top = (pos.top + obj.offsetHeight) + "px";
         cap.style.visibility = 'visible';

         get("hidOpc").value = opc;
         get("rec").getElementsByTagName("input")[0].focus();	
}

function sendReco() {
	var opc = get("hidOpc").value;
	var cod = get("hidCode").value;
	var inps = get("rec").getElementsByTagName("input");
	var tNom = inps[0].value;
	var tEma = inps[1].value;
	var sNom = inps[2].value;
	var sEma = inps[3].value;

	var img = get("imgAmp").src;
	var pi = img.indexOf("img=") + 4;
	var pf = img.indexOf("&", pi);
	img = img.substr(pi, pf - pi);
	
	if (tNom == "" || tEma == "" || sNom == "" || sEma == "") alert("Todos los campos son requeridos");
	else {
		rst = 0;
		if (!isEmail(tEma)) rst += 1;
		if (!isEmail(sEma)) rst += 2;
		
		if (rst == 3) {
			inps[1].select();
			alert("Ambos E-mails ingresados no tienen un formato v\u00E1lido");
		}	else if (rst == 2) {
			inps[3].select();
			alert("El E-mail ingresado en el campo \"Su E-mail\" no tiene un formato v\u00E1lido");
		}	else if (rst == 1) {
			inps[1].select();
			alert("El E-mail ingresado en el campo \"Tu E-mail\" no tiene un formato v\u00E1lido");
		}	else if (rst == 0) {
				 var url = "pross.php?reco="+opc+"&tNom="+tNom+"&tEma="+tEma+"&sNom="+sNom+"&sEma="+sEma+"&cod="+cod+"&img="+img+"&rnd="+Math.random();
				 //alert(snd);
				 ajx.open("GET", url, true);
	
				 ajx.onreadystatechange = function() {
				 	var cnt = "rec";
				 	if (ajx.readyState == 1) {
				 		enab(cnt, false);
				 	}
				 	if (ajx.readyState == 4) {
				 		var rst = ajx.responseText;
				 		//alert(rst);
				 		//document.write(rst);
				 		enab(cnt, true);
				 		if (rst == "1") {
				 			closeCap(cnt);
				 			alert("Se ha enviado correctamente la recomendaci\u00F3n");
				 		}  else {
				 			alert("No se ha conseguido enviar la recomendaci\u00F3n");
				 		}
				 	}
				 }
				 //ajx.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				 ajx.send(null);
		}
	}
	
}

function setMain(gal) {
	get("imgAmp").src = "class.imagen.php?img="+escape("catalogo/"+gal+"/01.jpg")+"&tip=2&w=507&h=212";
	iniLoa();
    var cod =  gal.substr(0, 3).toUpperCase() + "1101";
	get("hidCode").value = cod;
	get("spaCod").firstChild.nodeValue = cod;
}	

function find(obj) {
		 closCaps();
		 obj.className = 'sele';
         var pos = getPos(obj);
         var cap = get("bus");
         cap.style.left = pos.left + "px";
         if (getIE()) cap.style.left = (pos.left + 1) + "px";
         cap.style.top = (pos.top + obj.offsetHeight) + "px";
         cap.style.visibility = 'visible';

         cap.getElementsByTagName("input")[0].focus();	
}

function finding() {
	var inp = get("bus").getElementsByTagName("input")[0];
	var fnd = escape(inp.value);
	var car = fnd.substr(0, 3);
	var sep = fnd.substr(3, 2);
	var num = fnd.substr(5);
	
	if (fnd.length == 7 && isNaN(car) && !isNaN(num) && sep == "11") {
		
		var url = "pross.php?find="+car+"&num="+num+"&rnd="+Math.random();
		//alert(url);
		ajx.open("GET", url, true);
	
		ajx.onreadystatechange = function() {
			var cnt = "bus";
			if (ajx.readyState == 1) {
				enab(cnt, false);
			}
			if (ajx.readyState == 4) {
				enab(cnt, true);
				var rst = ajx.responseText;
				if (rst == "0") alert("No se ha encontrado el producto buscado");
				else {
					closeCap(cnt);
					var crp = get("hidCode").value.substr(0, 3);
					eval("var ars="+rst+";");
					if (crp.toUpperCase() != car.toUpperCase()) window.frames[0].window.location.replace("galeria.php?gal="+ars[0]+"&nor");
					get("imgAmp").src = "class.imagen.php?img="+escape(ars[1])+"&tip=2&w=507&h=212&rnd="+Math.random();
					iniLoa();
					fnd = fnd.toUpperCase();
					get("spaCod").firstChild.nodeValue = fnd;
					get("hidCode").value = fnd;
					alert("Se ha encontrado el producto buscado");
				}
			}
		}
		//ajx.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajx.send(null);
	}	else {
		inp.select();
		alert("El c\u00F3digo ingresado no tiene un formato v\u00E1lido");
	}
}