
function _CF_onError(form_object, input_object, object_value, error_message)
    {
	alert(error_message);
       	return false;	
    }



function _CF_hasValue(obj, obj_type)
    {
    if (obj_type == "TEXT" || obj_type == "PASSWORD")
	{
    	if (obj.value.length == 0) 
      		return false;
    	else 
      		return true;
    	}
    else if (obj_type == "SELECT")
	{
        for (i=0; i < obj.length; i++)
	    	{
		if (obj.options[i].selected)
			return true;
		}

       	return false;	
	}
    else if (obj_type == "SINGLE_VALUE_RADIO" || obj_type == "SINGLE_VALUE_CHECKBOX")
	{

		if (obj.checked)
			return true;
		else
       		return false;	
	}
    else if (obj_type == "RADIO" || obj_type == "CHECKBOX")
	{

        for (i=0; i < obj.length; i++)
	    	{
		if (obj[i].checked)
			return true;
		}

       	return false;	
	}
	}



function _CF_checkinteger(object_value)
    {
    //Returns true if value is a number or is NULL
    //otherwise returns false	

    if (object_value.length == 0)
        return true;

    //Returns true if value is an integer defined as
    //   having an optional leading + or -.
    //   otherwise containing only the characters 0-9.
	var decimal_format = ".";
	var check_char;

    //The first character can be + -  blank or a digit.
	check_char = object_value.indexOf(decimal_format)
    //Was it a decimal?
    if (check_char < 1)
	return _CF_checknumber(object_value);
    else
	return false;
    }



function _CF_checknumber(object_value)
    {
    //Returns true if value is a number or is NULL
    //otherwise returns false	

    if (object_value.length == 0)
        return true;

    //Returns true if value is a number defined as
    //   having an optional leading + or -.
    //   having at most 1 decimal point.
    //   otherwise containing only the characters 0-9.
	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;

    //The first character can be + - .  blank or a digit.
	check_char = start_format.indexOf(object_value.charAt(0))
    //Was it a decimal?
	if (check_char == 1)
	    decimal = true;
	else if (check_char < 1)
		return false;
        
	//Remaining characters can be only . or a digit, but only one decimal.
	for (var i = 1; i < object_value.length; i++)
	{
		check_char = number_format.indexOf(object_value.charAt(i))
		if (check_char < 0)
			return false;
		else if (check_char == 1)
		{
			if (decimal)		// Second decimal.
				return false;
			else
				decimal = true;
		}
		else if (check_char == 0)
		{
			if (decimal || digits)	
				trailing_blank = true;
        // ignore leading blanks

		}
	        else if (trailing_blank)
			return false;
		else
			digits = true;
	}	
    //All tests passed, so...
    return true
    }



function _CF_checkzip(object_value)
    {
    if (object_value.length == 0)
        return true;
		
    if (object_value.length != 5 && object_value.length != 10)
        return false;

	// make sure first 5 digits are a valid integer
	if (object_value.charAt(0) == "-" || object_value.charAt(0) == "+")
        return false;

	if (!_CF_checkinteger(object_value.substring(0,5)))
		return false;

	if (object_value.length == 5)
		return true;
	
	// make sure

	// check if separator is either a'-' or ' '
	if (object_value.charAt(5) != "-" && object_value.charAt(5) != " ")
        return false;

	// check if last 4 digits are a valid integer
	if (object_value.charAt(6) == "-" || object_value.charAt(6) == "+")
        return false;

	return (_CF_checkinteger(object_value.substring(6,10)));
    }



function _CF_checkcreditcard(object_value)
    {
	var white_space = " -";
	var creditcard_string="";
	var check_char;


    if (object_value.length == 0)
        return true;

	// squish out the white space
	for (var i = 0; i < object_value.length; i++)
	{
		check_char = white_space.indexOf(object_value.charAt(i))
		if (check_char < 0)
			creditcard_string += object_value.substring(i, (i + 1));
	}	

	// if all white space return error
    if (creditcard_string.length == 0)
        return false;
	 
	 	
	// make sure number is a valid integer
	if (creditcard_string.charAt(0) == "+")
        return false;

	if (!_CF_checkinteger(creditcard_string))
		return false;

    // now check mod10

	var doubledigit = creditcard_string.length % 2 == 1 ? false : true;
	var checkdigit = 0;
	var tempdigit;

	for (var i = 0; i < creditcard_string.length; i++)
	{
		tempdigit = eval(creditcard_string.charAt(i))

		if (doubledigit)
		{
			tempdigit *= 2;
			checkdigit += (tempdigit % 10);

			if ((tempdigit / 10) >= 1.0)
			{
				checkdigit++;
			}

			doubledigit = false;
		}
		else
		{
			checkdigit += tempdigit;
			doubledigit = true;
		}
	}	
	return (checkdigit % 10) == 0 ? true : false;

    }


function  _CF_checkorderform(_CF_this)

    {

    if  (!_CF_hasValue(_CF_this.ship_name, "TEXT" )) 

        {

        if  (!_CF_onError(_CF_this, _CF_this.ship_name, _CF_this.ship_name.value, "Shipping Name is required."))

            {

            return false; 

            }

        }


    if  (!_CF_hasValue(_CF_this.ship_address1, "TEXT" )) 

        {

        if  (!_CF_onError(_CF_this, _CF_this.ship_address1, _CF_this.ship_address1.value, "Shipping Address is required."))

            {

            return false; 

            }

        }


    if  (!_CF_hasValue(_CF_this.ship_city, "TEXT" )) 

        {

        if  (!_CF_onError(_CF_this, _CF_this.ship_city, _CF_this.ship_city.value, "Shipping City is required."))

            {

            return false; 

            }

        }


    if  (!_CF_hasValue(_CF_this.ship_zip, "TEXT" )) 

        {

        if  (!_CF_onError(_CF_this, _CF_this.ship_zip, _CF_this.ship_zip.value, "Shipping Zip Code must be five digits."))

            {

            return false; 

            }

        }

		
		
    if  (!_CF_checkzip(_CF_this.ship_zip.value))

        {

        if  (!_CF_onError(_CF_this, _CF_this.ship_zip, _CF_this.ship_zip.value, "Shipping Zip Code must be at least five digits."))

            {

            return false; 

            }

        }
		
		
		
    if  (!_CF_hasValue(_CF_this.ship_phone, "TEXT" )) 

        {

        if  (!_CF_onError(_CF_this, _CF_this.ship_phone, _CF_this.ship_phone.value, "Shipping Phone number is required."))

            {

            return false; 

            }

        }


    if  (!_CF_hasValue(_CF_this.cc_name, "TEXT" )) 

        {

        if  (!_CF_onError(_CF_this, _CF_this.cc_name, _CF_this.cc_name.value, "Credit Card Holder (Name on card) is required."))

            {

            return false; 

            }

        }


    if  (!_CF_hasValue(_CF_this.cc_number, "TEXT" )) 

        {

        if  (!_CF_onError(_CF_this, _CF_this.cc_number, _CF_this.cc_number.value, "Credit Card number is invalid."))

            {

            return false; 

            }

        }


    if  (!_CF_checkcreditcard(_CF_this.cc_number.value))

        {

        if  (!_CF_onError(_CF_this, _CF_this.cc_number, _CF_this.cc_number.value, "Credit Card number is invalid."))

            {

            return false; 

            }

        }


    if  (!_CF_hasValue(_CF_this.email, "TEXT" )) 

        {

        if  (!_CF_onError(_CF_this, _CF_this.email, _CF_this.email.value, "Your Email is required. We use it to provide you with a receipt."))

            {

            return false; 

            }

        }


    if  (!_CF_hasValue(_CF_this.bill_address1, "TEXT" )) 

        {

        if  (!_CF_onError(_CF_this, _CF_this.bill_address1, _CF_this.bill_address1.value, "Billing Address is required."))

            {

            return false; 

            }

        }


    if  (!_CF_hasValue(_CF_this.bill_city, "TEXT" )) 

        {

        if  (!_CF_onError(_CF_this, _CF_this.bill_city, _CF_this.bill_city.value, "Billing City is required."))

            {

            return false; 

            }

        }


    if  (!_CF_hasValue(_CF_this.bill_zip, "TEXT" )) 

        {

        if  (!_CF_onError(_CF_this, _CF_this.bill_zip, _CF_this.bill_zip.value, "Billing Zip Code must be at least five digits."))

            {

            return false; 

            }

        }


    if  (!_CF_checkzip(_CF_this.bill_zip.value))

        {

        if  (!_CF_onError(_CF_this, _CF_this.bill_zip, _CF_this.bill_zip.value, "Billing Zip Code must be at least five digits."))

            {

            return false; 

            }

        }


    if  (!_CF_hasValue(_CF_this.bill_phone, "TEXT" )) 

        {

        if  (!_CF_onError(_CF_this, _CF_this.bill_phone, _CF_this.bill_phone.value, "Billing phone number is required."))

            {

            return false; 

            }

        }


    return true;

    }


