
// ***** Popup Windows ************************************************************************************** //

/* START JS Browser detect code */
var bow="n";
var bow1="n";

bName = navigator.appName;
bVer = parseInt(navigator.appVersion);

if (bName == "Netscape" && bVer >= 3)
{
	bow = "ok";
	bow1 = "ok";
}
else if (bName == "Microsoft Internet Explorer" && bVer > 3)
{
	bow = "ok";
	bow1="ok";
}
else if (bName == "Microsoft Internet Explorer" && bVer >=2)
{
	bow = "ok";
}

var IS_AOL=0;

if (navigator.userAgent)
{
	if (navigator.userAgent.indexOf("AOL") >=0)
	{
		IS_AOL=1;
		bow1="n";
	}
}
else
{
	bow="n"
	bow1="n";
}

/* Detect iphones and ipads */
if ( (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) ) {
  checkIphoneCookie();
}
/*  no support for Ipad so commenting out for now
else if (navigator.userAgent.match(/iPad/i)) {
}*/
/* END JS Browser detect code */

/* create cookie 'iphoneChecked', valid for 1 week */
function setIphoneCookie() {
  var exdate = new Date();
  exdate.setDate(exdate.getDate()+7);
  document.cookie = "iphoneChecked=true; expires="+exdate.toUTCString()+"; path=/;";
}

/* check if 'iphoneChecked' cookie exists */
function checkIphoneCookie() {
  var iphonechecked = getIphoneCookie('iphoneChecked');
  if (iphonechecked == null || iphonechecked == "") {
    setIphoneCookie();
    var entryURL = window.location.toString();
    window.location = '/iphone-landing.cfm?entryURL=' + encodeURIComponent(entryURL);
  }
}

function getIphoneCookie() {
  if (document.cookie.length > 0) {
    var c_start = document.cookie.indexOf("iphoneChecked=");
    if (c_start != -1) {
      c_start = c_start + 14;
      var c_end=document.cookie.indexOf(";",c_start);
      if (c_end==-1) c_end = document.cookie.length;
      return unescape(document.cookie.substring(c_start,c_end));
    }
  }
  return "";
}



/* Popup Sized Window */
function openWinSized(f,t,w,h){
	return openWindow(f, t, 'no', 'no', 'yes', 'no', w, h, 'no', 'no', 'no');
}

/* general utility function that accepts parameters for url of filename (f),
target Window (t), 
menubar (m - yes/no), location (l - yes/no), 
scrollbars (s - yes/no), resizeable (r - yes/no), 
window width (w), window height (h). 
Default values are laid out in the function.
*/
function openWindow(f, t, m, l, s, r, w, h, st, tool, d)
{
	var fn_filename = "";
	var fn_target = "_blank";
	var fn_menubar = "yes";
	var fn_location = "yes";
	var fn_scrollbars = "yes";
	var fn_resizable = "yes";
	var fn_width = "600";
	var fn_height = "400";
	var fn_status = "yes";
	var fn_toolbar = "yes";
	var fn_directories = "yes";
	
	if (arguments.length > 0)
	{
		if (f != "" || f != null) fn_filename = f;
		if (t != "" || t != null) fn_target = t;
		if (m == "no") fn_menubar = m;
		if (l == "no") fn_location = l;
		if (s == "no") fn_scrollbars = s;
		if (r == "no") fn_resizable = r;
		if (w > 0) fn_width = w;
		if (h > 0) fn_height = h;
		if (st == "no") fn_status = st;
		if (tool == "no") fn_toolbar = tool;
		if (d == "no") fn_directories = d;
	}
	
	self.name="MainWin";
	
  if (bow!="ok") return (true);
	
	argumentString = 'menubar='+fn_menubar+',location='+fn_location+',scrollbars='+fn_scrollbars+',resizable='+fn_resizable+',width='+fn_width+',height='+fn_height+',status='+fn_status+',toolbar='+fn_toolbar+',directories='+fn_directories;
	
	remote =  window.open(fn_filename,fn_target,argumentString);

    if (remote == null) return true;

    if (bow1 =="ok") window.setTimeout('remote.focus()',1000);
	return (false);
}


// DestInfo form navigation
function goDestInfo() {
  
  if (document.destInfoNavigation.destInfoURL.value != '') {
     window.location = document.destInfoNavigation.destInfoURL.value;
  } else {
    return false;
  }
	
}


// ***** TRACKING FUNCTIONS ************************************************************************************** //

// function to track SiteCatalyst Lead links
function trackSCLead(productID, leadType, leadEvent, brochureName) {
  var s=s_gi(s_account);
  
  if (productID != '') {
    s.linkTrackVars="events,eVar17,products";
  } else {
    s.linkTrackVars="events,eVar17,eVar18";
  }
  
  s.products = ";" + productID;
  s.eVar18=brochureName;
  s.eVar17=leadType;
  
  if (leadEvent == 0) {
    s.events="event9";
    s.linkTrackEvents="event9";
  } else {
    s.events="event10";
    s.linkTrackEvents="event10";
  }
  
  s.tl(this,'o','Lead');
  
}

// function to track SiteCatalyst Lead links
function trackSCInternalLead(productID, leadType) {
  var s=s_gi(s_account);
  s.products = ";" + productID;
  s.eVar16=leadType;
  s.events="event16";
  s.linkTrackEvents="event16";
  s.linkTrackVars="events,eVar16,products";
  s.tl(this,'o','Lead');
}

function trackSCDownload(downloadName) {
  var s=s_gi(s_account);
  s.linkTrackVars='eVar18,events';
  s.linkTrackEvents='event11';
  s.events='event11';
  s.eVar18=downloadName;
  s.tl(this,'d',downloadName);
}

function trackSCEvent(eventID) {
  var s=s_gi(s_account);
  s.events=eventID
  s.linkTrackVars="events"
  s.linkTrackEvents=eventID
  s.tl(true,'o','Login');
}

function trackSCPartnerRefer(partnerName) {
  var s=s_gi(s_account);
  s.events="event13"
  s.eVar20=partnerName;
  s.linkTrackVars="eVar20,events"
  s.linkTrackEvents="event13"
  s.tl(true,'o','Partner Referral');
}


function trackSCPromotionSearch(dealFilter) {
  if (dealFilter.dealCampaign.selectedIndex > 0) {
    selectedPromoText = dealFilter.dealCampaign[dealFilter.dealCampaign.selectedIndex].text;
    if (selectedPromoText != '') {
      var s=s_gi(s_account);
      s.eVar19 = selectedPromoText + " - search";
      s.linkTrackVars="eVar19"
      s.tl(true,'o','Holiday Deals Promotion');
    }
  }
}


// ***** EYE MAG ************************************************************************************** //

function openEyemag(projectnr, siteid, pagenumber) {

  var fileurl = 'http://www.eyemag.se/core/main.php?PROJECTNR=' + projectnr + '&SITEID=' + siteid + '&OPENPAGE=' + pagenumber;
  
  if (document.all) 
    var xMax = screen.width, yMax = screen.height-16; 
  else { 
    if (document.layers) 
      var xMax = window.outerWidth, yMax = window.outerHeight; 
    else var xMax = 1000, yMax=800; 
  }
  
  var w = xMax-10;
  var h = yMax-12;
  var pop = window.open(fileurl,'Eyemag','width='+w+',height='+h+', screenX=0,screenY=0,top=0,left=0,menubar=0,toolbar=0,status=no,resizable=no');
  
} 


// ***** TRAVEL PACK FUNCTIONS ************************************************************************************** //

function closeTPDialog(linkID) {
  $('#travelPackDialogDiv'+linkID).dialog('close');
  $('#confirmationView-'+linkID).hide();
  $('#formView-'+linkID).show();
}

function writeTPStatus(loggedIn, itemCount, bookmarkLink, bodyClass) {
  
  document.write('<div id="travelPack">');
    
		document.write('<a href="/travel-pack/index.cfm"><img src="/fms/graphics/themes/common/title-my-queensland-holiday.png" border="0" alt="My Queensland Holiday" width="157" height="11" /></a><br/>');
    
		if (loggedIn == 0) {
      document.write('<a href="/travel-pack/index.cfm?tpAction=signin">Sign in</a> or <a href="/travel-pack/index.cfm?tpAction=register">Register</a> -');
    }
    
		document.write(' <a href="/travel-pack/index.cfm"><span class="tpItemCountValue" id="travelPackItemCount" attr="' + itemCount + '">' + itemCount + ' item');
    if (itemCount != 1) {
      document.write('s');
    }
    document.write(' collected</span></a>');
    
  document.write('</div>');
}

function writeTpBookmarkLink(bookMarkLink, bodyClass, productLabel, linkID) {
  document.write('<a href="' + bookMarkLink + '" target="tpWindow" onclick="return openWinSized(this.href, this.target, 800, 600);" class="actionButtons" title="' + productLabel + ' - Add to My Queensland Holiday"><img src="/fms/graphics/themes/common/button-labels/add-qld-holiday.png" alt="' + productLabel + ' - Add to My Queensland Holiday" border="0" class="button btnLong" /></a>');
}

function writeTpBookmarkLinkProduct(bookMarkLink, bodyClass, productLabel, linkID) {
  document.write('<a href="' + bookMarkLink + '" target="tpWindow" onclick="return openWinSized(this.href, this.target, 800, 600);" title="' + productLabel + ' - Add to My Queensland Holiday"><img src="/fms/graphics/themes/common/button-labels/product-add-qld-holiday.png" alt="' + productLabel + ' - Add to My Queensland Holiday" border="0" class="button btnProduct" /></a>');
}


function validateTPRegister(register) {
  
  var formValid = true;
  
  var registerFormFields = $([])
   .add( $("#firstname") )
   .add( $("#surname") )
   .add( $("#country") )
   .add( $("#address_state") )
   .add( $("#email") )
   .add( $("#password") )
   .add( $("#subscribe") );
   
  registerFormFields.removeClass('ui-state-highlight');
  $(".formFieldHint").text("").removeClass('ui-state-highlight');
  
  // required: all
  if ( $("#firstname").val() == "") {
    //$("#firstname").addClass('ui-state-highlight');
    $("#firstname_hint").text("required").addClass('ui-state-highlight');
    formValid = false;
  }
  if ( $("#surname").val() == "") {
    //$("#surname").addClass('ui-state-highlight');
    $("#surname_hint").text("required").addClass('ui-state-highlight');
    formValid = false;
  }
  
  if ( $("#country").val() == "Australia" && $("#address_state").val() == "") {
    //$("#address_state").addClass('ui-state-highlight');
    $("#address_state_hint").text("required").addClass('ui-state-highlight');
    formValid = false;
  }
  if ($("#country").val() == "") {
    //$("#country").addClass('ui-state-highlight');
    $("#country_hint").text("required").addClass('ui-state-highlight');
    formValid = false;
  }
  
  // validate the email address
  if ( $("#email").val() != "" && !(validateEmail($("#email").val())) ) {
    //$("#email").addClass('ui-state-highlight');
    $("#email_hint").text("required").addClass('ui-state-highlight');
    formValid = false;
  }
  if ($("#password").val() == "") {
    //$("#password").addClass('ui-state-highlight');
    $("#password_hint").text("required").addClass('ui-state-highlight');
    formValid = false;
  }
  
  return formValid;
}

function changeTPCountry(stateObj) {
  var country = document.getElementById('country');
  
  if (stateObj.value != ""){
    country.selectedIndex = 1;
  } else {
    country.selectedIndex = 0;
  }
  
}

function checkTPCountry(countryObj) {
  var address_state = document.getElementById('address_state');
  
  if (countryObj.value == "Australia"){
    address_state.disabled = false;
  } else {
    address_state.selectedIndex = 0;
    address_state.disabled = true;
  }
  
}



function validateTPSignIn(signin) {
  if (signin.useremail.value == "") {
    alert('Please enter your email');  
    signin.useremail.focus();
    return false;
  }
  if (signin.password.value == "") {
    alert('Please enter your password');  
    signin.password.focus();
    return false;
  }
  return true;
}


function validateTPPasswordReminder(password) {
  if (password.useremail.value == "") {
    alert('Please enter your email');  
    password.useremail.focus();
    return false;
  }
  return true;
}


function getTPSelectedItems(tpForm) {
  
  var itemUUIDList = '';
  var itemUUIDElements = document.forms[tpForm];
  
	for (var i=0; i < itemUUIDElements.elements.length; i++) 	{
    if (itemUUIDElements.elements[i].checked) {
      itemUUIDList = itemUUIDList + itemUUIDElements.elements[i].value + ',';
    }
	}
	
  return itemUUIDList;
  
}

function getTPAllItems(tpForm) {
  
  var itemUUIDList = '';
  var itemUUIDElements = document.forms[tpForm];
  
	for (var i=0; i < itemUUIDElements.elements.length; i++) 	{
    itemUUIDList = itemUUIDList + itemUUIDElements.elements[i].value + ',';
	}
	
  return itemUUIDList;
  
}


function tpSelectAll(tpForm) {
  var itemUUIDElements = document.forms[tpForm];
	for (var i=0; i < itemUUIDElements.elements.length; i++) 	{
    itemUUIDElements.elements[i].checked = true;
	}
}
function tpSelectNone(tpForm) {
  var itemUUIDElements = document.forms[tpForm];
	for (var i=0; i < itemUUIDElements.elements.length; i++) 	{
    itemUUIDElements.elements[i].checked = false;
	}
}


function tpMoveItems(itemUUID, folders) {
  
  if (itemUUID == '') {
    alert("Please select the items you'd like to move.");
    
  } else {
    for (i=0; i<folders.length; i++) {
      if (folders[i].selected) {
        folderUUID = folders[i].value;
      }
    }
    window.location = '/travel-pack/index.cfm?tpAction=itemMove&moveToFolderUUID=' + folderUUID + '&itemUUIDList=' + itemUUID +'&folderUUID=' + folderUUID;
    
  }
}

function tpMapItems(tpForm) {
  var itemUUIDList = getTPSelectedItems(tpForm);
  if (itemUUIDList == '') {
    var itemUUIDList = getTPAllItems(tpForm);
  }
  openWinSized('/travel-pack/travel-pack-window.cfm?tpAction=map&bodyClass=blue&itemUUIDList=' + itemUUIDList, this.target, 584, 600);
  
}

function tpDeleteItems(tpForm) {
  var itemUUIDList = getTPSelectedItems(tpForm);
  if (itemUUIDList == '') {
    alert("Please select the items you'd like to delete.");
  } else {
    window.location = '/travel-pack/index.cfm?tpAction=itemDelete&itemUUIDList=' + itemUUIDList;
  }
}

function tpEmailItems(tpForm) {
  var itemUUIDList = getTPSelectedItems(tpForm);
  if (itemUUIDList == '') {
    var itemUUIDList = getTPAllItems(tpForm);
  }
  openWinSized('/travel-pack/travel-pack-window.cfm?tpAction=email&itemUUIDList=' + itemUUIDList, this.target, 492, 600);
}

function tpPrintItems(tpForm) {
  var itemUUIDList = getTPSelectedItems(tpForm);
  if (itemUUIDList == '') {
    var itemUUIDList = getTPAllItems(tpForm);
  }
  openWinSized('/travel-pack/travel-pack-window.cfm?tpAction=print&bodyClass=white&itemUUIDList=' + itemUUIDList, this.target, 585, 600);
}

function closeTPWindow() {
  //var openedFromURL = window.opener.location;
  //window.opener.location = openedFromURL;
  window.opener.location.reload();
  setTimeout("window.close()", 3000);
}


function validateTPEmail(send) {
	
  var formValid = true;
  
  var emailFormFields = $([])
   .add( $("#friendName") )
   .add( $("#friendEmail") )
   .add( $("#ownName") )
   .add( $("#ownEmail") )
   .add( $("#subject") );
   
  emailFormFields.removeClass('ui-state-highlight');
  $(".formFieldHint").text("").removeClass('ui-state-highlight');
  
  if ( $("#friendName").val() == "") {
    $("#friendName_hint").text("Please enter your friend's name").addClass('ui-state-highlight');
    formValid = false;
  }
  if ( $("#friendEmail").val() == "") {
    $("#friendEmail_hint").text("Please enter your friend's email address").addClass('ui-state-highlight');
    formValid = false;
  }
  if ( $("#ownName").val() == "") {
    $("#ownName_hint").text("Please enter your own name").addClass('ui-state-highlight');
    formValid = false;
  }
  if ( $("#ownEmail").val() == "") {
    $("#ownEmail_hint").text("Please enter your own email address").addClass('ui-state-highlight');
    formValid = false;
  }
  if ( $("#subject").val() == "") {
    $("#subject_hint").text("Please enter a subject").addClass('ui-state-highlight');
    formValid = false;
  }
  
  if (formValid) {
    //submitTPEmail();
    return true;
  } else {
    alert("Please check that you have completed all equired fields marked with *.");
  }
	
	return false;
  
}


function validateAddFolder() {
	
  var formValid = true;
  
  var folderFormFields = $([])
   .add( $("#new_folder_name") );
   
  folderFormFields.removeClass('ui-state-highlight');
  $(".formFieldHint").text("").removeClass('ui-state-highlight');
  
  // required: folder name
  if ( $("#new_folder_name").val() == "") {
    $("#new_folder_name_hint").text("Please enter a name for your new folder").addClass('ui-state-highlight');
    formValid = false;
  }
  
  if (formValid) {
    //submitTPFolder('add');
    return true;
  } else {
    alert("Please check that you have completed all equired fields marked with *.");
  }
	
	return false;
}


function validateRemoveFolder() {
	
  var formValid = true;
  
  $(".remove_folder_hint").text("").removeClass('ui-state-highlight');
  
  // required: folder name
  if ( $('input[name=removeFolderUUIDs]:checked').length == 0 ) {
    $("#remove_folder_hint").text("Please click in the checkbox next to at least one folder you wish to remove").addClass('ui-state-highlight');
    formValid = false;
  }
  
  if (formValid) {
    //submitTPFolder('remove');
    return true;
  } else {
    alert("Please check that you have completed all equired fields marked with *.");
  }
	
	return false;
}


function validateNote(itemUUID) {
	
  var formValid = true;
  
  $(".note_hint").text("").removeClass('ui-state-highlight');
  
  // required: folder name
  if ( $("#note").val == "" ) {
    $("#val_hint").text("Please enter the note you would like to add").addClass('ui-state-highlight');
    formValid = false;
  }
  
  if (formValid) {
    //submitTPNote(itemUUID);
    return true;
  } else {
    alert("Please check that you have completed all equired fields marked with *.");
  }
	
	return false;
}



// ***** VALIDATION FUNCTIONS ************************************************************************************** //

// function to validate email addresses
function validateEmail(theEmail) {
  
  var regExp = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i;
  
  if ( !( regExp.test( theEmail ) ) ) {
    return false;
  } else {
    return true;
  }
  
}


// function to clear the keyword field on search forms
function clearKeywordValue(keywordField)  {
  if (keywordField.value == "Enter a Keyword/s" || keywordField.value == "Search using keywords") {
    keywordField.value = "";
  }
}

// function to check the keyword search before submitting
function checkKeywordValue(searchForm)  {
  if (searchForm.keyword.value == "Enter a Keyword/s" || searchForm.keyword.value == "Search using keywords" || searchForm.keyword.value == "") {
    alert("Please enter a keyword to search for.");
    return false;
  }
}


function closeDialog(objectUUID) {
  $('.dialogWindow'+objectUUID).dialog('close');
}


function validateCompetitionEntry(extraJSValidateFields) {
  
  var formValid = true;
  var strFields = new String('');
  var arExtraFields = new Array();
  
  if (extraJSValidateFields != "") {
    strFields = new String(extraJSValidateFields);
    arExtraFields = strFields.split(",");
  }
  
  
  $(".formFieldHint").text("").removeClass('ui-state-highlight');
  
  // required
  if ( $("#title").val() == "") {
    $("#title_hint").text("required").addClass('ui-state-highlight');
    formValid = false;
  }
  if ( $("#firstName").val() == "") {
    $("#firstName_hint").text("required").addClass('ui-state-highlight');
    formValid = false;
  }
  if ( $("#surname").val() == "") {
    $("#surname_hint").text("required").addClass('ui-state-highlight');
    formValid = false;
  }
  if ( $("#email").val() == "") {
    $("#email_hint").text("required").addClass('ui-state-highlight');
    formValid = false;
  }
  if ( $("#confirm_email").val() == "") {
    $("#confirm_email_hint").text("required").addClass('ui-state-highlight');
    formValid = false;
  }
  if ( $("#address_state").val() == "") {
    $("#address_state_hint").text("required").addClass('ui-state-highlight');
    formValid = false;
  }
  
  if (arExtraFields.length > 0) {
    for (i=0; i<arExtraFields.length; i++) {
      
      if ($("#" + arExtraFields[i] ).is("input[type='checkbox']")) {
        if ( $("#"+arExtraFields[i]+":checked").length == 0 ) {
          $("#"+arExtraFields[i]+"_hint").text("required").addClass('ui-state-highlight');
          formValid = false;
        }
      } else {
        if ( $("#" + arExtraFields[i] ).val() == "") {
          $("#"+arExtraFields[i]+"_hint").text("required").addClass('ui-state-highlight');
          formValid = false;
        }
      }
    }
  }
  
  
  if (formValid) {
    return true;
  } else {
    alert("Please check that you have completed all required fields marked with *.");
  }
  
  return false;
  
}

function validateCompetitionRefer(requiredFriendsNumber) {
  
  var formValid = true;
  $(".formFieldHint").text("").removeClass('ui-state-highlight');
  
  if (requiredFriendsNumber > 0) {
    for (i=1; i<=requiredFriendsNumber; i++) {
      if ( $("#friendName" + i).val() == "") {
        $("#friendName" + i + "_hint").text("required").addClass('ui-state-highlight');
        formValid = false;
      }
      if ( $("#friendEmail" + i).val() == "") {
        $("#friendEmail" + i + "_hint").text("required").addClass('ui-state-highlight');
        formValid = false;
      } else if ( $("#friendEmail" + i).val() != "" && !(validateEmail($("#friendEmail" + i).val())) ) {
        $("#friendEmail" + i + "_hint").text("required").addClass('ui-state-highlight');
        formValid = false;
      }
    }
  }
  
  if (formValid) {
    return true;
  } else {
    alert("Please check that you have completed all required fields marked with *.");
  }
  
  return false;
  
}

/* AJAX VERSION
function validateBrochureOrder(brochureUUID) {
  
  var formValid = true;
  
  var brochureFormFields = $([])
   .add( $("#title-"+brochureUUID) )
   .add( $("#first_name-"+brochureUUID) )
   .add( $("#surname-"+brochureUUID) )
   .add( $("#postal_address-"+brochureUUID) )
   .add( $("#postal_suburb-"+brochureUUID) )
   .add( $("#postal_state-"+brochureUUID) )
   .add( $("#postal_postcode-"+brochureUUID) )
   .add( $("#subsribe-"+brochureUUID) )
   .add( $("#email-"+brochureUUID) );
  
  brochureFormFields.removeClass('ui-state-highlight');
  $(".formFieldHint").text("").removeClass('ui-state-highlight');
  
  // required: first name, surname, (email or phone), enquiry, captcha
  if ( $("#first_name-"+brochureUUID).val() == "") {
    $("#first_name_hint-"+brochureUUID).text("required").addClass('ui-state-highlight');
    formValid = false;
  }
  if ( $("#surname-"+brochureUUID).val() == "") {
    $("#surname_hint-"+brochureUUID).text("required").addClass('ui-state-highlight');
    formValid = false;
  }
  
  if ( $("#postal_address-"+brochureUUID).val() == "" ) {
    $("#postal_address_hint-"+brochureUUID).text("required").addClass('ui-state-highlight');
    formValid = false;
  }
  if ( $("#postal_suburb-"+brochureUUID).val() == "" ) {
    $("#postal_suburb_hint-"+brochureUUID).text("required").addClass('ui-state-highlight');
    formValid = false;
  }
  if ( $("#postal_state-"+brochureUUID).val() == "" ) {
    $("#postal_state_hint-"+brochureUUID).text("required").addClass('ui-state-highlight');
    formValid = false;
  }
  if ( $("#postal_postcode-"+brochureUUID).val() == "" ) {
    $("#postal_postcode_hint-"+brochureUUID).text("required").addClass('ui-state-highlight');
    formValid = false;
  }
  
  // email required if subscribe option ticked
  if ( $("#email-"+brochureUUID).val() == "" && $("#subscribe-"+brochureUUID+":checked").length > 0 ) {
    $("#email_hint-"+brochureUUID).text("required").addClass('ui-state-highlight');
    formValid = false;
  } 
  // validate the email address
  else if ( $("#email-"+brochureUUID).val() != "" && !(validateEmail($("#email-"+brochureUUID).val())) ) {
    $("#email_hint-"+brochureUUID).text("required").addClass('ui-state-highlight');
    formValid = false;
  }
  
  if (formValid) {
    submitBrochureOrder(brochureUUID);
  } else {
    alert("Please check that you have completed all required fields marked with *.");
  }
  
  return false;
  
}
*/
function validateBrochureOrder() {
  
  var formValid = true;
  
  var brochureFormFields = $([])
   .add( $("#title") )
   .add( $("#first_name") )
   .add( $("#surname") )
   .add( $("#postal_address") )
   .add( $("#postal_suburb") )
   .add( $("#postal_state") )
   .add( $("#postal_postcode") )
   .add( $("#subsribe") )
   .add( $("#email") );
  
  brochureFormFields.removeClass('ui-state-highlight');
  $(".formFieldHint").text("").removeClass('ui-state-highlight');
  
  // required: first name, surname, (email or phone), enquiry, captcha
  if ( $("#first_name").val() == "") {
    $("#first_name_hint").text("required").addClass('ui-state-highlight');
    formValid = false;
  }
  if ( $("#surname").val() == "") {
    $("#surname_hint").text("required").addClass('ui-state-highlight');
    formValid = false;
  }
  
  if ( $("#postal_address").val() == "" ) {
    $("#postal_address_hint").text("required").addClass('ui-state-highlight');
    formValid = false;
  }
  if ( $("#postal_suburb").val() == "" ) {
    $("#postal_suburb_hint").text("required").addClass('ui-state-highlight');
    formValid = false;
  }
  if ( $("#postal_state").val() == "" ) {
    $("#postal_state_hint").text("required").addClass('ui-state-highlight');
    formValid = false;
  }
  if ( $("#postal_postcode").val() == "" ) {
    $("#postal_postcode_hint").text("required").addClass('ui-state-highlight');
    formValid = false;
  }
  
  // email required if subscribe option ticked
  if ( $("#email").val() == "" && $("#subscribe-"+":checked").length > 0 ) {
    $("#email_hint").text("required").addClass('ui-state-highlight');
    formValid = false;
  } 
  // validate the email address
  else if ( $("#email").val() != "" && !(validateEmail($("#email").val())) ) {
    $("#email_hint").text("required").addClass('ui-state-highlight');
    formValid = false;
  }
  
  if (formValid) {
    return true;
  } else {
    alert("Please check that you have completed all required fields marked with *.");
  }
  
  return false;
  
}

/*
function validateProductEnquiry() {
  
  var formValid = true;
  var doNextEmailCheck = true;
  
  var productEnquiryFields = $([])
   .add( $("#first_name") )
   .add( $("#surname") )
   .add( $("#email") )
   .add( $("#phone") )
   .add( $("#enquiryDetail") )
   .add( $("#captchaUserInput") )
   .add( $("#subsribe") );
   
  productEnquiryFields.removeClass('ui-state-highlight');
  $(".formFieldHint").text("").removeClass('ui-state-highlight');

  // required: first name, surname, (email or phone), enquiry, captcha
  if ( $("#first_name").val() == "") {
    $("#first_name_hint").text("Please enter your first name").addClass('ui-state-highlight');
    formValid = false;
  }
  if ( $("#surname").val() == "") {
    $("#surname_hint").text("Please enter your surname").addClass('ui-state-highlight');
    formValid = false;
  }
  if ( $("#email").val() == "" && $("#phone").val() == "" ) {
    $("#email_hint").text("Please enter a preferred contact").addClass('ui-state-highlight');
    formValid = false;
    doNextEmailCheck = false;
  }
  if ( $("#enquiryDetail").val() == "" ) {
    $("#enquiry_hint").text("Please enter your enquiry").addClass('ui-state-highlight');
    formValid = false;
  }
  if ( $("#captchaUserInput").val() == "" ) {
    $("#captcha_hint").text("Please enter the security code").addClass('ui-state-highlight');
    formValid = false;
  }
  
  // validate the email address
  if ( $("#email").val() != "" && !(validateEmail($("#email").val())) ) {
    $("#email_hint").text("Please enter a valid email address").addClass('ui-state-highlight');
    formValid = false;
    doNextEmailCheck = false;
  }
  
  // check BCC and subscribe for email only if email is already not valid
  if (doNextEmailCheck) {
    // email required if BCC option ticked
    if ( $("#email").val() == "" &&  $("#emailMe:checked").length > 0   ) {
      $("#email_hint").text("Please enter an email address so we can send you a copy of this enquiry").addClass('ui-state-highlight');
      formValid = false;
    }
    // email required if subscribe option ticked
    if ( $("#email").val() == "" && $("#subscribe:checked").length > 0 ) {
      $("#email_hint").text("Please enter an email address to subscribe to the enewsletter").addClass('ui-state-highlight');
      formValid = false;
    }
  }
  
  if (formValid) {
    submitProductContact();
  } else {
    alert("Please check that you have completed all required fields marked with *.");
  }
  
  return false;
  
}
*/
function validateProductEnquiry() {
  
  var formValid = true;
  var doNextEmailCheck = true;
  
  var productEnquiryFields = $([])
   .add( $("#first_name") )
   .add( $("#surname") )
   .add( $("#email") )
   .add( $("#phone") )
   .add( $("#enquiryDetail") )
   .add( $("#captchaUserInput") )
   .add( $("#subsribe") );
   
  productEnquiryFields.removeClass('ui-state-highlight');
  $(".formFieldHint").text("").removeClass('ui-state-highlight');

  // required: first name, surname, (email or phone), enquiry, captcha
  if ( $("#first_name").val() == "") {
    $("#first_name_hint").text("Please enter your first name").addClass('ui-state-highlight');
    formValid = false;
  }
  if ( $("#surname").val() == "") {
    $("#surname_hint").text("Please enter your surname").addClass('ui-state-highlight');
    formValid = false;
  }
  if ( $("#email").val() == "" && $("#phone").val() == "" ) {
    $("#email_hint").text("Please enter a preferred contact").addClass('ui-state-highlight');
    formValid = false;
    doNextEmailCheck = false;
  }
  if ( $("#enquiryDetail").val() == "" ) {
    $("#enquiry_hint").text("Please enter your enquiry").addClass('ui-state-highlight');
    formValid = false;
  }
  if ( $("#captchaUserInput").val() == "" ) {
    $("#captcha_hint").text("Please enter the security code").addClass('ui-state-highlight');
    formValid = false;
  }
  
  // validate the email address
  if ( $("#email").val() != "" && !(validateEmail($("#email").val())) ) {
    $("#email_hint").text("Please enter a valid email address").addClass('ui-state-highlight');
    formValid = false;
    doNextEmailCheck = false;
  }
  
  // check BCC and subscribe for email only if email is already not valid
  if (doNextEmailCheck) {
    // email required if BCC option ticked
    if ( $("#email").val() == "" &&  $("#emailMe:checked").length > 0   ) {
      $("#email_hint").text("Please enter an email address so we can send you a copy of this enquiry").addClass('ui-state-highlight');
      formValid = false;
    }
    // email required if subscribe option ticked
    if ( $("#email").val() == "" && $("#subscribe:checked").length > 0 ) {
      $("#email_hint").text("Please enter an email address to subscribe to the enewsletter").addClass('ui-state-highlight');
      formValid = false;
    }
  }
  
  if (formValid) {
    return true;
  } else {
    alert("Please check that you have completed all required fields marked with *.");
  }
  
  return false;
  
}


//check that at least one field on a product search form is completed
function validateSearchForm(searchType) {
  
  var formValid = false;
  
  //check the keyword - on all forms
  if ( $("#productSearch input[name='keyword']").val() != "" ) { formValid = true; }
  
  if (searchType == "event" ) {
    if ( $("#productSearch #dateFrom").val() != "" ) { formValid = true; }
    if ( $("#productSearch #dateTo").val() != "" ) { formValid = true; }
  }
  
  //find if any checkboxes are checked
  var allCheckboxInputs = $("#productSearch :checkbox:checked");
  if ( allCheckboxInputs.length > 0  ) {
    formValid = true;
  }
  
  //find if any selects are selected
  var $allSelectInputs = $("#productSearch option:selected");
  $allSelectInputs.each(
    function(index) {
      if ( $allSelectInputs[index].value != "" ) {
        formValid = true;
      }
    }
  );
  
  
  if (!formValid) {
    alert("Please select at least one search option to continue.");
    return false;
  }
  
  return true;
}



// ***** AJAX FUNCTIONS ************************************************************************************** //


// brochure order submit and response functions
function submitBrochureOrder(brochureUUID) {
  var proxy = new ajaxProxy();
  proxy.setCallbackHandler(brochureOrderResponse);
  proxy.setForm('brochureOrderForm'+brochureUUID+'');
  proxy.setHTTPMethod('POST')
  proxy.sendBrochureOrder();
}

function brochureOrderResponse(result) {
  if (result.FORMVALID) {
    $('#brochureFormView'+result.BROCHUREUUID).hide();
    $('#confirmationView'+result.BROCHUREUUID).show();
    trackSCLead('', 'Brochure Order', 1, result.BROCHURELABEL);
  } else { 
    alert("Please check that you have completed all equired fields marked with *.");
  }
}


// product enquiry submit and response functions
function submitProductContact() {
  var proxy = new ajaxProxy();
  proxy.setCallbackHandler(sendEnquiryResponse);
  proxy.setForm('enquiryForm');
  proxy.setHTTPMethod('POST')
  proxy.sendProductEnquiry();
}

function sendEnquiryResponse(result) {
  if (result.FORMVALID) {
    $('#enquiryFormView').hide();
    $('#confirmationView').show();
    trackSCLead(result.PRODUCTATDWID, "Product Enquiry Email", 1);
  } else {
    alert("Please check that you have completed all equired fields marked with *.");
  }
}


// tp add folder submit and response functions
function submitTPFolder(tpAction) {
  var proxy = new ajaxProxy();
  proxy.setCallbackHandler(submitTPFolderResponse);
  if (tpAction == "add") {
    proxy.setForm('tpAddFolderForm');
  } else {
    proxy.setForm('tpRemoveFolderForm');
  }
  proxy.setHTTPMethod('POST')
  proxy.tpFolder();
}

function submitTPFolderResponse(result) {
  
  if (result.FORMVALID) {
    $('#formView').hide();
    $('#confirmationView').show();
    $('#tpFolderList').html(result.HTMLUPDATE.HTMLLIST);
    $('#tpFolderSelect').html(result.HTMLUPDATE.HTMLSELECT);
    
  } else {
    alert("not valid: " + result.INVALIDFIELDS);
    
  }
  
}


// tp email submit and response functions
function submitTPEmail() {
  var proxy = new ajaxProxy();
  proxy.setCallbackHandler(submitTPEmailResponse);
  proxy.setForm('tpEmailForm');
  proxy.setHTTPMethod('POST')
  proxy.tpEmail();
}

function submitTPEmailResponse(result) {
  
  if (result.FORMVALID) {
    $('#formView').hide();
    $('#confirmationView').show();
    
  } else {
    alert("not valid: " + result.INVALIDFIELDS);
    
  }
  
}


// tp note submit and response functions
function submitTPNote(itemUUID) {
  
  var proxy = new ajaxProxy();
  proxy.setCallbackHandler(submitTPNoteResponse);
  proxy.setForm('tpNoteForm-'+itemUUID);
  proxy.setHTTPMethod('POST')
  proxy.tpNote();
}

function submitTPNoteResponse(result) {
  
  if (result.FORMVALID) {
    $('#formView-'+result.ITEMUUID).hide();
    $('#confirmationView-'+result.ITEMUUID).show();
    $('#'+result.ITEMUUID+'-notes').html(result.UPDATEDNOTE);
    
  } else {
    alert("Please enter a note");
    
  }
  
}


// tp note submit and response functions
function submitTPDelete(itemUUID) {
  var proxy = new ajaxProxy();
  proxy.setCallbackHandler(submitTPDeleteResponse);
  proxy.setForm('tpDeleteForm-'+itemUUID);
  proxy.setHTTPMethod('POST')
  proxy.tpDelete();
  return false;
}

function submitTPDeleteResponse(result) {
  
  if (result.FORMVALID) {
    $('#formView-'+result.ITEMUUID).hide();
    $('#confirmationView-'+result.ITEMUUID).show();
    $('#item-'+result.ITEMUUID).remove();
    $('.tpItemCountValue').text(result.ITEMCOUNT);
    
  } else {
    alert("Unable to delete this item");
    
  }
  
}


function changeTabLabelImage(uiIndex, tabListID) {
  
  var $currentListTitle = "";
  var currentBackPos = "";
  var splitPositions = "";
  var currentBackPosX = 0;
  
  var $listItems = $("ul#"+tabListID+" > li");
  
  for (var i=0; i<$listItems.length; i++) {
    
    $currentListTitle = $("ul#"+tabListID+" > li:eq("+i+") > a > div");
    
    //find the current x pos, so it doesn't change
    currentBackPos = $currentListTitle.css('background-position');
    
    if (currentBackPos == 'undefined' || currentBackPos == null) {
        currentBackPosX = $currentListTitle.css('background-position-x'); //IE
    } else {
      splitPositions = currentBackPos.split(" ");
      currentBackPosX = splitPositions[0];
    }
    
    //if this is the tab just clicked
    if (i == uiIndex) {
      $currentListTitle.css("background-position", ""+currentBackPosX+" 33px");
    } else {
      $currentListTitle.css("background-position", ""+currentBackPosX+" 0px");
    }
    
  }
  
}


// ***** DOCUMENT READY FUNCTIONS ************************************************************************************** //
  
var $loading = $('<img src="/fms/graphics/icons/loading.gif" alt="loading" class="loading">');

$(document).ready(function() {
  
  //tabs
  $("#interestingTabs").tabs({ cache: true, selected: 0,
    select: function(event, ui) {
      changeTabLabelImage(ui.index, "tabsInteresting");
    }
  });
  
  $("#socialMediaTabs").tabs({ cache: true });
  
  $("#tabs1").tabs({ cache: true, selected: 0,
    show: function(event, ui) {
      if (ui.panel.id == "tab-mapview") {
        resetMap();
      }
    },
    select: function(event, ui) {
      var listItemID = $("#tabs1 > ul").attr('id');
      changeTabLabelImage(ui.index, listItemID);
    },
    add: function(event, ui) {
      var listItemID = $("#tabs1 > ul").attr('id');
      changeTabLabelImage(ui.index, listItemID);
    }
  });
  
  $("#tabs2").tabs({ cache: true, selected: 0,
    show: function(event, ui) {
      if (ui.panel.id == "tab-mapview") {
        resetMap();
      }
    },
    select: function(event, ui) {
      var listItemID = $("#tabs2 > ul").attr('id');
      changeTabLabelImage(ui.index, listItemID);
    }
  });
  
      
  $('.listViewLink').click(function() { // bind click event to link
    $('#tabs1').tabs('select', 0); // switch to first tab
    return false;
  });
  
  $('.listViewLink').click(function() { // bind click event to link
    $("#tabs2").tabs('select', 0); // switch to first tab
    return false;
  });
  
  $('.servicesLink').click(function() { // link on products to the services tab
    $("#tabs1").tabs('select', 1); // switch to second tab
    return false;
  });
  
  $('.mapviewLink').click(function() { // link on product static maps to Nearby/Around tab
    $("#tabs1").tabs('select', 2); // switch to second tab
    return false;
  });
  $('.mapviewLinkDestInfo').click(function() { // link on DESTINFO product static maps to Nearby/Around tab
    $("#tabs1").tabs('select', 1); // switch to second tab
    return false;
  });
  
  
  $('.productDepotsLink').click(function(event) {
		//prevent the default action for the click event
		event.preventDefault();
    
    $("#tabs1").tabs('select', 0); // switch to first tab

		//get the top offset of the target anchor
		var target_offset = $("#productSites").offset();
		var target_top = target_offset.top;

		//goto that anchor by setting the body scroll top to anchor top
		$('html, body').animate({scrollTop:target_top}, 500);
    
    return false;
  });
  
  $('.productVenuesLink').click(function(event) {
		//prevent the default action for the click event
		event.preventDefault();
    
    $("#tabs1").tabs('select', 1); // switch to second tab

		//get the top offset of the target anchor
		var target_offset = $("#productVenues").offset();
		var target_top = target_offset.top;

		//goto that anchor by setting the body scroll top to anchor top
		$('html, body').animate({scrollTop:target_top}, 500);
    
    return false;
  });
  
  
  $('.routeSegmentLink').click(function(event) {
		//prevent the default action for the click event
		event.preventDefault();
    
    $("#tabs1").tabs('select', 1); // switch to second tab
    
    segmentAnchorName = $(this).attr('href');
    
		//get the top offset of the target anchor
		var target_offset = $('#' + segmentAnchorName).offset();
		var target_top = target_offset.top;
    
		//goto that anchor by setting the body scroll top to anchor top
		$('html, body').animate({scrollTop:target_top}, 100);
    
    return false;
  });
  
  
  $('#productSlideshowImages').cycle({
    delay: 500,
    timeout: 4000,
    speed: 500,
    fit: 0,
    width: 210, 
    height: 158, 
    before: onBefore,
    pause: 1,
    prev: '#productSlideshowPrev',
    next: '#productSlideshowNext'
  });
  function onBefore() {
    $('#productSlideshowCaption').html(this.title);
  }
  
  $('#productSlideshowPause').click(function() {
    
    $('#productSlideshowImages').cycle('toggle');
    
    if ( $('#productSlideshowPauseImg').attr('alt') == 'Pause Photos' ) {      
      $('#productSlideshowPauseImg').attr('alt', 'Play Photos');
      $('#productSlideshowPauseImg').attr('src', '/fms/graphics/buttons/play.png');
    } else {
      $('#productSlideshowPauseImg').attr('alt', 'Pause Photos');
      $('#productSlideshowPauseImg').attr('src', '/fms/graphics/buttons/pause.png');
    }
    
    return false;
  });
  
  
  $('.orderBrochureLink').each(function() {
    
    var $brochureDialog = $('<div id="brochureOrderWindow" class="dialogWindow'+$(this).attr('id')+'"></div>')
      .append($loading.clone());
      
    var $brochure = $(this).one('click', function() {
      $brochureDialog  
        .load( $brochure.attr('href') )
        .dialog({
          title: $brochure.attr('title'),
          width: 600,
          height: 550,
          modal: true,
          
          open: function() {
            $("#brochureOrderForm"+$(this).attr('id')+" #title").val("");
            $("#brochureOrderForm"+$(this).attr('id')+" #first_name").val("");
            $("#brochureOrderForm"+$(this).attr('id')+" #surname").val("");
            $("#brochureOrderForm"+$(this).attr('id')+" #postal_address").val("");
            $("#brochureOrderForm"+$(this).attr('id')+" #postal_suburb").val("");
            $("#brochureOrderForm"+$(this).attr('id')+" #postal_state").val("");
            $("#brochureOrderForm"+$(this).attr('id')+" #postal_postcode").val("");
            $("#brochureOrderForm"+$(this).attr('id')+" #subsribe").val("");
            $("#brochureOrderForm"+$(this).attr('id')+" #email").val("");
          }
          
        })
      ;
      
      $brochure.click(function() {
        $brochureDialog.dialog('open');
        return false;
      });
      
      return false;
      
    }); // end $brochure
    
  }); // end for each brochure links
  
  
  
  $('.doTravelPack').each(function() {
    
    var $travelPackDialog = $('<div id="travelPackDialogDiv'+$(this).attr('id')+'"></div>')
      .append($loading.clone());
      
    var $travelPack = $(this).click(function() {
      $travelPackDialog
        .load( $travelPack.attr('href') )
        .dialog({
          title: $travelPack.attr('title'),
          width: 600,
          height: 500,
          modal: true,
    			show: 'blind',
    			hide: 'blind'
        })
      ;
      
      $travelPack.click(function() {
        $travelPackDialog.dialog('open');
        return false;
      });
    
      return false;
      
    }); // end $travelPack
    
  }); // end for each doTravelPack links
  
  $('.tpRegisterLink').click(function() { // bind click event to link
    $("#tabs1").tabs('select', 0); // switch to second tab
    return false;
  });
  $('.tpSigninLink').click(function() { // bind click event to link
    $("#tabs1").tabs('select', 1); // switch to second tab
    return false;
  });
  $('.tpPasswordLink').click(function() { // bind click event to link
    $("#tabs1").tabs('select', 2); // switch to second tab
    return false;
  });
  
  
    
  // product enquiry dialog window
  $("#contactFormPanel").dialog({
    autoOpen: false,
    height: 700,
    width: 600,
    modal: true
  });
  
  $('.contactProduct').click(function() {
    $('#contactFormPanel').dialog('open');
    return false;
  });
  
  
});

