﻿function msg(id, str)
{
	document.getElementById(id).firstChild.nodeValue = str; 
}

function isValidCardNumber(strNum) 
{
   if(strNum=="")
   {
	 return false;   
   }
   
   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;
      }
   }
   return (nCheck % 10) == 0;
}
function isDigit (c)
{
   var strAllowed = "1234567890";
   return (strAllowed.indexOf (c) != -1);
}
function isCardTypeCorrect (strNum, type)
{
   var nLen = 0;
   for (n = 0; n < strNum.length; n++)
   {
      if (isDigit (strNum.substring (n,n+1)))
         ++nLen;
   }
   
   if (type == 'Visa')
      return ((strNum.substring(0,1) == '4') && (nLen == 13 || nLen == 16));
   else if (type == 'Amex')
      return ((strNum.substring(0,2) == '34' || strNum.substring(0,2) == '37') && (nLen == 15));
   else if (type == 'Master Card')
      return ((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
      return false;
   
}

function orderCheck()
{
 	var month = document.checkout.expMon.value;	
	var year = document.checkout.expYear.value;
	var now = new Date(); 
	
	var curMonth = now.getMonth();
	var curYear = now.getFullYear(); 
	
	if(curYear>parseInt(year))
	{
			msg("error", "* Error: Credit Card Expired in Year "+year); 
			return false; 
	}
	else if(curYear == parseInt(year) )
	{
		if( (curMonth+1) > month)
		{
			msg("error", "* Error: Credit Card Expired in Month "+month); 
			return false;
		}
	}
	
	if(!isValidCardNumber(document.checkout.ccnum.value))
	{
		msg("error", "* Error: Invalid Credit Card Number"); 
		return false;
	}
	
	if(document.checkout.addressSel.value ==-1)
	{
		if(document.checkout.fName.value=="" ||
		   document.checkout.lName.value=="")
		{
			msg("error", "* Error: Please provide first/last name"); 
			return false; 
		}
		
		if(document.checkout.address.value=="" || 
		   document.checkout.city.value=="" || 
		   document.checkout.state.value=="" || 
		   document.checkout.zip.value=="" || 
		   document.checkout.email.value=="" || 
		   document.checkout.fNameb.value=="" || 
		   document.checkout.lNameb.value=="" || 
		   document.checkout.addressb.value=="" || 
		   document.checkout.cityb.value=="" || 
		   document.checkout.zipb.value=="" || 
		   document.checkout.ccfName.value=="" || 
		   document.checkout.cclName.value=="")
		{
			msg("error", "* Please fill out all required fields"); 
			return false; 	
		}
		
	}
	//msg("error", "No errors?"); 
	return true; 
}