
  
  var Cesta = new Array();
  
  function GetCesta()
  {
  		return Cesta;
  }

  function FInit()
  {
	var fr = document.forms[0];
	fr.Productos.value = "0";
	fr.Total.value = "0";

  }
  
  function Producto (ref,des,cant,prec)
  {
	this.referencia = ref;
	this.descripcion = des;
	this.cantidad = cant;
	this.precio = prec;
  }
  
  function GetTotal()
  {
  	var fr = document.forms[0];
  
  	return parseFloat(fr.Total.value);
  
  }
  
  function rd(x)
  {
	return Math.round(100*x)/100
  }

  function Configure()
  {
  	var Cantidad = 0;
  	var Precio = 0;
  	var PTotal = 0;
  	var x = 0;
  	var Cont = 0;
  	var fr = document.forms[0];
  
  	Cantidad = parseInt(fr.Productos.value);
  	Precio = parseFloat(fr.Total.value);
  	
  	for(x=0;x<Cesta.length;x++)
  	{
  		if (Cesta[x].referencia != "-")
  		{
  			Cantidad = parseInt(Cesta[x].cantidad);
  			Precio = parseFloat(Cesta[x].precio.replace(",",".")) * Cantidad;
  			
			PTotal += Precio;
			
  		}
  		else
  			Cont ++;
  	}
  	
  	fr.Productos.value = (x - Cont);
	
  	fr.Total.value = rd(PTotal);
  	
  		
  }
  
  function Indice()
  {
 		var x = 0;
 		
  		for(x=0;x<Cesta.length;x++)
  		{	
  			if (typeof(Cesta[x]) == "undefined")
  			  		return x;
  		}
  					return x;
  }
  
  function Insertar(ref,des,cant,prec)
  {
  	var indice = Indice();
  	
  	if(ValidarReferencia(ref,cant))
  	{
  		Ver();
  		alert("El producto ha sido actualizado.");
  		return false;
  	}
  	
	Cesta[indice] = new Producto(ref,des,cant,prec);
	
	Ver();
  }

  function ValidarReferencia(ref,cant)
  {
  	for(var x=0;x<Cesta.length;x++)
  		if(Cesta[x].referencia == ref)
  		{
  			Cesta[x].cantidad = cant;
  			return true;
  		}
  			
  			return false;
  }
  
  function EliminarAll()
  {
  		Cesta= null;
  		Cesta = new Array();
		FInit();
  }
  
  function Eliminar(ref)
  {
  	var PrecioTot = 0;
  	var PrecioArticulo = 0;
  	var CantidadArticulo = 0;
  	var fr = document.forms[0];
  	 
  	for(var x=0;x<Cesta.length;x++)
  	{
  		if(Cesta[x].referencia == ref)
  		{
  			PrecioTot= parseFloat(fr.Total.value);
  			PrecioArticulo = parseFloat(Cesta[x].precio.replace(",","."));
  			CantidadArticulo = parseInt(Cesta[x].cantidad);
  			fr.Total.value = PrecioTot - (PrecioArticulo * CantidadArticulo);
  			fr.Productos.value = (fr.Productos.value - 1)

  			Cesta[x].referencia = "-";
  			Cesta[x].descripcion = Cesta[x].descripcion + " - (Anulado)";
  			Cesta[x].cantidad = "0";
  			Cesta[x].precio = "0";  			
  			Ver();
  		}
  	}

  }

  function Anadir()
  {
  	var obj = document.frm;
  	var referencia = obj.ref.value;
  	var descripcion = obj.des.value;
  	var cantidad = obj.cant.value;
  	var precio = obj.prec.value;
  	  	
  	Insertar(referencia ,descripcion ,cantidad ,precio);

  }
  
  function Ver()
  {
  	//for(var x=0;x<Cesta.length;x++)
  	//		alert(Cesta[0].precio);
  			

  			Configure();
  }
  
  //-------------------------------------------------------------------------
  
  function showtime (){
	var now = new Date();
	var hours= now.getHours();
	var minutes= now.getMinutes();
	var seconds= now.getSeconds();
	var months= now.getMonth();
	var dates= now.getDate();
	var years= now.getYear();
	var timeValue = "";
	timeValue += ((months >9) ? "" : " ");
	timeValue += ((dates >9) ? "" : " ");
	timeValue = ( months +1);
	timeValue +="/"+ dates;
	timeValue +="/"+  years;
	var ap="A.M.";
	if (hours == 12) {
		ap = "P.M.";
	}
	if (hours == 0) {
		hours = 12;
	}
	if(hours >= 13){
		hours -= 12;
		ap="P.M.";
	}
	var timeValue2 = " " + hours;
	timeValue2 += ((minutes < 10) ? ":0":":") + minutes + " " + ap;
	return timeValue2;
}

function MakeArray(n) {
	this.length = n;
	return this;
}

monthNames = new MakeArray(12);
monthNames[1] = "de Enero";
monthNames[2] = "de Febrero";
monthNames[3] = "de Marzo";
monthNames[4] = "de Abril";
monthNames[5] = "de Mayo";
monthNames[6] = "de Junio";
monthNames[7] = "de Julio";
monthNames[8] = "de Agosto";
monthNames[9] = "de Septiembre";
monthNames[10] = "de Octubre";
monthNames[11] = "de Noviembre";
monthNames[12] = "de Diciembre";
daysNames = new MakeArray(7);
daysNames[1] = "Domingo";
daysNames[2] = "Lunes";
daysNames[3] = "Martes";
daysNames[4] = "Miercoles";
daysNames[5] = "Jueves";
daysNames[6] = "Viernes";
daysNames[7] = "Sabado";

function customDateSpring(oneDate,st) {
	var theDay = daysNames[oneDate.getDay() +1];
	var theDate =oneDate.getDate();
	var theMonth = monthNames[oneDate.getMonth() +1];
	var dayth="th";

	if ((theDate == 1) || (theDate == 21) || (theDate == 31)) {
		dayth="st";
	}
	if ((theDate == 2) || (theDate ==22)) {
		dayth="nd";
	}
	if ((theDate== 3) || (theDate  == 23)) {
		dayth="rd";
	}
	if (st==1)
	{	
		//return theDay + ", " + theDate +  " " + theMonth + ",";
		return theDate +  " " + theMonth + ",";
	}else{
		//return theDay + ", " + theDate +  " " + theMonth + " ";
		return theDate +  " " + theMonth + " ";
	}
}
