// form validation function //
function validate(form) {
  var name = form.name.value;
  var email = form.email.value;
  
  if(document.form.phone2.checked){
	   var contact_phone = true;
	   var phone_number = form.phone_number.value;
  }else{
	  var contact_phone = false;
  }
  
  var adults_number = form.adults_number.value;
  var children_number = form.children_number.value;
  var room_number = form.room_number.value;
  var vacation_type = form.vacation_type.value;
  
  if(document.form.include_airfare.checked){
	   var include_airfare = true;
	   var preferred_airport = form.preferred_airport.value;
  }else{
	  var include_airfare = false;
  }
  if(document.form.vac_package.checked || document.form.hotel_only.checked ){
	   var vac_type = "vacation_package";
		  }else if(document.form.cruise_only.checked || document.form.cruise_package.checked){
			  var vac_type = "cruise_package";
			  }
  var vacation_budget = form.vacation_budget.value;
  var city_state = form.city_state.value;
  var destination = form.destination.value;
  var dest = document.getElementById ('destination');
  var destination_text = dest[dest.selectedIndex].text.toLowerCase();  
  
  switch (destination_text) {
	    case 'mexico':
			var select_hotel = form.select_mexico_hotel.value;
		break;
		case 'caribbean':
			var select_hotel = form.select_caribbean_hotel.value;
		break;
		case 'hawaii':
			var select_hotel = form.select_hawaii_hotel.value;
		break;
		case 'europe':
			var select_hotel = form.select_europe_hotel.value;
		break;
		default: 
			var select_hotel = "";		
	}
  
  var destination_details = form.destination_details.value;
  var destination_cruise = form.destination_cruise.value;
  var select_mexico_cruise = form.select_mexico_cruise.value;
  var select_departing_port = form.select_departing_port.value;
  
  var image_verification = form.image_verification.value;
  
  var port = form.select_departing_port.value;
	
  var depart_date = form.depart_date.value;
  var return_date = form.return_date.value;
 
  if(document.form.other.checked){
	   var other_option = true;
	   var other_text = form.other_option.value;
  }else{
	  var other_option = false;
  }
 
  if(document.form.terms_of_use.checked){
	   var terms_of_use = true;
  }else{
	  var terms_of_use = false;
  }
	  
  
  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(name == "") {
    inlineMsg('name','You must enter your name.',2);
    return false;
  }
  if(!name.match(nameRegex)) {
    inlineMsg('name','<strong>Error</strong><br />You have entered an invalid name.',2);
    return false;
  }
  if(email == "") {
    inlineMsg('email','<strong>Error</strong><br />You must enter your email.',2);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','<strong>Error</strong><br />You have entered an invalid email.',2);
    return false;
  }
  if(contact_phone == true) {
	  if (phone_number == "" || phone_number == "- Enter your phone number -"){
		inlineMsg('phone_number','<strong>Error</strong><br />You must enter your phone number.',2);
		return false;
	  }
  }
  if(vacation_type == "0" && vacation_type == "0") {
    inlineMsg('vacation_type','<strong>Error</strong><br />You must select the vacation type.',2);
    return false;
  }
  if(adults_number == "0" && children_number == "0") {
    inlineMsg('children_number','<strong>Error</strong><br />You must select the number of travelers.',2);
    return false;
  }
  if(room_number == "0") {
    inlineMsg('room_number','<strong>Error</strong><br />You must select the number of rooms.',2);
    return false;
  }
  if(vacation_budget == "0") {
    inlineMsg('vacation_budget','<strong>Error</strong><br />You must select a vaction budget.',2);
    return false;
  }
  if(city_state == "") {
    inlineMsg('city_state','<strong>Error</strong><br />You must enter a city and state.',2);
    return false;
  }
  if(include_airfare == true && preferred_airport == "") {
    inlineMsg('preferred_airport','<strong>Error</strong><br />You must select your preferred airport.',2);
    return false;
  }
  if(destination == "" && vac_type == "vacation_package") {
    inlineMsg('destination','<strong>Error</strong><br />You must select a destination.',2);
    return false;
  }
  if(destination_cruise == "" && vac_type == "cruise_package") {
    inlineMsg('destination_cruise','<strong>Error</strong><br />You must select a destination.',2);
    return false;
  }
  if(select_mexico_cruise == "" && vac_type == "cruise_package") {
    inlineMsg('select_mexico_cruise','<strong>Error</strong><br />You must select a cruise length.',2);
    return false;
  }
  if(select_departing_port == "" && vac_type == "cruise_package") {
    inlineMsg('select_departing_port','<strong>Error</strong><br />You must select a cruise port.',2);
    return false;
  }  
  if(destination != "" && select_hotel == "" && vac_type == "vacation_package") {
	switch (destination_text) {
	    case 'mexico':
			inlineMsg('select_mexico_hotel','<strong>Error</strong><br />You must select a hotel.',2);
    		return false;
		break;
		case 'caribbean':
			inlineMsg('select_caribbean_hotel','<strong>Error</strong><br />You must select a hotel.',2);
    		return false;
		break;
		case 'hawaii':
			inlineMsg('select_hawaii_hotel','<strong>Error</strong><br />You must select a hotel.',2);
    		return false;
		break;
		case 'europe':
			inlineMsg('select_europe_hotel','<strong>Error</strong><br />You must select a hotel.',2);
    		return false;
		break;
		default: 
			return true;		
	}
  }
  
  if(destination_details.match(messageRegex)) {
    inlineMsg('destination_details','You have entered an invalid message.');
    return false;
  }
  if(depart_date == "") {
    inlineMsg('depart_calendar','<strong>Error</strong><br />You must enter a departure date.',2);
    return false;
  }
   if(return_date == "") {
    inlineMsg('return_calendar','<strong>Error</strong><br />You must enter a return date.',2);
    return false;
  }
  if(return_date < depart_date) {
    inlineMsg('return_calendar','<strong>Error</strong><br />You must enter a date that returns after your departure.',2);
    return false;
  }  
  if(other_option == true) {
	  if (other_text == "" || other_text == "- Additional Option -"){
		inlineMsg('other_option','<strong>Error</strong><br />You must enter an additional option.',2);
		return false;
	  }
  }  
  if(!terms_of_use) {
    inlineMsg('terms_of_use','<strong>Error</strong><br />You must agree with the terms of use to continue.',2);
    return false;
  }
  if(image_verification == ""){
	inlineMsg('image_verification','<strong>Error</strong><br />You must enter security code.',2);
    return false;
  }
  
  return true
}


function captcha()
{
	image_verification = document.form.image_verification.value;
	document.form.sub.disabled = true;
	if(image_verification.length == 6)
	{
		if(image_verification != "")
		{
			//var xmlhttp =  new XMLHttpRequest();
			
			var xmlhttp = null; 
            if (window.XMLHttpRequest) 
				{ xmlhttp=new XMLHttpRequest(); 
				} 
			else 
				{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
				} 
			if (xmlhttp==null) 
				{ alert("Your browser doesn't support AJAX."); 
				  return false 
				}
			
			//xmlhttp.open('POST', 'rating-submit.php', true);
			xmlhttp.open("POST","check_captcha.php?val="+image_verification,true);
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4) {
					if(xmlhttp.responseText == true)
					{
						document.form.sub.disabled = false;
					}
					else
					{
						document.form.image_verification.value = "";
						inlineMsg('image_verification','<strong>Error</strong><br />You must enter security code.',2);
					}
				}
			}
			xmlhttp.send(null);
		}
	}	
}

// START OF MESSAGE SCRIPT //

var MSGTIMER = 20;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 3;

function Sidedown(bDisabled)
  {
   var sel = document.getElementById ('destination');
   var city = sel[sel.selectedIndex].text.toLowerCase();  
            /*document.getElementById ('selectval').value = 
			document.getElementById ('destination_details').value =
	     	sel[sel.selectedIndex].text;*/

  switch (city) {
	case 'mexico':
			if (!bDisabled)
				  {
					new Effect.BlindDown('mexico'); 
					new Effect.BlindUp('caribbean'); 
					new Effect.BlindUp('hawaii'); 
					new Effect.BlindUp('europe'); 
					return true;
				  }
		break;
		case 'caribbean':
			if (!bDisabled)
				  {
					new Effect.BlindDown('caribbean'); 
					new Effect.BlindUp('mexico'); 
					new Effect.BlindUp('hawaii'); 
					new Effect.BlindUp('europe'); 
					return true;
				  }
		break;
		case 'hawaii':
			if (!bDisabled)
				  {
					new Effect.BlindDown('hawaii'); 
					new Effect.BlindUp('mexico');
					new Effect.BlindUp('caribbean');
					new Effect.BlindUp('europe'); 
					return true;
				  }
		break;
		case 'europe':
			if (!bDisabled)
				  {
					new Effect.BlindDown('europe'); 
					new Effect.BlindUp('mexico');
					new Effect.BlindUp('caribbean');
					new Effect.BlindUp('hawaii'); 
					return true;
				  }
		break;
		default: 
					new Effect.BlindUp('mexico');
					new Effect.BlindUp('caribbean');
					new Effect.BlindUp('hawaii'); 
					new Effect.BlindUp('europe');
		
	}
}

function Sidedown2(bDisabled)
  {
    new Effect.BlindDown('mexico_cruise');
}
function Sidedown3(bDisabled)
  {
    new Effect.BlindDown('departing_port'); 
}

function SideDest()
  {
   if(document.form.vac_package.checked || document.form.hotel_only.checked ){
	   var vac_type = "vacation_package";
	   new Effect.BlindDown('dest_vacation'); 
	   new Effect.BlindUp('dest_cruise');
	   new Effect.BlindUp('mexico_cruise'); 
 	   new Effect.BlindUp('departing_port');
	   Sidedown();
		  }else if(document.form.cruise_only.checked || document.form.cruise_package.checked){
			  var vac_type = "cruise_package";
			  new Effect.BlindDown('dest_cruise');
		      new Effect.BlindUp('dest_vacation'); 
			  new Effect.BlindUp('mexico');
		  	  new Effect.BlindUp('caribbean');
			  new Effect.BlindUp('hawaii'); 
			  new Effect.BlindUp('europe');
			  }
			
}
function SideAppear()
  {
  new Effect.BlindDown('phone_number','appear'); return false;
}
function SideAppear2()
  {
  new Effect.BlindUp('phone_number','appear'); return false;
}
function OptionAppear()
  {
  new Effect.toggle('other_option','appear'); return false;
}
function SideDown()
  {
  new Effect.BlindDown('airportpreferred','appear'); return false;
}
function SideUp()
  {
  new Effect.BlindUp('airportpreferred','appear'); return false;
}
// 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"; 
}