function presenceCheck (elementId, minLength, maxLength) {
	if (document.getElementById(elementId).value.length < minLength) {
		return false;
	} else if (document.getElementById(elementId).value.length > maxLength) {
		// text too long
		return false;
	} else {
		// valid
		return true;
	}
}

function emailCheck(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)

	if (str.indexOf(at)==-1){
	  return false;
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	  return false;
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	  return false;
	}
	if (str.indexOf(at,(lat+1))!=-1){
	  return false;
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	  return false;
	}
	if (str.indexOf(dot,(lat+2))==-1){
	  return false;
	}
	if (str.indexOf(" ")!=-1){
	  return false;
	}
	return true;
}


function validateComments () {
	var valid = "";
	//attribute_name
	if (!presenceCheck("attribute_name", 2, 30))
		valid += "Name must be between 2 and 30 characters.\n";
	//attribute_email
	if (!emailCheck(document.getElementById("attribute_email").value))
		valid += "You must provide a valid email address.\n";
	//attribute_body
	if (!presenceCheck("attribute_body", 10, 999999))
		valid += "You must provide some comments.\n";

	if (valid.length != 0) {
		alert(valid);
		return false;
	}else{
		document.forms[0].submit();
	}
}

function validateContactUs () {
	var valid = "";
	//attribute_name
	if (!presenceCheck("c_name", 2, 30))
		valid += "Name must be between 2 and 30 characters.\n";
	//attribute_email
	if (!emailCheck(document.getElementById("c_email").value))
		valid += "You must provide a valid email address.\n";
	//attribute_body
	if (!presenceCheck("c_body", 1, 999999))
		valid += "You must provide an enquiry.\n";

	if (valid.length != 0) {
		alert(valid);
		return false;
	}else{
		document.forms[0].submit();
	}
}

function validateSubmitYourCv() {
	var valid = "";

	if (!presenceCheck("name", 2, 30))
		valid += "Name must be between 2 and 30 characters.\n";

	if (!emailCheck(document.getElementById("email").value))
		valid += "You must provide a valid email address.\n";

	if (!presenceCheck("reason_for_contact", 5, 999999))
		valid += "You must provide some reasons for contacting us.\n";

	if (!presenceCheck("file", 4, 999999))
		valid += "You must provide a copy of your CV.\n";

	if (!presenceCheck("phone", 5, 25))
		valid += "You must provide a phone number.\n";

	if (valid.length != 0) {
		alert(valid);
		return false;
	}else{
		document.forms[0].submit();
	}
}

function clearTextarea(id) {
	var element = document.getElementById(id);
	if(element.innerHTML == " ") {
		element.innerHTML = "";
	}
}