
//*******FUNCIONES AJAX**********************************************************************************

/*********************************************************************************************
  Función llamarAJAXpost:
	funcion para mandar llamar el AJAX con post
  Parámetros:
	pagina = La página que sera leida por el AJAX
	formid = el ID del formulario
*********************************************************************************************/
function llamarAJAXpost(pagina, formid){
	document.getElementById('atras').style.display = 'none';
	document.getElementById('contenido').style.display = 'none';
	postDataReturnText(pagina, mostrar, formid);
}

/*********************************************************************************************
  Función llamarAJAX:
	funcion para mandar llamar el AJAX
  Parámetros:
	pagina = La página que sera leida por el AJAX
*********************************************************************************************/
function llamarAJAX(pagina){
	document.getElementById('atras').style.display = 'none';
	//document.getElementById('loading').style.display = 'block';
	
	//document.getElementById('contenido').style.overflow="hidden";
	document.getElementById('contenido').className="div_contenido_de";
	
	document.getElementById('contenido').style.display = 'none';
	quita_menu_foto();
	getDataReturnText(pagina, mostrar);
}

/*********************************************************************************************
  Función carrito:
	funcion para mandar llamar el AJAX
  Parámetros:
	pagina = La página que sera leida por el AJAX
*********************************************************************************************/
function elcarrito(pagina){
	document.getElementById('carrito').style.display = 'none';
	//new Effect.toggle($('carrito'),'appear');
	getDataReturnText(pagina, muestracarrito);
}
function muestracarrito(text){
	with(document.getElementById('carrito')){
		style.display = 'none';
		innerHTML = text;
	}
	new Effect.toggle($('carrito'),'appear');
}
function carrito_agrega(){
	fun_precio = document.getElementById('precio_total').value;
	fun_cantidad = document.getElementById('cantidad').value;
	fun_codigo = document.getElementById('codigo').value;
	fun_color = document.getElementById('elcolores').value;
	fun_textura = document.getElementById('latexturaes').value;
	fun_textil = document.getElementById('eltextiles').value;
	fun_adicional = document.getElementById('adicional_nombre').value;
	fun_nombre = document.getElementById('nombre').value;
	fun_envio = document.getElementById('envio_oculto').value;
	pagina = "/plantilla_carrito.php?funcion=agregar&precio=" + fun_precio + "&cantidad=" + fun_cantidad + "&codigo=" + fun_codigo + "&color=" + fun_color + "&textura=" + fun_textura + "&textil=" + fun_textil + "&adicional=" + fun_adicional + "&nombre=" + fun_nombre + "&envio=" + fun_envio;
	elcarrito(pagina);
}
function carrito_actualizar(id,cantidad){
	contenido = "<input type='text' name='" + id + "' id='" + id + "' value='" + cantidad + "' style='width:15px; height:15px;' onblur='javascript:carrito_actualizar_submit(this.value, this.name);' onkeypress='checkEnter(event, this.value, this.name);'>";
	document.getElementById(id).innerHTML = contenido;
}
function carrito_actualizar_submit(cantidad,id){
	pagina = "/plantilla_carrito.php?funcion=editar&codigo=" + id + "&cantidad=" + cantidad;
	elcarrito(pagina);	
}

/*********************************************************************************************
  Función mostrar:
	funcion para Mostrar el contenido traido por el AJAX. 
  Parámetros:
	text = El texto que trajo el AJAX.
*********************************************************************************************/ 
function mostrar(text){
	with(document.getElementById('contenido')){
		style.display = 'none';
		innerHTML = text;
	}
	new Effect.toggle($('contenido'),'appear');
	//document.getElementById('loading').style.display = 'none';
}


/*********************************************************************************************
  Función color:
	funcion para Mostrar el color o la textura en grande traido por el AJAX. 
  Parámetros:
	text = El texto que trajo el AJAX.
*********************************************************************************************/ 
function llamarCOLOR(pagina){
	document.getElementById('bandera_mouse').value = 0;
	document.getElementById('descripcion').style.visibility = 'hidden';
	document.getElementById('descripcion').style.display = 'none';
	getDataReturnText(pagina, color);
	document.onmousemove = follow;
}
function color(text){
	with(document.getElementById('descripcion')){
		innerHTML = text;
		style.visibility = 'visible';
		style.display = 'block';
	}
	//new Effect.toggle($('descripcion'),'appear');
	//document.getElementById('loading').style.display = 'none';
}
/*********************************************************************************************
  Funcion foto:
	funcion para Mostrar foto en grande traido por el AJAX. 
  Parámetros:
	text = El texto que trajo el AJAX.
*********************************************************************************************/ 
function foto(){
	var pagina = "/foto_grande.php?id=" + document.getElementById('imagenAagrandar').value;
	document.getElementById('bandera_mouse').value = 0;
	document.getElementById('descripcion').style.visibility = 'hidden';
	document.getElementById('descripcion').style.display = 'none';
	document.getElementById('descripcion').style.left = '50%';
	document.getElementById('descripcion').style.top = '157px';
	document.getElementById('descripcion').style.marginLeft = '-320px';
	getDataReturnText(pagina, color);
}
/*********************************************************************************************
  Funcion fotonueva:
	funcion para Mostrar foto en grande traido por el AJAX. 
  Parámetros:
	text = El texto que trajo el AJAX.
*********************************************************************************************/ 
function fotonueva(){
	var pagina = "/images/productos/grandes/" + document.getElementById('imagenAagrandar').value + '.jpg';
	return pagina;
}

function LightboxDelegate(url,caption) {
   var objLink = document.createElement('a');
   objLink.setAttribute('href',url);
   objLink.setAttribute('rel','lightbox');
   objLink.setAttribute('title',caption);
   Lightbox.prototype.start(objLink);
}


/*********************************************************************************************
  Función cambia_imagen:
	funcion para reemplazar una imagen por otra. 
*********************************************************************************************/ 
function cambia_imagen(img_nombre,img_src,elid) {
document[img_nombre].src=img_src;
document.getElementById('imagenAagrandar').value = elid;
document.getElementById('zoomes').innerHTML = '<a href="/images/productos/grandes/' + elid + '_.jpg" rel="lightbox" style="text-decoration:none; color:#383838;"><img src="/images/zoom.jpg" width="18" height="19" border="0" style="vertical-align:middle;"><span style="font-weight:normal; line-height:19px; color:#383838; text-decoration:none;"> zoom</span></a>';
}

/*********************************************************************************************
  Función color:
	funcion para reemplazar el color y asignarlo a un hidden. 
*********************************************************************************************/ 
function colorselecciona(ruta,id,nombre) {
	contenido = '<a href="javascript:void(\'\');" onmouseover="llamarCOLOR(\'/color.php?idcolor=' + id + '&nombrecolor=' + nombre + '\')" onmouseout="detiene()"><img src="' + ruta +'" name="colorseleccionado" border="0" width="25" height="25"></a><input name="elcolores" id="elcolores" value="' + id + '" type="hidden">';
document.getElementById('colorsel').innerHTML = contenido;
}

/*********************************************************************************************
 Función textilselecciona:
	funcion para reemplazar el textil y asignarlo a un hidden. 
*********************************************************************************************/ 
function textilselecciona(ruta,id,nombre) {
	contenido = '<a href="javascript:void(\'\');" onmouseover="llamarCOLOR(\'/textil.php?idtextil=' + id + '&nombretextil=' + nombre + '\')" onmouseout="detiene()"><img src="' + ruta +'" name="textilseleccionado" border="0" width="25" height="25"></a><input name="eltextiles" id="eltextiles" value="' + id + '" type="hidden">';
document.getElementById('textilsel').innerHTML = contenido;
}

/*********************************************************************************************
  Función textura:
	funcion para reemplazar el color y asignarlo a un hidden. 
*********************************************************************************************/ 
function textura(ruta,id,nombre) {
	contenido = '<a href="javascript:void(\'\');" onmouseover="llamarCOLOR(\'/texturas.php?idtextura=' + id + '&nombretextura=' + nombre + '\')" onmouseout="detiene()"><img src="' + ruta +'" name="texturaseleccionada" border="0" width="25" height="25"></a><input name="latexturaes" id="latexturaes" value="' + id + '" type="hidden">';
document.getElementById('texturasel').innerHTML = contenido;
}

/*********************************************************************************************
  Función imagengrande:
	funcion para mostrar imagen grande. 
*********************************************************************************************/ 
function imagengrande(ruta) {
	contenido = '<img src="' + ruta +'" name="fotogrande" id="fotogrande" border="0" width="25" height="25"></a><input name="latexturaes" id="latexturaes" value="' + id + '" type="hidden">';
document.getElementById('texturasel').innerHTML = contenido;
}
/*********************************************************************************************
 * 
 * Funcion para dar formato a un numero
 * 
 *********************************************************************************************/
 function number_format( number, decimals, dec_point, thousands_sep ) {
    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "." : dec_point;
    var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}
/*********************************************************************************************
  Función precio:
	funcion para calcular el precio de acuerdo a lo seleccionado. 
*********************************************************************************************/ 
function generaprecio() {
	if(document.getElementById('tamano')){
		tamano_a_separar = document.getElementById('tamano').value;
		var tamano_separado = tamano_a_separar.split('|');
		document.getElementById('precio_oculto').value = tamano_separado[0];
		document.getElementById('envio_oculto').value = tamano_separado[1];
	}
	if(document.getElementById('adicional')){
		document.getElementById('adicional_oculto').value = document.getElementById('adicional').value;
	}
	adicionalasumar = document.getElementById('adicional_oculto').value;
	precioasumar = document.getElementById('precio_oculto').value;
	if(Math.floor(Number(adicionalasumar)) != 0){
		precio_nuevo = Number(adicionalasumar) + Number(precioasumar);
		precio_nuevo_formato = number_format(precio_nuevo, 2, '.', '');
	} else if (Math.floor(Number(adicionalasumar)) == 0){
		precio_nuevo = (Number(adicionalasumar) + Number(1)) * Number(precioasumar);
		precio_nuevo_formato = number_format(precio_nuevo, 2, '.', '');
	}
	document.getElementById('muestra_precio').innerHTML='$' + number_format(precio_nuevo, 2, '.', ',');
	document.getElementById('precio_total').value = precio_nuevo_formato;
	//document.getElementById('adicional_nombre').value = document.getElementById('adicional').name;
	document.getElementById('adicional_nombre').value = document.getElementById('adicional')[document.getElementById('adicional').selectedIndex].innerHTML;

}

/*********************************************************************************************
  Función checkEnter:
	funcion para checar si se presiona la tecla enter y actualiza el carrito. 
*********************************************************************************************/ 
function checkEnter(e,valor,nombre){ //e is event object passed from function invocation
var characterCode //literal character code will be stored in this variable

if(e && e.which){ //if which property of event object is supported (NN4)
e = e
characterCode = e.which //character code is contained in NN4's which property
}
else{
e = event
characterCode = e.keyCode //character code is contained in IE's keyCode property
}

if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
carrito_actualizar_submit(valor,nombre);
return false
}
else{
return true
}

}

function addslashes(str) {
str=str.replace(/\'/g,'iii');
str=str.replace(/\"/g,'III');
str=str.replace(/\\/g,'\\\\');
str=str.replace(/\0/g,'\\0');
str=str.replace(/\</g,'HiH');
str=str.replace(/\>/g,'hih');
return str;
}
function stripslashes(str) {
str=str.replace(/\\'/g,'\'');
str=str.replace(/\\"/g,'"');
str=str.replace(/\\\\/g,'\\');
str=str.replace(/\\0/g,'\0');
return str;
}

/*********************************************************************************************
  Funciones valida_opciones:
	las funcion revisa si el producto tiene color, textura y/o color textil seleccionados. 
	
*********************************************************************************************/ 

function valida_opciones (opcion){
	switch (opcion) {
		case 1:
		if(document.getElementById('elcolores').value != ""){
			carrito_agrega();
		} else {
			alert("Es necesario seleccionar un color antes de agregar el producto al carrito de compras");
		}
		break;
		case 2:
		if(document.getElementById('latexturaes').value != ""){
			carrito_agrega();
		} else {
			alert("Es necesario seleccionar una textura antes de agregar el producto al carrito de compras");
		}
		break;
		case 4:
		if(document.getElementById('eltextiles').value != ""){
			carrito_agrega();
		} else {
			alert("Es necesario seleccionar un textil antes de agregar el producto al carrito de compras");
		}
		break;
		case 3:
		if(document.getElementById('elcolores').value != "" && document.getElementById('latexturaes').value != ""){
			carrito_agrega();
		} else {
			if(document.getElementById('elcolores').value == ""){
				alert("Es necesario seleccionar un color antes de agregar el producto al carrito de compras");
			}
			if(document.getElementById('latexturaes').value == ""){
				alert("Es necesario seleccionar una textura antes de agregar el producto al carrito de compras");
			}
		}
		break;
		case 5:
		if(document.getElementById('elcolores').value != "" && document.getElementById('eltextiles').value != ""){
			carrito_agrega();
		} else {
			if(document.getElementById('elcolores').value == ""){
				alert("Es necesario seleccionar un color antes de agregar el producto al carrito de compras");
			}
			if(document.getElementById('eltextiles').value == ""){
				alert("Es necesario seleccionar un textil antes de agregar el producto al carrito de compras");
			}
		}
		break;
		case 6:
		if(document.getElementById('eltextiles').value != "" && document.getElementById('latexturaes').value != ""){
			carrito_agrega();
		} else {
			if(document.getElementById('latexturaes').value == ""){
				alert("Es necesario seleccionar una textura antes de agregar el producto al carrito de compras");
			}
			if(document.getElementById('eltextiles').value == ""){
				alert("Es necesario seleccionar un textil antes de agregar el producto al carrito de compras");
			}
		}
		break;
		case 7:
		if(document.getElementById('elcolores').value != "" && document.getElementById('latexturaes').value != "" && document.getElementById('eltextiles').value != ""){
			carrito_agrega();
		} else {
			if(document.getElementById('latexturaes').value == ""){
				alert("Es necesario seleccionar una textura antes de agregar el producto al carrito de compras");
			}
			if(document.getElementById('eltextiles').value == ""){
				alert("Es necesario seleccionar un textil antes de agregar el producto al carrito de compras");
			}
			if(document.getElementById('elcolores').value == ""){
				alert("Es necesario seleccionar un color antes de agregar el producto al carrito de compras");
			}
		}
		break;
		default:
		carrito_agrega();
		break;
	}
}
/*********************************************************************************************
  Funcion siguiente:
	continua a la siguiente pagina de acuerdo a la seleccion. 
	
*********************************************************************************************/ 
function siguiente (nombre){
	paginaair = document.getElementById(nombre).value;
	if(paginaair != ""){
	open('/deconcepto/' + paginaair + '.html','_self');
	} else {
		alert('Seleccione una opcion');
	}
}
function bloquear(nombre,bandera){
	if(bandera == "on"){
    	document.getElementById(nombre).style.display = "block";
	} else {
		document.getElementById(nombre).style.display = "none";
	}
}
function validaforma(form){
	for(x = 0; x < document.getElementById(form).elements.length; x++){
		temp=document.getElementById(form).elements[x].id;
		temp2=temp.indexOf("*");
		if( document.getElementById(form).elements[x].value == "" && document.getElementById(form).elements[x].type != "submit" && temp2 == 0){
			nombre = temp.replace("*","");
			nombre = nombre.replace("_"," ");
			alert("El campo "+nombre+" esta vacio");
			document.getElementById(form).elements[x].focus();
			return false;
		}
	}
	return true;
}
/*********************************************************************************************
  Funcion imprimir:
	imprime el div de impresion. 
	
*********************************************************************************************/ 
function imprimir()
{
var a = window. open('','','scrollbars=yes,width=700,height=400');

a.document.open("text/html");
a.document.write('<html><head><link rel="stylesheet" href="/includes/estilos.css" /><style type="text/css">#frame{background-image:none;background-color:#FFFFFF;}</style></head><body style="padding-left:20px;background-image:none;background-color:#FFFFFF;"><img src="/images/logo.png" border="0"><br>');
a.document.write(document.getElementById('contenido_impresion').innerHTML);
a.document.write('</body></html>');
a.document.close();
a.print();
}

function enviar_email()
{
var a = window. open('','','scrollbars=yes,width=700,height=400');

a.document.open("text/html");
a.document.write('<html><head><link rel="stylesheet" href="/includes/estilos.css" /><style type="text/css">#frame{background-image:none;background-color:#FFFFFF;}</style></head><body style="padding-left:20px;background-image:none;background-color:#FFFFFF;" class="style7"><img src="/images/logo.png" border="0"><br>');
texto = addslashes('<img src="/images/logo.png" border="0"><br>' + document.getElementById('contenido_impresion').innerHTML);
a.document.write('<br><form name="forma" id="forma" action="/email_compartir.php" method="post" enctype="multipart/form-data"><input type="hidden" id="contenido" name="contenido" value="' + texto + '">Su nombre <input type="text" id="nombre" name="nombre" value=""><br>Su email <input type="text" id="email" name="email" value=""><br>Nombre del destinatario <input type="text" id="destinatario" name="destinatario" value=""><br>Email del destinatario <input type="text" id="email_destinatario" name="email_destinatario" value=""><br>Comentarios<br><textarea name="comentarios" cols="50" rows="6" class="texto"></textarea><br><input type="submit" value="Enviar"></form>');
a.document.write('</body></html>');
a.document.close();
}


//*******FUNCIONES AJAX**********************************************************************************
