// form validation function //
function validate() {
  var commentType = document.getElementById('commentType').value;
  var subject = document.getElementById('subject').value;
  var subjectOther = document.getElementById('subjectOther').value;
  var comments = document.getElementById('comments').value;
  var cname = document.getElementById('cname').value;
  var cemail = document.getElementById('cemail').value;
  var ctel = document.getElementById('ctel').value;
  var cfax = document.getElementById('cfax').value;
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var postCodeRegex=/^[a-zA-Z]{1,2}[0-9][0-9A-Za-z]? [0-9][a-zA-Z]{2}$/;
  var indianZipRegex=/^\d{6}$/;
  var usZipRegex=/(^\d{5}$)|(^\d{5}-\d{4}$)/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var indianPhoneRegex=/^0{0,1}[1-9]{1}[0-9]{2}[\s]{0,1}[\-]{0,1}[\s]{0,1}[1-9]{1}[0-9]{6}$/;
  var usPhoneRegex=/^([\(]{1}[0-9]{3}[\)]{1}[\.| |\-]{0,1}|^[0-9]{3}[\.|\-| ]?)?[0-9]{3}(\.|\-| )?[0-9]{4}$/;
  var found_it //initial value is null because we gave it no other value
  
  for (var i=0; i<document.frmContact.commentType.length; i++)  {
	if (document.frmContact.commentType[i].checked)  {
	found_it = document.frmContact.commentType[i].value //set found_it equal to checked button's value
	}
	}
	if(!found_it){ //if found_it is equal to false or null, a button has NOT been checked
    inlineMsg('commentType','You must enter a comment type.');
    return false;
	}
if(commentType == "") {
    inlineMsg('commentType','You must enter a comment type.');
    return false;
  }
    if(subject == "") {
    inlineMsg('subject','You must select a subject.');
    return false;
  }
  if(subject=="other"){
	if(subjectOther == "") {
	inlineMsg('subjectOther','You must enter other subject.');
	return false;
	}
  }
    if(comments == "") {
    inlineMsg('comments','You must enter comments.');
    return false;
  }
    if(cname == "") {
    inlineMsg('cname','You must enter a contact name.');
    return false;
  }
    if(cemail == "") {
    inlineMsg('cemail','You must enter contact email.');
    return false;
  }
    if(!cemail.match(emailRegex)) {
    inlineMsg('cemail','You have entered an invalid contact email.',2);
    return false;
  }
  if(ctel == "") {
    inlineMsg('ctel','You must enter contact telephone.',2);
    return false;
  }
  if(cfax == "") {
    inlineMsg('cfax','You must enter contact fax.',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 * 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;
}

