// JavaScript Document
/*
function showex() {
window.open("invoice.html", null, "height=620,width=645,status=no,toolbar=no,menubar=no,location=no,resizable=no,scrollbars=no,top=20");	
}
*/

var xmlHttp_main;
var xmlHttp_buttons;
var xmlHttp_proceed;
var xmlHttp_card;

function createXMLHttpRequest() {
	if (window.ActiveXObject) {
		var myXMLhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) {
		var myXMLhttp = new XMLHttpRequest();
	}
	return myXMLhttp;
}

/*
function getButtons() {
	xmlHttp_buttons = createXMLHttpRequest();
	xmlHttp_buttons.onreadystatechange = handleButtonChange;
	xmlHttp_buttons.open("GET", "get_buttons.php", true);
	xmlHttp_buttons.send(null);
}
function handleButtonChange() {
	if ((xmlHttp_buttons.readyState == 4) && (xmlHttp_buttons.status == 200)) {
		document.getElementById("buttons").innerHTML = xmlHttp_buttons.responseText;
	}
}
*/
/*
function getChangeCard() {
	xmlHttp_card = createXMLHttpRequest();
	xmlHttp_card.onreadystatechange = handleChangeCard;
	xmlHttp_card.open("GET", "isinvoices.php", true);
	xmlHttp_card.send(null);
}
function handleChangeCard() {
	if ((xmlHttp_card.readyState == 4) && (xmlHttp_card.status == 200)) {
		var resp = xmlHttp_card.responseText;
		if (resp == "NONE") {
			document.getElementById("cardtype").disabled = false;
			document.getElementById("cardtype").className = "select";
		}
	}
}
*/
/*
function getProceed() {
	xmlHttp_proceed = createXMLHttpRequest();
	xmlHttp_proceed.onreadystatechange = handleProceedChange;
	xmlHttp_proceed.open("GET", "get_proceed.php", true);
	xmlHttp_proceed.send(null);
}
function handleProceedChange() {
	if ((xmlHttp_proceed.readyState == 4) && (xmlHttp_proceed.status == 200)) {
		document.getElementById("checkout").innerHTML = xmlHttp_proceed.responseText;
	}
}
*/

function addInvoice() {
	var errorbox = document.getElementById("error");
	var invoice_no = ''+document.getElementById("invoice_no").value;
	if (invoice_no == '') {
		errorbox.innerHTML = "Please fill out a valid invoice number<br />";
		return;
	}
	var code = ''+document.getElementById("code").value;
	if (code == '') {
		errorbox.innerHTML = "Please fill out a valid account number<br />";
		return;
	}
	var forename = ''+document.getElementById("forename").value;
	if (forename == '') {
		errorbox.innerHTML = "Please fill out a valid student forename<br />";
		return;
	}
	var surname = ''+document.getElementById("surname").value;
	if (surname == '') {
		errorbox.innerHTML = "Please fill out a valid student surname<br />";
		return;
	}
	var postcode = ''+document.getElementById("postcode").value;
	if (postcode == '') {
		errorbox.innerHTML = "Please fill out a valid postcode<br />";
		return;
	}
	var amount = ''+document.getElementById("amount").value;
	if (amount == '') {
		errorbox.innerHTML = "Please fill out a valid amount payable<br />";
		return;
	}
	if (amount.indexOf("-") == 0) {
		errorbox.innerHTML = "Please fill out a valid amount payable, negative values are not allowed<br />";
		return;
	}
	var cardtype_elm = document.getElementById("cardtype");
	var cardtype = cardtype_elm.options[cardtype_elm.selectedIndex].value;
	
	if (cardtype == '--- Please Select ---') {
		errorbox.innerHTML = "Please select a valid card type<br />";
		return;
	}
	document.getElementById("cardtype").disabled = true;
	document.getElementById("cardtype").className = "select_disabled";
	errorbox.innerHTML = "";
	document.getElementById("invoice_no").value = "";
	document.getElementById("code").value = "";
	document.getElementById("forename").value = "";
	document.getElementById("surname").value = "";
	document.getElementById("amount").value = "";
	
	var phone = document.getElementById('phone').value;
	var email = document.getElementById('email').value;
	var url = "/includes/secpay/add_to_invoices.asp?"+
	          "invoice_no=" + invoice_no +"&"+
		      "code=" + code +"&"+
   		      "forename=" + forename +"&"+
		      "surname=" + surname +"&"+
		      "postcode=" + postcode +"&"+
 		      "amount=" + amount +"&"+
			  "cardtype=" + cardtype + "&" +
			  "phone=" + email + "&" +
			  "email=" + phone;
	xmlHttp_main = createXMLHttpRequest();
	xmlHttp_main.onreadystatechange = handleInvoiceChange;
	xmlHttp_main.open("GET", url, true);
	xmlHttp_main.send(null);
}

function delInvoice(invoice_no) {
	var url = "/includes/secpay/delete_invoice.asp?invoice_no=" + invoice_no;

	xmlHttp_main = createXMLHttpRequest();
	xmlHttp_main.onreadystatechange = handleInvoiceChange;
	xmlHttp_main.open("GET", url, true);
	xmlHttp_main.send(null);
}

function handleInvoiceChange() {
	if ((xmlHttp_main.readyState == 4) && (xmlHttp_main.status == 200)) {
		document.getElementById("invoice_ajax").innerHTML = xmlHttp_main.responseText;
		document.getElementById('checkout').onclick = function() {
			document.secpay.submit();
		};
		/*
		getButtons();
		getProceed();
		getChangeCard();
		*/
	} else {
		document.getElementById('checkout').onclick = function() {};
	}
}

function checkout() {
	document.secpay.submit();
	/*
	xmlHttp_main = createXMLHttpRequest();
	xmlHttp_main.onreadystatechange = handleCheckoutSubmit;
	var url = "/includes/secpay/checkout.asp";
	xmlHttp_main.open("GET", url, true);
	xmlHttp_main.send(null);
	*/
}

/*
function handleCheckoutSubmit() {
	if ((xmlHttp_main.readyState == 4) && (xmlHttp_main.status == 200)) {
		// Database updated
		var resp = xmlHttp_main.responseText;
		if (resp == "SUCCESS") {
			document.secpay.submit();
		} else {
			document.getElementById("checkout").innerHTML = "<span class=\"error\">"+			
				xmlHttp_main.responseText + 
				"<br /> Please call: 01622 358419 Between 9am and 5pm or "+
				"Email: <a href=\"mailto:it@kent-music.com\">The Kent Music IT Department</a></span>";
		}
	}		
}
*/
