/***************************************************************************************/
/* Form validation functions */
/***************************************************************************************/
function validateEmailAddress(obj, formObj, formValue) {
	var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	return regex.test(formValue);
}

function validateOptionalEmail(obj, formObj, formValue) {
	if(formValue != '') {
		var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
		return regex.test(formValue);
	} else {
		return true
	}
}

/***************************************************************************************/
/* Change the display of an element */
/***************************************************************************************/
function changeElementDisplay(el)
{
	displayItem = document.getElementById(el).style
	if (displayItem.display == 'none')
		displayItem.display = '';
	else
		displayItem.display = 'none';
}

/***************************************************************************************/
/* Open window function */
/***************************************************************************************/
function openStatsWindow(url)
{
		var newWindow = window.open(url, "", "HEIGHT=338,WIDTH=600,toolbar=no,scrollbars=yes,status=no,menubar=no,resizeable=no");
}

function openAltFlightsWindow(url)
{
		var newWindow = window.open(url, "AltFlightsWindow", "HEIGHT=300,WIDTH=450,toolbar=no,scrollbars=yes,status=no,menubar=no,resizeable=no");
}

/***************************************************************************************/
/* Check destination functions for holidays and flights */
/***************************************************************************************/
function checkHolidayForm(formObj) {
	if (formObj.DestCodes) {
		if (formObj.DestCodes.value == 0) {
			alert('Please select a destination inside this region. Thank you.');
			return false;
		}
	}
	if (formObj.DeptCodes) {
		if (formObj.DeptCodes.value == 0) {
			alert('Please select a departure airport. Thank you.');
			return false;
		}
	}
	if (formObj.cal1input) {
		if (checkYCalDate(1) == false) {
			alert('Please enter date in the format DD/MM/YYYY');
			return false;
		}
	}
	if (formObj.cal2input) {
		if (checkYCalDate(2) == false) {
			alert('Please enter date in the format DD/MM/YYYY');
			return false;
		}
	}
	
	if(!js_checkChildAgesFilled(formObj)) {
		alert('Please enter the ages of all children before continuing');
		return false;
	}
	
	return true;
}

function checkFlightDestination(formObj) {
	if (formObj.frm_destination_point) {
		if (formObj.frm_destination_point.value == '') {
			alert('The route you have selected is not available.\nPlease adjust your route and try again. Thank you.');
			return false;
		}
	}
	if (formObj.frm_departure_point) {
		if (formObj.frm_departure_point.value == '') {
			alert('The route you have selected is not available.\nPlease adjust your route and try again. Thank you.');
			return false;
		}
	}
	return true;
}

/***************************************************************************************/
/* Rollover code */
/***************************************************************************************/
function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

var preloadFlag = false;
function preloadImages() {
	if (document.images) {
//		button_home_over = newImage("/images/nav_home_on.gif");
//		button_airports_over = newImage("/images/nav_airports_on.gif");
//		button_holiday_search_over = newImage("/images/nav_holiday_search_on.gif");
//		button_flight_search_over = newImage("/images/nav_flight_search_on.gif");
//		button_citybreaks_over = newImage("/images/nav_citybreaks_on.gif");
//		button_skiing_over = newImage("/images/nav_skiing_on.gif");
//		button_car_hire_over = newImage("/images/nav_car_hire_on.gif");
//		button_extras_over = newImage("/images/nav_extras_on.gif");
		cal_close = newImage("/images/cal_close.png");
		cal_sprite = newImage("/images/cal_sprite.png");
		tabs_sprite = newImage("/images/sprite_tabs.png");
		sprite_logos = newImage("/images/sprite_logos.png");
		preloadFlag = true;
	}
}

/***************************************************************************************/
/* Rollover code */
/***************************************************************************************/
function imageWindow(path, imgWidth, imgHeight) {
	var newWindow = window.open('/displayImage.cfm?imageURL='+path, 'imageWindow', 'HEIGHT='+imgHeight+',WIDTH='+imgWidth+',toolbar=no,scrollbar=no,status=no,menubar=no,resizeable=no');
}

/***************************************************************************************/
/* Used in the CMS */
/* Macromedia Dreamweaver Rollover code */
/***************************************************************************************/
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


// Function to select a value in the select list
function js_selectValue(str_formID, str_changeToValue, bol_CallOnChange) {
	var obj_SelectList = document.getElementById(str_formID);
	var num_SelectListItems = obj_SelectList.childNodes.length;
	if (num_SelectListItems != 0) {
		for (i=0; i<num_SelectListItems; i++) {
			if (obj_SelectList.childNodes[i].value == str_changeToValue) {
				obj_SelectList.childNodes[i].selected = true;
				if (bol_CallOnChange)
					obj_SelectList.onchange();
				return true;
			}
		}
	}
}

function js_updateNightsRadio(num_Nights) {
	var obj_N7 = document.getElementById('nightsRadio7');
	var obj_N14 = document.getElementById('nightsRadio14');
	// Clear both checked
	obj_N7.checked = '';
	obj_N14.checked = '';
	// If 7 or 14 check the appropriate one
	if (num_Nights == 7)
		obj_N7.checked = 'checked';
	else if (num_Nights == 14)
		obj_N14.checked = 'checked';	
}

function showMoreOptions(dir) {
	if (dir == 1) {
		if ($('hs_MinStarRating')) $('hs_MinStarRating').style.display = 'block';
		if ($('hs_BoardBases')) $('hs_BoardBases').style.display = 'block';
		if ($('hs_adv_opts')) $('hs_adv_opts').style.display = 'block';
		$('lnk_MO').innerHTML = 'Less options';		
		$('lnk_MO').href = 'javascript:showMoreOptions(0);'		
	} else {
		if ($('hs_MinStarRating')) $('hs_MinStarRating').style.display = 'none';
		if ($('hs_BoardBases')) $('hs_BoardBases').style.display = 'none';
		if ($('hs_adv_opts')) $('hs_adv_opts').style.display = 'none';
		$('ff_SR').selectedIndex = 0;
		$('ff_BB').selectedIndex = 0;
		$('lnk_MO').innerHTML = 'More options';
		$('lnk_MO').href = 'javascript:showMoreOptions(1);'		
	}
}

function js_OnlineClosed(open_time,closing_time) {
	
	alert('Please try again at ' + open_time + '. Our supplier\'s system is closed at present and will not be available until then.');
}

/* New behaviour - remove the old onload and wrap in a document observe */
function cycleUntilReady (callback) {
	document.observe('dom:loaded',callback);
}
