//////////////////////////////////////////////////////////////////////
//
//	- modulo de funcions js
//
//	Creado por David González 24/05/2008 | david@dagonsoft.com
//
//////////////////////////////////////////////////////////////////////	
	
	browser_name = navigator.appName;
	browser_version = parseFloat(navigator.appVersion); 
	
	if (browser_name == "Netscape" && browser_version >= 3.0){roll = 'true';}
	if (browser_version >= 4.0){roll = 'true';}
	var isNS = (navigator.appName == "Netscape" )


	function menu(categoria)
	{	
		frm = document.getElementById('frmProcess');
		frm.action.value = "showFamily";	
		frm.section.value = "";
		frm.option.value = categoria;
		frm.submit();	
	}

	function addSectionList(objSource, objDestination)
	{
		var source = document.getElementById(objSource);
		var destination = document.getElementById(objDestination);		
		if (source.value != '')
		{
			var len = destination.length;
			var found = false;			
			for(var count = 0; count < len; count++) 
			{
				if (destination.options[count] != null) 
				{
					if (destination.options[count].text == source.value) 
					{
						found = true;
						break;
					}
				}
			}			
			if (found != true) 
				destination.options[len] = new Option(source.value);		
		}
		refreshList('sections',objDestination);
		source.value = "";
		source.focus();
	}

	function delSectionList(objDestination) 
	{
		var destination = document.getElementById(objDestination);
		var len = destination.options.length;
		for(var i = (len-1); i >= 0; i--)
		{
			if ((destination.options[i] != null) && (destination.options[i].selected == true)) 
			{
				destination.options[i] = null;
			}
		}
		refreshList('sections',objDestination)
	}
	
	function refreshList(objSections,objSource)
	{
		var sections = document.getElementById(objSections);
		var source = document.getElementById(objSource);
		var len = source.length;
		var data = "";
		for(var count = 0; count < len; count++) 
		{
			if (source.options[count] != null) 
				data = data + source.options[count].text + ";"
		}	
		sections.value = data;
	}

	function addProductList(objSource, objDestination)
	{
		var source = document.getElementById(objSource);
		var destination = document.getElementById(objDestination);		
		if (source.value != '')
		{
			var len = destination.length;
			var found = false;		
			var itemsTemp = Array();
			var indexSelected = -1;
			for(var count = 0; count < len; count++) 
			{
				
				if(destination.options[count].selected == true)
					indexSelected = count;
				
				itemsTemp[count] = destination.options[count].text;
				
				if (destination.options[count] != null) 
				{
					if (destination.options[count].text == source.value) 
					{
						found = true;
						break;
					}
				}
			}			
			if (found != true) 
			{
				if(indexSelected == -1)
					destination.options[len] = new Option(source.value);
				else
				{
					for (var count = 0 ; count < len ; count++)
						destination.remove(0);
					for (count = 0 ; count < itemsTemp.length ; count++)
					{
						if(indexSelected == count)
						{
							var opt = new Option(source.value);
							destination.options.add(opt);							
							var opt = new Option(itemsTemp[count]);
							destination.options.add(opt);
						}
						else
						{
							var opt = new Option(itemsTemp[count]);
							destination.options.add(opt);
						}
					}
				}
			}
		}
		refreshList('products',objDestination);
		source.value = "";
		source.focus();
	}

	function delProductList(objDestination) 
	{
		var destination = document.getElementById(objDestination);
		var len = destination.options.length;
		for(var i = (len-1); i >= 0; i--)
		{
			if ((destination.options[i] != null) && (destination.options[i].selected == true)) 
			{
				destination.options[i] = null;
			}
		}
		refreshList('products',objDestination);
	}



	function addRelationsList(objSource, objDestination)
	{
		var source = document.getElementById(objSource);
		var destination = document.getElementById(objDestination);		
		if (source.value != '')
		{
			var len = destination.length;
			var found = false;			
			for(var count = 0; count < len; count++) 
			{
				if (destination.options[count] != null) 
				{
					if (destination.options[count].text == source.value) 
					{
						found = true;
						break;
					}
				}
			}			
			if (found != true) 
				destination.options[len] = new Option(source.value);		
		}
		refreshList('relations',objDestination);
		source.value = "";
		source.focus();
	}

	function delRelationsList(objDestination) 
	{
		var destination = document.getElementById(objDestination);
		var len = destination.options.length;
		for(var i = (len-1); i >= 0; i--)
		{
			if ((destination.options[i] != null) && (destination.options[i].selected == true)) 
			{
				destination.options[i] = null;
			}
		}
		refreshList('relations',objDestination);
	}



	function processFamily(family,code)
	{
		var frm = document.getElementById('frmProcess');
		frm.action.value = "showProducts";
		frm.section.value = code;
		frm.option.value = family;
		frm.submit();
	}

	function editProduct(objSource)
	{
		var source = document.getElementById(objSource);
		var len = source.options.length;
		for(var i = (len-1); i >= 0; i--)
		{
			if ((source.options[i] != null) && (source.options[i].selected == true)) 
			{
				url = "products.php?action=edit&product_code="+source.options[i].text;
				MM_openBrWindow(url,'editProduct','toolbar=no,scrollbars=no,width=650,height=600');
			}
		}
	}
	
	function processQuantity(idCode,option,multiple,minimum)
	{
		var objQuantity = document.getElementById(idCode);
		if(option == 'add')
		{
			objQuantity.value = eval(objQuantity.value) + eval(multiple);
		}
		else if(option == 'del' && objQuantity.value != 0)
		{
			objQuantity.value = eval(objQuantity.value) - eval(multiple);
			if(objQuantity.value < minimum && objQuantity.value != 0) 
				objQuantity.value = minimum;
		}	
	}

	function proccesProduct(code)
	{
		var frm = document.getElementById('frmProcess');
		frm.action.value = "showProduct";		
		frm.section.value = code;
		frm.option.value = "";
		frm.submit();
		//var id_obj = "div_ficha_articulo";
		//document.getElementById (id_obj).innerHTML = '<br><br><br><div align="center"><img src="images/loading.gif"></div>';
		// mostrar la informacion del producto en el div.
		//var url = "product.php?code=" + code ;
		//getRequest (url, id_obj);
	
	}

	function processBuy()
	{
		var frm = document.getElementById('frmBuy');
		var dataProducts = "";	
		var vacia = true;
		var count = frm.elements.length;
		// validamos que haya algun producto seleccionado
		for (i=0; i<count; i++) 
		{
			var element = frm.elements[i];
			if(element.value != 0 && element.type == 'text')
				vacia = false;
		}
		if(vacia)
			alert('Ha de seleccionar algun article abans de preme el boto de comprar');
		else
		{
			for (i=0; i<count; i++) 
			{
				var element = frm.elements[i]; 
				if(element.type == 'text')
					dataProducts = dataProducts + element.name + "=" + element.value + ";";
			}
			
			var frm = document.getElementById('frmProcess');
			frm.method = "post";
			frm.action.value = "processBasket";
			frm.section.value = dataProducts;
			frm.option.value = "";
			frm.submit();
		}
	}
	
	function processPagination(pag,action,section,option)
	{
		var frm = document.getElementById('frmProcess');
		frm.action.value = action;
		frm.section.value = section;
		frm.option.value = option;
		frm.limit.value = pag;
		frm.submit();
	}
	
	function processDeleteProduct(codeProduct)
	{
		var frm = document.getElementById('frmProcess');
		frm.action.value = "deleteProduct";
		frm.section.value = codeProduct;
		frm.submit();
	}
	
	function processDeleteBasket()
	{
		var confirmation = confirm("Premeu OK per confirmar.");
		if(confirmation == true)
		{
			var frm = document.getElementById('frmProcess');
			frm.action.value = "deleteBasket";
			frm.submit();
		}
	}
	
	function processShowHistory()
	{
		var frm = document.getElementById('frmProcess');
		frm.action.value = "showHistory";
		frm.submit();
	}
	function processCalculateBasket()
	{
		var frm = document.getElementById('frmBasket');
		var dataProducts = "";	
		var vacia = true;
		var count = frm.elements.length;
		// validamos que haya algun producto seleccionado
		/*for (i=0; i<count; i++) 
		{
			var element = frm.elements[i];
			if(element.value != 0 && element.type == 'text')
				vacia = false;
		}*/
		if(0 == 1)
			alert('Ha de seleccionar algun aticule avans de preme el boton comprar');
		else
		{
			for (i=0; i<count; i++) 
			{
				var element = frm.elements[i]; 
				if(element.type == 'text')
					dataProducts = dataProducts + element.name + "=" + element.value + ";";
			}
			
			var frm = document.getElementById('frmProcess');
			frm.method = "post";
			frm.action.value = "processCalculateBasket";
			frm.section.value = dataProducts;
			frm.option.value = "";
			frm.submit();
		}
	}

	function processShowBasket()
	{
		var frm = document.getElementById('frmProcess');
		frm.action.value = "showBasket";
		frm.submit();
	}
	
	function processSearch()
	{
	
		var textSearch = document.getElementById('search').value;
		if(textSearch != '')
		{
			var frm = document.getElementById('frmProcess');
			frm.action.value = "processSearch";
			frm.section.value = textSearch;
			frm.submit();
		}
		else
			alert('Ha de especificar algun text per poder buscra.');
	}
	
	function processOrderBasket()
	{
		var frm = document.getElementById('frmProcess');
		frm.action.value = "processOrderBasket";
		frm.submit();
	}
	
	function processConfirmData()
	{
		var frm = document.getElementById('frmOrder');
		frm.action.value = "processConfirmData";
		frm.option.value = "";
		frm.section.value = "";
		frm.submit();
	}
	
	function processPayOrder()
	{
		var frm = document.getElementById('frmOrder');
		frm.action.value = "processPayOrder";
		if(frm.conditions.checked)
			frm.submit();
		else
			alert('Per continuar la comanda ha de marcar la casella de les condicions.');
			
	}
	function processPrintOrder(orderNumber)
	{
		var frm = document.getElementById('frmProcess');
		frm.target = "_blank";
		frm.action.value = "showPrintTicket";
		frm.section.value = orderNumber;
		frm.method = "post";
		frm.submit();
		frm.method = "get";
	}
	function processBids(codeBid)
	{
		var frm = document.getElementById('frmProcess');
		frm.action.value = "showBid";
		frm.section.value = codeBid;
		frm.submit();
	}
	
	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_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_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 processContents(content)
	{
		var id_obj = "div_contenidos_empresa";
		document.getElementById (id_obj).innerHTML = '<br><br><br><div align="center"><img src="images/loading.gif"></div>';
		// mostrar la informacion del contenido en el div.
		var url = "contents.php?section=" + content ;
		getRequest (url, id_obj);
	}
	
	function gotoNextPage()
	{
		var sel = document.getElementById('page');		
		var i = eval(sel.selectedIndex + 1);
		if(sel.options[i] != null)
		{
			sel.options[i].selected = true;
			document.getElementById('frm_paginacion').submit();
		}
	}
	
	function gotoPreviusPage()
	{
		var sel = document.getElementById('page');		
		var i = eval(sel.selectedIndex - 1);
		if(sel.selectedIndex > 0)
		{
			sel.options[i].selected = true;
			document.getElementById('frm_paginacion').submit();
		}
	}
//////////////////////////////////////////////////////////////////////
//
// 	Funciones para AJAX	
//
//////////////////////////////////////////////////////////////////////
	
	function getRequest (url, id_obj)
	{
		var pageRequest = false;
		if (window.XMLHttpRequest)// Mozilla, Safari etc
		{
			pageRequest = new XMLHttpRequest ();
		} 
		else if (window.ActiveXObject)// IE
		{
			try 
			{
				pageRequest = new ActiveXObject ("Msxml2.XMLHTTP");
			}
			catch (e)// old version
			{
				try
				{
					pageRequest = new ActiveXObject ("Microsoft.XMLHTTP");
				}
				catch (e)
				{
				}
			}
		} 
		else
			return false;
		pageRequest.onreadystatechange = function ()
		{
			pageLoad (pageRequest, id_obj);
		}
		pageRequest.open ('GET', url, true);
		pageRequest.send (null);
	}
	
	function pageLoad (pageRequest, id_obj)
	{
		if (pageRequest.readyState == 4 && (pageRequest.status == 200 || window.location.href.indexOf ("http") == - 1))
			document.getElementById (id_obj).innerHTML = pageRequest.responseText;
	}
	
	function MM_validateForm() { //v4.0
	  if (document.getElementById){
		var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
		for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
		  if (val) { nm=val.name; if ((val=val.value)!="") {
			if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
			  if (p<1 || p==(val.length-1)) errors+='- '+nm+' no es un e-mail vŕlid.\n';
			} else if (test!='R') { num = parseFloat(val);
			  if (isNaN(val)) errors+='- '+nm+' no conte un numero.\n';
			  if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
				min=test.substring(8,p); max=test.substring(p+1);
				if (num<min || max<num) errors+='- '+nm+' no conte un numero entre '+min+' i '+max+'.\n';
		  } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' es necessari.\n'; }
		} if (errors) alert('Els seguents errors s\'han produit:\n'+errors);
		document.MM_returnValue = (errors == '');
	} }
	
	function processDeleteOrder()
	{
		var frm = document.getElementById('frmProcess');
		frm.action.value = "init";
		frm.submit();
	}
	
	function processLogOut()
	{
		var confirmation = confirm("Premeu OK per confirmar.");
		if(confirmation == true)
		{
			var frm = document.getElementById('frmLogin');
			frm.submit();
		}
	}
//////////////////////////////////////////////////////////////////////
//
//	showScales(code) - muestra los baremosd el producto
//
//////////////////////////////////////////////////////////////////////	
	function showScales(code)
	{
		TagToTip('scales');
		var id_obj = "scales";
		document.getElementById (id_obj).innerHTML = '<img src="images/loading.gif">';
		var url = "process.php?action=getScales&id=" + code ;
		getRequest (url, id_obj);
		
	}
//////////////////////////////////////////////////////////////////////
//
//	hideScales() - oculta los baremosd el producto
//
//////////////////////////////////////////////////////////////////////	
	function hideScales()
	{
		TagToTip('scales');
	}	
//////////////////////////////////////////////////////////////////////
//
//	confirmAlert() - Alerta de confirmacion
//
//////////////////////////////////////////////////////////////////////
	function confirmAlert(txt) 
	{ 
		var msg = confirm(txt);
		if (msg) return true;
		else return false ;
	}
//////////////////////////////////////////////////////////////////////
//
//	processSaveMyBasket() - guardar la cesta
//
//////////////////////////////////////////////////////////////////////
	function processSaveMyBasket(nameBasket) 
	{ 
		jPrompt('Introdueixi un nom per la cistella:', nameBasket, 'Guardar cistella', function(r) {
    		if( r == '' )
			{
				jAlert('El camp esta buit!', 'Guardar cistella');
			}
			else if( r != null )
			{
				$.ajax({
				   type: "POST",
				   url: "process.php",
				   data: "action=saveMyBasket&option=" + r,
				   success: function(msg){
					 jAlert( "Cistella guardada amb el nom: " + msg , 'Guardar cistella' );
					 window.location = "?action=showBasketSaved";
				   }
				 });	
				
			}
		});
	}
//////////////////////////////////////////////////////////////////////
//
//	alert() - modificamos el objeto alert de js
//
//////////////////////////////////////////////////////////////////////
	var oAlert = alert;
	function alert(txt, title) 
	{
		try {
			jAlert(txt, title);
		} catch (e) {
			oAlert(txt);
		}
	}
