// JavaScript Document

var http = createRequestObject();

// Browser sniffer
var ns4 = (document.layers); 
var ie4 = (document.all && !document.getElementById);
var ie5 = (document.all && document.getElementById);
var ns6 = (!document.all && document.getElementById);

var arImages = new Array('/images/boxes/dpink_tl.gif','/images/boxes/dpink_tr.gif','/images/boxes/vdpurp_tl.gif','/images/boxes/vdpurp_tr.gif','/images/boxes/dblue_tl.gif','/images/boxes/dblue_tr.gif');
for(i = 0; i < arImages.length;i++) {
	var oImage = new Image()
	oImage.src = arImages[i];
}

function getSelectedRadio(buttonGroup) {
   if (buttonGroup[0]) { 
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; }
   }
   return -1;
}

function getSelectedRadioValue(buttonGroup) {
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { 
         return buttonGroup[i].value;
      } else { 
         return buttonGroup.value;
      }
   }
} 


function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        ro = new XMLHttpRequest();
    }
    return ro;
}

function clearList(OptionList) {
   for (x = OptionList.length; x >= 0; x--) {
      OptionList[x] = null;
   }
}

function sndReqCountry(sContinent, sCountry) {
	lstCountry = document.getElementById("lstcountry");
	clearList(lstCountry);
	lstCountry.options[0] = new Option('Please wait...', '-');
	http.open('get', '/includes/ajax_getCountries.asp?continent='+sContinent+'&country='+sCountry);
	http.onreadystatechange = handleCountry;
	http.send(null);
	
}

function handleCountry() {
	
	lstCountry = document.getElementById("lstcountry");
	clearList(lstCountry);
	lstCountry.options[0] = new Option(" - - Select Country - -", "");
	if(http.readyState == 4){
		xmldoc = http.responseXML;
		nodes = xmldoc.getElementsByTagName("country");
		for(var i = 0; i < nodes.length; i++) {
			if (nodes.length != 0) {
				optText = nodes.item(i).childNodes.item(0).nodeValue;
				optValue = optText.trim(); //nodes.item(i).getAttribute('id');
				optSelected = nodes.item(i).getAttribute('selected');
				
				lstCountry.options[i+1] = new Option(optText, optValue);
				if(optSelected == "yes") { lstCountry.selectedIndex = i+1; }
			}
		}
	}
}

function redirectToCountry() {
	lstCountry = document.getElementById("lstcountry");
	if(lstCountry.selectedIndex < 1) {
		alert('Please select a continent and then a country from the drop down list boxes.');
		document.getElementById("lstDestinations").focus();
		return false;
	}
	var sTemp = lstCountry.options[lstCountry.selectedIndex].value.replace(" ", "_");
	
	window.location.href = '/destination-guides/countries/'+sTemp+'/';
}

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

function switchButton(oImage, sOnOff) {
	var sTemp = oImage.src;
	if(sOnOff == "on") { sNewSrc = sTemp.replace('_off', '_on'); } else { sNewSrc = sTemp.replace('_on', '_off'); }
	oImage.src = sNewSrc;
}

function object_attach(id)
{
	var obj;
	if(ns4) obj = document.layers[id];
	else if(ie4) obj = document.all[id];
	else if(ie5 || ns6) obj = document.getElementById(id);
	return obj;
}

// Clears a text field only if the value matches what is passed in
function clearField(fId, sMatch, sChange)
{
	if(sChange == null) sChange = '';
	if(fId.value == sMatch)
	{ fId.value = sChange; }
}

function showCalendar3(calTitle,calTarget,calInit)
{
	if(calInit!=null)
	{ calendarWin=window.open("/includes/calendar.asp?initDate=" + eval("document." + calInit + "_d.value") + "/" + eval("document." + calInit + "_m.value") + "/" + eval("document." + calInit + "_y.value") + "&title=" + escape(calTitle) + "&field=" + escape(calTarget),"calendarWin","width=220,height=225,left=" + ((screen.width-220)/2) + ",top=" + ((screen.height-200)/2) + ",status=no,toolbar=no,menubar=no,scrollbars=no,resizable=no"); calendarWin.focus(); }
	else
	{ calendarWin=window.open("/includes/calendar.asp?initDate=" + eval("document." + calTarget + "_d.value") + "/" + eval("document." + calTarget + "_m.value") + "/" + eval("document." + calTarget + "_y.value") + "&title=" + escape(calTitle) + "&field=" + escape(calTarget),"calendarWin","width=220,height=225,left=" + ((screen.width-220)/2) + ",top=" + ((screen.height-200)/2) + ",status=no,toolbar=no,menubar=no,scrollbars=no,resizable=no"); calendarWin.focus(); }
	if(!calendarWin.opener) calendarWin.opener=self;
}

function div_switch(id, bShow, bBlock)
{
	temp_Obj = object_attach(id);
	if(ns4)
	{
		if(bShow==true)
		{
			if(bBlock==true) { temp_Obj.display = "block"; }
			else { temp_Obj.visibility = "show"; }
		}
		else
		{
			if(bBlock==true) { temp_Obj.display = "none"; }
			else { temp_Obj.visibility = "hide"; }
		}
	}
	else
	{
		if(bShow==true)
		{
			if(bBlock==true) { temp_Obj.style.display = "block"; }
			else { temp_Obj.style.visibility = "visible"; }
		}
		else
		{
			if(bBlock==true) { temp_Obj.style.display = "none"; }
			else { temp_Obj.style.visibility = "hidden"; }
		}
	}
}

function bookmarkPage(sPage, sName) { 
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(sName, sPage,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( sPage, sName); }
	else if(window.opera && window.print) { // Opera Hotlist
		return true; 
	} else {
		alert("Sorry! Your browser doesn't support this function."); 
	}
}
function showStatus(sMsg) {
    window.status = sMsg ;
    return true ;
}
function IsNumeric(sText)

{
   var ValidChars = "0123456789 ";
   var IsNumber=true;
   var Char;
	if(sText == ""){
 		return false;
	}
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }
function validate_accepttel(form){
	if(!IsNumeric(form.phone.value)){
		alert("please enter a valid phone number");
		return false;
	}
}
function validate_passwordchange(form){
	if(form.password.value.length < 6){
		alert("Ensure your password is 6 or more characters");
		return false;
	}
	else if(form.password.value !== form.confirmpassword.value){
		alert("Ensure you have confirmed your password");
		return false;
	}
}

function echeck(str) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(str)){
		return (true)
	}
	return (false)
}


function validate_signup(form){
	if(form.title.value == "-"){
		alert("Please select your title");
		return false;
	}
	else if(form.firstname.value == ""){
		alert("Please enter your firstname");
		return false;
	}
	else if(form.lastname.value == ""){
		alert("Please enter your lastname");
		return false;
	}
	else if((form.dobd.value == "-") || (form.dobm.value == "-")){
		alert("Please enter a valid date of birth");
		return false;
	}
	else if(form.prefferred.value == "-"){
		alert("Please select your preferred departure airport");
		return false;
	}
	else if(!echeck(form.memEmail.value)){
		alert("Please enter a valid email");
		return false;
	}
}
function validate_newsletter(form){
	if(form.newslettername.value == ""){
		alert("Please enter your name");
		return false;
	}
	else if(!echeck(form.nltreremail.value)){
		alert("Please enter a valid email");
		return false;
	}
}

// Opens a popup window
function openPopupWin(popUrl, popWid, popHgt, popScroll, popResize)
{
	popupWin=window.open(popUrl,"popupWin","width=" + popWid + ",height=" + popHgt + ",left=" + ((screen.width-popWid)/2) + ",top=" + ((screen.height-popHgt)/2) + ",status=yes,toolbar=no,menubar=no,scrollbars=" + popScroll + ",resizable=" + popResize);
	if(!popupWin.opener) popupWin.opener=self;
}

function validateFreetextSearch() {
	if(document.destinationBox.txtlocation.value == '-- enter destination --' || document.destinationBox.txtlocation.value == '') {
		alert('Please enter a destination to search for.');
		document.destinationBox.txtlocation.focus();
		return false;
	}
	
	return true;
}
function MM_openBrWindow(theURL,winName,features) {
	window.open(theURL,winName,features);
  return false;
}


