function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function validRequired(formField,fieldLabel)
{
	var result = true;
	
	if (formField.value == "")
	{
		alert('please enter a value for the "' + fieldLabel +'" field.');
		formField.focus();
		result = false;
	}
	
	return result;
}

function allDigits(str)
{
	return inValidCharSet(str,"0123456789");
}

function inValidCharSet(str,charset)
{
	var result = true;

	// Note: doesn't use regular expressions to avoid early Mac browser bugs	
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}
	
	return result;
}

function validEmail(formField,fieldLabel,required)
{
	var result = true;
	
	if (required && !validRequired(formField,fieldLabel))
		result = false;

	if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
	{
		alert("please enter a complete email address in the form: yourname@yourdomain.com");
		formField.focus();
		result = false;
	}
   
  return result;

}

function validNum(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		if (!allDigits(formField.value))
 		{
 			alert('please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
	} 
	
	return result;
}


function validInt(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;

	if (result)
 	{
    		var ValidChars = "0123456789";
		var Char;
		var num = formField.value;

   		for (i = 0; i < num.length && result == true; i++) 
      		{ 
     	 		Char = num.charAt(i); 
		      	if (ValidChars.indexOf(Char) == -1) 
         		{
 			alert('please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false; 
        		}
      		}

	} 
	
	return result;
}


function validQty(formField1,formField2,fieldLabel)
{
	var result = true;

	var num1 = Number(formField1.value);
	var num2 = Number(formField2.value);
	var totQty = num1 + num2;

    	if (totQty == 0) 
         	{
 		alert('please enter valid value for "' + fieldLabel +'" field.');
		formField1.focus();		
		result = false; 
        	}
	
	return result;
}


function validDate(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		var elems = formField.value.split("/");
 		
 		result = (elems.length == 3); // should be three components
 		
 		if (result)
 		{
 			var month = parseInt(elems[0],10);
  			var day = parseInt(elems[1],10);
 			var year = parseInt(elems[2],10);
			result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
					 allDigits(elems[1]) && (day > 0) && (day < 32) &&
					 allDigits(elems[2]) && ((elems[2].length == 2) || (elems[2].length == 4));
 		}
 		
  		if (!result)
 		{
 			alert('please enter a date in the format MM/DD/YYYY for the "' + fieldLabel +'" field.');
			formField.focus();		
		}
	} 
	
	return result;
}


function validExpire(expireMonth, expireYear, fieldLabel)
{
	var result = true;
	var ccExpYear = 20 + expireYear.value;
    	var ccExpMonth = expireMonth.value;
        	var expDate=new Date();
    	expDate.setFullYear(ccExpYear, ccExpMonth, 1);
    	var today = new Date();

    	if (expDate<today)
	{
		alert('please enter valid value for the "' + fieldLabel +'" field.');
		expireMonth.focus();		
		result = false;
	}
	
	return result;
}



function validCardType(cardNum, cardTyp, fieldLabel)
{
	var result = true;
	var strNum = cardNum.value;
	var type = cardTyp.value;
	var theResult;

	var nLen = 0;
	for (n = 0; n < strNum.length; n++)
	{
		if (isDigit(strNum.substring (n,n+1)))
		++nLen;
	}
   
	if (type == 'visa')
		theResult = ((strNum.substring(0,1) == '4') && (nLen == 13 || nLen == 16));
	else if (type == 'amex')
		theResult = ((strNum.substring(0,2) == '34' || strNum.substring(0,2) == '37') && (nLen == 15));
	else if (type == 'mastercard')
		theResult = ((strNum.substring(0,2) == '51' || strNum.substring(0,2) == '52' || strNum.substring(0,2) == '53' || strNum.substring(0,2) == '54' || strNum.substring(0,2) == '55') && (nLen == 16));
	else if (type == 'discover')
		theResult = ((strNum.substring(0,4) == '6011') && (nLen == 16));
	else if (type == 'diners')
		theResult = ((strNum.substring(0,2) == '30' || strNum.substring(0,2) == '36' || strNum.substring(0,2) == '38') && (nLen == 14));
	else if (type == 'enroute')
		theResult = ((strNum.substring(0,4) == '2014' || strNum.substring(0,4) == '2149') && (nLen == 15));
	else if (type == 'jcb')
		theResult = ((strNum.substring(0,4) == '3088' || strNum.substring(0,4) == '3096' || strNum.substring(0,4) == '3112' || strNum.substring(0,4) == '3158' || strNum.substring(0,4) == '3337' || strNum.substring(0,4) == '3528') && (nLen == 16));
	else
		theResult = false;

	result = theResult;

	if (!result)
	{
		alert('please enter valid value for the "' + fieldLabel +'" field or check that the card number is valid.');
		cardTyp.focus();		
		result = false;
	}
	
	return result;
}


function isDigit(c)
{
   var strAllowed = "1234567890";
   return (strAllowed.indexOf(c) != -1);
}



function validCardNumber(cardNum, fieldLabel)
{
	var result = true;
	var strNum = cardNum.value;
	var theResult;
   	var nCheck = 0;
   	var nDigit = 0;
   	var bEven = false;
   
   	for (n = strNum.length - 1; n >= 0; n--) 
   	{
      		var cDigit = strNum.charAt (n);
      		if (isDigit (cDigit))
      		{
         		var nDigit = parseInt(cDigit, 10);
         		if (bEven)
         		{
            			if ((nDigit *= 2) > 9)
               				nDigit -= 9;
         		}
         		nCheck += nDigit;
         		bEven = ! bEven;
      		}
      		else if (cDigit != ' ' && cDigit != '.' && cDigit != '-')
      		{
         		return false;
      		}
   	}
   	
	theResult = ((nCheck % 10) == 0);

	result = theResult;

	if (!result)
	{
		alert('please enter valid value for the "' + fieldLabel +'" field.');
		cardNum.focus();		
		result = false;
	}
	
	return result;

}



function validateBuyForm(theForm)
{

	// Customize these calls for your form

	// Start ------->

	if (!validInt(theForm.bot250mlV1,"250ml V1 bottle qty", true))
		return false;

	if (!validInt(theForm.bot500mlV2,"500ml V2 bottle qty", true))
		return false;

	if (!validQty(theForm.bot250mlV1, theForm.bot500mlV2,"bottle qty"))
		return false;


	// <--------- End
	
	return true;
}








function validateCartForm(theForm)
{

	// Customize these calls for your form

	// Start ------->


	if (!validInt(theForm.bot250mlV1,"250ml V1 bottle qty", true))
		return false;

	if (!validInt(theForm.bot500mlV2,"500ml V2 bottle qty", true))
		return false;

	if (!validQty(theForm.bot250mlV1, theForm.bot500mlV2,"bottle qty"))
		return false;

	if (!validRequired(theForm.Country,"country"))
		return false;

	if (!validRequired(theForm.State,"province / state"))
		return false;

	if (!validRequired(theForm.Zip,"postal / zip"))
		return false;

	// <--------- End
	
	return true;
}




function validatePayForm(theForm)
{

	// Customize these calls for your form

	// Start ------->

	if (!validCardType(theForm.Card, theForm.CardType,"card type"))
		return false;

	if (!validCardNumber(theForm.Card, "card number"))
		return false;

	if (!validRequired(theForm.Name,"name"))
		return false;

	if (!validRequired(theForm.Address,"address"))
		return false;

	if (!validRequired(theForm.City,"city"))
		return false;

	if (!validRequired(theForm.Phone,"phone"))
		return false;

	if (!validEmail(theForm.Email,"email"))
		return false;

	if (!validInt(theForm.ExpireM,"expiry month", true))
		return false;

	if (!validInt(theForm.ExpireY,"expiry year", true))
		return false;

	if (!validExpire(theForm.ExpireM,theForm.ExpireY,"expiry date"))
		return false;

	if (!validInt(theForm.Code,"security code", true))
		return false;

	// <--------- End
	
	return true;
}
