// form validation functions //

function validatecontactus(contactusform) {
  var fname = contactusform.fname.value;
  var position = contactusform.position.value;
  var company = contactusform.company.value;
  var email = contactusform.email.value;
  var phonework = contactusform.phonework.value;
  var phonemobile = contactusform.phonemobile.value;
  var calltime = contactusform.calltime.value;
  var comments = contactusform.comments.value;
  var _vJob1Fr = contactusform._vJob1Fr.value;
  var _vJob1EstM = contactusform._vJob1EstM.value;
  var _vJob1DHole = contactusform._vJob1DHole.value;
  var _vJob1AvgD = contactusform._vJob1AvgD.value;
  var _vJob1Details = contactusform._vJob1Details.value; 
  
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var contactRegex = /^\(?\d{2}\)?-?\s*\d{8}$/;
  
  if(fname == "") {
    inlineMsg('fname','You must enter your name!',2);
    return false;
  }
  if(!fname.match(nameRegex)) {
    inlineMsg('fname','You have entered an invalid name!',2);
    return false;
  }
  if(position == "") {
    inlineMsg('position','You must enter your position!',2);
    return false;
  }
  if(company == "") {
    inlineMsg('company','You must enter your company!',2);
    return false;
  }
  if(email == "") {
    inlineMsg('email','You must enter your email address!',2);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','<strong>Error</strong><br />You have entered an invalid email address!',2);
    return false;
  }
  if(phonework == "") {
    inlineMsg('phonework','You must enter your work contact number!',2);
    return false;
  }
  if(phonemobile == "") {
    inlineMsg('phonemobile','You must enter your mobile contact number!',2);
    return false;
  }
  if(comments == "") {
    inlineMsg('comments','What is your enquiry about?',2);
    return false;
  } 
  if(_vJob1Fr == "") {
    inlineMsg('_vJob1Fr','Please complete all fields!',2);
    return false;
  } 
  if(_vJob1EstM == "") {
    inlineMsg('_vJob1EstM','Please complete all fields!',2);
    return false;
  } 
  if(_vJob1DHole == "") {
    inlineMsg('_vJob1DHole','Please complete all fields!',2);
    return false;
  } 
  if(_vJob1AvgD == "") {
    inlineMsg('_vJob1AvgD','Please complete all fields!',2);
    return false;
  } 

  return true;
}

function validateenquiryform(enquiryform) {
  var fname = enquiryform.fname.value;
  var company = enquiryform.company.value;
  var email = enquiryform.email.value;
  var phonework = enquiryform.phonework.value;
  var phonemobile = enquiryform.phonemobile.value;
  var calltime = enquiryform.calltime.value;
  var comments = enquiryform.comments.value;

  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var contactRegex = /^\(?\d{2}\)?-?\s*\d{8}$/;
  
  if(fname == "") {
    inlineMsg('fname','You must enter your name!',2);
    return false;
  }
  if(!fname.match(nameRegex)) {
    inlineMsg('fname','You have entered an invalid name!',2);
    return false;
  }
  if(company == "") {
    inlineMsg('company','You must enter your company!',2);
    return false;
  }
  if(email == "") {
    inlineMsg('email','You must enter your email address!',2);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','<strong>Error</strong><br />You have entered an invalid email address!',2);
    return false;
  }
  if(phonework == "") {
    inlineMsg('phonework','You must enter your work contact number!',2);
    return false;
  }
  if(phonemobile == "") {
    inlineMsg('phonemobile','You must enter your mobile contact number!',2);
    return false;
  }
  if(comments == "") {
    inlineMsg('comments','What is your enquiry about?',2);
    return false;
  } 

  return true;
}

function chechExtension()
		{
			var extString = document.appform.attachFile.value;
			if(extString == "")
			{
				return true;
			}
			else
			{
				var position = extString.lastIndexOf(".");
				var ext = extString.substring(position);
				if (ext == ".txt" || ext == ".doc" || ext == ".pdf" || ext == ".png" || ext == ".bmp" || ext == ".jpg" || ext == ".gif")
				{
					return true;
				}
				else
				{
					return false;
				}
			}
		}
		
function validateapp(appform) {
  var fname = appform.fname.value;
  var email = appform.email.value;
  var phone = appform.phone.value;
  var calltime = appform.calltime.value;
  var comments = appform.comments.value;
  var attachFile = appform.attachFile.value;

  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var contactRegex = /^\(?\d{2}\)?-?\s*\d{8}$/;
  
  if(fname == "") {
    inlineMsg('fname','You must enter your name!',2);
    return false;
  }
  if(!fname.match(nameRegex)) {
    inlineMsg('fname','You have entered an invalid name!',2);
    return false;
  }
  if(email == "") {
    inlineMsg('email','You must enter your email address!',2);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','<strong>Error</strong><br />You have entered an invalid email address!',2);
    return false;
  }
  if(phone == "") {
    inlineMsg('phone','You must enter your contact number!',2);
    return false;
  }
  if(comments == "") {
    inlineMsg('comments','What is your enquiry about?',2);
    return false;
  } 
  if(attachFile == "") {
    inlineMsg('attachFile','You must upload a file!',2);
    return false;
  } 
	if (!chechExtension())
	{
    inlineMsg('attachFile','Selected File Type Is Not Allowed, Please Select In [ .txt, .bmp, .jpg, .doc, .gif, .png, .pdf ]',2);
	return false;
	}

  return true;
}

// START OF MESSAGE SCRIPT //

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

// 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;
  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 * 2000));
}

// 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"; 
}

function set(el)
{
   document.getElementById("_id"+el.name).style.display = (el.value=='y')? 'inline':'none';
}

function addJobReq()
{
   var i,nextRow,lastRow;
   for (i=1;nextRow=document.getElementById("_job"+i);i++)
      lastRow = nextRow;

   nextRow = document.createElement("tr");
   nextRow.id = "_job"+i;

   tdCell  = document.createElement("td");
   tdCell.innerHTML = "<input name='_vJob"+i+"Fr' type='text' size='12' maxlength='24'>";
   nextRow.appendChild(tdCell);
	
   tdCell  = document.createElement("td");
   tdCell.innerHTML = "<input name='_vJob"+i+"EstM' type='text' size='6' maxlength='64'>";
   nextRow.appendChild(tdCell);

   tdCell  = document.createElement("td");
   tdCell.innerHTML = "<input name='_vJob"+i+"DHole' type='text' size='6' maxlength='64'>";
   nextRow.appendChild(tdCell);
   
   tdCell  = document.createElement("td");
   tdCell.innerHTML = "<input name='_vJob"+i+"AvgD' type='text' size='6' maxlength='64'>";
   nextRow.appendChild(tdCell);
   
   tdCell  = document.createElement("td");
   tdCell.innerHTML = "<select name='_vJob"+i+"Program'><option>Exploration</option><option>Grade Control</option><option>Other</option>";
   nextRow.appendChild(tdCell);

   tdCell  = document.createElement("td");
   tdCell.innerHTML = "<textarea name='_vJob"+i+"Details' rows='1' cols='19'></textarea>";
   nextRow.appendChild(tdCell);
		
   tdCell  = document.createElement("td");
   tdCell.id = "_td"+i;
   tdCell.innerHTML = "<input id='_btnjob"+i+"' type='button' value='x' class='submit' onclick=\"removeItem(this,'job')\" />";
   nextRow.appendChild(tdCell);

   lastRow.parentNode.appendChild(nextRow);
   i--;
   if(btnToDisable = document.getElementById("_btnjob"+i))
      btnToDisable.disabled=true;
}
function addOthReq()
{
   var i,nextRow,lastRow;
   for (i=1;nextRow=document.getElementById("_Oth"+i);i++)
      lastRow = nextRow;

   nextRow = document.createElement("tr");
   nextRow.id = "_Oth"+i;
   //nextRow.setAttribute('style','height:20px');

   tdCell  = document.createElement("td");
   tdCell.innerHTML = "<input name='_vOth"+i+"Area' type='text'  size='6' maxlength='12'>";
   nextRow.appendChild(tdCell);

   tdCell  = document.createElement("td");
   tdCell.innerHTML = "<input name='_vOth"+i+"NTown' type='text' size='6' maxlength='12'>";
   nextRow.appendChild(tdCell);
	
   tdCell  = document.createElement("td");
   tdCell.innerHTML = "<input name='_vOth"+i+"GCond' type='text' size='6' maxlength='80'>";
   nextRow.appendChild(tdCell);
  
   tdCell  = document.createElement("td");
   tdCell.innerHTML = "<select name='_vOth"+i+"CFac'><option>Supplied</option><option>To Be Supplied</option>";
   nextRow.appendChild(tdCell);
   
   tdCell  = document.createElement("td");
   tdCell.innerHTML = "<select name='_vOth"+i+"Fuel'><option>Supplied</option><option>To Be Supplied</option>";
   nextRow.appendChild(tdCell);

   tdCell  = document.createElement("td");
   tdCell.innerHTML = "<textarea name='_vOth"+i+"Comments' rows='1' cols='20'></textarea>";
   nextRow.appendChild(tdCell);
		
   tdCell  = document.createElement("td");
   tdCell.id = "_td"+i;
   tdCell.innerHTML = "<input class='submit' id='_btnOth"+i+"' type='button' value='x' onclick=\"removeItem(this,'Oth')\" />";
   nextRow.appendChild(tdCell);

   lastRow.parentNode.appendChild(nextRow);
   i--;
   if(btnToDisable = document.getElementById("_btnOth"+i))
      btnToDisable.disabled=true;
}
function removeItem(el, prefix)
{
   var i = el.id.substring(el.id.indexOf(prefix)).substring(prefix.length);
   node = document.getElementById("_"+prefix+i);
   node.parentNode.removeChild(node);
   i--;
   if(btnToEnable = document.getElementById("_btn"+prefix+i))
      btnToEnable.disabled = false;
}
