String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

function checkDiscount() {
	var code = document.getElementById("discountId").value;
	if (code=="") {
//		alert("clear discount");
		clearDiscount();
	} else {
//		alert("check discount");
		var url = "checkSchoolDiscount.php?discount="+encodeURIComponent(code);
//		request.onreadystatechange = updatePage;
		request.open("GET", url, false); // synchronuous
		request.send(null);
		updatePage();
	}
}

function updatePage() {
//	alert("update page, readyState: "+request.readyState+", status: "+request.status);
	if (request.readyState == 4) {
		if (request.status == 200) {
			var schoolName = request.responseText;
//			alert("school name: "+schoolName);
			if (schoolName == "UNKNOWN") {
				// no discount -> reset
				clearDiscount();
				alert("Unknown discount code");
			} else {
				// discount
				displayDiscount(schoolName);
			}
		}
	}
}

function displayDiscount(schoolName) {
	// display striked price
	var oldPriceCDNode = document.getElementById("oldPriceCD");
	removeAllChildren(oldPriceCDNode);
	oldPriceCDNode.appendChild(document.createTextNode("US$109.95"));
	var oldPriceDownloadNode = document.getElementById("oldPriceDownload");
	removeAllChildren(oldPriceDownloadNode);
	oldPriceDownloadNode.appendChild(document.createTextNode("US$99.95"));
	// display discounted price
	var priceCDNode = document.getElementById("priceCD")
	document.getElementById("priceCD").lastChild.nodeValue = "US$87.95";
	var priceCDNode = document.getElementById("priceDownload")
	document.getElementById("priceDownload").lastChild.nodeValue = "US$79.95";
	//display school description
	var descCDNode = document.getElementById("discountDescriptionCD");
	descCDNode.className = "promotion";
	removeAllChildren(descCDNode);
	descCDNode.appendChild(document.createTextNode("Educational discount for "+schoolName));
	var descDownloadNode = document.getElementById("discountDescriptionDownload");
	descDownloadNode.className = "promotion";
	removeAllChildren(descDownloadNode);
	descDownloadNode.appendChild(document.createTextNode("Educational discount for "+schoolName));
	// add the discount code to the forms
	var discount = document.getElementById("discountId");
	document.getElementById("codeCD").value = discount.value;
	document.getElementById("codeDownload").value = discount.value;
}

function clearDiscount() {
	// reset main price to default
	document.getElementById("priceCD").lastChild.nodeValue = "US$109.95";
	document.getElementById("priceDownload").lastChild.nodeValue = "US$99.95";
	// remove striked price, if any
	var oldPriceCDNode = document.getElementById("oldPriceCD");
	removeAllChildren(oldPriceCDNode);
	var oldPriceDownloadNode = document.getElementById("oldPriceDownload");
	removeAllChildren(oldPriceDownloadNode);
	// remove school description, if any
	var descCDNode = document.getElementById("discountDescriptionCD");
	descCDNode.className = null;
	removeAllChildren(descCDNode);
	var descDownloadNode = document.getElementById("discountDescriptionDownload");
	descDownloadNode.className = null;
	removeAllChildren(descDownloadNode);
	// remove the discount code from the forms
	document.getElementById("codeCD").value = "";
	document.getElementById("codeDownload").value = "";
}

function removeAllChildren(node) {
	var children = node.childNodes;
	for (var i=0;i<children.length;i++) {
		node.removeChild(children[i]);
	}
}