function Customer_Add(divID) {
	if (XMLHttpRequestObject) {
		var obj = document.getElementById(divID);
		var username = document.getElementById("username").value;
		var password = document.getElementById("password").value;
		var passwordcheck = document.getElementById("passwordcheck").value;
		var forename = document.getElementById("forename").value;
		var surname = document.getElementById("surname").value;
		var email = document.getElementById("email").value;
		var contact = document.getElementById("contact").value;
		var address = document.getElementById("address").value;
		var tax = document.getElementById("tax");
		if (tax.checked == true) {
			var tax = 1;
		} else {
			var tax = 0;
		}
		
		if (username == '' || password == '' || email == '') {
			alert ("Please enter required fields:\n\n- Username\n- Password\n- Email Address");
			return false;
		} else if (password !== passwordcheck) {
			alert ("Passwords did not match, please retry");
			return false;
		} else {
		
			// Must tell it that we are opening it in POST format
			XMLHttpRequestObject.open("POST", "/admin.php?do=customers&run=add&cmd=now", true);
			// Also tell if which content-type
			XMLHttpRequestObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			// Create a null function
			XMLHttpRequestObject.onreadystatechange = function() {
				// Check to make sure download is 100% ready
				// 200 to make sure download is ready
				if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
					// Respond with error code / response in divid
					var response = XMLHttpRequestObject.responseText;
					var details = new Array();
					
					details = response.split('|');
					
					if (details[0] == 'refresh') {
						window.location = details[1];
					} else {
						obj.innerHTML = details[0];	
					}
					
				}	
			}
			XMLHttpRequestObject.send("&username=" + encodeURI(username) + "&password=" + encodeURI(password) + "&forename=" + encodeURI(forename) + "&surname=" + encodeURI(surname) + "&email=" + encodeURI(email) + "&contact=" + encodeURI(contact) + "&address=" + encodeURI(address) + "&tax=" + encodeURI(tax));
		}
	}
}

function Customer_Add_Product(divID, customerid) {
	if (XMLHttpRequestObject) {
		var obj = document.getElementById(divID);
		var product = document.getElementById("product").value;
		var details = document.getElementById("details").value;
		var ip = document.getElementById("ip").value;
		var currency = document.getElementById("currency").value;
		var price = document.getElementById("price").value;
		var cycle = document.getElementById("cycle").value;
		var day = document.getElementById("day").value;
		var month = document.getElementById("month").value;
		var year = document.getElementById("year").value;
		var paid = document.getElementById("paid");
		if (paid.checked == true) {
			var paid = 1;
		} else {
			var paid = 0;
		}
		
		if (product == '' || details == '' || currency == '' || cycle == '' || price == '') {
			alert ("Please enter all details on this page. Everything is required EXCEPT the IP field.");
			return false;
		} else {
			// Must tell it that we are opening it in POST format
			XMLHttpRequestObject.open("POST", "/admin.php?do=products&run=add&cmd=now", true);
			// Also tell if which content-type
			XMLHttpRequestObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			// Create a null function
			XMLHttpRequestObject.onreadystatechange = function() {
				// Check to make sure download is 100% ready
				// 200 to make sure download is ready
				if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
					// Respond with error code / response in divid
					var response = XMLHttpRequestObject.responseText;
					var details = new Array();
					
					details = response.split('|');
					
					if (details[0] == 'refresh') {
						window.location = details[1];
					} else {
						obj.innerHTML = details[0];	
					}
					
				}	
			}
			XMLHttpRequestObject.send("&customerid=" + encodeURI(customerid) + "&product=" + encodeURI(product) + "&details=" + encodeURI(details) + "&ip=" + encodeURI(ip) + "&currency=" + encodeURI(currency) + "&price=" + encodeURI(price) + "&cycle=" + encodeURI(cycle) + "&day=" + encodeURI(day) + "&month=" + encodeURI(month) + "&year=" + encodeURI(year) + "&paid=" + encodeURI(paid));
		}
	}
}

function AddCredit(customerid) {
	var amount = document.getElementById("amount").value
	var pd_day = document.getElementById("pd_day").value
	var pd_month = document.getElementById("pd_month").value
	var pd_year = document.getElementById("pd_year").value
	var bankid = document.getElementById("bankid").value
	
	if (XMLHttpRequestObject) {
		if (customerid == '') {
			alert("System Error. Customerid empty.");
			return false;
		} else if (amount == '') {
			alert("Please enter an amount.");
			return false;
		} else {
			// Must tell it that we are opening it in POST format
			XMLHttpRequestObject.open("POST", "/admin.php?do=billing&run=credit", true);
			// Also tell if which content-type
			XMLHttpRequestObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			// Create a null function
			XMLHttpRequestObject.onreadystatechange = function() {
				// Check to make sure download is 100% ready
				// 200 to make sure download is ready
				if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
					// Respond with error code / response in divid
					var response = XMLHttpRequestObject.responseText;
					var details = new Array();
					
					details = response.split('|');
					
					if (details[0] == 'refresh') {
						alert("Payment Accepted");
						window.location = details[1];
					} else {
						alert(details[0]);	
					}
					
				}	
			}
			XMLHttpRequestObject.send("&customerid=" + encodeURI(customerid) + "&amount=" + encodeURI(amount) + "&pd_day=" + encodeURI(pd_day) + "&pd_month=" + encodeURI(pd_month) + "&pd_year=" + encodeURI(pd_year) + "&bankid=" + encodeURI(bankid));
		}
	}
}

function DisplayProduct(cpid, divID) {
	var obj = document.getElementById(divID);
	
	DisplayWin('products', '400');
	
	if (XMLHttpRequestObject) {
		// Must tell it that we are opening it in POST format
		XMLHttpRequestObject.open("POST", "/admin.php?do=customers&run=viewproducts&cmd=now", true);
		// Also tell if which content-type
		XMLHttpRequestObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		// Create a null function
		XMLHttpRequestObject.onreadystatechange = function() {
			// Check to make sure download is 100% ready
			// 200 to make sure download is ready
			if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
				// Respond with error code / response in divid
				var response = XMLHttpRequestObject.responseText;
				var details = new Array();
				
				details = response.split('|');
				
				if (details[0] == 'refresh') {
					alert("Payment Accepted");
					window.location = details[1];
				} else {
					obj.innerHTML = details[0];	
				}
				
			}	
		}
		XMLHttpRequestObject.send("&cpid=" + encodeURI(cpid));
	}
}

function check_product_edit() {
	var details = document.form.details.value;
	var price = document.form.price.value;
	
	if (details == '' || price == '') {
		alert ("Please enter required fields:\n\n- Details\n- Price");
		return false;
	} else {
		return true;
	}
}

function EditCustomer(divID, customerid) {
	var obj = document.getElementById(divID);
	
	DisplayWin('customeredit', '400');
	
	if (XMLHttpRequestObject) {
		// Must tell it that we are opening it in POST format
		XMLHttpRequestObject.open("POST", "/admin.php?do=customers&run=edit", true);
		// Also tell if which content-type
		XMLHttpRequestObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		// Create a null function
		XMLHttpRequestObject.onreadystatechange = function() {
			// Check to make sure download is 100% ready
			// 200 to make sure download is ready
			if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
				// Respond with error code / response in divid
				var response = XMLHttpRequestObject.responseText;
				var details = new Array();
				
				details = response.split('|');
				
				if (details[0] == 'refresh') {
					alert("Payment Accepted");
					window.location = details[1];
				} else {
					obj.innerHTML = details[0];	
				}
				
			}	
		}
		XMLHttpRequestObject.send("&customerid=" + encodeURI(customerid));
	}
}

function EditMachine(divID, machineid) {
	var obj = document.getElementById(divID);
	DisplayWin('machineedit', '400');
	
	if (XMLHttpRequestObject) {
		// Must tell it that we are opening it in POST format
		XMLHttpRequestObject.open("POST", "/admin.php?do=machines&run=edit", true);
		// Also tell if which content-type
		XMLHttpRequestObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		// Create a null function
		XMLHttpRequestObject.onreadystatechange = function() {
			// Check to make sure download is 100% ready
			// 200 to make sure download is ready
			if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
				// Respond with error code / response in divid
				var response = XMLHttpRequestObject.responseText;
				var details = new Array();
				
				details = response.split('|');
				
				if (details[0] == 'refresh') {
					alert("Changes Accepted");
					window.location = details[1];
				} else {
					obj.innerHTML = details[0];	
				}
				
			}	
		}
		XMLHttpRequestObject.send("&machineid=" + encodeURI(machineid));
	}
}

function ConfirmRemoval(id) {
	var answer = confirm("Are you sure you wish to delete this payment?");
	if (answer) {
		window.location = "/admin.php?do=billing&run=removecredit&id=" + id;
	} 
}

function RemoveIncoming(id) {
	var answer = confirm("Are you sure you wish to delete this payment?");
	if (answer) {
		window.location = "/admin.php?do=banks&run=removeincoming&id=" + id;
	} 
}

function RemoveOutgoing(id) {
	var answer = confirm("Are you sure you wish to delete this transaction?");
	if (answer) {
		window.location = "/admin.php?do=banks&run=removeoutgoing&id=" + id;
	} 
}

function RemoveBank(id) {
	var answer = confirm("Are you sure you wish to remove this bank?\n\nPlease be aware this will remove all linked invoices and payments.\n\nTHIS IS UNDOABLE!");
	if (answer) {
		window.location = "/admin.php?do=banks&run=remove&id=" + id;
	}
}