loadstatus = 0;

function showError(inputID, errorID, checkID) {
	document.getElementById(inputID).style.border = "1px solid red";
	document.getElementById(errorID).style.display = "inline";
	document.getElementById(checkID).style.visibility = "hidden";
}

function showCheck(inputID, errorID, checkID) {
	document.getElementById(inputID).style.border = "1px solid green";
	document.getElementById(errorID).style.display = "none";
	document.getElementById(checkID).style.visibility = "visible";
}

/*
// these functions are still in form.dropbox.php

function updateorder(field,value) {
	console.log("updateorder()");
	loadstatus = 0;
	ajax.location.href = '<?php echo FORM_WEBROOT ?>update.php?id=<?php echo $id; ?>&field='+field+'&value='+value+'';
}

function keepalive(id) {
	console.log("keepalive()");
	console.log(id);
	if (loadstatus == 1) {
		console.log("<?php echo FORM_WEBROOT ?>update.php?id="+id+"&field=status&value=0");
		ajax.location.href = "<?php echo FORM_WEBROOT ?>update.php?id="+id+"&field=status&value=0";
	}
	setTimeout ("keepalive(id)","10000"); //10 seconds
}
*/

function checkform(field) {
//	console.log("checkform");
	if (typeof field == "undefined")
	  field = "all";
//	console.log(field);
	error = 0;
	if (field == "email" || field == "all") {
		if (document.getElementById("email")) {
			//Email Must Be Valid
			email = document.order.email.value
			emailregex = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i ;
			if (!email.match(emailregex)) {
				showError("email", "emailerror", "emailcheck");
				error = 1;
			}
			else {
				showCheck("email", "emailerror", "emailcheck");
			}
		}
	}

	if (field == "name" || field == "all") {
		if (document.getElementById("name")) {
			//Must Include A Name
			name = document.order.name.value
			nameregex = /^.+$/i
			if (!name.match(nameregex)) {
				showError("name", "nameerror", "namecheck");
				error = 1;
			}
			else {
				showCheck("name", "nameerror", "namecheck");
			}
		}
	}

	if (field == "phone" || field == "all") {
		if (document.getElementById("phone")) {
			//Must Include Phone Numbers

			phone = document.order.phone.value
			phoneregex = /^.{3,}$/i ;
			if (!phone.match(phoneregex)) {
				showError("phone", "phoneerror", "phonecheck");
				error = 1;
			}
			else {
				showCheck("phone", "phoneerror", "phonecheck");
			}
		}
	}

	if (field == "title" || field == "all") {
		if (document.getElementById("title")) {
			//Must have a title of at least 10 characters
			title = document.order.title.value
			titleregex = /^.{10,}$/im;
			if (!title.match(titleregex)) {
				showError("title", "titleerror", "titlecheck");
				error = 1;
			} else {
				showCheck("title", "titleerror", "titlecheck");
			}
		}
	}

	if (field == "topic" || field == "all"){
		if (document.getElementById("topic")){
			//Value must not be missed
			topic = document.getElementById("topic").value;
			if (topic == "missed"){
				showError("topic","topicerror","topiccheck");
				error = 1;
			}else{
				showCheck("topic","topicerror","topiccheck");

				if (topic == 45){
					document.getElementById("lawdropdown").style.display = "inline";
					lawtopic = document.order.LawTopic.value;

					if (lawtopic == 'missed'){
						document.order.LawTopic.style.border = "1px solid red";
						error = 1;
					}else{
						document.order.LawTopic.style.border = "1px solid green";
					}

					if (lawtopic == 'Other'){
						document.getElementById("lawother").style.display = "inline";
					}else{
						document.getElementById("lawother").style.display = "none";
					}
				}else{
					document.getElementById("lawdropdown").style.display = "none";
					document.getElementById("lawother").style.display = "none";
				}
			}
		}
	}
}

function dosubmit() {
//	console.debug("dosubmit()");
	checkform();
	if (error == 1){
		alert ("Oops! We think you missed something - please make sure you have filled in all the sections in red");
		return false;
	}
	else {
		// form has been completed correctly
//		console.debug("submit the form");
		document.order.submit();
	}
}
