<!-- Hide from older browsers
// ----------------------------------------------------------------------------
//    Copyright (C) 1999   SENTIENT WEB UNLIMITED  
//    Author: Michael Tsu
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program; if not, write to the Free Software
//    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                  
// ----------------------------------------------------------------------------

var whitespace = " \t\n\r";

//-----------------------------------------------------------------------------
// Returns true if string s is empty or length 0 
//-----------------------------------------------------------------------------
function isEmpty(s)
{   return ((s == null) || (s.length == 0));
}

//-----------------------------------------------------------------------------
// Returns true if string s is empty or 
// whitespace characters only.
//-----------------------------------------------------------------------------

function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

//-----------------------------------------------------------------------------
//FUNCTION:  isCreditCard(st)
//
//INPUT:     st - a string representing a credit card number
//
//RETURNS:  true, if the credit card number passes the Luhn Mod-10
//		test.
//	  false, otherwise
//-----------------------------------------------------------------------------

function isCreditCard(st) {
  // Encoding only works on cards with less than 19 digits
  if (st.length > 19)
    return (false);

  sum = 0; mul = 1; l = st.length;
  for (i = 0; i < l; i++) {
    digit = st.substring(l-i-1,l-i);
    tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
// Uncomment the following line to help create credit card numbers
// 1. Create a dummy number with a 0 as the last digit
// 2. Examine the sum written out
// 3. Replace the last digit with the difference between the sum and
//    the next multiple of 10.

//  document.writeln("<BR>Sum      = ",sum,"<BR>");
//  alert("Sum      = " + sum);

  if ((sum % 10) == 0)
    return (true);
  else
    return (false);

} // END FUNCTION isCreditCard()



//-----------------------------------------------------------------------------
//    FUNCTION:  isVisa()
// 
//    INPUT:     cc - a string representing a credit card number
//
//    RETURNS:  true, if the credit card number is a valid VISA number.
//		    
//	      false, otherwise
//
//    Sample number: 4111 1111 1111 1111 (16 digits)
//-----------------------------------------------------------------------------

function isVisa(cc)
{
  if (((cc.length == 16) || (cc.length == 13)) &&
      (cc.substring(0,1) == 4))
    return isCreditCard(cc);
  return false;
}  // END FUNCTION isVisa()

//-----------------------------------------------------------------------------
//    FUNCTION:  isExpDate()
// 
//    INPUT:     expdate - a string representing a expiry date (4 digits MMYY)
//
//    RETURNS:  true, if expiry date is in correct MMYY format
//		    
//	      false, otherwise
//
//    Sample number: 0109
//-----------------------------------------------------------------------------

function isExpDate(expdate)
{


    if ( expdate.length != 4 ) {
        return false;
    }

    for (i=0; i< expdate.length; i++) {
        digit=parseInt( expdate.substring(i,i+1) )
        if ( isNaN(digit) ) {
            return false;         
        }
    }
    return true;

}

//-----------------------------------------------------------------------------
// OOS Food Drive donation page form validation
// EL: 2010Oct25
//-----------------------------------------------------------------------------
function validate_donationform( theForm )
{
 	if ( isWhitespace( theForm.firstname.value ) ) {
        window.alert("You must enter your first name." );
        theForm.firstname.focus();
        return false;
    }
	if ( isWhitespace( theForm.lastname.value ) ) {
        window.alert("You must enter your last name." );
        theForm.lastname.focus();
        return false;
    }
	if ( isWhitespace( theForm.username.value ) ) {
        window.alert("You must enter your email address." );
        theForm.username.focus();
        return false;
    }
	if ( isWhitespace( theForm.addr1.value ) ) {
        window.alert("You must enter your address." );
        theForm.addr1.focus();
        return false;
    }
 	if ( isWhitespace( theForm.city.value ) ) {
        window.alert("You must enter your city." );
        theForm.city.focus();
        return false;
    }
 	if ( theForm.province.value == "select" ) {
        window.alert("You must select your province." );
        theForm.province.focus();
        return false;
    }	
	if ( isWhitespace( theForm.postalcode.value ) ) {
        window.alert("You must enter your postal code." );
        theForm.postalcode.focus();
        return false;
    }
	if ( isWhitespace( theForm.phone.value ) ) {
        window.alert("You must enter a phone number where you can be reached." );
        theForm.phone.focus();
        return false;
    }
	if ( isWhitespace( theForm.donationamt.value ) ) {
        window.alert("You must enter a donation amount." );
        theForm.donationamt.focus();
        return false;
    }
 	if ( theForm.card_type.value == "select" ) {
        window.alert("You must select a credit card type." );
        theForm.card_type.focus();
        return false;
    }	
	if ( isWhitespace( theForm.card_num.value ) ) {
        window.alert("You must enter a credit card number." );
        theForm.card_num.focus();
        return false;
    }
    if (theForm.card_type.value == "visa") {
        if ( !isVisa( theForm.card_num.value ) ) {
        	window.alert("You entered an invalid credit card number." );
        	theForm.card_num.focus();
        	return false;
    	}	
    }
	if ( isWhitespace( theForm.sec_code.value ) ) {
        window.alert("You must enter the security code at the back of your credit card (3 digits)." );
        theForm.sec_code.focus();
        return false;
    }
	if ( isWhitespace( theForm.card_name.value ) ) {
        window.alert("You must enter the name that appears on the credit card." );
        theForm.card_name.focus();
        return false;
    }
	if ( isWhitespace( theForm.expiry.value ) ) {
        window.alert("You must enter an expiry date." );
        theForm.expiry.focus();
        return false;
    }
    if (!isExpDate( theForm.expiry.value ) ) {
        window.alert("Please enter 4 digits for the expiry date in the MMYY format." );
        theForm.expiry.focus();
        return false;
    }
	if ( !theForm.consent.checked ) {
		window.alert("On order to submit your donation you must agree to have O.O.S. Medical submit your donation to Food Banks Canada on your behalf by clicking the checkbox at the bottom of the donation form." );
        theForm.consent.focus();
        return false;
    }
	return true;
}

//-----------------------------------------------------------------------------
// Checkout Form Validation
//-----------------------------------------------------------------------------

function validate_checkout( theForm )
{

    if ( isWhitespace( theForm.name.value ) ) {
        window.alert("You must enter your name." );
        theForm.name.focus();
        return false;
    }

    if ( isWhitespace( theForm.address1.value ) ) {
        window.alert("You must enter an address." );
        theForm.address1.focus();
        return false;
    }

    if ( isWhitespace( theForm.city.value ) ) {
        window.alert("You must enter a city." );
        theForm.city.focus();
        return false;
    }

    if ( isWhitespace( theForm.state.value ) ) {
        window.alert("You must enter a state/province." );
        theForm.state.focus();
        return false;
    }

    if ( isWhitespace( theForm.country.value ) ) {
        window.alert("You must enter a country." );
        theForm.country.focus();
        return false;
    }

    if ( isWhitespace( theForm.zip.value ) ) {
        window.alert("You must enter a zip/postal code." );
        theForm.zip.focus();
        return false;
    }

    if ( isWhitespace( theForm.phone.value ) ) {
        window.alert("You must enter a contact number." );
        theForm.phone.focus();
        return false;
    }

    
    //if ( isWhitespace( theForm.email.value ) ) {
    //    window.alert("You must enter an email address." );
    //    theForm.email.focus();
    //    return false;
    //}

    // Validate shipping info

    if ( isWhitespace( theForm.ship_address1.value ) ) {
        window.alert("You must enter a shipping address." );
        theForm.ship_address1.focus();
        return false;
    }

    if ( isWhitespace( theForm.ship_city.value ) ) {
        window.alert("You must enter a shipping city." );
        theForm.ship_city.focus();
        return false;
    }

    if ( isWhitespace( theForm.ship_state.value ) ) {
        window.alert("You must enter a shipping state/province." );
        theForm.ship_state.focus();
        return false;
    }

    if ( isWhitespace( theForm.ship_country.value ) ) {
        window.alert("You must enter a shipping country." );
        theForm.ship_country.focus();
        return false;
    }

    if ( isWhitespace( theForm.ship_zip.value ) ) {
        window.alert("You must enter a shipping zip/postal code." );
        theForm.ship_zip.focus();
        return false;
    }
    if ( isWhitespace( theForm.ship_phone.value ) ) {
        window.alert("You must enter a shipping contact number." );
        theForm.ship_phone.focus();
        return false;
    }

    return true;
}

//-----------------------------------------------------------------------------
// Products Form Validation
//-----------------------------------------------------------------------------
function validate_add( theForm )
{
    var quant = 0;

    if ( theForm.quantity.value.length == 0 ) {
        window.alert("You must enter a quantity." );
        theForm.quantity.value = "";
        theForm.quantity.focus();
        return false;
    }

    quant = parseInt( theForm.quantity.value );

    if( (isNaN( quant )) || ( quant <= 0 ) ) {
        window.alert("Invalid quantity. Must be an integer value greater than 0.\n" );
        theForm.quantity.value = "";
        theForm.quantity.focus();
        return false;
    }

    if( quant > 1000 ) {
        window.alert("Invalid quantity. Maximum quantity allowed is 1000.\n" );
        theForm.quantity.value = "";
        theForm.quantity.focus();
        return false;
    }

    theForm.quantity.value = quant;
    return true;
}

//-----------------------------------------------------------------------------
// Credit Cart Validation
//-----------------------------------------------------------------------------

function validate_creditcard( theForm )
{
    if ( isWhitespace( theForm.card_num.value ) ) {
        window.alert("You must enter a credit card number." );
        theForm.card_num.focus();
        return false;
    }


    //right now we dont have master card credit card number validation, so just pass it thru
    if (theForm.card_type.value == "mastercard") {
        return true;
    }

    if ( !isVisa( theForm.card_num.value ) ) {
        window.alert("You entered an invalid credit card number." );
        theForm.card_num.focus();
        return false;
    }

    if (!isExpDate( theForm.expiry.value ) ) {
        window.alert("Please enter 4 digits for the expiry date in the MMYY format." );
        theForm.expiry.focus();
        return false;
    }

}

//-----------------------------------------------------------------------------
// Fill in the shipping info same as personal info
//-----------------------------------------------------------------------------

function SetShippingInfo( theForm )
{
    if( theForm.shipsame.checked == true ) {
        theForm.ship_address1.value = theForm.address1.value;
        theForm.ship_address2.value = theForm.address2.value;
        theForm.ship_city.value     = theForm.city.value;
        theForm.ship_state.value    = theForm.state.value;
        theForm.ship_country.value  = theForm.country.value;
        theForm.ship_zip.value      = theForm.zip.value;
        theForm.ship_phone.value    = theForm.phone.value;
    }
    else {
        theForm.ship_address1.value = "";
        theForm.ship_address2.value = "";
        theForm.ship_city.value     = "";
        theForm.ship_state.value    = "";
        theForm.ship_country.value  = "";
        theForm.ship_zip.value      = "";
        theForm.ship_phone.value    = "";
    }        
}

//-->
