/****FUNCION QUE VALIDA EL FORMULARIO DE REGISTRO DE LA CESTA*******/
function ValidarFormulario(formulario)
{
	var flag=true;
	var reg_mail = /^[\.A-Za-z0-9_-]+\@[\.A-Za-z0-9_-]+\.[\.A-Za-z0-9_-]+$/;
	
	with(formulario)
	{
		if(PRIMER_APELLIDO.value == "")
		{
			flag=false;
			alert("Please specify a FIRST NAME");
		}
		if(DIRECCION.value == "")
		{
			flag=false;
			alert("Please specify an STREET");
		}
		if(LOCALIDAD.value == "")
		{
			flag=false;
			alert("Please specify a CITY");
		}
		if(CODIGO_POSTAL.value == "")
		{
			flag=false;
			alert("Please specify a ZIP CODE");
		}
		if(PROVINCIA.value == "")
		{
			flag=false;
			alert("Please specify a STATE");
		}
		if(TELEFONO.value == "")
		{
			flag=false;
			alert("Please specify a PHONE");
		}
		if(EMAIL.value == "")
		{
			flag=false;
			alert("You didn't specify a valid E-MAIL");
		}
		else
		{
			if (!(reg_mail.test(EMAIL.value)))
			{
				flag=false;
				alert("You didn't specify a valid E-MAIL");
			}
		}
		if(PWD.value == "")
		{
			flag=false;
			alert("Please choose a password");
		}
	}
	return flag;
}

/****FUNCION QUE VERFICA EL TIPO DE DATO DE LA CANTIDAD Y QUE NO SUPERE LA CANTIDAD DE PRODUCTOS EXISTENTES******/
function CantidadNumerica(formulario,CantDisponible)
{
	if(formulario.name.toLowerCase() == "catalogo")
	{
		with(formulario)
		{
			if(cantidad.value == "")
			{
				alert("Debe introducir un valor numerico en la casilla cantidad");
				return false;
			}
			else
			{
				if((cantidad.value >= 0 && cantidad.value <=1000))
				{
					if(cantidad.value > CantDisponible)
					{
						alert("No se dispone de la cantidad introducida para este producto");
						return false;
					}
				}
				else
				{
					if(cantidad.value < 0)
					{
						alert("Ha introducido una cantidad negativa");
						return false;
					}
					else
					{
						if(cantidad.value > 1000)
						{
							alert("No se dispone de la cantidad introducida");
							return false;
						}
						else
						{
							alert("Debe introducir un valor númerico en la casilla de cantidad");
							return false;
						}
					}
				}
			}
		}
	}
	
	
	if(formulario.name.toLowerCase() == "cesta")
	{	
		with(formulario)
		{
			for(i=0;i<elements.length;i++)
			{
				if(elements[i].name.substring(0,8).toLowerCase() == "cantidad")
				{
					if(elements[i].value == "")
					{
						alert("Debe introducir un valor numerico en la casilla cantidad");
						return false;
					}
					else
					{
						if(!(elements[i].value >= 0 && elements[i].value <=1000))
						{
							if(elements[i].value < 0)
							{
							alert("Ha introducido una cantidad negativa");
							return false;
						}
						else
						{
							if(elements[i].value > 1000)
							{
								alert("Ha introducido una cantidad superior a la permitida");
								return false;
							}
							else
							{
								alert("Debe introducir un valor númerico en la casilla de cantidad");
								return false;
							}
						}
					  }
					}
				}
			}
		}
	}	
	
}
