
// Checks a drop down is not set to the initial value (e.g. "please select...")
 function checkSelect(select){
	
	if (select.selectedIndex == 0) {
		fieldset.className = "error";
		}
		else {
			fieldset.className = "done";
			}
}



// Checks the phone number for the correct format
function checkNumber(whatYouTyped) {
	var fieldset = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (/^([0-9 ]{0,14})$/.test(txt)) {
		fieldset.className = "done";
		}
		else {
			fieldset.className = "";
			}
}

// This function checks if the username field
// is at least 6 characters long.
// If so, it attaches class="done" to the containing fieldset.
function checkUsernameForLength(whatYouTyped) {
	var fieldset = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (txt.length > 5) {
		fieldset.className = "done";
	}
	else {
		fieldset.className = "";
	}
}

// If the password is at least 4 characters long, the containing 
// fieldset is assigned class="almost".
// If it's at least 8 characters long, the containing
// fieldset is assigned class="done", to give the user
// the indication that they've selected a harder-to-crack password.
function checkPassword(whatYouTyped) {
	var fieldset = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (txt.length > 3 && txt.length < 8) {
		fieldset.className = "almost";
	} else if (txt.length > 7) {
		fieldset.className = "done";
	} else {
		fieldset.className = "";
	}
}

// Checks the phone number for the correct format
function checkString(whatYouTyped) {
	var fieldset = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (/^([A-Za-z-.' ]*)$/.test(txt)) {
		fieldset.className = "done";
		} 
		else {
			fieldset.className = "";
			}
}

// This function checks the email address to be sure
// it follows a certain pattern:
// blah@blah.blah
// If so, it assigns class="done" to the containing
// fieldset.
function checkEmail(whatYouTyped) {
	var fieldset = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(txt)) {
		fieldset.className = "done";
	} else {
		fieldset.className = "";
	}
}

// this part is for the form field hints to display
// only on the condition that the text input has focus.
// otherwise, it stays hidden.
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}


function prepareInputsForHints() {
  var inputs = document.getElementsByTagName("input");
  for (var i=0; i<inputs.length; i++){
    inputs[i].onfocus = function () {
      this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
    }
    inputs[i].onblur = function () {
      this.parentNode.getElementsByTagName("span")[0].style.display = "none";
    }
  }
}
addLoadEvent(prepareInputsForHints);






//------------------- form validation ------------------
function validate(form){
 var firstName 	= form.FirstName.value;
 var surname 	= form.Surname.value;
 var email 		= form.Email.value;
 var errors 	= [];

// CV validation
function validateCV()
{
var extensions 		= new Array("doc","docx","rtf","txt");
var uploaded_cv 	= document.applyJobForm.uploaded_cv.value;
var image_length 	= document.applyJobForm.uploaded_cv.value.length;
var pos 			= uploaded_cv.lastIndexOf('.') + 1;
var ext 			= uploaded_cv.substring(pos, image_length);
var final_ext 		= ext.toLowerCase();

if (uploaded_cv=="") {
	errors[errors.length] = "Please Browse & Select Your CV";
	}
	else
	if (uploaded_cv!="") {
		for (i = 0; i < extensions.length; i++)
		{
			if(extensions[i] == final_ext) {
				return true;
			}
		}
}

errors[errors.length] = "Only Extensions ["+ extensions.join(' ') +"]";
return false;
}
  
 //Title
 if ( !checkSelect(form.Title) ) {
	errors[errors.length] = "Please Select Your Title.";
 }
 
 //First Name
 if (!checkLength(firstName)) {
	errors[errors.length] = "Please Enter Your Firstname.";
 }
 
 //Surname
 if (!checkLength(surname)) {
	errors[errors.length] = "Please Enter Your Surname.";
 }
 
//Email Address
if (!checkLength(email)) {
	errors[errors.length] = "Please Enter Your Email Address.";
}
	else {
		   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		   
		   if (reg.test(email) == false) {
			  errors[errors.length] = "Your Email Address Is Not Valid.";
		   }
	}

 //Where Did You Hear About KDC
 if (!checkSelect(form.HearAbout)) {
  errors[errors.length] = "Please Select Where You Heard About Us.";
 }
 
 //checks the value of a dropdown is not 0 (i.e. the first option)
 function checkSelect(select){
	return (select.selectedIndex > 0);
}

//Checks the length of text boxes is between the max/min values
function checkLength(text, min, max){
 min = min || 1;
 max = max || 10000;

 if (text.length < min || text.length > max) {
  return false;
 }
 return true;
}

//Displays the errors from the above functions
function reportErrors(errors){
 var msg = "Sorry, There Were Some Problems...\n";
 var numError;
 for (var i = 0; i<errors.length; i++) {
  numError = i + 1;
  msg += "\n" + numError + ". " + errors[i];
 }
 alert(msg);
}
 
//Calculates if there are any error
 if (errors.length > 0) {
  reportErrors(errors);
  return false;
 }
 
 //If all the above checks pass, the fuction returns true.
 return true;
}


