function subscription_options() {

	// if there is an end date, show options for renewal and EOS notification
	var end_opts = document.getElementById('end_opts');
	var discount = document.getElementById('discount');

	if (end_opts) {
		var end_date = document.getElementById('end_date');
		if (end_date && end_date.value) {
			end_opts.style.display = 'block';
		} else end_opts.style.display = 'none';
	}

	if (discount) {
		val = discount.value;
		num = '';
		if (val)
			num = parseInt(val.toString().replace(/%/g, ''));
		if (!val || num > 100 || num < 0) {
			alert('ERROR: Please specify a discount between 0% and 100%.');
			return false;
		}
	}

	get_total();
}

// change the user's auto renew status
function autorenew_get (username) {
	var data = { action: "autorenew_get", username: username };
	var a = new Ajax.Request('/admin/users/ajax', { method:'post', parameters:'data='+encodeURIComponent(Object.toJSON(data)),
		onSuccess: function(r) {
			var response = r.responseText.evalJSON();
			update(response);
	}});
}

// change the user's auto renew status
function autorenew_save (username) {
	var data = { action: "autorenew_save", username: username };
	var a = new Ajax.Request('/admin/users/ajax', { method:'post', parameters:'data='+encodeURIComponent(Object.toJSON(data)),
		onSuccess: function(r) {
			var response = r.responseText.evalJSON();
			update(response);
	}});
}

// if there is an org_id, print a list of the org's billing groups to select from
function get_billing_groups (val) {
	// clear hidden org_id value if organization is empty
	var org =  document.getElementById('organization');
	var org_id = "";
	if (document.getElementById('organization_id'))
		org_id = document.getElementById('organization_id').value;

	// if there is an organization name and a hidden org_id value, get and show
	// the org's address
	if (org_id && org && org.value) {
		var data = { action: "get_billing_groups", org_id: org_id, value: val };
		var a = new Ajax.Request('/admin/users/ajax', { method:'post', parameters:'data='+encodeURIComponent(Object.toJSON(data)),
			onSuccess: function(r) {
				var response = r.responseText.evalJSON();
				update(response);
		}});
	} else {
		var e = document.getElementById('e_billing_groups');
		if (e)
			e.innerHTML = '';
	}
}

function print_opts() {
	var sub_type = '';
	var products = new Array();
	var sub_types = document.getElementsByName('sub_type');
	var h_org = document.getElementById('org_id');

	// interate through the sub_types
	//   expand any divs for the selected sub_type
	///  set the sub_type var
	//   collapse any divs for unselected sub_types
	for (var i = 0; i < sub_types.length; i++) {
		var div = document.getElementById('div_' + sub_types[i].value + '_opts');
		if (sub_types[i].checked) {
			sub_type = sub_types[i].value;

			if (div)
				div.style.display = 'block';
		} else {
			if (div)
				div.style.display = 'none';
		}
	}

	// create products array with selected products
	var p_list = document.getElementsByName(sub_type + '[]');
	// iterate through products
	for (var i = 0; i < p_list.length; i++) {
		// if value != on and checked, add to products list
		if (p_list[i].checked && p_list[i].value != 'on') {
			products.push(p_list[i].value);
		}
	}

	get_cost(sub_type, products);
}

function get_cost(sub_type, products) {

	// use ajax to print the cost
	var data = { action: "get_cost", sub_type: sub_type, products: products };
	var a = new Ajax.Request('/register/ajax', { method:'post',
		parameters:'data='+encodeURIComponent(Object.toJSON(data)),
		onSuccess: function(r) {
			var response = r.responseText.evalJSON();
			update(response);
			get_total();
    }});
}

function get_total() {

	var e = document.getElementById('e_cost');
	var e2 = document.getElementById('discount');
	var e3 = document.getElementById('e_total');

	if (e && e2 && e3) {
		cost = parseInt(e.innerHTML.replace(/\$/g, ''));
		discount = parseInt(e2.value.toString().replace(/%/g, ''));
		e3.innerHTML = '$' + formatAsMoney(cost/100 * (100 - discount));
	}
}

function formatAsMoney (mnt) {
	mnt -= 0;
	mnt = (Math.round(mnt*100))/100;
	return (mnt == Math.floor(mnt)) ? mnt + '.00'
		: ( (mnt*10 == Math.floor(mnt*10)) ? mnt + '0' : mnt);
}

// get and display the organization's address
function get_address(username) {

	// clear hidden org_id value if organization is empty
	var org =  document.getElementById('organization');
	var org_id = document.getElementById('organization_id').value;

	// if there is an organization name and a hidden org_id value, get and show
	// the org's address
	if (org_id && org && org.value) {
		data = { action: 'get_address', org_id: org_id, username: username };
		var a = new Ajax.Request('/register/ajax', { method:'post',
			parameters: 'data='+encodeURIComponent(Object.toJSON(data)),
			onSuccess: function(r) {
				var response = r.responseText.evalJSON();

				if (response.error_msg.length) {
					alert("ERROR: "+response.error_msg);
					return;
				}

				var disable = eval('response.disable');
				// display address
				for (var i = 0; i < response.types.length; i++) {
					var e = document.getElementById(response.types[i]);
					if (e) {
						e.value = eval('response.' + response.types[i]);
						if (disable)
							e.disabled = true;
						else e.disabled = false;
					}
				}
		}});

	} else if (document.getElementById('address1').disabled) {
		// clear organization and address if there is no org_id and elements
		// are disabled

		document.getElementById('organization_id').value = '';
		var types = new Array ('address1', 'address2', 'city', 'zip');
		for (var i = 0; i < types.length; i++) {
			var e = document.getElementById(types[i]);
			if (e) {
				e.value = '';
				e.disabled = false;
			}
		}
		var e = document.getElementById('state');
		e.disabled = false;
		e.value = 'AL';
	}
}
