/***************************************************************************************/
/* 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);
}


/***************************************************************************************/
/* Check destination functions for holidays and flights */
/***************************************************************************************/
function checkHolidayForm(formObj) {
	var s_site = '';
	if($('s_site')) s_site = $('s_site').value;
	formObj = Element.extend(formObj);
	switch (s_site) {
		case 'DLF':
			$('frm_holidaySearch').submit();
			return true;
			break;

		case 'DLH': // DLH check
			// searchL1 stuff
			var arrBudgetSearchRadio = formObj.getInputs('radio','budgetSearchType');
			if (arrBudgetSearchRadio.length>0) {
				switch (arrBudgetSearchRadio.find(function(radio){return radio.checked;}).value) {
					case 'range':
						if (
							$('rangeMin').value == ''
							||
							$('rangeMax').value == ''
						) {
							alert('Please enter the minimum and maximum amount you wish to spend.  Thank you.');
							return false;
						}
						break;
					case 'plusMinus':
						if (
							$('amountPlusMinus').value == ''
						) {
							alert('Please enter the amount you wish to spend.  Thank you.');
							return false;
						}
						break;
				}
			}

			if (formObj.DestCodes) {
				if (formObj.DestCodes.value == 0 || formObj.DestCodes.value == "") {
					if ( !(StructKeyExists(formObj, "hotel_name") && formObj.hotel_name.value != "") )
					{
						alert('Please select a holiday destination. Thank you.');
						return false;
					}
				}
			}
			if (formObj.DeptCodes) {
				if (formObj.DeptCodes.value == 0) {
					alert('Please select a departure airport. Thank you.');
					return false;
				}
			}

			if (formObj.daysLater) {
				if (formObj.daysLater.value == '') {
					alert('Please select how flexible your departure date is. Thank you.');
					return false;
				}
			}

			break;

		case 'DLA': // DLA check
			if (formObj.num_DLA_destID) {
				if (formObj.num_DLA_destID.value == 0 || formObj.num_DLA_destID.value == "") {
					alert('Please select a hotel destination. Thank you.');
					return false;
				}
			}
			break;
	}


	try{
		if(!js_checkChildAgesFilled(formObj)) {
			alert('Please enter the ages of all children before continuing');
			return false;
		}
	}catch(e){/*alert(e);*/}

	if (!searchHasSplashed) {
		searchSplash();
		//return true;
		window.setTimeout(function(){ $('frm_holidaySearch').submit(); }, 1000);
		//$('frm_holidaySearch').submit();
	}
	return false;
}

function preloadImages() {
	strCostImageToDisplay = new Element('img', { 'src': strCostImageToDisplay, 'class': 'splashMainImage' });
	strSearchImageToDisplay = new Element('img', { 'src': strSearchImageToDisplay, 'class': 'splashMainImage' });

	objSearchContent = new Element('div');
	objSearchContent.innerHTML = strSearchContent;

	objCostContent = new Element('div');
	objCostContent.innerHTML = strCostContent;
}

// Function to select a value in the select list
function js_selectValue(str_formID, str_changeToValue, bol_CallOnChange) {
	var obj_SelectList = $(str_formID);
	if (obj_SelectList) {
		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 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(timeopens, timecloses) {
	if (timeopens !== undefined)
	{
		alert('We are sorry to advise that the booking system for this particular holiday is now closed and will be open between the hours of ' + timeopens + ' and ' + timecloses);
	}
	else
	{
		alert('Please try again at 8am. Our supplier\'s system is closed at present and will not be available until then.');
	}
}


//change tabs on more details
function changeTab(id){
	var arr_tabs = $$('.tab_on');
	arr_tabs.each(function(s, ind){
		$(s).className = 'tab_off';
	});
	$('tab_md_'+id).className = 'tab_on';
	var arr_cont = $$('.content_on');
	arr_cont.each(function(s, ind){
		$(s).className = 'content_off';
	});
	$('cont_md_'+id).className = 'content_on';
}

function getPageSize(req) {

var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;

	if (self.innerHeight) { // all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth;
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
	pageWidth = xScroll;
	} else {
	pageWidth = windowWidth;
	}

	if(req=='pagesize'){
		return [pageWidth,pageHeight];
	}else{
		return [windowWidth,windowHeight];
	}
}

function StructKeyExists(obj, key)
{
	if  (typeof(obj[key]) == "undefined")
	{
		return false;
	}
	return true;
}

/* Email This */

function emailThis(){
	$('emailForm').style.display='block';
	var arr_pageSize = getPageSize('pagesize');

	$('newLayerBg').style.height = arr_pageSize[1]+'px';
	$('newLayerBg').style.width = arr_pageSize[0]+'px';
	scroll(0,0);
}

function closeEmailThis(){
	hideAnElement('emailForm');
}

function closeEmailSent(){
	hideAnElement('divEmailSent');
}

function hideAnElement(eleID) {
	var objToHide = $(eleID);
	if (objToHide) {
		objToHide.style.display='none';
	}
};

function emailSent(){
	$('divEmailSent').style.display='block';
	var arr_pageSize = getPageSize('pagesize');

	$('newLayerBgSent').style.height = arr_pageSize[1]+'px';
	$('newLayerBgSent').style.width = arr_pageSize[0]+'px';
}

function changeActiveBudgetAreas(searchType) {
	// Loop over all the fields and disable them
	var fieldList = new Array("rangeMin", "rangeMax", "selectRange", "amountPlusMinus", "plusMinusAmount");
	for (var i=0; i<fieldList.length; i++) {
		//alert(fieldList[i]);
		el = document.getElementById(fieldList[i]);
		el.disabled = true;
		if (el.type == 'text')
			el.value = '';
	}
	// Depending on the searchType passed re-enable some fields
	switch (searchType) {
		case 'range':
			document.getElementById('rangeMin').disabled = false;
			document.getElementById('rangeMax').disabled = false;
			break;
		case 'rangeSelect':
			document.getElementById('selectRange').disabled = false;
			break;
		case 'plusMinus':
			document.getElementById('amountPlusMinus').disabled = false;
			document.getElementById('plusMinusAmount').disabled = false;
			break;
	}
}



function preventAutoSettlement(strDepositType)
{
	if (confirm("Are you sure you want to prevent the "+strDepositType+" autosettlement of this reference?"))
		{
			location.href = location.href + "&PrevAutoSettle"+strDepositType+"=true";
		}
	return false;
}

