// form validation function //
function validate(form) {
	var fname = form.fname.value;
  	var lname = form.lname.value;
	var email = form.email.value;
 	var primaryphone1 = form.primaryphone1.value;
  	//var primaryphone2 = form.primaryphone2.value;
	//var primaryphone3 = form.primaryphone3.value;
   	var secondaryhone1 = form.secondaryhone1.value;
//  	var secondaryhone2 = form.secondaryhone2.value;
//	var secondaryhone3 = form.secondaryhone3.value;
/*
  var message = form.message.value;*/
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
 var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
   var primaryphone1Regex =/^\d{10}-\d{10}|\d{10}|[A-Z]\d[A-Z] \d[A-Z]\d$/;
  //var primaryphone2Regex =/^\d{3}-\d{3}|\d{3}|[A-Z]\d[A-Z] \d[A-Z]\d$/;
  //var primaryphone3Regex =/^\d{4}-\d{4}|\d{4}|[A-Z]\d[A-Z] \d[A-Z]\d$/;


  var secondaryhone1Regex =/^\d{10}-\d{10}|\d{10}|[A-Z]\d[A-Z] \d[A-Z]\d$/;
 // var secondaryhone2Regex =/^\d{3}-\d{3}|\d{3}|[A-Z]\d[A-Z] \d[A-Z]\d$/;
//  var secondaryhone3Regex =/^\d{4}-\d{4}|\d{4}|[A-Z]\d[A-Z] \d[A-Z]\d$/;

  var Procedure = form.Procedure.value;
  var ReferredBy = form.ReferredBy.value;
  var data_comments = form.data_comments.value;
 /* var code = form.code.value;


  var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);*/
  if(!document.form.C1.checked && !document.form.C2.checked  && !document.form.C4.checked  ) {
    inlineMsg('C1','Please select services you like..');
	return false;
  }
 
   if(!document.form.YES[0].checked && !document.form.YES[1].checked ) {
    inlineMsg('YES','Select your current patient status.');
	return false;
  }

	if(fname == "") {
    inlineMsg('fname','You must enter your first name.',2);
    return false;
  }
	if(!fname.match(nameRegex)) {
    inlineMsg('fname','You have entered an invalid fisrt name.',2);
    return false;
  }

	if(lname == "") {
    inlineMsg('lname','You must enter your last name.',2);
    return false;
  }
	if(!lname.match(nameRegex)) {
    inlineMsg('lname','You have entered an invalid last name.',2);
    return false;
  }
	
	if(email == "") {
    inlineMsg('email','You must enter your email.',2);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','<strong>Warning:</strong><br />You have entered an invalid email.',2);
    return false;
  }
  
  
    if(primaryphone1 == "") {
    inlineMsg('primaryphone1','You must enter a Phone.',2);
    return false;
  }
  if(!primaryphone1.match(primaryphone1Regex)) {
    inlineMsg('primaryphone1','You have entered an invalid Phone',2);
    return false;
  }
  /*if(primaryphone2 == "") {
    inlineMsg('primaryphone2','You must enter a Phone.',2);
    return false;
  }*/
  /*if(!primaryphone2.match(primaryphone2Regex)) {
    inlineMsg('primaryphone2','You have entered an invalid Phone',2);
    return false;
  }*/
 /* if(primaryphone3 == "") {
    inlineMsg('primaryphone3','You must enter a Phone.',2);
    return false;
  }
  if(!primaryphone3.match(primaryphone3Regex)) {
    inlineMsg('primaryphone3','You have entered an invalid Phone',2);
    return false;
  }*/
  
  
  
      if(secondaryhone1 == "") {
    inlineMsg('secondaryhone1','You must enter a Phone.',2);
    return false;
  }
  if(!secondaryhone1.match(secondaryhone1Regex)) {
    inlineMsg('secondaryhone1','You have entered an invalid Phone',2);
    return false;
  }
/*  if(secondaryhone2 == "") {
    inlineMsg('secondaryhone2','You must enter a Phone.',2);
    return false;
  }
  if(!secondaryhone2.match(secondaryhone2Regex)) {
    inlineMsg('secondaryhone2','You have entered an invalid Phone',2);
    return false;
  }
  if(secondaryhone3 == "") {
    inlineMsg('secondaryhone3','You must enter a Phone.',2);
    return false;
  }
  if(!secondaryhone3.match(secondaryhone3Regex)) {
    inlineMsg('secondaryhone3','You have entered an invalid Phone',2);
    return false;
  }

*/
   if(!document.form.Morning.checked && !document.form.Noon.checked  && !document.form.Afternoon.checked  && !document.form.Evening.checked  ) {
    inlineMsg('Morning','Select best time to cal you..');
	return false;
  }


   if(!document.form.Consult.checked && !document.form.Visit.checked  && !document.form.Other.checked) {
    inlineMsg('Consult','Select Nature of your Appointment');
	return false;
  }

     if(!document.form.Day.checked && !document.form.Mon.checked  && !document.form.Tues.checked  && !document.form.Wed.checked  && !document.form.Thurs.checked  && !document.form.Fri.checked) {
    inlineMsg('Day','Select Preferred Day for your Appointment');
	return false;
	 }


     if(!document.form.Time.checked && !document.form.Earliest.checked  && !document.form.LateMorning.checked  && !document.form.Lunchtime.checked  && !document.form.EarlyAfternoon.checked  && !document.form.LatestAvailable.checked) {
    inlineMsg('Time','Select Preferred Time for your Appointment');
	return false;
	 }


	if(document.form.Procedure.value=="Select") {
    inlineMsg('Procedure','You must select your Procedure.',2);
    return false;
  }

  /*if(Procedure == "") {
    inlineMsg('Procedure','<strong>Error</strong><br />You must select your Procedure.',2);
    return false;
  }*/

 
  if(document.form.ReferredBy.value=="Select") {
    inlineMsg('ReferredBy','You must select your ReferredBy.',2);
    return false;
  }
 
if(data_comments == "") {
    inlineMsg('data_comments','You must enter a message.');
    return false;
  }
  if(data_comments.match(messageRegex)) {
    inlineMsg('data_comments','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));
}

function checkLen(x,y)
{

if (y.length==x.maxLength)
	{
	
		if(x.name=='primaryphone1')
			document.form.homephone1.focus();
		if(x.name=="primaryphone2")
	    	document.form.homephone2.focus();	
				
	}
}


function checkLe(x,y)
{

if (y.length==x.maxLength)
	{
	
		if(x.name=='secondaryhone1')
			document.form.secondaryhone1.focus();
		if(x.name=="secondaryhone2")
	    	document.form.secondaryhone2.focus();	
				
	}
}


// 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"; 
}
