// form validation function //
function validate(form1) {
  var name = form1.name2.value;
  var email = form1.email2.value;
  var phone =form1.phone1.value;
  var bt = form1.besttime.value;
  var np = form1.numberpages.value;
  var cr = form1.contentready.value;
  var dc = form1.designcomments.value;
  var sd = form1.startdate.value;
  var dc = form1.designcomments.value;
  var fc = form1.functioncomments.value;
  var gc = form1.comments2.value;
  var b = form1.budget.value;
 // var gender = form.gender.value;
  //var comments = form.comments.value;
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var numchar =  /^[0-9]+$/;
   var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
  if(name == "") {
    inlineMsg('name2','You must enter your name.',2);
    return false;
  }
  if(!name.match(nameRegex)) {
    inlineMsg('name2','You have entered an invalid name.',2);
    return false;
  }
  if(email == "") {
    inlineMsg('email2','<strong>Error</strong><br />You must enter your email.',2);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email2','<strong>Error</strong><br />You have entered an invalid email.',2);
    return false;
  }
   if(phone == "") {
    inlineMsg('phone1','<strong>Error</strong><br />You must enter your phone number.',2);
    return false;
  }
  if(!phone.match(numchar)) {
    inlineMsg('phone1','<strong>Error</strong><br />You have entered an invalid phone number.',2);
    return false;
  }
  
  if(bt == "") {
    inlineMsg('besttime','<strong>Error</strong><br />You must enter your best time to reach.',2);
    return false;
  }
  if(sd == "Choose One") {
    inlineMsg('startdate','Please select any one.',2);
    return false;
  }
 if(np == "Choose One") {
    inlineMsg('numberpages','<strong>Error</strong><br />You must enter the number of pages.',2);
    return false;
  }
 
if(cr == "") {
    inlineMsg('contentready','<strong>Error</strong><br />Please enter if the content of the file prepared.',2);
    return false;
  }
  if(dc == "Please tell us what you want to see in the design of your site"){	
  inlineMsg('designcomments','<strong>Error</strong><br />You must enter the design comments.',2);
    return false;
  }
  if(dc == ""){	
  inlineMsg('designcomments','<strong>Error</strong><br />You must enter some design comments.',2);
    return false;
  }
  if(fc=="Describe what development or functionality you need your site to have."){
	  inlineMsg('functioncomments','<strong>Error</strong><br />You must enter the functionality comments.',2);
    return false;
  }
   if(fc==""){
	  inlineMsg('functioncomments','<strong>Error</strong><br />You must enter some functionality comments.',2);
    return false;
  }
  if(gc=="Please tell us what the main purpose of the site is."){
	  inlineMsg('comments2','<strong>Error</strong><br />You must enter the general comments.',2);
    return false;
  }
   if(gc==""){
	  inlineMsg('comments2','<strong>Error</strong><br />You must enter some general comments.',2);
    return false;
  }
  if(b == ""){
	  inlineMsg('budget','<strong>Error</strong><br />You must enter your Idea.',2);
    return false;
  }
  
    if(message == "") {
    inlineMsg('message','You must enter a message.');
    return false;
  }
  if(message.match(messageRegex)) {
    inlineMsg('message','You have entered an invalid message.');
    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;
}

// preload the arrow //
if(document.images) {
  arrow = new Image(7,80); 
  arrow.src = "images/msg_arrow.gif"; 
}