
function djfsContinent_onChange(){
    if(dijit.byId("djfsContinent").value != "0" && dijit.byId("djfsContinent").validate()){
	var cid = dijit.byId("djfsContinent").value;	
	var countryStore = new dojo.data.ItemFileReadStore({url: "/villa/countriesjson/cid/"+cid});
	dijit.byId("djfsCountry").attr("displayedValue", "Loading...");
	dijit.byId("djfsCountry").setDisabled(true);
	dijit.byId("djfsCity").setDisabled(true);
	//dijit.byId("btnSearchProperties").setDisabled(true);
	countryStore.fetch({
	    onBegin: function(){		
		
	    },
	    onComplete: function(items){
		if(items.length > 0){
		    var selectedItem = items[0].name;
		    dijit.byId("djfsCountry").store = countryStore;
		    dijit.byId("djfsCountry").setDisabled(false);
		    if(dojo.byId("unitCountryName").value == "0"){
			dijit.byId("djfsCountry").attr("displayedValue",selectedItem.toString());
		    }else{
			dijit.byId("djfsCountry").attr("displayedValue",dojo.byId("unitCountryName").value);
			dojo.byId("unitCountryName").value = "0";
		    }
		    
		    //dojo.byId("djfsCountry").select();
		    //dijit.byId("btnSearchProperties").setDisabled(false);
		}else{
		    dijit.byId("djfsCountry").attr("displayedValue","Currently no listings for this continent...");
		}
	    }
	});
    }
}

function djfsCountry_onChange(){
    if(dijit.byId("djfsCountry").validate()){
	var cid = dijit.byId("djfsCountry").value;
	var cityStore = new dojo.data.ItemFileReadStore({url: "/villa/citiesjson/cid/"+cid});
	dijit.byId("djfsCity").attr("displayedValue", "Loading...");
	dijit.byId("djfsCity").setDisabled(true);
	//dijit.byId("btnSearchProperties").setDisabled(true);
	cityStore.fetch({
	    onBegin: function(){
		
	    },
	    onComplete: function(items){
		if(items.length > 0){
		    var selectedItem = items[0].name;
		    dijit.byId("djfsCity").store = cityStore;
		    dijit.byId("djfsCity").setDisabled(false);
		    if(dojo.byId("unitCityName").value == "0"){
			dijit.byId("djfsCity").attr("displayedValue",selectedItem.toString());
		    }else{
			dijit.byId("djfsCity").attr("displayedValue",dojo.byId("unitCityName").value);
			dojo.byId("unitCityName").value = "0";
		    }		    
		    //dojo.byId("djfsCity").select();
		    //dijit.byId("btnSearchProperties").setDisabled(false);
		}else{
		    dijit.byId("djfsCity").attr("displayedValue","Currently no listings for this country...");
		}
	    }
	});
    }
}

function djfsFromDate_onChange(){
    var dteFromObj = dijit.byId("djfsFromDate").value;
    var dteToDate = dijit.byId("djfsToDate").value;
    var minDate = dojo.date.add(dteFromObj, "day", 1);

    //MINIMUM SELECTABLE DATE FOR TEH TO DATE
    dijit.byId('djfsToDate').constraints.min = minDate;

    //IF TO DATE IS LESS THAN FROM DATE SET TO 1 DAY AFTER
    if(dteToDate < dteFromObj){
	dijit.byId("djfsToDate").attr("displayedValue", dojo.date.locale.format(minDate, {datePattern:"dd.MM.yyyy",selector: "date", formatLength: "short"}));
    }

    dojo.byId("unitFromDate").value = dojo.date.locale.format(dteFromObj, {datePattern:"dd.MM.yyyy",selector: "date", formatLength: "short"});
}

function djfsToDate_onChange(){
    dojo.date.locale.format(dijit.byId("djfsToDate").value, {datePattern:"dd.MM.yyyy",selector: "date", formatLength: "short"})
    dojo.byId("unitToDate").value = dojo.date.locale.format(dijit.byId("djfsToDate").value, {datePattern:"dd.MM.yyyy",selector: "date", formatLength: "short"});
}

function advanceFeatures_onClick(){

    if(dojo.byId("advanceSearch").value == "1"){
	hideAdvanceFeatures();
	dojo.byId("advanceSearch").value = "0";
    }else{
	showAdvanceFeatures();
	dojo.byId("advanceSearch").value = "1";
    }
}

function showAdvanceFeatures(){
    var fx = dojo.fx.wipeIn({node: "divAdvanceFeatures",duration: 1000});
    fx.play();
}

function hideAdvanceFeatures(){
    var fx = dojo.fx.wipeOut({node: "divAdvanceFeatures",duration: 1000});
    fx.play();
}

function btnSearchProperties_onClick(){
    if(dijit.byId("frmSearchProperties").validate() && dijit.byId("djfsContinent").value != "0"){
	dojo.byId("messagetext").innerHTML = "Please wait....";
	dojo.byId("loader").style.display = "block";
	dijit.byId("messagebox").show();
	dojo.byId("unitContinentName").value = dojo.byId("djfsContinent").value;
	dojo.byId("unitCountryName").value = dojo.byId("djfsCountry").value;
	dojo.byId("unitCityName").value = dojo.byId("djfsCity").value;
	dojo.byId("frmSearchProperties").action = "/villa/bookingstep1";
	dojo.byId("frmSearchProperties").submit();
    }else{
	dijit.byId("djfsContinent").displayMessage("Please type or select a continent");
    }
}

function btnSelectUnit_onClick(id){
    dojo.byId("messagetext").innerHTML = "Please wait....";
    dojo.byId("loader").style.display = "block";
    dijit.byId("messagebox").show();
    dojo.byId("unitPropertyId").value = id;
    dojo.byId("unitContinentName").value = dojo.byId("djfsContinent").value;
    dojo.byId("unitCountryName").value = dojo.byId("djfsCountry").value;
    dojo.byId("unitCityName").value = dojo.byId("djfsCity").value;
    dojo.byId("unitIsAvail").value = dojo.byId("unitIsAvail"+id).value;
    dojo.byId("frmSearchProperties").action = "/villa/bookingstep2";
    dojo.byId("frmSearchProperties").submit();
}

function loadImageGallery(id){
    var galleryStore	= new dojo.data.ItemFileReadStore({url: "/villa/imagegallery/pid/"+id});
    var galleryLightBox = new dojox.image.Lightbox();   
    
    galleryLightBox.startup();
    galleryLightBox.show();
    
    if(!galleryLightBox._attachedDialog._groups["group"+id]){
	galleryStore.fetch({
	    onBegin:function(){
		
	    },
	    onComplete: function(items){
		if (items.length>0){
		    for(var i=0; i < items.length;i++){
			var imgs = {
			    title: "",
			    href: "../images/villas/villaThumbs/fs/"+items[i].name
			};
			galleryLightBox._attachedDialog.addImage(imgs,"group"+id);
		    }
		    galleryLightBox.group = "group"+id;
		    galleryLightBox.show();
		}
	    },
	    onError:function(error){
		alert(error);
	    }
	});
    }else{
	galleryLightBox.group = "group"+id;
	galleryLightBox.show();
    }
    galleryStore.close(null);
}

function getMap(id){
    Shadowbox.open({
		content:    'maps/id/'+id,
		player:     "iframe",
		title:      "Villa Map",
		height:     500,
		width:      700
	});
}

function btnAddUnit_onClick(){
    if(dojo.byId("cbTerms").checked){
	dojo.byId("frmSearchProperties").action = "/villa/addunittocart";
	//dojo.byId("frmSearchProperties").submit();
	dojo.byId("messagetext").innerHTML = "Calculating pricing....";
	dojo.byId("loader").style.display = "block";
	dijit.byId("messagebox").show();
	dojo.xhrPost({
	    form:dojo.byId("frmSearchProperties"),
	    handleAs:"xml",
	    timeout:5000,
	    load:function(response,ioArgs){
		dijit.byId("messagebox").title = "Adding to cart..."
		var text = response.getElementsByTagName("message");
		if(text[0].childNodes[0].nodeValue){
		    LoadCart("Item(s) added to your basket");
		    dijit.byId("messagebox").hide();
		}
	    },
	    error:function(response,ioArgs){
		alert(response)
	    }
	});
    }else{
	dojo.byId("messagetext").innerHTML = "Please confirm you have read and agreed to the terms and conditions.";
	dojo.byId("loader").style.display = "none";
	dijit.byId("messagebox").show();
    }
}
