function calculatePrice(id) {
	var qs = [];
	var price = 0;
	selectItems = $("#productSettings select");
	for (i = 0; i < selectItems.length; i++) {
		e = selectItems[i];
		qs[qs.length] = e.name + "=" + e.value;
	}
	radioItems = $("#productSettings input[type=radio]");
	for (i = 0; i < radioItems.length; i++) {
		e = radioItems[i];
		if (e.checked) {
			qs[qs.length] = e.name + "=" + e.value;
		}
	}
	qs = qs.join("&");
	$.get("/product-calculate-price.php?id=" + id + "&" + qs, function(data) {
		aPrices = data.split(",");
		$("#calculatedPrice").html(aPrices[0]);
		$("#calculatedPriceDiscount").html(aPrices[1]);
		if ($("#calculatedPriceDiscount") && aPrices[0] > aPrices[1]) {
		}
	});
}

function changeShippingAddress(id) {
	location.href = "/checkout-shipping-addr.php?id=" + id;
}

function estimateShipping(id) {
	zipcode = $("#zipcode").val();
	sSettings = "";
	$("div.product-settings input:checked, div.product-settings select").each(function() {
		if (this.nodeName == "SELECT") {
			sSettings += "&" + $(this).attr("name") + "=" + $(this).val();
		} else if (this.nodeName == "INPUT") {
			sSettings += "&" + $(this).attr("name") + "=" + $(this).val();
		}
	});
	$.get("/product-estimate-shipping.php?id=" + id + "&zipcode=" + zipcode + sSettings, function(data) {
		sOutput = "";
		aPrices = data.split(",");
		if (aPrices.length > 0) {
			for (i = 0; i < aPrices.length; i++) {
				sOutput += "<li>" + aPrices[i] + "</li>";
			}
			sOutput = "<ul>" + sOutput + "</ul>";
		}
		$("#shipping-estimate").html(sOutput);
	});
}

function selectAllFiles(a) {
	$("table.account-files input").attr("checked", a);
}

function checkout3reqFields(a) {
	$("fieldset.formbox span.required").each(function(n) {
		if (!a && n > 3) {
			$(this).hide();
		} else {
			$(this).show();
		}
	});
}
function checkout4reqFields(a) {
	$("fieldset.formbox li.payment-method span.required, fieldset.formbox li.billing-details span.required").each(function(n) {
		if (a) {
			$(this).show();
		} else {
			$(this).hide();
		}
	});
}

function sameAsShipping(billing) {
	for (key in billing) {
		if (key != "dummy") {
			$("*[name='B"+key+"']").val(billing[key]);
		}
	}
}

function uploadMore(n) {
//	$('<div class="inputs fileupload"><label>File ' + $("#upload-form > div.fileupload").size() + ':</label><input class="file" type="file" name="upload[]" /></div>').insertAfter($("#upload-form div.fileupload:last"));
	$('<div class="inputs fileupload"><label>File ' + (1+$("ul.order-files li.job"+n+" div.fileupload").size()) + ':</label><input class="file" type="file" name="upload['+n+'][]" /></div>').insertAfter($("ul.order-files li.job"+n+" div.fileupload:last"));
}

function selectFiles(n) {
	$("ul.order-files li.job"+n+" div.file-list").toggle();	
}

function uploadAll() {
	$("#upload-form").submit();
}

var sReqfld = "(required field)";
var sOptfld = "(optional field)";

function formValidate(fid) {
	$("#" + fid).submit(function () {
		$("input.required", this).each(function () {
			if ($(this).val() == sReqfld) {
				$(this).val("");
			}
		});
		$("input.optional", this).each(function () {
			if ($(this).val() == sOptfld) {
				$(this).val("");
			}
		});
	});
	$("#" + fid + " input.text").each(function() {
		$(this).removeClass("shadow");
		formValidateSetvalues(this);
	});
	$("#" + fid + " input.text").click(function() {
		$(this).removeClass("shadow");
		if ($(this).hasClass("required") && $(this).val() == sReqfld) {
			$(this).val("");
		} else if ($(this).hasClass("optional") && $(this).val() == sOptfld) {
			$(this).val("");
		}
	});
	$("#" + fid + " input.text").blur(function() {
		formValidateSetvalues(this);
	});
}

function formValidateSetvalues(e) {
	if ($(e).val() == "") {
		if ($(e).hasClass("required")) {
			$(e).addClass("shadow");
			$(e).val(sReqfld);
		} else if ($(e).hasClass("optional")) {
			$(e).addClass("shadow");
			$(e).val(sOptfld);
		}
	}
}

function checkout3fields(v) {
	if (v == "no") {
		$("#address-form").hide();
	} else {
		$("#address-form").show();
	}
}

function checkout4fields(v) {
	return;
	if (v == "cc") {
		$("#billing-address").show();
	} else {
		$("#billing-address").hide();
	}
}

function loadShippingAddress(v) {
	$.ajax(
		{ type: "get", url: "checkout-getaddr.php", data: "id=" + $("select[name='select_address']").val(), dataType: "json", success:
			function(data, textStatus) {
				$.each(data, function(i, val) {
					if (i == "state" || i == "country") {
						$("select[name='"+i+"']").val(val);
					} else {
						$("input[name='"+i+"']").val(val);
					}
				});
				formValidate('checkout3');
			}
		});
}

function loadBillingAddress() {
	$.ajax(
		{ type: "get", url: "checkout-getaddr.php", data: "id=" + $("select[name='select_address']").val(), dataType: "json", success:
			function(data, textStatus) {
				$.each(data, function(i, val) {
					if (i == "state" || i == "country") {
						$("select[name='B"+i+"']").val(val);
					} else {
						$("input[name='B"+i+"']").val(val);
					}
				});
				formValidate('checkout4');
			}
		});
}

$(document).ready(function() {
	$("table.account-files label").click(function() {
		$(this).prev().click();
	});
	$("ul.product-tabs li a").each(function(n) {
		$(this).click(function() {
			$("ul.product-tabs li").removeClass("active");
			$(this).parent().addClass("active");
			$("div.product-tab").each(function(k) {
				if (k == n) {
					$(this).show();
				} else {
					$(this).hide();
				}
			});
			return false;
		});
	});
});


