// form validation function //
function validate(form) {
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var ukDateRegex = /^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$/;
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var currencyRegex = /^\$?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(.[0-9][0-9])?$/;
  var ukTelRegex =/(((\+44)? ?(\(0\))? ?)|(0))( ?[0-9]{3,4}){3}/;
  var ukOutcodeRegex = /^([a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1}) *([0-9][A-Za-z]{2}){0,1}$/;
  
  if (form.name == "job_search" ) {
      if(!form.postcode.value.match(ukOutcodeRegex)) {
        inlineMsg('postcode','Please enter the first part of your postcode (i.e. CO10).',2);
        return false;
      }
  }

  if (form.name == "admin_upload_cv" ) {
      if(!form.fullname.value.match(nameRegex)) {
        inlineMsg('fullname','Please enter candidates name.',2);
        return false;
      }
      if(!form.cv.value){
        inlineMsg('cv','Please browse for CV to upload.',2);
        return false;
      }
      if(!TestFileType(form.cv.value, ['pdf'])) {
        //inlineMsg('cv', '.pdf files only please!',2);
        //return false;
      }
  }

  if (form.name == "job_application" ) {
    if(!form.fullname.value.match(nameRegex)) {
      inlineMsg('fullname','Please enter your full name.',2);
      return false;
    }
    if(form.email.value > "" && !form.email.value.match(emailRegex)) {
      inlineMsg('email','Please enter a valid email address.',2);
      return false;
    }
    if(!form.telephone.value.match(ukTelRegex)) {
      inlineMsg('telephone','Please enter a valid contact telephone number.',2);
      return false;
    }
    if(form.own_transport.value == "-") {
      inlineMsg('own_transport','Please select your answer to this question.',2);
      return false;
    }
    if(form.temporary_work.value == "-") {
      inlineMsg('temporary_work','Please select your answer to this question.',2);
      return false;
    }
    if(form.permanent_work.value == "-") {
      inlineMsg('permanent_work','Please select your answer to this question.',2);
      return false;
    }
    if(!form.salary.value.match(currencyRegex) && !form.hourly.value.match(currencyRegex) ) {
      inlineMsg('salary','Please enter your salary preferences.',2);
      return false;
    }
    if(form.availability.value == "-") {
      inlineMsg('availability','Please select your answer to this question.',2);
      return false;
    }
    if(!form.cv.value){
    	    inlineMsg('cv','Please select your CV to upload.',2);
    	    return false;
    }
    if(!TestFileType(form.cv.value, ['doc', 'docx', 'pdf', 'rtf', 'txt'])) {
      inlineMsg('cv','.doc, .pdf, .rtf or .txt files only please!',2);
      return false;
    }
    if(!form.eligible.checked) {
      inlineMsg('eligible','You must confirm that you are eligible to work in the U.K.',2);
      return false;
    }
  }
  
  if (form.name == "admin_edit_location" ) {
    if(form.location_title.value == "") {
      inlineMsg('location_title','You must enter a name for this town.',2);
      return false;
    }
    
    if(form.location_postcode.value == "") {
      inlineMsg('location_postcode','You must enter a postcode.',2);
      return false;
    }
  }
  
  if (form.name == "admin_edit_area" ) {
    if(form.area_title.value == "") {
      inlineMsg('area_title','You must enter a name for this county.',2);
      return false;
    }
  }
  
  
  if (form.name == "admin_edit_category" ) {
    if(form.category_title.value == "") {
      inlineMsg('category_title','You must enter a name for this category.',2);
      return false;
    }
  }
  
  if (form.name == "admin_edit_job" ) {
    var job_expiry = form.job_expiry.value;
    var job_added = form.job_added.value;
    var email= form.job_email_alias.value;

    if(form.job_title.value == "") {
      inlineMsg('job_title','You must enter a title for this job.',2);
      return false;
    }
    
    if(!job_added.match(ukDateRegex)) {
      inlineMsg('job_added','You must enter a valid date (day/month/year).',2);
      return false;
    }
    
    if(!job_expiry.match(ukDateRegex)) {
      inlineMsg('job_expiry','You must enter a valid date (day/month/year).',2);
      return false;
    }
    
    if(form.job_desc.value == "") {
      inlineMsg('job_desc','You must enter a description for this job.',2);
      return false;
    }
    
    if(form.job_teaser.value == "") {
      inlineMsg('job_teaser','You must enter a teaser for this job.',2);
      return false;
    }
    
    if(form.job_salary.value == "") {
      inlineMsg('job_salary','You must specify the salary for this job.',2);
      return false;
    }
    
    if(form.job_start_date.value == "") {
      inlineMsg('job_start_date','You must enter a start date for this job.',2);
      return false;
    }
    
    if(!email.match(emailRegex)) {
    inlineMsg('job_email_alias','You must enter a valid email address.',2);
    return false;
    }
  }
  
  return true;
}
// Functions
function TestFileType( fileName, fileTypes ) {
	if (!fileName) return;
	
	dots = fileName.split(".")
	//get the part AFTER the LAST period.
	fileType = "." + dots[dots.length-1];
	
	return (fileTypes.join(".").indexOf(fileType) != -1) ? true : false;
}
// START OF MESSAGE SCRIPT //

var MSGTIMER = 20;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 5;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {
  var msg;
  var msgcontent;
  if(!document.getElementById('msg')) {
    msg = document.createElement('div');
    msg.id = 'msg';
    msgcontent = document.createElement('div');
    msgcontent.id = 'msgcontent';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter = 'alpha(opacity=0)';
    msg.style.opacity = 0;
    msg.alpha = 0;
  } else {
    msg = document.getElementById('msg');
    msgcontent = document.getElementById('msgcontent');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;
  var targetdiv = document.getElementById(target);
  targetdiv.focus();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  //window.scroll(0,topposition Ð 40);
  msg.style.top = topposition + 'px';
  msg.style.left = leftposition + 'px';
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
    autohide = MSGHIDE;  
  }
  window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
  var msg = document.getElementById('msg');
  if(!msg.timer) {
    msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  }
}

// face the message box //
function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
  var top = 0;
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}

// preload the arrow //
if(document.images) {
  arrow = new Image(7,80); 
  arrow.src = "images/msg_arrow.gif"; 
}
