
var value = ""
var shipping_qty = 0

function checkInput() {
	var alert_msg = ""
	var missing = ""
	if(document.rxyoga_order.total_qty.value <= 0) {
		alert_msg += 'No selections were entered.\n'
	}

	if(alert_msg != "") {
		alert(alert_msg)
		return false
	} else {
		document.rxyoga_order.submit()
		return true
	}
}

function clearForm() {
	value = ""
	var state = document.rxyoga_order.ship_state.selectedIndex = 0
	shipping_qty = 0
	for( i = 0; i < document.rxyoga_order.elements.length; i++) {
		if(document.rxyoga_order.elements[i].type == "text" || document.rxyoga_order.elements[i].type == "textarea") {
			document.rxyoga_order.elements[i].value = ""
		}
	}
}

function noEdit(tag_name) {
	eval("document.rxyoga_order." + tag_name + ".blur();");
	alert("Product quantity and amount entries are not editable.\nSales tax will be added if your shipping state below is California.")
	return true
}

function setValue() {
	var item = setValue.arguments[0]
	var tag = setValue.arguments[1]
	value = document.rxyoga_order.elements[(item - 1) * 6 + tag - 1].value
}

function getValue() {
	return value

}

function precision() {
	var num = precision.arguments[0]
	var exp = precision.arguments[1]
	num *= Math.pow(10, exp)
	num = Math.round(num)
	num /= Math.pow(10, exp)
	return num
}

function decPlace() {
	var num = decPlace.arguments[0] + ""
	var places = decPlace.arguments[1]
	parts = new Array()
	parts[0] = num.split(".")[0]
	parts[1] = num.split(".")[1]
	var out_string = parts[0] + "."
	var len = 0
	if(parts[1] != null) {
		len = parts[1].length
		out_string += parts[1]
	}
	if(len < places) {
		var index = len
		for(index; index < places; index++) {
			out_string += "0"
		}
	}
	return out_string
}

function usDollars() {
	var in_string = usDollars.arguments[0]
	in_string = parseFloat(in_string)
	in_string = precision(in_string, 2)
	in_string = decPlace(in_string, 2)
	return in_string
}

function increment(item, count) {
	shipping_qty += count;
	eval("var qty = document.rxyoga_order.qty_" + item + ".value;");
	qty++
	eval("document.rxyoga_order.qty_" + item + ".value = qty;");

	eval("var price = document.rxyoga_order.price_" + item + ".value;");
	var cost = (price * qty)
	cost = precision(cost, 2)
	var new_cost = decPlace(cost, 2)
	eval("document.rxyoga_order.cost_" + item + ".value = new_cost;");

	var current_total_cost = document.rxyoga_order.total_cost.value
	var new_total_cost = (current_total_cost*1 + price*1)
	new_total_cost = precision(new_total_cost, 2)
	document.rxyoga_order.total_cost.value = decPlace(new_total_cost, 2)

	document.rxyoga_order.total_qty.value = shipping_qty // new_total_qty
	calc_total()
}

function decrement(item, count) {
	eval("var qty = document.rxyoga_order.qty_" + item + ".value;");
	if(qty <= 0) {return}
	shipping_qty -= count
	qty--
	if(qty > 0) {
		eval("document.rxyoga_order.qty_" + item + ".value = qty;");
	} else {
		eval("document.rxyoga_order.qty_" + item + ".value = '';");
	}

	eval("var price = document.rxyoga_order.price_" + item + ".value;");
	if(qty > 0) {
		var cost = price * qty
		cost = precision(cost, 2)
		cost = decPlace(cost, 2)
	} else {
		var cost = ""
	}
	var new_cost = (cost > 0) ? decPlace(cost, 2) : "";
	eval("document.rxyoga_order.cost_" + item + ".value = new_cost;");

	var current_total_cost = document.rxyoga_order.total_cost.value
	var new_total_cost = (current_total_cost*1 - price*1)

	if(shipping_qty > 0) {
		document.rxyoga_order.total_qty.value = shipping_qty // new_total_qty
	} else {
		document.rxyoga_order.total_qty.value = ""
	}
	if(new_total_cost > 0) {
		new_total_cost = precision(new_total_cost, 2)
		document.rxyoga_order.total_cost.value = decPlace(new_total_cost, 2)
		calc_total()
	} else {
		document.rxyoga_order.total_cost.value = ""
		document.rxyoga_order.total_charges.value = ""
	}
}

function calc_total() {
	var total_charges = document.rxyoga_order.total_cost.value
	total_charges = parseFloat(total_charges)
	if(total_charges > 0) {
		document.rxyoga_order.total_charges.value = usDollars(total_charges)
	} else {
		document.rxyoga_order.total_charges.value = ""
	}
}

