// Javascript file to Validate various forms throughout the website.

// File: validation.js

function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email.value);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
    {
		result = true;
	}
  }
  if(!result)
  {
  	email.focus();
  }
  return result;
}

function validRequired(formField,fieldLabel)
{
	var result = true;

	if (formField.value == "")
	{
		alert(fieldLabel);
		formField.focus();
		result = false;
	}
	return result;
}

function validateProduct(theForm)
{
	var MyReturn = false;
	var Quantities = theForm.elements;
	
	for(i=0; i < Quantities.length; i = i + 2)
	{
		if(Quantities[i].value > 0)
		{
			MyReturn = true;
		}
	}
	
	if(!MyReturn)
	{
		alert("All of your quantities are listed as 0!  Please enter at least one positive quantity to add to your cart.");
	}
	
	for(i=0; i < Quantities.length; i = i + 2)
	{
		if(Quantities[i].value > 4)
		{
			Quantities[i].value = 4;
			alert("This website has a limit of 100 brochures per item.  Brochures are delivered in sets of 25, so you may not select more than 4 of any brochures.");
			MyReturn = false;
		}
	}
	
	return MyReturn;
}

function validateCart(theForm)
{
	var MyReturn = true;
	var Elements = theForm.elements;
	
	for(i=1; i < Elements.length; i = i + 3)
	{
		if(Elements[i].value > 4)
		{
			alert("This website has a limit of 100 brochures per item.  Brochures are delivered in sets of 25, so you may not select more than 4 of any brochures.  The quantity listed as " + Elements[i].value + " will be set to 4.");
			Elements[i].value = 4;
		}
	}
	
	return MyReturn;
}

function validateCheckout(theForm)
{
	if(!isEmailAddr(theForm.elements["FieldValues[EMAIL]"]))
	{
		alert("We were unable to recognize the Email address you entered.  Please double check and try agian!");
		return false;
	}

	if(!validRequired(theForm.elements["FieldValues[FCONTACT]"], "We\'re sorry, but we had trouble reading the first name entered.  Please double check, and try again.  If our error persists, please contact us with the information below!"))
	{
		return false;
	}
	if(!validRequired(theForm.elements["FieldValues[LCONTACT]"], "We\'re sorry, but we had trouble reading the last name entered.  Please double check, and try again.  If our error persists, please contact us with the information below!"))
	{
		return false;
	}
	if(!validRequired(theForm.elements["FieldValues[ADDRESS1]"], "We\'re sorry, but we had trouble reading the address entered.  Please double check, and try again.  If our error persists, please contact us with the information below!"))
	{
		return false;
	}
	if(!validRequired(theForm.elements["FieldValues[CITY]"], "We\'re sorry, but we had trouble reading the city entered.  Please double check, and try again.  If our error persists, please contact us with the information below!"))
	{
		return false;
	}
	if(!validRequired(theForm.elements["FieldValues[ZIP]"], "We\'re sorry, but we had trouble reading the zip code entered.  Please double check, and try again.  If our error persists, please contact us with the information below!"))
	{
		return false;
	}
//	if(!validRequired(theForm.elements["FieldValues[CARDNUM]"], "We\'re sorry, but we had trouble reading in the credit card number you provided!  There is still one more screen before we will process the credit card you provide.  Please double check and try again."))
//	{
//		return false;
//	}

	return true;
}