function addItemToBasket(idCantUni, idCantBulto,currCod){
	var unitValue = document.getElementById(idCantUni).value;
	var bultoValue;

	if (document.getElementById(idCantBulto)){
	    bultoValue = document.getElementById(idCantBulto).value;
	}
	else{
	    bultoValue = '';
	}
	


	if (unitValue == '' && bultoValue == ''){
		alert("No se ha especificado la cantidad.\nIngrese una cantidad y vuelva a intentarlo.");
		return;
	}
	else {
		if (unitValue == ''){
			addProductToBasket($('idProductBulto_'+currCod).value, bultoValue);
		}
		else{
			addProductToBasket($('idProductUnit_'+currCod).value, unitValue);

			if(bultoValue != ''){
				addProductToBasket($('idProductBulto_'+currCod).value, bultoValue);
			}
		}
	}
}


function insertaFilaSeparador(table){

	var trSeparador = document.createElement('tr');

	var bordeExterior = document.createElement('img');
	bordeExterior.setAttribute('src','mayorista/listas/_imagenes/green.gif');
	bordeExterior.setAttribute('width','4');
	bordeExterior.setAttribute('border','0');
	bordeExterior.setAttribute('height','1');

	var bordeInterno = document.createElement('img');
	bordeInterno.setAttribute('src','mayorista/listas/_imagenes/green.gif');
	bordeInterno.setAttribute('width','1');
	bordeInterno.setAttribute('border','0');
	bordeInterno.setAttribute('height','1');

	var bordeExterior2 = document.createElement('img');
	bordeExterior2.setAttribute('src','mayorista/listas/_imagenes/green.gif');
	bordeExterior2.width = '6';
	bordeExterior2.setAttribute('border','0');
	bordeExterior2.setAttribute('height','1');
// 	bordeExterior2.style.border = '1px solid #000000';

	var spacer = document.createElement('img');
	spacer.setAttribute('src','mayorista/listas/_imagenes/spacer.gif');
	spacer.setAttribute('width','57');
	spacer.setAttribute('border','0');
	spacer.setAttribute('height','1');

	var tdBordeExt = document.createElement('td');
	tdBordeExt.appendChild(bordeExterior);
	var tdBordeInt = document.createElement('td');
	tdBordeInt.appendChild(bordeInterno);

	
	trSeparador.appendChild(tdBordeExt);

	var tdFoto = document.createElement('td');
	tdFoto.appendChild(spacer);
	trSeparador.appendChild(tdFoto);
	
	trSeparador.appendChild(tdBordeInt);
	var tdCodigo = document.createElement('td');

	var spacer2 = document.createElement('img');
	spacer2.setAttribute('src','mayorista/listas/_imagenes/spacer.gif');
	spacer2.setAttribute('width','25');
	spacer2.setAttribute('border','0');
	spacer2.setAttribute('height','1');
	tdCodigo.appendChild(spacer2);
	trSeparador.appendChild(tdCodigo);

	var bordeInterno2 = document.createElement('img');
	bordeInterno2.setAttribute('src','mayorista/listas/_imagenes/green.gif');
	bordeInterno2.setAttribute('width','1');
	bordeInterno2.setAttribute('border','0');
	bordeInterno2.setAttribute('height','1');
	var tdBordeInt2 = document.createElement('td');
	tdBordeInt2.appendChild(bordeInterno2);
	trSeparador.appendChild(tdBordeInt2);
	var tdDesc = document.createElement('td');

	var spacer3 = document.createElement('img');
	spacer3.setAttribute('src','mayorista/listas/_imagenes/spacer.gif');
	spacer3.setAttribute('width','165');
	spacer3.setAttribute('border','0');
	spacer3.setAttribute('height','1');
	tdDesc.appendChild(spacer3);
	trSeparador.appendChild(tdDesc);

	var bordeInterno3 = document.createElement('img');
	bordeInterno3.setAttribute('src','mayorista/listas/_imagenes/green.gif');
	bordeInterno3.setAttribute('width','1');
	bordeInterno3.setAttribute('border','0');
	bordeInterno3.setAttribute('height','1');
	var tdBordeInt3 = document.createElement('td');
	tdBordeInt3.appendChild(bordeInterno3);

	trSeparador.appendChild(tdBordeInt3);
	var tdPrecioUni = document.createElement('td');

	var spacer4 = document.createElement('img');
	spacer4.setAttribute('src','mayorista/listas/_imagenes/spacer.gif');
	spacer4.setAttribute('width','44');
	spacer4.setAttribute('border','0');
	spacer4.setAttribute('height','1');
	tdPrecioUni.appendChild(spacer4);
	trSeparador.appendChild(tdPrecioUni);


	var bordeInterno4 = document.createElement('img');
	bordeInterno4.setAttribute('src','mayorista/listas/_imagenes/green.gif');
	bordeInterno4.setAttribute('width','1');
	bordeInterno4.setAttribute('border','0');
	bordeInterno4.setAttribute('height','1');
	var tdBordeInt4 = document.createElement('td');
	tdBordeInt4.appendChild(bordeInterno4);
	trSeparador.appendChild(tdBordeInt4);
	var tdCantUni = document.createElement('td');

	var spacer5 = document.createElement('img');
	spacer5.setAttribute('src','mayorista/listas/_imagenes/spacer.gif');
	spacer5.setAttribute('width','48');
	spacer5.setAttribute('border','0');
	spacer5.setAttribute('height','1');
	tdCantUni.appendChild(spacer5);
	trSeparador.appendChild(tdCantUni);


	var bordeInterno5 = document.createElement('img');
	bordeInterno5.setAttribute('src','mayorista/listas/_imagenes/green.gif');
	bordeInterno5.setAttribute('width','1');
	bordeInterno5.setAttribute('border','0');
	bordeInterno5.setAttribute('height','1');
	var tdBordeInt5 = document.createElement('td');
	tdBordeInt5.appendChild(bordeInterno5);
	trSeparador.appendChild(tdBordeInt5);
	var tdPrecioBul = document.createElement('td');

	var spacer6 = document.createElement('img');
	spacer6.setAttribute('src','mayorista/listas/_imagenes/spacer.gif');
	spacer6.setAttribute('width','50');
	spacer6.setAttribute('border','0');
	spacer6.setAttribute('height','1');
	tdPrecioBul.appendChild(spacer6);
	trSeparador.appendChild(tdPrecioBul);


	var bordeInterno6 = document.createElement('img');
	bordeInterno6.setAttribute('src','mayorista/listas/_imagenes/green.gif');
	bordeInterno6.setAttribute('width','1');
	bordeInterno6.setAttribute('border','0');
	bordeInterno6.setAttribute('height','1');
	var tdBordeInt6 = document.createElement('td');
	tdBordeInt6.appendChild(bordeInterno6);
	trSeparador.appendChild(tdBordeInt6);
	var tdCantBul = document.createElement('td');

	var spacer7 = document.createElement('img');
	spacer7.setAttribute('src','mayorista/listas/_imagenes/spacer.gif');
	spacer7.setAttribute('width','30');
	spacer7.setAttribute('border','0');
	spacer7.setAttribute('height','1');
	tdCantBul.appendChild(spacer7);
	trSeparador.appendChild(tdCantBul);
	

	var bordeInterno7 = document.createElement('img');
	bordeInterno7.setAttribute('src','mayorista/listas/_imagenes/green.gif');
	bordeInterno7.setAttribute('width','1');
	bordeInterno7.setAttribute('border','0');
	bordeInterno7.setAttribute('height','1');
	var tdBordeInt7 = document.createElement('td');
	tdBordeInt7.appendChild(bordeInterno7);
	trSeparador.appendChild(tdBordeInt7);
	var tdCantPedidaBul = document.createElement('td');
	
	var spacer8 = document.createElement('img');
	spacer8.setAttribute('src','mayorista/listas/_imagenes/spacer.gif');
	spacer8.setAttribute('width','48');
	spacer8.setAttribute('border','0');
	spacer8.setAttribute('height','1');
	tdCantPedidaBul.appendChild(spacer8);
	trSeparador.appendChild(tdCantPedidaBul);

	
	var bordeInterno8 = document.createElement('img');
	bordeInterno8.setAttribute('src','mayorista/listas/_imagenes/green.gif');
	bordeInterno8.setAttribute('width','1');
	bordeInterno8.setAttribute('border','0');
	bordeInterno8.setAttribute('height','1');
	var tdBordeInt8 = document.createElement('td');
	tdBordeInt8.appendChild(bordeInterno8);
	trSeparador.appendChild(tdBordeInt8);
	var tdAcciones = document.createElement('td');

	var spacer9 = document.createElement('img');
	spacer9.setAttribute('src','mayorista/listas/_imagenes/spacer.gif');
	spacer9.setAttribute('width','23');
	spacer9.setAttribute('border','0');
	spacer9.setAttribute('height','1');
	tdAcciones.appendChild(spacer9);
	trSeparador.appendChild(tdAcciones);

	var tdBordeExt2 = document.createElement('td');
	tdBordeExt2.appendChild(bordeExterior2);
	trSeparador.appendChild(tdBordeExt2);

	table.appendChild(trSeparador);

	return true;
}



function generateTableProducts(idTable,arrProduct){

	if (arrProduct.length > 0){
		var currCodigo = '';
		
		for (var i=0;i<arrProduct.length;i++){
			var row = arrProduct[i];
			var codigo = row['codigo_interno'];

			if (currCodigo != codigo){
				var tdExterna = document.createElement('td');
				tdExterna.style.width = '1px';
				tdExterna.style.backgroundImage = "url('mayorista/listas/_imagenes/green.gif')";
				tdExterna.style.backgroundRepeat = 'repeat';
				var espacioBlanco = document.createTextNode(' ');
				tdExterna.appendChild(espacioBlanco);

				var tdBordeInterno = document.createElement('td');
				tdBordeInterno.style.width = '1px';
				tdBordeInterno.style.backgroundImage = "url('mayorista/listas/_imagenes/green.gif')";
				tdBordeInterno.style.backgroundRepeat = 'repeat-y';
				tdBordeInterno.appendChild(espacioBlanco);

				currCodigo = codigo;
				insertaFilaSeparador(idTable);
				var currTr = document.createElement('tr');
				currTr.setAttribute('height','17');
				currTr.appendChild(tdExterna);
				
				var tdImagen = document.createElement('td');
				tdImagen.setAttribute('align','center');
				tdImagen.setAttribute('valign','middle');
				tdImagen.className = 'listaCodigo';

				if (row['imagen'] != ''){
					var linkImg = document.createElement('a');
					linkImg.setAttribute('href', row['imagen_max']);
					linkImg.setAttribute('rel', 'lightbox');
					linkImg.setAttribute('title', row['nombre']);
					var imgProdu = document.createElement('img');
					imgProdu.setAttribute('src', '/'+row['imagen']);
					imgProdu.setAttribute('alt', 'Ampliar imagen del producto '+row['codigo_interno']);
					imgProdu.setAttribute('valign', 'middle');
					imgProdu.setAttribute('width', '50');
					imgProdu.setAttribute('height', '50');
					imgProdu.setAttribute('border', '0');

					linkImg.appendChild(imgProdu);
					tdImagen.appendChild(linkImg);
				}
				else{
					var texto = document.createTextNode(' Sin Foto');
					tdImagen.appendChild(texto);
				}

// 				FOTO
				currTr.appendChild(tdImagen);
				currTr.appendChild(tdBordeInterno);

				var tdCodigo = document.createElement('td');
				tdCodigo.setAttribute('align', 'right');
				tdCodigo.className = 'listaCodigo';
				var textoCod = document.createTextNode(row['codigo_interno']);
				tdCodigo.appendChild(textoCod);
				
				var tdBordeInterno2 = document.createElement('td');
				tdBordeInterno2.style.width = '1px';
				tdBordeInterno2.style.backgroundImage = "url('mayorista/listas/_imagenes/green.gif')";
				tdBordeInterno2.style.backgroundRepeat = 'repeat-y';
				tdBordeInterno2.appendChild(espacioBlanco);

// 				CODIGO
				currTr.appendChild(tdCodigo);
				currTr.appendChild(tdBordeInterno2);

				var tdDescripcion = document.createElement('td');
				tdDescripcion.className = 'listaNombre';
				var textoDesc = document.createTextNode(row['nombre']);
				tdDescripcion.appendChild(textoDesc);
				
				var tdBordeInterno3 = document.createElement('td');
				tdBordeInterno3.style.width = '1px';
				tdBordeInterno3.style.backgroundImage = "url('mayorista/listas/_imagenes/green.gif')";
				tdBordeInterno3.style.backgroundRepeat = 'repeat-y';
				tdBordeInterno3.appendChild(espacioBlanco);

// 				DESCRIPCION
				currTr.appendChild(tdDescripcion);
				currTr.appendChild(tdBordeInterno3);

				var tdPrecio = document.createElement('td');
				tdPrecio.className = 'listaPrecio';
				tdPrecio.setAttribute('id','td_precio_unidad_'+currCodigo);
				var negrita = document.createElement('strong');
				negrita.setAttribute('id','precio_unidad_'+currCodigo);
				var tdPrecioBulto = document.createElement('td');
				var tdCantBulto = document.createElement('td');
				tdCantBulto.setAttribute('id','cant_bulto_'+currCodigo);
				tdCantBulto.setAttribute('name','cant_bulto_'+currCodigo);
				tdCantBulto.className = 'listaPrecio'; /*CREO EL TD PARA PONERLE LA CANTIDAD, LUEGO SE AGREGA LA TD AL TR*/

				var tdCantPedirBulto = document.createElement('td'); /*SE CREA ACA PARA INSERTAR EL INPUT DE CANTIDAD X BULTO <=> EXISTE CANTIDAD POR BULTO*/
				tdCantPedirBulto.setAttribute('id','idCantidadBulto_'+currCodigo);

				if(!row['cantidad_bulto']){
					var textoPrecio = document.createTextNode(row['moneda']+' '+row['precio']);
					negrita.appendChild(textoPrecio);
					var hiddenIdProduct = document.createElement('input');
					hiddenIdProduct.setAttribute('type','hidden');
					hiddenIdProduct.setAttribute('name','idProductUnit_'+currCodigo);
					hiddenIdProduct.setAttribute('id','idProductUnit_'+currCodigo);
					hiddenIdProduct.setAttribute('value',row['id']);
					tdPrecio.appendChild(hiddenIdProduct);
				}
				else{
					var textoPrecioBulto = document.createTextNode(row['moneda']+' '+row['precio']);
					tdPrecioBulto.appendChild(textoPrecioBulto);
					var textoCantBulto = document.createTextNode(row['cantidad_bulto']);
					tdCantBulto.appendChild(textoCantBulto);
					var hiddenIdProduct = document.createElement('input');
					hiddenIdProduct.setAttribute('type','hidden');
					hiddenIdProduct.setAttribute('name','idProductBulto_'+currCodigo);
					hiddenIdProduct.setAttribute('id','idProductBulto_'+currCodigo);
					hiddenIdProduct.setAttribute('value',row['id']);
					tdPrecioBulto.appendChild(hiddenIdProduct);

					var inputCantPideBulto = document.createElement('input');
					inputCantPideBulto.style.width = '38px';
					inputCantPideBulto.style.borderColor = '#999999';
					inputCantPideBulto.setAttribute('type', 'text');
					inputCantPideBulto.name = 'quantity_bulto_'+currCodigo;
					inputCantPideBulto.setAttribute('id','quantity_bulto_'+currCodigo);
					inputCantPideBulto.value = '';
					tdCantPedirBulto.appendChild(inputCantPideBulto);
				}
				tdPrecio.appendChild(negrita);

				var tdBordeInterno4 = document.createElement('td');
				tdBordeInterno4.style.width = '1px';
				tdBordeInterno4.style.backgroundImage = "url('mayorista/listas/_imagenes/green.gif')";
				tdBordeInterno4.style.backgroundRepeat = 'repeat-y';
				tdBordeInterno4.appendChild(espacioBlanco);

// 				PRECIO
				currTr.appendChild(tdPrecio);
				currTr.appendChild(tdBordeInterno4);

				var tdCantidad = document.createElement('td');
				tdCantidad.setAttribute('align', 'center');
				tdCantidad.style.padding = '3px';
				var inputCantidad = document.createElement('input');
				inputCantidad.style.width = '38px';
				inputCantidad.style.borderColor = '#999999';
				inputCantidad.setAttribute('type', 'text');
				inputCantidad.name = 'quantity_'+currCodigo;
				inputCantidad.setAttribute('id','quantity_'+currCodigo);
				inputCantidad.value = '';
				tdCantidad.appendChild(inputCantidad);

				var tdBordeInterno5 = document.createElement('td');
				tdBordeInterno5.style.width = '1px';
				tdBordeInterno5.style.backgroundImage = "url('mayorista/listas/_imagenes/green.gif')";
				tdBordeInterno5.style.backgroundRepeat = 'repeat-y';
				tdBordeInterno5.appendChild(espacioBlanco);

// 				CANTIDAD A PEDIR UNITARIA
				currTr.appendChild(tdCantidad);
				currTr.appendChild(tdBordeInterno5);

				tdPrecioBulto.setAttribute('align', 'left');
				tdPrecioBulto.setAttribute('id', 'precioBulto_'+currCodigo);
				tdPrecioBulto.name = 'precioBulto_'+currCodigo;
				tdPrecioBulto.className = 'listaPrecio';

				var tdBordeInterno6 = document.createElement('td');
				tdBordeInterno6.style.width = '1px';
				tdBordeInterno6.style.backgroundImage = "url('mayorista/listas/_imagenes/green.gif')";
				tdBordeInterno6.style.backgroundRepeat = 'repeat-y';
				tdBordeInterno6.appendChild(espacioBlanco);

// 				PRECIO POR BULTO
				currTr.appendChild(tdPrecioBulto);
				currTr.appendChild(tdBordeInterno6);

				var tdBordeInterno7 = document.createElement('td');
				tdBordeInterno7.style.width = '1px';
				tdBordeInterno7.style.backgroundImage = "url('mayorista/listas/_imagenes/green.gif')";
				tdBordeInterno7.style.backgroundRepeat = 'repeat-y';
				tdBordeInterno7.appendChild(espacioBlanco);
		
// 				CANTIDAD POR BULTO
				currTr.appendChild(tdCantBulto);
				currTr.appendChild(tdBordeInterno7);

				
				tdCantPedirBulto.setAttribute('align', 'center');
				tdCantPedirBulto.style.padding = '3px';

				var tdBordeInterno8 = document.createElement('td');
				tdBordeInterno8.style.width = '1px';
				tdBordeInterno8.style.backgroundImage = "url('mayorista/listas/_imagenes/green.gif')";
				tdBordeInterno8.style.backgroundRepeat = 'repeat-y';
				tdBordeInterno8.appendChild(espacioBlanco);

// 				CANTIDAD A PEDIR POR BULTO
				currTr.appendChild(tdCantPedirBulto);
				currTr.appendChild(tdBordeInterno8);

				var tdAgregarCarrito = document.createElement('td');
				tdAgregarCarrito.setAttribute('align', 'center');
				tdAgregarCarrito.setAttribute('valign', 'middle');
				tdAgregarCarrito.setAttribute('id', currCodigo);
				tdAgregarCarrito.name = currCodigo;
				var imgCarrito = document.createElement('img');
				imgCarrito.setAttribute('src', '/mayorista/listas/_imagenes/mas_off_a.gif');
				imgCarrito.setAttribute('id', 'mas'+currCodigo);
				imgCarrito.name = 'mas'+currCodigo;
				imgCarrito.setAttribute('alt', '');
				imgCarrito.setAttribute('border', '0');
				imgCarrito.onmouseout = function(){MM_swapImgRestore();};
				imgCarrito.onmouseover = function(){MM_swapImage(this.id,'','mayorista/listas/_imagenes/mas_on_a.gif',1);};
				imgCarrito.onclick = function(){addItemToBasket('quantity_'+this.id.substring(3),'quantity_bulto_'+this.id.substring(3),this.parentNode.id);};
				imgCarrito.style.cursor = 'pointer';

				tdAgregarCarrito.appendChild(imgCarrito);


				var tdExterna2 = document.createElement('td');
				tdExterna2.style.width = '1px';
				tdExterna2.style.backgroundImage = "url('mayorista/listas/_imagenes/green.gif')";
				tdExterna2.style.backgroundRepeat = 'repeat';
				var espacioBlanco2 = document.createTextNode(' ');
				tdExterna2.appendChild(espacioBlanco2);

// 				AGREGAR AL CARRITO
				currTr.appendChild(tdAgregarCarrito);
				currTr.appendChild(tdExterna2);

				idTable.appendChild(currTr);
			}
			else{
				if(!row['cantidad_bulto']){
					var negrita = $('precio_unidad_'+currCodigo);
					var tdPrecio = $('td_precio_unidad_'+currCodigo);
					var textoPrecioUnidad = document.createTextNode(row['moneda']+' '+row['precio']);
					negrita.appendChild(textoPrecioUnidad);
					var hiddenIdProduct = document.createElement('input');
					hiddenIdProduct.setAttribute('type','hidden');
					hiddenIdProduct.setAttribute('name','idProductUnit_'+currCodigo);
					hiddenIdProduct.setAttribute('id','idProductUnit_'+currCodigo);
					hiddenIdProduct.setAttribute('value',row['id']);
					tdPrecio.appendChild(hiddenIdProduct);
				}
				else{
					var tdPrecioPorBulto = $('precioBulto_'+currCodigo);
					var textoPrecioPorBulto = document.createTextNode(row['moneda']+' '+row['precio']);
					tdPrecioPorBulto.appendChild(textoPrecioPorBulto);
					var hiddenIdProduct = document.createElement('input');
					hiddenIdProduct.setAttribute('type','hidden');
					hiddenIdProduct.setAttribute('name','idProductBulto_'+currCodigo);
					hiddenIdProduct.setAttribute('id','idProductBulto_'+currCodigo);
					hiddenIdProduct.setAttribute('value',row['id']);
					tdPrecioPorBulto.appendChild(hiddenIdProduct);
					var tdCantBulto = $('cant_bulto_'+currCodigo);
					var textoCantBulto = document.createTextNode(row['cantidad_bulto']);
					tdCantBulto.appendChild(textoCantBulto);

					var inputCantPideBulto = document.createElement('input');
					inputCantPideBulto.style.width = '38px';
					inputCantPideBulto.style.borderColor = '#999999';
					inputCantPideBulto.setAttribute('type', 'text');
					inputCantPideBulto.name = 'quantity_bulto_'+currCodigo;
					inputCantPideBulto.setAttribute('id','quantity_bulto_'+currCodigo);
					inputCantPideBulto.value = '';
					$('idCantidadBulto_'+currCodigo).appendChild(inputCantPideBulto);
				}
			}
		}

	}



}


function checkSearchSubmitCatalogue(e) {
	var keynum;
	if(window.event){ // IE
		keynum = e.keyCode;
	}
	else if(e.which){ // Netscape/Firefox/Opera
		keynum = e.which;
	}

	if (keynum==13) { //presionaron enter entonces hago la búsqueda
		$('_a').value = 'searchItems';
		$('id').value = '';
 		validaCantidadCaracteresCatalogue(2);
 		document.frm.submit();
	}
}

function checkSearchSubmitBuscador(e) {
	var keynum;
	if(window.event){ // IE
		keynum = e.keyCode;
	}
	else if(e.which){ // Netscape/Firefox/Opera
		keynum = e.which;
	}	

	if (keynum==13) { //presionaron enter entonces hago la búsqueda
		$('_a').value = 'searchItems';
		$('id').value = '';
		validaCantidadCaracteresBuscador(2);
// 		document.frm.submit();
	}
}

function checkSearchSubmitSearchItem(e) {
	var keynum;
	if(window.event){ // IE
		keynum = e.keyCode;
	}
	else if(e.which){ // Netscape/Firefox/Opera
		keynum = e.which;
	}

	if (keynum==13) { //presionaron enter entonces hago la búsqueda
		$('_a').value = 'searchItems';
		$('id').value = '';
		validaCantidadCaracteresSearchItems(2);
// 		document.frm.submit();
	}
}


function validaCantidadCaracteresBuscador(cantidad) {
		if (document.frm_buscador.searchField__product__name_product__like.value.length < cantidad) {
			alert("Debe ingresar 2 o más caracteres.");
			return false;
		}
		else {
			document.frm_buscador.submit();
		}
	}

function validaCantidadCaracteresCatalogue(cantidad) {
	if (document.frm.searchField__product__name_product__like.value.length < cantidad) {
		alert("Debe ingresar 2 o más caracteres.");
		return false;
	}
	else {
		document.frm.submit();
	}
}

function validaCantidadCaracteresSearchItems(cantidad) {
	if (document.frm.searchField__product__name_product__like.value.length < cantidad) {
		alert("Debe ingresar 2 o más caracteres.");
		return false;
	}
	else {
		document.frm.submit();
	}
}

function getBuscador() {
	var rand = Math.round(100*Math.random()); //  IE CACHE FIX
	new Ajax.Updater({ success: 'buscador',failure: '',exception:''}, '/index.cgi', {
					evalScripts:true,
					evalJS:true,
					parameters: 'accion=getPlugin&pluginName=get_template&template=plugin_catalogo_buscador_home.tmpl&_nocache=' + rand,
					method: 'get',
					onSuccess: function(){
						if (200 != transport.status){
							alert('Ha ocurrido un error en el sistema. Vuelva a intentar la operación por favor más tarde.');
						}
// 						$('LoaderProductsBasket').style.display = 'block';
					},
					onFailure: function(){alert('Ha ocurrido un error en el sistema. Vuelva a intentar la operación por favor.');}
				});
}

function getOfertas() {
	var rand = Math.round(100*Math.random()); //  IE CACHE FIX
	new Ajax.Updater( 'ofertas', '/index.pl', {
					evalScripts:true,
					parameters: '_c=serviceProvider&_a=getService&serviceName=get_catalogue_products&template=ofertas_home.tmpl&in_offer=0&destacados=0&quantity=1&rnd=1&_nocache='+rand,
					onCreate: function(){
// 							$('LoaderProductsBasket').style.display = 'block';
					},
					onComplete: function(transport) {
// 						$('LoaderProductsBasket').style.display = 'none';
					},
					onFailure: function(){ alert('No se pudieron mostrar las ofertas...'); }
				});
		//setTimeout("getOfertas();",5000);
	}


function initWidgetsTicket() {
	new Ajax.Updater('ticket', '/index.pl', {
					evalScripts:true,
					parameters: '_c=serviceProvider&_a=getService&serviceName=get_ticket&template=plugin_catalogo_ticket.tmpl',
					onSuccess: function(){
						if (200 != transport.status){
							alert('Ha ocurrido un error en el sistema. Vuelva a intentar la operación por favor.');
						}
// 						$('LoaderProductsBasket').style.display = 'block';
					},
					onComplete: function(transport) {
//  						$('LoaderProductsBasket').style.display = 'none';
					},
					onFailure: function(){ alert('Ha ocurrido un error en el sistema. Vuelva a intentar la operación por favor.');}
				});
}

function addProductToBasket(idProduct, quantity) {
	if (quantity > 0){
		new Ajax.Request('/index.pl', {
				parameters: '_a=addProductToBasket&_c=catalogueFront&id_product='+idProduct +'&quantity='+quantity,
				onSuccess: function(transport) {
					var response = transport.responseText;
				},
				onComplete: function(transport) {
								initWidgetsTicket();
								alert('Se ha agregado el producto al carro de compras');
							},
				onFailure: function(){ alert('No se pudo agregar el producto...id_product='+idProduct +'&quantity='+quantity) }
			});
	}
	else { alert("No se ha especificado la cantidad.\nIngrese una cantidad y vuelva a intentarlo.");}
}

var l=screen.availWidth-(420+20);
function recordar() {
    window.open("/recordar.cgi","Contrasenia","width=223,directories=no,toolbar=no,resizable=no,menubar=no,scrollbars=no,top=20,left="+l);
}

function enviarDatos() {
    if (document.login2.usuario.value == '') {
        alert("Debe colocar su nombre de usuario.");
        document.login2.usuario.focus();
	return false;
    }
    else if (document.login2.pass.value == '') {
        alert("Debe colocar su password."); 
        document.login2.pass.focus();
	return false;
    }
    else if ((document.login2.usuario.value != '') && (document.login2.pass.value != '')) {
		var url = '/';
		
		if (document.login2.back.value != '') {
			url = document.login2.back.value;
		}

		document.login2.back.value = url.replace(/&/ig, "*");
        
        temp = hex_sha1(document.login2.pass.value);
        document.login2.password.value = temp
        document.login2.submit();
    }
}

function logout() {
		    var url = location.href;
		    url = url.replace(/^http[s]?:\/\/\w[\.\w\-\_]+/ig, "");
		    url = url.replace(/&/ig, "*");
		    location.href = '/serviceLogin.cgi?accion=logout&back=/index.cgi?static=home_nueva*skin=home';
		    }

function detectCookies(){
	var _body = document.getElementById('mensaje');
	if(document.cookie.indexOf('id_sesion') == -1){
		alert('Su navegador tiene desactivada la escritura de cookies por favor active esta función.\nPara más información ingrese aquí:\nhttp://www.redex.org/mensajes/cookies.htm');
	}

	if(navigator.userAgent.indexOf('MSIE 6') > 0 || navigator.userAgent.indexOf('MSIE 7') > 0){
		var div = document.createElement('div');
		div.innerHTML = "<div style='border: 1px solid #F7941D; background: #FEEFDA; text-align: center; clear: both; height: 75px; position: relative; z-index:5000'><div style='position: absolute; right: 3px; top: 3px; font-family: courier new; font-weight: bold;'><a href='#' onclick='javascript:this.parentNode.parentNode.style.display=\"none\"; return false;'><img src='/images/ie6nomore-cornerx.jpg' style='border: none;' alt='Cerrar este aviso'/></a></div><div style='width: 100%; margin: 0 auto; text-align: center; padding: 0; overflow: hidden; color: black;' align='center'><div style='width: 75px; float: left;'><img src='/images/ie6nomore-warning.jpg' alt='Advertencia!'/></div><div style='width: 275px; float: left; font-family: Arial, sans-serif; color:#000'><div style='font-size: 14px; font-weight: bold; margin-top: 12px; color:#000'>Su navegador no soporta la escritura de cookies</div><div style='font-size: 12px; margin-top: 6px; line-height: 12px; color:#000'>Para una mejor experiencia al usar este sitio, por favor actualize su navegador.</div></div><div style='width: 75px; float: left;'><a href='http://www.firefox.com' target='_blank'><img src='/images/ie6nomore-firefox.jpg' style='border: none;' alt='Get Firefox 3.5'/></a></div><div style='width: 75px; float: left;'><a href='http://www.browserforthebetter.com/download.html' target='_blank'><img src='/images/ie6nomore-ie8.jpg' style='border: none;' alt='Get Internet Explorer 8'/></a></div><div style='float: left; width: 73px;'><a href='http://www.google.com/chrome' target='_blank'><img src='/images/ie6nomore-chrome.jpg' style='border: none;' alt='Get Google Chrome'/></a></div></div></div>";
		_body.appendChild(div);
	}
	else{
		_body.style.display = 'none';
	}
}
