function godoc(doc) {
	document.location.href = doc;
}

function checkforblanks() {
	for (var i=0; i<arguments.length; i+=2) {
		if (!arguments[i]) {
			alert("El campo " + arguments[i+1] + " es obligatorio.");
			return false;
		}
	}
	return true;
}

function valida() {
	var isFull = checkforblanks(document.getElementById("nombre").value, "[Nombre]",
	document.getElementById("telefono").value, "[Teléfono]",
	document.getElementById("email").value, "[E-mail]",
	document.getElementById("fechaE").value, "[Fecha de entrada]",
	document.getElementById("fechaS").value, "[Fecha de salida]");
	if (!isFull) {
		return false;
	}
	var dirmail = document.getElementById("email").value;
	var filter = /^.+@.+\..{2,4}$/;
	if (!filter.test(dirmail)) {
		alert("La dirección de correo electrónico no es correcta.");
		return false;
	}
}

function validate2() {
	var isFull = checkforblanks(document.getElementById("nombre").value, "[Nombre]",
	document.getElementById("email").value, "[E-Mail]",
	document.getElementById("asunto").value, "[Asunto]",
	document.getElementById("comentario").value, "[Comentario]");
	if (!isFull) {
		return false;
	}
	var dirmail = document.getElementById("email").value;
	var filter = /^.+@.+\..{2,4}$/;
	if (!filter.test(dirmail)) {
		alert("La dirección de correo electrónico no es correcta.");
		return false;
	}
	if(document.getElementById("avlegal").checked==false) {
		alert("Debe leer y aceptar el aviso legal");
		return false;
	}
}

function valida3() {
	var isFull = checkforblanks(document.getElementById("nombre").value, "[Nombre]",
	document.getElementById("telefono").value, "[Teléfono]",
	document.getElementById("email").value, "[E-mail]",
	document.getElementById("ccnum").value, "[Número de tarjeta]",
	document.getElementById("ccmonth").value, "[Mes de caducidad]",
	document.getElementById("ccyear").value, "[Año de caducidad]");
	if (!isFull) {
		return false;
	}
	var dirmail = document.getElementById("email").value;
	var filter = /^.+@.+\..{2,4}$/;
	if (!filter.test(dirmail)) {
		alert("La dirección de correo electrónico no es correcta.");
		return false;
	}
}

function MailLink (texto,usuario,dominio,tld) {
	var arroba = '@';
	var punto = '.';
	var etiqueta = 'ma' + '' + 'il';
	var dospuntos = 'to:';
	var localizador = usuario;
	localizador = localizador + arroba + dominio;
	localizador = localizador + punto + tld;
	if(texto=='') {
		texto = localizador;
	}
	document.write('<a href="' + etiqueta + dospuntos + localizador + '">' + texto + '</a>');
}

function fCapa(id) {
	if (document.getElementById(id).style.display=='block') {
		document.getElementById(id).style.display='none';
	} else {
		document.getElementById(id).style.display='block';
	}
}

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

function showDark() {
	document.getElementById("dark").style.display = 'inline';
	document.getElementById("dark").style.height = getDocHeight()+'px';
}

function showVideo(parametro) {
	var strVideo = '';
	strVideo = strVideo + '<object width="494" height="400">';
	strVideo = strVideo +  '<param name="movie" value="http://www.youtube.com/v/'+parametro+'&color1=0xb1b1b1&color2=0xcfcfcf&hl=en_US&feature=player_embedded&fs=1&rel=0"></param>';
	strVideo = strVideo +  '<param name="allowFullScreen" value="true"></param>';
	strVideo = strVideo +  '<param name="allowScriptAccess" value="always"></param>';
	strVideo = strVideo +  '<embed src="http://www.youtube.com/v/'+parametro+'&color1=0xb1b1b1&color2=0xcfcfcf&hl=en_US&feature=player_embedded&fs=1&rel=0" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="494" height="324"></embed>';
	strVideo = strVideo +  '</object>';
	strVideo = strVideo + '<p><a href="javascript:hideVideo()">CERRAR</a></p>';
	document.getElementById("marcovideo").innerHTML = strVideo;
	document.getElementById("marcovideo").style.display = 'inline';

	var windowY = f_clientHeight();
	var scrollY = f_scrollTop();
	var divY = document.getElementById("marcovideo").offsetHeight;
	var topY = parseInt(windowY/2)-parseInt(divY/2)+scrollY;
	var strY = topY+'px';

	var windowX = f_clientWidth();
	var scrollX = f_scrollLeft();
	var divX = document.getElementById("marcovideo").offsetWidth;
	var leftX = parseInt(windowX/2)-parseInt(divX/2);
	var strX = leftX+'px';

	document.getElementById("marcovideo").style.top = strY;
	document.getElementById("marcovideo").style.left = strX;
	//showDark();
}

function showFoto(n,w,h) {
	var strImg = "catalogo/raelma-"+n+'.jpg';
	var newHtml = '<a href="javascript:hideFoto()"><img class="galeria" width="'+w+'" height='+h+'" src="'+strImg+'" alt="cerrar imagen" /></a>';
	newHtml = newHtml + '<a href="javascript:hideFoto()">CERRAR</a>';
	document.getElementById("marcofoto").innerHTML = newHtml;
	document.getElementById("marcofoto").style.display = 'inline';
	
	var windowY = f_clientHeight();
	var scrollY = f_scrollTop();
	var divY = document.getElementById("marcofoto").offsetHeight;
	var topY = parseInt(windowY/2)-parseInt(divY/2)+scrollY;
	var strY = topY+'px';

	var windowX = f_clientWidth();
	var scrollX = f_scrollLeft();
	var divX = document.getElementById("marcofoto").offsetWidth;
	var leftX = parseInt(windowX/2)-parseInt(divX/2);
	var strX = leftX+'px';

	document.getElementById("marcofoto").style.top = strY;
	document.getElementById("marcofoto").style.left = strX;
	//showDark();
}

function hideFoto(dato) {
	document.getElementById("marcofoto").innerHTML = '';
	document.getElementById("marcofoto").style.display = 'none';
	//document.getElementById("dark").style.display = 'none';
}

function hideVideo(dato) {
	document.getElementById("marcovideo").innerHTML = '';
	document.getElementById("marcovideo").style.display = 'none';
	//document.getElementById("dark").style.display = 'none';
}

function htags() {
	var t1 = 'ta';
	var t2 = 'gs';
	var t3 = t1+t2;
	document.getElementById(t3).style.display = 'none';
}

function getRadio(campo) {
	var strRadio = '';
	var radioButtons = document.getElementsByName(campo);
	for (var x=0; x<radioButtons.length; x ++) {
		if (radioButtons[x].checked) {
			strRadio = radioButtons[x].value;
		}
	}
	return strRadio;
}

function loadUrl(strurl,strdest) {
	ajaxurl = strurl;
	ajaxtarget = strdest;
	if (window.XMLHttpRequest) { // code for Mozilla, etc.
		xmlhttp=new XMLHttpRequest();
		xmlhttp.onreadystatechange=xmlhttpChange;
		xmlhttp.open("GET",ajaxurl,true);
		xmlhttp.send(null);
	}
	else if (window.ActiveXObject) { // code for IE
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		if (xmlhttp) {
			xmlhttp.onreadystatechange=xmlhttpChange;
			xmlhttp.open("GET",ajaxurl,true);
			xmlhttp.send();
		}
	}
}

function xmlhttpChange() {
	var nuevoTexto = "";
	if (xmlhttp.readyState==4) {
		if (xmlhttp.status==200) {
			if (xmlhttp.responseText!="") {
				nuevoTexto = xmlhttp.responseText;
				document.getElementById(ajaxtarget).innerHTML = URLDecode(nuevoTexto);
			}
		} else {
			alert("Ha ocurrido un error al intentar recuperar los datos.");
		}
	}
}

function URLEncode(strEncode) {
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = strEncode;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
	return false;
}

function URLDecode(strDecode) {
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = strDecode;
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return plaintext;
   return false;
}
