
// change display of fields to required conditionally 
function change_required_fields (obj) {
// fill an array of values to change on
 values_array=['USA', 'Canada'];
// fill an array with fields to change 
 fields_to_change_array=['#fields_state', '#fields_zip'];
	current_value = $(obj).val();
	if(jQuery.inArray(current_value, values_array) >= 0){
		for(field in fields_to_change_array){
			if($(fields_to_change_array[field]).prev().html().search(/<span class="red">\*<\/span> /) == -1){
					$(fields_to_change_array[field]).addClass('req').prev().addClass('req').prepend('<span class="red">*</span> ');
				}
		}
	}
	else {
		for(field in fields_to_change_array){
			field = $(fields_to_change_array[field])
			label = field.prev();
			label_value = label.html();
			field.removeClass('req')
			label.removeClass('req').html(label_value.replace(/<span class="red">\*<\/span> /, ''))
		}
	}
}


// set up alert text var for following function to share.
var alert_text = '';

function validate_required(field, alerttxt)
 {
    f = $(field);
    // clear any old validation info
    f.removeClass('invalid')
    f.removeClass('valid')

    if (f.val() == null || f.val() == "" || f.val() == alerttxt)
    {
        alert_text += ' ' + alerttxt + ' \n'
        f.addClass('invalid')
        f.val(alerttxt)
        return false;
    }
    else
    {
        f.addClass('valid')
        return true;
    }

}


function validate_in_array(field, array, alerttxt)
 {

    f = $(field);
    // clear any old validation info
    f.removeClass('invalid');
    f.removeClass('valid');
    // current value
    var current_value = f.val();
    // does the value exist in the array?
    if (jQuery.inArray(current_value, array) >= 0) {
        f.addClass('valid')
        return true;
    }
    else
    //(current_value == null || current_value == "" || current_value == alerttxt)
    {
        alert_text += ' ' + alerttxt + ' \n'
        f.addClass('invalid')
        f.val(alerttxt)
        return false;
    }
}

function validate_with_regexp(field, regexp, alerttxt)
 {

    f = $(field);
    // clear any old validation info
    f.removeClass('invalid');
    f.removeClass('valid');
    // current value
    var current_value = f.val();
    // does the value exist in the array?
    if (current_value != '' && regexp.test(current_value)) {
        f.addClass('valid')
        return true;
    }
    else
    //(current_value == null || current_value == "" || current_value == alerttxt)
    {
        alert_text += ' ' + alerttxt + ' \n'
        f.addClass('invalid')
        return false;
    }
}


function cart_has_items() {
	if ($('.item_field').length > 0) {
		return true
	}
	return false
}

function validate_form(thisform)
 {
	var is_valid;
    // clear the alert text in case we have already faild the form
    alert_text = '';

	if( cart_has_items() ){

    	with(thisform)
	    {
	        if (validate_required(fields_fname, "Your first name is required.") == false)
	        {
	            is_valid = 'false'
	        }

	        if (validate_required(fields_lname, "Your last name is required.") == false)
	        {
	            is_valid = 'false'
	        }

	        if (validate_with_regexp(fields_email, /(^([^@\s]+)@((?:[-_a-z0-9]+\.)+[a-z]{2,})$)|(^$)/i , "A valid email address is required.") == false)
	        {
	            is_valid = 'false'
	        }

	        if (validate_required(fields_phone, "Your phone number is required.") == false)
	        {
	            is_valid = 'false'
	        }

	        if (validate_required(fields_address1, "Your address is required.") == false)
	        {
	            is_valid = 'false'
	        }

	        if (validate_required(fields_city, "Your city is required") == false)
	        {
	            is_valid = 'false'
	        }

	        if (validate_required(fields_country, "Your country is required.") == false)
	        {
	            is_valid = 'false'
	        }
    

	    // conditionally check state/zip for USA & Canada
	    if (fields_country.value == 'USA') {

	        var states = new Array("OR", "WA", "CA", "AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY", "PR","AS","GU","VI");
                                                                                                                                                                                                                                                                                                                                                                   
	        if (validate_in_array(fields_state, states, "A valid US state is required.") == false)                                                                                                                                                                                                                                                                     
	        {                                                                                                                                                                                                                                                                                                                                                          
	            is_valid = 'false'
	        }
	        if (validate_with_regexp(fields_zip, /\d{5}(-\d{4})?/, "A valid US zip code is required.") == false)
	        {
	            is_valid = 'false'
	        }
	    }

	    if (fields_country.value == 'Canada') {
	        var provinces = new Array("AB", "BC", "MB", "NB", "NF", "NT", "NS", "NU", "ON", "PE", "PQ", "SA", "YT");

	        if (validate_in_array(fields_state, provinces, "A valid Canadian province is required.") == false)
	        {
	            is_valid = 'false'
	        }
	        if (validate_with_regexp(fields_zip, /(^s*([a-z](\s)?\d(\s)?){3}$)s*/i, "A valid Canadian postal code is required.") == false)
	        {
	            is_valid = 'false'
	        }
	    }

	    // did we accrue an validation erro text?
	    if (alert_text != '') {
	        alert(alert_text)
	    }

	    // are we valid?
		if (is_valid != 'false'){
	    return true;
		}
	
		return false;
	}	
}
else{
alert('Your cart does not have any items at this time.')
return false
}
}


function is_numeric(sText)
{
	var ValidChars = "0123456789";
	var is_number=true;
	var Char;

	if (sText.length < 1)
	{
        is_number = false;
	} else {

		for (i = 0; i < sText.length && is_number == true; i++) 
		{ 
			Char = sText.charAt(i); 
			if (ValidChars.indexOf(Char) == -1) 
			{
				is_number = false;
			}
		}
	}
   return is_number;
}
