// Globals
var isFormOk = true;
var isErrorState = false;
var errorFontSize = "100%";
var errorFontColor = "red";

function checkForm(){
	var orderForm = document.forms[0];
	
	// No organization name
	if (orderForm.orgName.value == "") {
		document.getElementById("orgname").style.fontSize= errorFontSize;
		document.getElementById("orgname").style.color= errorFontColor;
		isFormOk = false;
		isErrorState = true;
	}
	// No forename
	if (orderForm.contactForename.value == "") {
		document.getElementById("fname").style.fontSize= errorFontSize;
		document.getElementById("fname").style.color= errorFontColor;
		isFormOk = false;
		isErrorState = true;
	}
	// No surname
	if (orderForm.contactSurname.value == "") {
		document.getElementById("surname").style.fontSize= errorFontSize;
		document.getElementById("surname").style.color= errorFontColor;
		isFormOk = false;
		isErrorState = true;
	}
	// No job title
	if (orderForm.jobTitle.value == "") {
		document.getElementById("jobtitle").style.fontSize= errorFontSize;
		document.getElementById("jobtitle").style.color= errorFontColor;
		isFormOk = false;
		isErrorState = true;
	}
	// No e-mail
	if (orderForm.email.value == "") {
		document.getElementById("email").style.fontSize= errorFontSize;
		document.getElementById("email").style.color= errorFontColor;
		isFormOk = false;
		isErrorState = true;
	}
	// No city
	if (orderForm.city.value == "") {
		document.getElementById("city").style.fontSize= errorFontSize;
		document.getElementById("city").style.color= errorFontColor;
		isFormOk = false;
		isErrorState = true;
	}
	
	emailString = orderForm.email.value;
	// Is e-mail in correct format?
	if ( (emailString != "") && (verifyEmail(emailString) == false) ) {
		alert("E-posta adresinizde hata var. Lütfen kontrol ediniz.");
		document.getElementById("email").style.fontSize= errorFontSize;
		document.getElementById("email").style.color= "red";
		isFormOk = false;
		isErrorState = true;	
	}
	
	// If everything OK submit the form
	if (isFormOk==true) {
		// Submit the form
		// A new transaction
		
		if (orderForm.submitted.value == "0") {
			orderForm.submitted.value = "1";
			orderForm.submit();
		} else {
			alert("Bilgi isteği formunuz gönderildi. Yeni bir form göndermek için lütfen baştan başlayınız.");
			location = "index.php";
		}
	}
	else {
		document.getElementById("required-warning").style.color = errorFontColor;
		location.href= "#anchor-required";
	}
	
	return isFormOk;
}

function fieldChanged(fieldName) {
	var orderForm = document.forms[0];
	
	if (isErrorState==false) return;	// If no error state we have no other business here
	
	switch (fieldName) {
		case "orgname":
			if (orderForm.orgName.value != "") {
				var orgnameLabel = document.getElementById("orgname");
				if (orgnameLabel.style.color=="red") {
					document.getElementById("orgname").style.fontSize= "100%";
					document.getElementById("orgname").style.color= "black";
				}
			} else if (orderForm.orgName.value == "") {
				var orgnameLabel = document.getElementById("orgname");
				if (isErrorState == true) {
					document.getElementById("orgname").style.fontSize= errorFontSize;
					document.getElementById("orgname").style.color= errorFontColor;
				}
			}
			break;
		case "fname":
			if (orderForm.contactForename.value != "") {
				var forenameLabel = document.getElementById("fname");
				if (forenameLabel.style.color=="red") {
					document.getElementById("fname").style.fontSize= "100%";
					document.getElementById("fname").style.color= "black";
				}
			} else if (orderForm.contactForename.value == "") {
				var forenameLabel = document.getElementById("fname");
				if (isErrorState == true) {
					document.getElementById("fname").style.fontSize= errorFontSize;
					document.getElementById("fname").style.color= errorFontColor;
				}
			}
			break;
		case "surname":
			if (orderForm.contactSurname.value != "") {
				var surnameLabel = document.getElementById("surname");
				if (surnameLabel.style.color=="red") {
					document.getElementById("surname").style.fontSize= "100%";
					document.getElementById("surname").style.color= "black";
				}
			} else if (orderForm.contactSurname.value == "") {
				var surnameLabel = document.getElementById("surname");
				if (isErrorState == true) {
					document.getElementById("surname").style.fontSize= errorFontSize;
					document.getElementById("surname").style.color= errorFontColor;
				}
			}
			break;
		case "jobtitle":
			if (orderForm.jobTitle.value != "") {
				var jobtitleLabel = document.getElementById("jobtitle");
				if (jobtitleLabel.style.color=="red") {
					document.getElementById("jobtitle").style.fontSize= "100%";
					document.getElementById("jobtitle").style.color= "black";
				}
			} else if (orderForm.jobTitle.value == "") {
				var jobtitleLabel = document.getElementById("jobtitle");
				if (isErrorState == true) {
					document.getElementById("jobtitle").style.fontSize= errorFontSize;
					document.getElementById("jobtitle").style.color= errorFontColor;
				}
			}
			break;
		case "email":
			if (orderForm.email.value != "") {
				var emailLabel = document.getElementById("email");
				if (emailLabel.style.color=="red") {
					document.getElementById("email").style.fontSize= "100%";
					document.getElementById("email").style.color= "black";
				}
			} else if (orderForm.email.value == "") {
				var emailLabel = document.getElementById("email");
				if (isErrorState == true) {
					document.getElementById("email").style.fontSize= errorFontSize;
					document.getElementById("email").style.color= errorFontColor;
				}
			}
			break;
		case "city":
			if (orderForm.city.value != "") {
				var cityLabel = document.getElementById("city");
				if (cityLabel.style.color=="red") {
					document.getElementById("city").style.fontSize= "100%";
					document.getElementById("city").style.color= "black";
				}
			} else if (orderForm.city.value == "") {
				var cityLabel = document.getElementById("city");
				if (isErrorState == true) {
					document.getElementById("city").style.fontSize= errorFontSize;
					document.getElementById("city").style.color= errorFontColor;
				}
			}
			break;
	}
	// A global check
	if (isErrorState == true) {	// We are in error state; so let's check
		if ( (orderForm.contactForename.value != "")&&(orderForm.contactSurname.value != "")
					&&(orderForm.email.value != "")
					&&(orderForm.orgName.value != "")
					&&(orderForm.city.value != "")
					&&(orderForm.jobTitle.value != "") ) {
			// Remove error message or Display positive feedback
			isFormOk=true;
			document.getElementById("required-warning").style.color="black";
		} else {
			isFormOk=false;
			// Display error message
			document.getElementById("required-warning").style.color=errorFontColor;
		}
	}
}
