function formSubmit(formId)
{ 
required_trigger = 0;
email_trigger = 0;
email_dup_trigger = 0;
var error_required_string ='';
document.form.setAttribute('action','order4.php?plan='+ document.form.plan_id.value);
var inputs = document.form.getElementsByTagName('input');
for (var x = 0; x <inputs.length; x++)
{
	if (inputs[x].getAttribute('required') == 'true' && !inputs[x].value)
	{
		required_trigger = 1;
		inputs[x].style.backgroundColor = 'red';
		if (inputs[x].getAttribute('label')) {error_required_string += inputs[x].getAttribute('label') + "\n";}
	}
	else
	{
		inputs[x].style.backgroundColor = '';
	}
}

var selects = document.form.getElementsByTagName('select');
for (var x = 0; x <selects.length; x++)
{
	
	if (selects[x].getAttribute('required') == 'true' && !selects[x].options[selects[x].selectedIndex].value)
	{
		required_trigger = 1;
		selects[x].style.backgroundColor = 'red';
		if (selects[x].getAttribute('label')) {error_required_string += selects[x].getAttribute('label') + "\n";}
	}
	else
	{
		selects[x].style.backgroundColor = '';
	}
}

if (required_trigger)
{
	alert('The following required fields were not filled in:'+"\n" + error_required_string);
	return false;
}

if (document.getElementById('email2'))
{
	if (document.getElementById('email1').value == document.getElementById('email2').value && formId == 'process_form')
	{
		alert('Please enter two different emails.');
		return false;
	}
}

if (document.getElementById('password2') && document.getElementById('password1'))
{
	if (document.getElementById('password1').value != document.getElementById('password2').value)
	{
		alert('The passwords entered do not match.');
		return false;
	}
}

if (document.getElementById('email2'))
{
	if (!isValidEmail(document.getElementById('email1').value) || (!isValidEmail(document.getElementById('email2').value) && formId == 'process_form') )
	{
		alert('You have entered an invalid email address');
		return false;
	}
}

if (document.getElementById('checkbox3'))
{
	if (document.getElementById('checkbox3').checked != true  && formId == 'purchase_form_final')
	{
		alert('Please Read and Agree to the Business Terms before continuing with the purchase.');
		return false;
	}
}
if (document.getElementById('button3'))
{
	document.getElementById('button3').disabled = 'true';
}
document.getElementById(formId).submit();
}

function isValidEmail(str) {
   return (str.indexOf(".") >= 2) && (str.indexOf("@") > 0);
 
}

function changeShippingCost(select)
{
	ship_cost = Number(select.options[select.selectedIndex].getAttribute('amount'));
	sub_total = Number(document.getElementById('subtotal').innerHTML);
	if (document.getElementById('coupon_amount')) {coupon_amount = Number(document.getElementById('coupon_amount').innerHTML);} else {coupon_amount = 0;}
	new_total = number_format((sub_total + ship_cost -coupon_amount),2);
	document.getElementById('shipping_cost').innerHTML = ship_cost;
	document.getElementById('total_cost').innerHTML = new_total;
}

function updatePlan(input)
{
	if (input.checked)
	{
		cost_plan = Number(input.getAttribute('amount'));
		if (document.getElementById('coupon_amount')) {coupon_amount = Number(document.getElementById('coupon_amount').innerHTML);} else {coupon_amount = 0;}
		document.getElementById('subtotal').innerHTML = number_format(Number(document.getElementById('subtotal').innerHTML) - cost_plan + coupon_amount,2);
		document.getElementById('total_cost').innerHTML = number_format(Number(document.getElementById('total_cost').innerHTML) - cost_plan + coupon_amount,2);
		document.getElementById('order_table').deleteRow(1);
	}
}

function updateSimCost()
{
	//alert(document.getElementById('plan_cost').innerHTML);
	if (document.getElementById('plan_cost')) {var plan_cost_hold = document.getElementById('plan_cost').innerHTML; plan_cost2 = Number(plan_cost_hold);} else {plan_cost2 = 0;}
	if (document.getElementById('shipping_cost')) {ship_cost = Number(document.getElementById('shipping_cost').innerHTML);} else {ship_cost = 0;}
	if (document.getElementById('coupon_amount')) {coupon_amount = Number(document.getElementById('coupon_amount').innerHTML);} else {coupon_amount = 0;}
	if (document.getElementById('num_items')) {num_items = Number(document.getElementById('num_items').value);} else {num_items = 0;}
	cost_sim_pro = 149.00;
	if (document.getElementById('remove_sim_checkbox').checked || !num_items)
	{
		document.getElementById('subtotal').innerHTML = plan_cost2;
		if (document.getElementById('shipping_cost')) {document.getElementById('shipping_cost').innerHTML = '0.00';}
		document.getElementById('total_cost').innerHTML = number_format(plan_cost2 - coupon_amount,2);
		if (document.getElementById('order_table').rows.length == 1)
		{
			document.getElementById('order_table').deleteRow(1);
		}
		else
		{
			document.getElementById('order_table').deleteRow(2);
		}
		if (document.getElementById('shipping_input_table')) {document.getElementById('shipping_input_table').innerHTML = '';}
	}
	else
	{
		cost_total_sim_pro = cost_sim_pro * num_items;
		sub_total = cost_total_sim_pro + plan_cost2;
		total_cost = sub_total + ship_cost - coupon_amount; 
		document.getElementById('subtotal').innerHTML = number_format(sub_total,2);
		document.getElementById('total_cost').innerHTML = number_format(total_cost,2);
		if (document.getElementById('total_cost_sim_pro')) {document.getElementById('total_cost_sim_pro').innerHTML = number_format(cost_total_sim_pro,2)};
	}
}

function setShippingInfo()
{
	document.getElementById('ship_first_name').value = document.getElementById('cc_first_name').value
	document.getElementById('ship_last_name').value = document.getElementById('cc_last_name').value
	document.getElementById('ship_address').value = document.getElementById('cc_address').value
	document.getElementById('ship_city').value = document.getElementById('cc_city').value
	document.getElementById('ship_zip').value = document.getElementById('cc_zip').value
	document.getElementById('ship_state').value = document.getElementById('cc_state').value
}


function number_format( number, decimals, dec_point, thousands_sep ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://crestidg.com)
    // +     bugfix by: Benjamin Lupton
    // +     bugfix by: Allan Jensen (http://www.winternet.no)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +     bugfix by: Howard Yeend
    // *     example 1: number_format(1234.5678, 2, '.', '');
    // *     returns 1: 1234.57     
 
    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "." : dec_point;
    var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}
