// Scripts Used on the Contact Us Page //
// Version V1.0.0, 23/09/2007 WBM. //
// Copyright 2007. Property of Pip Consultancy //
// Produced by Design Atom, www.designatom.com //

// Clear the Comments Text Area when it gets Focus
function fctClearComments(thisControl) {
	thisControl.value = "";
}

// Clear the Information on the Form
function fctClearForm(thisForm) {
   // Step through each of the Form Fields in turn
   for (var counter=0; counter < thisForm.length; counter++) {
      // Exclude the Reset and Submit Buttons
      if(thisForm[counter].id!="reset" && thisForm[counter].id!="submit") {
		 // Find the Comments Textbox
		if(thisForm[counter].name!="details"){
			// Reset the Listbox
			thisForm[counter].value = "";
		 }
		 else {
			// Reset the Comments Textbox
			thisForm[counter].value = "Please Enter A Brief Summary Of Your Details Here.";
		 }
      }
      
      // Reset the focus to the First Listbox
      thisForm.contactname.focus()
   }
   return false;
}

// Validate the Information on the Form
function fctCheckForm(thisForm) {
   // Declare global variables
   var strSearch, strEmailAddress, strSubmit;
   
   // Set default values
   bolSubmit = "yes";
   strEmailAddress = "";
   
   // Check if we have an email object on this page and if so get the value
   if (thisForm.emailaddress) strEmailAddress = thisForm.emailaddress.value;
   
   // Replace all occurances of double quotes
   // Step through each of the Form Fields in turn
   for (var counter=0; counter < thisForm.length; counter++) {
      // Replace all double quotes for single quotes
      if(thisForm[counter].value!=null) {
      
         // Get the location of the first occurrence
         strSearch = thisForm[counter].value
         old_location = strSearch.indexOf("\"");
      
         // Loop until there are no more instances
         while (old_location!=-1) {
            strSearch = strSearch.replace("\"","'");
            old_location = strSearch.indexOf("\"");
         }
         
         // Rewrite string with no double quotes
         thisForm[counter].value = strSearch;
      }
   }
   
   
   // Check that a Contact Name has been entered
   if(!thisForm.contactname.value) {
      alert("You Have Not Entered Your Name.");
      thisForm.contactname.focus();
	  bolSubmit = "no";
	  return false;
   }

   // Check that an Email Address or Telephone Number has been entered
   if(!strEmailAddress && !thisForm.phone.value) {
      alert("You have not entered Your Contact Details.");
      thisForm.phone.focus();
	  bolSubmit = "no";
	  return false;
   }
   
   // Check for a valid email address
   var emailError = false;
   
   // Check if an email address has been entered
   if (strEmailAddress) {
      // Check the length
      if (strEmailAddress.length<7) emailError = true;

      // Verify email contains the correct seperators
      at_location = strEmailAddress.indexOf("@");
      dot_location = strEmailAddress.lastIndexOf(".");
      if (at_location == -1 || dot_location == -1 || at_location > dot_location) emailError = true;
     
      // Check there is a name field before the @
      if (at_location == 0) emailError = true;
   
      // Check that there is at least two characters in the url address
      if (dot_location - at_location <= 2) emailError = true;
   
      // Check that there are at least two characters in the country code
      if (strEmailAddress.length - dot_location <= 2) emailError = true;
   
      // If it is an invalid email address prompt the user
      if(emailError) {
         alert("The Email Address You Entered Is Invalid.");
         thisForm.emailaddress.focus();
		 bolSubmit = "no";
		 return false;
      }
   }

   // Check that some information has been entered into the text box
   if(!thisForm.details.value || thisForm.details.value.substr(0,12)=="Please Enter") {
      alert("You Have Not Entered Any Details.");
      thisForm.details.focus();
	  bolSubmit = "no";
	  return false;
   }
   
   
   // If the user is uploading their CV check that it has a valid format
   if(thisForm.cv.value) {
	  // Create an Array of valid file types
	  var arrAllowed = new Array(".pdf", ".doc", ".docx", ".dot", ".dotx", ".htm", ".html", ".xml", ".rtf", ".txt", ".asc", ".ans",  ".mcw", ".wps") 
	  
	  // Set default value
	  bolSubmit = "no";
	  
	  // Retrieve the file extension
      var intIndex = thisForm.cv.value.lastIndexOf(".");
      var strFileType = thisForm.cv.value.substr(intIndex);
      
      // Loop through the array to see if the filetype is allowed
      for (var intCount in arrAllowed) {
		  // Check for valid filetype
		  if (strFileType.toLowerCase() == arrAllowed[intCount]) {
		      // Format is allowed so submit form
		      bolSubmit = "yes";
		      break;
		  }
      }
	  
	  // Check if this file was an invalid format
	  if (bolSubmit == "no") {
		  // This format is not allowed so alert the user
		  alert("This File Format Is Not Allowed!\nYour CV Must Be In One Of The Following\nTypes - 'pdf', 'doc', 'docx', 'dot', 'dotx', 'htm', 'html', 'xml', 'rtf', 'txt', 'asc', 'ans',  'mcw', 'wps'.");
          thisForm.cv.focus();
          return false;
      }
   }

   // Check if we need to submit the form
   if (bolSubmit=="yes") return true;
}