function enviarDatos() {
	
	if (document.login2.usuario.value == '') {
		alert("Debe colocar su nombre de usuario.");
		document.login2.usuario.focus();
	}
	else if (document.login2.pass.value == '') {
		alert("Debe colocar su password."); 
		document.login2.pass.focus();
	}
	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 MM_openBrWindow(theURL,winName,features) { //v2.0
	window.open(theURL,winName,features);
	}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
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='+url;
}
function recordar() {
	var l=screen.availWidth-(420+20);
	window.open("/recordar.cgi","Contrasenia","width=505,height=200,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();
    }
    else if (document.login2.pass.value == '') {
        alert("Debe colocar su password.");
        document.login2.pass.focus();
    }
    else if ((document.login2.usuario.value != '') && (document.login2.pass.value != '')) {

		var url = location.href;
		url = url.replace(/^http[s]?:\/\/\w[\.\w\-\_]+/ig, "");
		document.login2.back.value = url.replace(/&/ig, "*");
        
        temp = hex_sha1(document.login2.pass.value);
        document.login2.password.value = temp;

        document.login2.submit();
    }
}

// load widgets

// addToWindowOnload(goforit);


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


function _checkStock(IdProduct,Quantity) {	
		new Ajax.Request(
				'/index.cgi?accion=getPlugin&pluginName=get_product_stock', 
				{
					method: 'get',
					parameters: {IdProduct: IdProduct, Quantity: Quantity},
					onSuccess: function(transport){
					var response = transport.responseText || "no response text";
					//document.getElementById('quantity-' + IdProduct).focus=true;
					if (response == 'fail'){
						return false;
// 						document.getElementById('quantity-' + IdProduct).focus();
// 						bnd=1;
						
					}
					else{
						return true;
// 						document.frmBasket.submit();
					}
    				}
				});
	}

function SetForm(elementId,valor) {
	if (document.getElementById(elementId)) {
		document.getElementById(elementId).value=valor;
	}
}

function submitSearch() {
	SetForm('page','');
	var formAction = document.getElementById('_a');
	formAction.value = 'getSearchItems';
	formAction.form.submit();
	formAction.value = 'getListItems';
	}

// getConfirmOrder
function SwitchMenu(obj){
		if(document.getElementById){
		var el = document.getElementById(obj);
	
			if(el.style.display == "none"){
				el.style.display = "block";
			}else{
				el.style.display = "none";
			}
		}
	}

function addProductToBasket(idProduct, quantity) {
// 	if (_checkStock(idProduct, quantity)){
		//alert('Ingresamos');
		new Ajax.Request('/index.pl', {
				parameters: '_a=addProductToBasket&_c=catalogueFront&id_product='+idProduct +'&quantity='+quantity,
				//onCreate: function(){
				//			$('LoaderProductsBasket').style.display = 'block';
				//			$('ticket').style.display = 'none';
				//		},
				onSuccess: function(transport) {
					var response = transport.responseText;
					//alert('Se ha agregado el producto al carro de compras2');
				},
				onComplete: function(transport) {
								initWidgetsTicket();
								//$('LoaderProductsBasket').style.display = 'none';
								//$('ticket').style.display = 'block';
								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("Lo sentimos pero no hay stock suficiente para este producto!");}
}

function getCategories() {
	var rand = Math.round(100*Math.random()); //  IE CACHE FIX
	new Ajax.Updater('categories', '/index.cgi', {
					evalScripts:true,
					parameters: 'accion=getPlugin&pluginName=get_categories&template=cataloguefront_get_categories.tmpl&_nocache='+rand,
					onCreate: function(){
// 							$('LoaderProductsBasket').style.display = 'block';
					},
					onComplete: function(transport) {
// 						$('LoaderProductsBasket').style.display = 'none';
					},
					onFailure: function(){ alert('Something went wrong...'); }
				});
	}



function showMediaPopup() {
	document.getElementById('popupMedia').style.display='block';
	document.getElementById('blockUI').style.display='block';
}

function closeMediaPopup() {
	document.getElementById('popupMedia').style.display='none';
	document.getElementById('blockUI').style.display='none';
}

function showFinanciacion() {
	document.getElementById('popupFinanciacion').style.display='block';
	document.getElementById('blockUI').style.display='block';
}

function closeFinanciacion() {
	document.getElementById('popupFinanciacion').style.display='none';
	document.getElementById('blockUI').style.display='none';
}

function getDestacados() {
	var rand = Math.round(100*Math.random()); //  IE CACHE FIX
	new Ajax.Updater( 'destacados', '/index.pl', {
					evalScripts:true,
					parameters: '_c=serviceProvider&_a=getService&serviceName=get_catalogue_products&template=destacados_home.tmpl&quantity=2&destacados=1&rnd=1&_nocache='+rand,
					onCreate: function(){
						$('destacados').style.display = 'block';
					},
					onComplete: function(transport) {
 						
					},
					onFailure: function(){ 
						alert('No se pudieron mostrar los destacados...'); 
						$('destacados').style.display = 'none';
					}
				});
		//setTimeout("getDestacados();",6000);
	}
function getOfertasResellers() {
	var rand = Math.round(100*Math.random()); //  IE CACHE FIX
	new Ajax.Updater( 'ofertasDestacados', '/index.pl', {
					evalScripts:true,
					parameters: '_c=serviceProvider&_a=getService&serviceName=get_catalogue_products&template=ofertas_home_resellers.tmpl&oferta_reseller_product=1&quantity=4&rnd=1&_nocache='+rand,
					onCreate: function(){
						$('ofertasDestacados').style.display = 'block';
					},
					onComplete: function(transport) {
 						
					},
					onFailure: function(){ 
						alert('No se pudieron mostrar las ofertas para resellers...');
						$('ofertasDestacados').style.display = 'none';
					}
				});
		//setTimeout("getOfertas();",5000);
	}
function getOfertasEmpresaFinal() {
	var rand = Math.round(100*Math.random()); //  IE CACHE FIX
	new Ajax.Updater( 'ofertasEmpresaFinal', '/index.pl', {
					evalScripts:true,
					parameters: '_c=serviceProvider&_a=getService&serviceName=get_catalogue_products&template=ofertas_home_empresa_final.tmpl&quantity=4&rnd=1&_nocache='+rand,
					onCreate: function(){
						$('ofertasEmpresaFinal').style.display = 'block';
					},
					onComplete: function(transport) {
 						
					},
					onFailure: function(){ 
						alert('No se pudieron mostrar las ofertas...'); 
						$('ofertasEmpresaFinal').style.display = 'none';
					}
				});
		//setTimeout("getOfertas();",5000);
	}
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=1&quantity=1&rnd=1&_nocache='+rand,
					onCreate: function(){
						$('ofertas').style.display = 'block';
					},
					onComplete: function(transport) {
 						
					},
					onFailure: function(){ 
						alert('No se pudieron mostrar las ofertas...'); 
						$('ofertas').style.display = 'none';
					}
				});
		//setTimeout("getOfertas();",5000);
	}
function getLogin() {
	var rand = Math.round(100*Math.random()); //  IE CACHE FIX
	new Ajax.Updater({ success: 'login',failure: '',exception:''}, '/index.cgi', {
					evalScripts:true,
					parameters: 'accion=getPlugin&pluginName=login&template=login_plugin.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.');
						}
// 						$('login').style.display = 'block';
					},
					onFailure: function(){ alert('Ha ocurrido un error en el sistema. Vuelva a intentar la operación por favor.');}
				});
}
function getBuscador() {
	var rand = Math.round(100*Math.random()); //  IE CACHE FIX
	new Ajax.Updater({ success: 'buscador',failure: '',exception:''}, '/index.cgi', {
					evalScripts:true,
					parameters: 'accion=getPlugin&pluginName=get_template&template=cataloguefront_get_search_products.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.');}
				});
}
