// JavaScript Document

if(nascar) {
}else{
	var nascar={};
}

nascar.validateGiftCert = function(orderForm) {
	var EmailOk  = true;
	var MessageOk = true;
	var AmountOk = true;
	var NumberOk = true;
	var Temp     = orderForm.email;
	var AtSym    = Temp.value.indexOf('@');
	var Period   = Temp.value.lastIndexOf('.');
	var Space    = Temp.value.indexOf(' ');
	var Length   = Temp.value.length - 1;
	var Amount   = orderForm.amount.value;
	var maxAmount = 1000;
	var submitForm = true;

	var toOk   = true;
	var fromOk = true;
	
	function trim(str)
	{
		while (str.charAt(0) == ' ')
		{
		     str = str.substring(1);
		}
	     //trim off trailing spaces
	    while (str.charAt(str.length-1) == ' ')
		{
		     str = str.substring(0,str.length-1);
		}
	 return str;
	}

	if((trim(orderForm.to.value)).length == 0)
	{ 
		toOk = false;
		submitForm=false;
		alert('Please enter the name of the recipient.');

	}

	if ((AtSym < 1) || (Period <= AtSym+1) || (Period == Length ) || (Space  != -1))
	{
		EmailOk = false;
		submitForm=false;
	    alert('Please enter a valid e-mail address.');
	}
	if (orderForm.message.value.length > 120 || orderForm.to.value.length == 0  || orderForm.from.value.length == 0)
	{
		MessageOk = false;
		submitForm=false;
		if(orderForm.message.value.length > 120)
		{
			orderForm.message.value = orderForm.message.value.substring(0,120);
			alert('The message you entered exceeded 120 characters. \n We have trimmed your message to fit. Please double-check your message.');
		}
		else
		{
			alert('Please be certain you have completed all required fields, and have agreed to the Terms and Conditions.');
		}
	}

	for (i = 0; i < Amount.length; i++)
 	{
		if (!((Amount.charAt(i) >= "0") && (Amount.charAt(i) <= "9")))
		{
			AmountOk = false;
			submitForm=false;
			break;
		}
	}

	if (!isNaN(Amount))
	{
		for (j = 0; j < Amount.length; j++)
		{
			if (Amount.charAt(j) == ".")
			{
				alert('Please enter a whole dollar amount');
				AmountOk = true;
				NumberOk = false;
				submitForm=false;
				orderForm.amount.focus();
				break;
			}
		}
	}
	if (!AmountOk)
	{   submitForm=false;
		alert('The amount you entered is not a number.\nPlease enter a number greater than or equal to 5');
		orderForm.amount.focus();
	}
	if (Amount < 5)
	{   submitForm=false;
		AmountOk = false;
		alert('Please enter a number greater than or equal to 5.');
		orderForm.amount.focus();
	}
	if (Amount > maxAmount)
	{   submitForm=false;
		AmountOK = false;
		alert('We apologize, however, at this time we only accept Online Gift Certificate purchases up to $1000.\nPlease contact Customer Service if there are any questions.');
		orderForm.amount.focus();
	}

    if((trim(orderForm.from.value)).length == 0)
	{    submitForm=false;
		 fromOk = false;
		 alert('Please enter the name of the sender.');
	}

  if(!submitForm)
	  return false;
  else if (toOk && fromOk && MessageOk && EmailOk && AmountOk && NumberOk && Amount < (maxAmount+1))
	{	
	orderForm.submit();
	
	}
	
}
