function validate_input(elem, e, match_to){
	//regexes for validation, standard definitions include hyphen space and apostrophe
	//_s denotes strict which does not include any punctuation or spaces
	var al = /^[a-zA-Z-' ]+$/;
	var nu = /^[0-9-' ]+$/;
	var alnu = /^[a-zA-Z0-9#-' ]+$/;

	var al_s =  /^[a-zA-Z]+$/;
	var nu_s = /^[0-9]+$/;
	var alnu_s = /^[a-zA-Z0-9-' ]+$/;
	var em = /[a-zA-Z0-9@._-~]/;

	var email = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	var reg_ex = eval(match_to);

	if (!e) var e = window.event
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	var character = String.fromCharCode(code);

	if(e.keyCode==8 || e.keyCode==9 || e.keyCode==35 || e.keyCode==36 || e.keyCode==46) return true;

	if(character.match(reg_ex)){
		return true;
	}else{
		return false;
	}
}

function same_as_billing(){
	if(document.getElementById('bill_to_ship').checked == true ){
		document.getElementById('first_name').value = document.getElementById('ship_first_name').value;
		document.getElementById('last_name').value = document.getElementById('ship_last_name').value;
		document.getElementById('street_address_1').value = document.getElementById('ship_street_address_1').value;
		document.getElementById('street_address_2').value = document.getElementById('ship_street_address_2').value;
		document.getElementById('city').value = document.getElementById('ship_city').value;
		document.getElementById('zip').value = document.getElementById('ship_zip').value;
	}else{
		
	}
}

function numbers_only(e){
	var unicode=e.charCode? e.charCode : e.keyCode
	if (unicode!=8){ //if the key isn't the backspace key (which we should allow)
		if (unicode<48||unicode>57) //if not a number
		return false //disable key press
	}
}

function letters_only(e){
	var unicode=e.charCode? e.charCode : e.keyCode
	if (unicode!=8){ //if the key isn't the backspace key (which we should allow)
		if (unicode<65 || unicode>90){ //if not a number
			if(unicode<97 || unicode >122){
				return false //disable key press
			}
		}
	}
}

function set_form_country(country,dropdown){
	var states = new Array();
	var state_code = new Array();
	var provinces = new Array();
	var province_code = new Array();

	//set up states
		states[0] = "Alabama";         state_code[0] = "AL";
		states[1] = "Alaska";          state_code[1] = "AK";
		states[2] = "Arizona";         state_code[2] = "AZ";
		states[3] = "Arkansas";        state_code[3] = "AR";
		states[4] = "California";      state_code[4] = "CA";
		states[5] = "Colorado";        state_code[5] = "CO";
		states[6] = "Connecticut";     state_code[6] = "CT";
		states[7] = "Delaware";        state_code[7] = "DE";
		states[8] = "Florida";         state_code[8] = "FL";
		states[9] = "Georgia";         state_code[9] = "GA";
		states[10] = "Hawaii";         state_code[10] = "HI";
		states[11] = "Idaho";          state_code[11] = "ID";
		states[12] = "Illinois";       state_code[12] = "IL";
		states[13] = "Indiana";        state_code[13] = "IN";
		states[14] = "Iowa";           state_code[14] = "IA";
		states[15] = "Kansas";         state_code[15] = "KS";
		states[16] = "Kentucky";       state_code[16] = "KY";
		states[17] = "Louisiana";      state_code[17] = "LA";
		states[18] = "Maine";          state_code[18] = "ME";
		states[19] = "Maryland";       state_code[19] = "MD";
		states[20] = "Massachusetts";  state_code[20] = "MA";
		states[21] = "Michigan";       state_code[21] = "MI";
		states[22] = "Minnesota";      state_code[22] = "MN";
		states[23] = "Mississippi";    state_code[23] = "MS";
		states[24] = "Missouri";       state_code[24] = "MO";
		states[25] = "Montana";        state_code[25] = "MT";
		states[26] = "Nebraska";       state_code[26] = "NE";
		states[27] = "Nevada";         state_code[27] = "NV";
		states[28] = "New Hampshire";  state_code[28] = "NH";
		states[29] = "New Jersey";     state_code[29] = "NJ";
		states[30] = "New Mexico";     state_code[30] = "NM";
		states[31] = "New York";       state_code[31] = "NY";
		states[32] = "North Carolina"; state_code[32] = "NC";
		states[33] = "North Dakota";   state_code[33] = "ND";
		states[34] = "Ohio";           state_code[34] = "OH";
		states[35] = "Oklahoma";       state_code[35] = "OK";
		states[36] = "Oregon";         state_code[36] = "OR";
		states[37] = "Pennsylvania";   state_code[37] = "PA";
		states[38] = "Rhode Island";   state_code[38] = "RI";
		states[39] = "South Carolina"; state_code[39] = "SC";
		states[40] = "South Dakota";   state_code[40] = "SD";
		states[41] = "Tennessee";      state_code[41] = "TN";
		states[42] = "Texas";          state_code[42] = "TX";
		states[43] = "Utah";           state_code[43] = "UT";
		states[44] = "Vermont";        state_code[44] = "VT";
		states[45] = "Virginia";       state_code[45] = "VA";
		states[46] = "Washington";     state_code[46] = "WA";
		states[47] = "West Virginia";  state_code[47] = "WV";
		states[48] = "Wisconsin";      state_code[48] = "WI";
		states[49] = "Wyoming";        state_code[49] = "WY";
	
	//set up provinces
		provinces[0] = "Alberta";				province_code[0]="AB";
		provinces[1] = "British Columbia";		province_code[1]="BC";
		provinces[2] = "Manitoba";				province_code[2]="MB";
		provinces[3] = "New Brunswick";			province_code[3]="NB";
		provinces[4] = "Newfoundland and Lab.";	province_code[4]="NL";
		provinces[5] = "Northwest Territories";	province_code[5]="NT";
		provinces[6] = "Nova Scotia";			province_code[6]="NS";
		provinces[7] = "Nunavut";				province_code[7]="NU";
		provinces[8] = "Ontario";				province_code[8]="ON";
		provinces[9] = "Prince Edward Island";	province_code[9]="PE";
		provinces[10] = "Quebec";				province_code[10]="QC";
		provinces[11] = "Saskatchewan";			province_code[11]="SK";
		provinces[12] = "Yukon";				province_code[12]="YT";
	
	drp = document.getElementById('state');
	
	for(i=drp.options.length-1;i>=0;i--){
		drp.remove(i);
	}

	if(country == 'US'){
        	for (i=0;i<states.length;i++) {
            		drp.options[i] = new Option(states[i], state_code[i], false, false);
        	}
	}else if(country == 'CA'){
        	for (i=0;i<provinces.length;i++) {
            		drp.options[i] = new Option(provinces[i], province_code[i], false, false);
        	}
	}else{
	        drp.options[0] = new Option('International', 'ZZ', false, false);
	}

	document.getElementById('country').value=country;
//	document.getElementById('floatdiv').style.display='none';
}


function set_card_type(ctype){
	if(ctype==undefined){
		ctype = document.order_form.card_type;
	}
		
	switch(ctype.value){
		case 'mc':
		case 'vi':
			document.order_form.credit_card_number_1.size = 4;
			document.order_form.credit_card_number_1.maxLength = 4;
			document.order_form.credit_card_number_2.size = 4;
			document.order_form.credit_card_number_2.maxLength = 4;
			document.order_form.credit_card_number_3.size = 4;
			document.order_form.credit_card_number_3.maxLength = 4;
			document.order_form.credit_card_number_4.size = 4;
			document.order_form.credit_card_number_4.maxLength = 4;
		break;
		case 'ax':
			document.order_form.credit_card_number_1.size = 4;
			document.order_form.credit_card_number_1.maxLength = 4;
			document.order_form.credit_card_number_2.size = 3;
			document.order_form.credit_card_number_2.maxLength = 3;
			document.order_form.credit_card_number_3.size = 3;
			document.order_form.credit_card_number_3.maxLength = 3;
			document.order_form.credit_card_number_4.size = 5;
			document.order_form.credit_card_number_4.maxLength = 5;
		break;
	}		
}
	
function enter_card_data(cfield){
	if(cfield.value.length == cfield.maxLength){
		if(cfield.name[cfield.name.length-1]<4){
			var next = cfield.name.substr(0,cfield.name.length-1)+(cfield.name[cfield.name.length-1]*1+1);
			document.order_form.elements[next].focus();
		}else{
			document.order_form.credit_card_cvv2.focus();
		}
	}
}

function change_country(dropdown){
	document.getElementById('floatdiv').style.display='block';
	document.getElementById('floatdiv').style.top=document.body.scrollTop+10;
}

function set_dropdown_country(country){
	var country_select = document.getElementById('country');
	country_select.value = country;
}

function check_form(){
        var err = '';
        var fields = new Array('ship_first_name','ship_last_name','ship_street_address_1','ship_city','ship_zip','phone_number','email_address');

// 2010-01-12 : adam : removed because (cleancoloncleanse.com/mobile is only page that uses 'i_agree' -> no traffic & this check breaks other pages)
//	if(!document.getElementById("i_agree").checked){
//		alert('You must indicate that you understand the payment procedure.');
//		document.getElementById('i_agree').checked=true;
//		return false;
//	}

        for(f in fields){
                var _f = fields[f];
                if(document.getElementById(_f).value.length == 0){
                        err = "Uh oh! You left out some important information that we need to fill your RISK FREE order - Please double check and try again.\n";
                }
        }
                
        if(err.length!=0){
                alert(err);
                return false;
        }else{
                return true;
        }
}

