function payment_option( opt ) {
	var options = [ 'paypal','google','check','cc','protx','po' ];
	$.each( options, function(o, option) {
		optid = "#" + option + "_payment_option";
		 if ( opt == option ) {
		 	$(optid).show('slow');
		 } else {
		 	$(optid).hide();
		 }
	});
}

function do_checkout( c_action ) {
	document.shipest.checkout_action.value = c_action;
	document.shipest.submit();
}

function write_country_select( sel_type, sel_country ) {
	
  	var sel = '<select name="' + sel_type + '[country]" id="country_select" onchange="update_locations_by_country(\'' + sel_type + '\')">';
	sel += '<option value="">Select a Country</option>';
	$.each( countries, function(i, country) {
		if( country.id == 7) {
			alert("Name:" + country.name);
		}
		( sel_country == country.id ) ? is_sel = " selected" : is_sel = "";
		sel += '<option value="' + country.id + '"' + is_sel + '>' + country.name + '</option>';
	});
	sel += '</select>';
	document.write(sel);
}	
  
function write_region_select( sel_type, sel_region, sel_country ) {
	var c_region = 0;
	var f_region = 0;
  	var sel = '<select name="' + sel_type + '[state]" id="region_select" onchange="update_locations_by_region(\'' + sel_type + '\')">';
	sel += '<option value="">Select a State / Region</option>';
	$.each( regions, function(i, region) {
		is_sel = "";
		if (sel_region == region.id) {
			f_region = 1;
			is_sel = " selected";
		}
		if (sel_country > 0) {
			if (sel_country == region.cid) {
				sel += '<option value="' + region.id + '"' + is_sel + '>' + region.name + '</option>';
				c_region++;
			}
		}
	});
	sel += '</select>';
	document.write(sel);
	if ( sel_country == 0 ) {
		$('#region_select').attr('disabled', true);
	}
	if (sel_country > 0) {
		if (c_region == 0) {
			$('#select_region').html('');
			$('#region_section').hide();
		}
	}	
}	  
  
function write_city_select( sel_type, sel_city, sel_city_name, sel_state, sel_country ) {
	var f_city = 0;
  	var sel = '<select name="' + sel_type + '[city]" id="city_select" onchange="check_for_other()">';
	sel += '<option value="">Select a City</option>';
	if (sel_city > 0) {
		$.each(cities, function(i, city){
			is_sel = "";
			if (sel_city == city.id) {
				f_city = 1;
				is_sel = " selected";
				sel += '<option value="' + city.id + '"' + is_sel + '>' + city.name + '</option>';
			}
			if (sel_state > 0) {
				if (sel_state == city.sid) {
					sel += '<option value="' + city.id + '"' + is_sel + '>' + city.name + '</option>';
				}
			}
		});
		if (f_city == 0) {
			(sel_city > 0) ? is_sel = " selected" : is_sel = "";
		}
		sel += '<option value="0"' + is_sel + '>Other</option>';
	}	
	sel += '</select>';
	document.write(sel);
	if ( f_city == 0 ) {
		if ( sel_city > 0 ) {
			var ohtml = '<input type="text" name="city_other" value="' + sel_city_name + '">';
			$(document).ready(function() {
				$('#city_other').html(ohtml);
				$('#city_other').show();
			});
		} else {
			$('#city_select').attr('disabled', true);
		}
	}	
}	

function build_region_select_by_country( sel_type, sel_country ) {
	var c_region = 0;
  	var sel = '<select name="' + sel_type + '[state]" id="region_select" onchange="update_locations_by_region(\'' + sel_type + '\')">';
	sel += '<option value="">Select a State / Region</option>';
	$.each( regions, function(i, region) {
		if (sel_country == region.cid) {
			sel += '<option value="' + region.id + '">' + region.name + '</option>';
			c_region++;
		}
	});
	sel += '</select>';
	if ( c_region > 0 ) {
		$('#select_region').html(sel);
		$('#region_select').removeAttr('disabled');
		$('#region_section').show();
		$('#city_select').attr('disabled', true);
	} else {
		$('#select_region').html('');
		$('#region_section').hide();
		$('#city_select').removeAttr('disabled');
	}
}

function build_city_select_by_country( sel_type, sel_country ) {
  	var sel = '<select name="' + sel_type + '[city]" id="city_select" onchange="check_for_other()">';
	sel += '<option value="">Select a City</option>';
	$.each( cities, function(i, city) {
		if (sel_country == city.cid) {
			sel += '<option value="' + city.id + '">' + city.name + '</option>';
		}
	});
	sel += '<option value="0">Other</option>';
	sel += '</select>';
	$('#select_city').html( sel );
	$('#city_select').removeAttr('disabled');
}

function build_city_select_by_region( sel_type, sel_region ) {
  	var sel = '<select name="' + sel_type + '[city]" id="city_select" onchange="check_for_other()">';
	sel += '<option value="">Select a City</option>';
	$.each( cities, function(i, city) {
		if ( sel_region == city.sid ) {
			sel += '<option value="' + city.id + '">' + city.name + '</option>';
		}
	});
	sel += '<option value="0">Other</option>';
	sel += '</select>';
	$('#select_city').html( sel );
	$('#city_select').removeAttr('disabled');
	$('#city_other').hide();
}

function check_for_other() {
	var sel_city = $('#city_select').val();
	if ( sel_city == 0 ) {
		var ohtml = '<input type="text" name="city_other" value="">';
		$('#city_other').html(ohtml);
		$('#city_other').show();
	} else {
		$('#city_other').html('');
		$('#city_other').hide();
	}
}

function update_locations_by_country( sel_type ) {
	sel_country = $("#country_select").val();
	if ( sel_country == "" ) {
		$('#region_select').attr('disabled', true);
		$('#city_select').attr('disabled', true);
	} else {
		build_region_select_by_country( sel_type, sel_country );
	}
	
}

function update_locations_by_region( sel_type ) {
	sel_region = $("#region_select").val();
	if ( sel_region == '') {
		$('#city_select').attr('disabled', true);
	} else {
		$('#city_select').removeAttr('disabled');
	}
}

function check_coupon(section, theme) {
	
	$("#coupon_message").html("Calculating discount...");
	var coupon_code = $('#coupon_code').val();
	var post_url = "/ajax/store/" + section + "/check_coupon";
	$.post(post_url, {coupon: ""+coupon_code+""}, function(data){
		if(data.message.length > 0 ) {
			$('#coupon_message').html(data.message);
			if ( data.success == "true" ) {
				$('#coupon_title').html("Discount " + data.code + ":");
				$('#coupon_amount').html(data.currency + data.amount);
				$('#checkout_subtotal').html(data.currency + data.subtotal);
				$('#coupon_remove').html('<a href="/site/actions/remove_from_cart/' + data.cart_id + '/"><img src="/themes/' + theme + '/images/icons/delete.png" border="0"></a>')
				$('.coupon').show();
			}
		}
	}, "json");
	
}

function check_credit(section, theme) {
	
	$("#credit_message").html("Calculating Credit...");
	var credit_code = $('#credit_code').val();
	var post_url = "/ajax/store/" + section + "/check_credit";
	$.post(post_url, {credit: ""+credit_code+""}, function(data){
		if(data.message.length > 0 ) {
			$('#credit_message').html(data.message);
			if ( data.success == "true" ) {
				document.location = "/" + section + "/checkout/5/";
			}
		}
	}, "json");
	
}

function show_enter_shipping(status) {
	if (status > 0) {
		$('#enter_shipping').show();
	} else {
		$('#enter_shipping').hide();
	}
}

function populate_address( address ) {
	var use_shipping = $('input[name=use_shipping]').attr('checked');
	if( use_shipping == true ) {
		// Populate Fields
		$("input[name='bill[first]']").val(address.first);
		$("input[name='bill[last]']").val(address.last);
		$("input[name='bill[company]']").val(address.company);
		$("input[name='bill[email]']").val(address.email);
		$("input[name='bill[phone]']").val(address.phone);
		$("input[name='bill[one]']").val(address.one);
		$("input[name='bill[two]']").val(address.two);
		$("input[name='bill[city]']").val(address.city);
		$("input[name='bill[zip]']").val(address.zip);
		$("#country_select").val( address.country );
		build_region_select_by_country( 'bill', address.country );
		$("#region_select").val( address.state );
	} else {
		// Clear Fields
		$("input[name='bill[first]']").val('');
		$("input[name='bill[last]']").val('');
		$("input[name='bill[company]']").val('');
		$("input[name='bill[email]']").val('');
		$("input[name='bill[phone]']").val('');
		$("input[name='bill[one]']").val('');
		$("input[name='bill[two]']").val('');
		$("input[name='bill[city]']").val('');
		$("input[name='bill[zip]']").val('');
		$("#country_select").val( '' );
		build_region_select_by_country( 'bill', '' );
		$("#region_select").val( '' );
	}
}

function set_deposit_on()
{
	$('.store_deposit').val('deposit');
}

function set_deposit_off()
{
	$('.store_deposit').val('');
}
	
$(document).ready(function() {
	payment_option();
	$('input:text').hint();
});