// form validation function //



function validatebusiness(form) {
  var firstname = form.firstname.value;
  var email = form.email.value;
  var companyemail = form.companyemail.value;
  var companytitle = form.companytitle.value;
  var companydetails = form.companydetails.value;
  var Code = form.Code.value;
  var password = form.password.value;
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
  

  
  
  if(firstname == "") {
    inlineMsg('firstname','Please enter your name.',3);
    return false;
  }
  if(!firstname.match(nameRegex)) {
    inlineMsg('firstname','You have entered an invalid name.',3);
    return false;
  }
  if(email == "") {
    inlineMsg('email','<strong>Hi!</strong><br />Please enter your email.',3);
    return false;
  }
  
  
   if(password == "") {
    inlineMsg('password','<strong>Hi!</strong><br />Please enter a password longer than 3 characters.',3);
    return false;
  }
  
  if(!email.match(emailRegex)) {
    inlineMsg('email','<strong>Hi!</strong><br />It seems you have entered an invalid email.',3);
    return false;
  }
  
   if(companytitle == "") {
    inlineMsg('companytitle','Please enter a company name.',2);
    return false;
  }
  if(companytitle.match(messageRegex)) {
    inlineMsg('companytitle','Looks like you have entered an invalid company name.',3);
    return false;
  }
  if(companydetails == "") {
    inlineMsg('companydetails','Please enter your company details.',3);
    return false;
  }
    if(companyemail == "") {
    inlineMsg('companyemail','<strong>Hi!</strong><br />Please enter your company email.',3);
    return false;
  }
  if(!companyemail.match(emailRegex)) {
    inlineMsg('email','<strong>Hi!</strong><br />It seems you have entered an invalid company mail.',3);
    return false;
  }
  
  if(companydetails.match(messageRegex)) {
    inlineMsg('companydetails','You have entered invalid details with HTML code.',3);
    return false;
  }
  
   
     if(Code == "") {
    inlineMsg('Code','Please enter the security code number below.',3);
    return false;
  }
 
 return howManyChecked('form','txtcats',3,1,'Categories');
 
  return true;
}



function validate(form) {
  var firstname = form.firstname.value;
  var email = form.email.value;
  var reviewvalue = form.reviewvalue.value;
  var Review = form.Review.value;
  var departid = form.departid.value;
  var arriveid = form.arriveid.value;
  var airlineid = form.airlineid.value;
  var Code = form.Code.value;
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
  if(firstname == "") {
    inlineMsg('firstname','Please enter your name.',3);
    return false;
  }
  if(!firstname.match(nameRegex)) {
    inlineMsg('firstname','You have entered an invalid name.',3);
    return false;
  }
  if(email == "") {
    inlineMsg('email','<strong>Hi!</strong><br />Please enter your email.',3);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','<strong>Hi!</strong><br />It seems you have entered an invalid email.',3);
    return false;
  }
  if(departid == "") {
    inlineMsg('depart','<strong>Hi!</strong><br />Please enter your departing city.',3);
    return false;
  }
  if(arriveid == "") {
    inlineMsg('arrive','<strong>Hi!</strong><br />Please enter your arrival city.',3);
    return false;
  }
  if(airlineid == "") {
    inlineMsg('airline','<strong>Hi!</strong><br />Please enter your airline.',3);
    return false;
  }
  if(reviewvalue == "0") {
    inlineMsg('ratingmsg','<strong>Hi!</strong><br />Please select your rating.',3);
    return false;
  }
  
  if(Review == "") {
    inlineMsg('Review','Please enter your review.',3);
    return false;
  }
  if(Review.match(messageRegex)) {
    inlineMsg('Review','You have entered an invalid review with code.',3);
    return false;
  }
     if(Code == "") {
    inlineMsg('Code','Please enter the security code number below.',3);
    return false;
  }

  return true;
}

function validateflight(form) {
  var startflight = form.departid.value;
  var endflight = form.arriveid.value;
  var numberRegex  = /(^-?\d\d*$)/;


 if(startflight == "") {
    inlineMsg('depart','<strong>Hi!</strong><br />It seems you have not chosen a starting flight.',2);
    return false;
  }
  if(endflight == "") {
    inlineMsg('arrive','<strong>Hi!</strong><br />It seems you have not chosen a destination.',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;
}

// preload the arrow //
if(document.images) {
  arrow = new Image(7,80); 
  arrow.src = "/messages/images/msg_arrow.gif"; 
}


<!--
    // Use the maxChecked variable to set the maximum number that can be checked
    var maxChecked = 3
    var totalChecked = 0

    function CountChecked(field) {
        if (field.checked)
            totalChecked += 1
        else
            totalChecked -= 1
        // 
        if (totalChecked > maxChecked) {
            //alert ("You cannot choose more than three categories.")
			inlineMsg('txtcats','You cannot choose more than three categories.',2);
            field.checked = false
            totalChecked = maxChecked
        }
    }

    function ResetCount(){
        totalChecked = 0
    }
//-->

	function RefreshImage(valImageId) {
		var objImage = document.images[valImageId];
		if (objImage == undefined) {
			return;
		}
		var now = new Date();
		objImage.src = objImage.src.split('?')[0] + '?x=' + now.toUTCString();
	}
	
	function howManyChecked(whichForm,whichCheckBoxArray,myMax,myMin,whichQuestion)
/*
  This function takes 5 paramaters:
  whichForm -- the NAME of the form to be validated, a string
  whichCheckBoxArray -- the NAME of the checkbox to be checked, a string
  myMax -- the most you want the user to be able to check, an integer
  myMin -- the least you want the user to be able to check, an integer
  whichQuestion -- a short description of the question, a string
  
  example use:
  howManyChecked('myform','cb_industry',6,1,'Industry');
*/
{
	var _countChecked = 0;
	var err = 0;
	/* iterate through all the elements in the checkbox array */
	for(i=0;i<document[whichForm][whichCheckBoxArray].length;i++)
	{
		/* and check to see if each is checked */
		if(document[whichForm][whichCheckBoxArray][i].checked==true)
			/* if it is, increment a counter */
			{ _countChecked++; }
	}
	/* is the count too high? */
	if(_countChecked > myMax)
	{ inlineMsg(whichCheckBoxArray,'<strong>Hi!</strong><br />Tick no more than 3 categories please.',3);
			err = 1;}
	/* of is the count too low */
	else if(_countChecked < myMin)
		{ inlineMsg(whichCheckBoxArray,'<strong>Hi!</strong><br />Please choose at least one category.',3);
			err = 1;}
	if (err == 1) { return false; }
}

