// JavaScript Document


/*   AJAX */

var http = createRequestObject();
var objectId = '';


function createRequestObject(htmlObjectId){
    var obj;
    var browser = navigator.appName;
    
    objectId = htmlObjectId;
	
	//alert(objectId+' - '+htmlObjectId);
    
    if(browser == "Microsoft Internet Explorer"){
        obj = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else{
        obj = new XMLHttpRequest();
    }
    return obj;    
}

function sendReq(serverFileName, variableNames, variableValues) {
	var paramString = '';
	
	variableNames = variableNames.split('|');
	variableValues = variableValues.split('|');
	
	for(i=0; i<variableNames.length; i++) {
		paramString += variableNames[i]+'='+variableValues[i]+'&';
	}
	paramString = paramString.substring(0, (paramString.length-1));
	
	//alert(serverFileName+'?'+paramString);			

	if (paramString.length == 0) {
	   	http.open('get', serverFileName);
	}
	else {
		http.open('get', serverFileName+'?'+paramString);
	}
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function handleResponse() {
	
	if(http.readyState == 4){
		responseText = http.responseText;
		document.getElementById(objectId).innerHTML = responseText;
    }
}



function showadditionaccom(add) {

	var additionadiv = 'accommodation_'+add;
	
	document.getElementById(additionadiv).style.display = 'block';

}
function closeMCE(){
    if (typeof tinyMCE != "undefined") {
        for(id in tinyMCE.editors){
            tinyMCE.execCommand('mceRemoveControl', false, id);
        }
    }else{
        alert('Editeur non initialisé');
    }
}
function savemessge(docid) {

	if(docid) {
	
		var value = document.getElementById(docid).value;
		
		var htmlObjectId = 'response_'+docid;
		
		document.getElementById(htmlObjectId).innerHTML = '';
	
		createRequestObject(htmlObjectId);
		
		sendReq('includes/ajax.php', 'function|value', docid+'|'+value);
	
	}

}
function HideContent(d) {
if(d.length < 1) { return; } else {
document.getElementById(d).style.display = "none";}
}

function ShowContent(d) {
if(d.length < 1) { return; } else {
document.getElementById(d).style.display = "block";}
}

function ReverseContentDisplay(d) {
if(d.length < 1) { return; }
if(document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = "block"; }
else { document.getElementById(d).style.display = "none"; }
}

function displayone(d) {

	
	for (var i=0; i<optionsarray.length; i++) {
		
		var tmp = 'a_'+optionsarray[i];
	
		if (d == optionsarray[i]) {
		
			ShowContent(optionsarray[i]);
			
			/*document.getElementById(tmp).className += "navselected";
			
			document.getElementById(tmp).style.backgroundColor = '#F78F1E';

			document.getElementById(tmp).style.color = '#FFF';*/

			
		} else {
			//alert(optionsarray[i]);
		
			HideContent(optionsarray[i]);
			
			/*document.getElementById(tmp).className += "notnavselected";
			
			document.getElementById(tmp).style.backgroundColor = '#FFF';

			document.getElementById(tmp).style.color = '#00458F';*/
		
		}
	
	}


}

// Nannette Thacker http://www.shiningstar.net
function confirmSubmit() 
{
var agree=confirm("Are you sure you wish to continue?");
if (agree)
	return true ;
else
	return false ;
}


function clearText(thefield){
		if (thefield.defaultValue == thefield.value) thefield.value = '';
    else if (thefield.value == '') thefield.value = thefield.defaultValue;
	
}

function upload_submit() {
	
	var error_array='';
	
	var disp;
	
	var no_image =0;

	for(var i =0 ; i <=9 ; i++) {
	
		disp=i+1;
		
		//if (document.getElementById('Alt_Text['+i+']').value == '') alert(document.getElementById('image_upload['+i+']').value);
	
		if(document.getElementById('image_upload['+i+']').value != '' && document.getElementById('Alt_Text['+i+']').value == '') error_array += 'Alt text needed for image '+disp+'\n';
		
		if(document.getElementById('image_upload['+i+']').value != '') no_image = 1;
	
	}
	
	if (no_image == 0) {
	
		alert('No image selected for uplaod');
		
	} else {
		
		if(error_array == '') {
		
			document.forms('image_upload').submit();
			
		}	else {
		
			alert(error_array);
		
		}
			
	}

}	


function search_accommodation(form) {
	
	var search_url = '1/';
	
	if(form.country.value == 0) { search_url = search_url + '0/'; } else { search_url = search_url + form.country.value+'/' }
	
	if(!form.resort.value) { search_url = search_url + '0/'; } else { search_url = search_url + form.resort.value+'/' }
	
	if(form.type.value == 0) { search_url = search_url + '0/'; } else { search_url = search_url + form.type.value+'/';  }
	
	if(form.star.value == 0) { search_url = search_url + '0/'; } else { search_url = search_url + form.star.value+'/';  }
	
	//if(form.vine.value == 'n') { search_url = search_url + 'n/'; } else { search_url = search_url + form.vine.value+'/';  }
	
	
	boxes = form.facilities.length;
	txt = "";
	for (i = 0; i < boxes; i++) {
		if (form.facilities[i].checked) {
			txt = txt + form.facilities[i].value + ",";
		}
	}
	if(txt != "") {
		tmplen = txt.length - 1;
		txt = txt.substring(0,tmplen);
		search_url = search_url + txt + '/';
	}
	
	//alert(search_url);
	
	window.location = '/holiday-search/'+search_url;

	
}



function offer_accommodation(form) {
	
	var search_url = form.accomid.value+'/';
	
	search_url = search_url + form.location.value+'/';
	
	search_url = search_url + '1/';
	
	search_url = search_url + form.name.value;
	
	
	window.location = '/offer/'+search_url;

	
}



		
		
function callme(filesrc,query) {


	if(!filesrc) { 
		src = "/pages/callme/";
	} else {
		src = "/pages/"+filesrc+"/";	
	}
	
	if(query) src = query;
		
	$.modal('<iframe src="' + src + '" height="450" width="590" style="border:0; background-color:#FFFFFF">', {
		close:true,
		closeHTML:"<div style='float:right; padding:10px;'><a href='#'>Close</a><div>",

		containerCss:{
			backgroundColor:"#fff",
			borderColor:"#FFF",
			padding:0,
			height: 500,
			width:600
		},
		overlayClose:true
	});


}

		
		
function modalpopup(src, height, width) {

	var windowheight = height+50;

	$.modal('<iframe src="' + src + '" height="' + height + '" width="' + width + '" style="border:0">', {
		close:true,
		closeHTML:"<div style='float:right; padding:10px;'><a href='#'>Close</a><div>",

		containerCss:{
			backgroundColor:"#fff",
			borderColor:"#FFF",
			padding:0,
			height: windowheight,
			width: width
		},
		overlayClose:true
	});


}



/*   JQUERY INCLUDES ALL PAGES */

	
	var profiles =
	{
		videopopup:
		{
			height:580,
			width:960,
			center:1,
			resizable:0,
			menubar:0,
			scrollbars:0,
			status:0
		},

		spatermspopup:
		{
			height:400,
			width:400,
			center:1,
			resizable:0,
			menubar:0,
			scrollbars:0,
			status:0
		},

		cartermspopup:
		{
			height:500,
			width:500,
			center:1,
			resizable:0,
			menubar:0,
			scrollbars:1,
			status:0
		},

		termspopup:
		{
			height:550,
			width:450,
			center:1,
			resizable:0,
			menubar:0,
			scrollbars:1,
			status:0
		},

		flightoptions:
		{
			height:600,
			width:730,
			center:1,
			resizable:0,
			menubar:0,
			scrollbars:0,
			status:0
		},

		atolpopup:
		{
			height:420,
			width:290,
			center:1,
			resizable:1,
			menubar:0,
			scrollbars:1,
			status:0
		},

		forecastpopup:
		{
			height:460,
			width:530,
			center:1,
			resizable:0,
			menubar:0,
			scrollbars:0,
			status:0
		},

		webcampopup:
		{
			height:510,
			width:670,
			center:1,
			resizable:0,
			menubar:0,
			scrollbars:0,
			status:0
		}

	};

	function unloadcallback(){
		alert("unloaded");
	};


   	$(function()
	{
   		$(".popupwindow").popupwindow(profiles);
		
		/**/
		
		$("#googlesearch").autocomplete({
			source: availableTags,
			minLength: 2,
			select: function(event, ui) {
				//alert(ui.item.value);
				$('#googlesearch').val(ui.item.value);
				$('#fSearch').submit();
			}

		});


		$("#keepinformed").dialog({
			autoOpen: false,
			height: 560,
			width: 420,
			modal: false
		});

				
		$("a").each( function() {   
			//alert(this.href);
			this.href = this.href.replace(/\s/g,"-");
			this.href = this.href.replace(/%20/g,"-");
		});

		
		$("input[type=text][title],textarea[title]")
			.each( showPlaceholder ) // initialize each control on page load
			.blur( showPlaceholder )
			.focus( hidePlaceholder );
		
		$("form").submit( function() {
			$( "input[type=text][title],textarea[title]", this ).each( hidePlaceholder );
		} );
		
		function showPlaceholder() {
			var $control = $(this);
			var placeholderText = $control.attr("title");
			if ( $control.val() === "" || $control.val() === placeholderText ) {
				$control.addClass("placeholder");
				$control.val(placeholderText);
			}
		};
		
		function hidePlaceholder() {
			var $control = $(this);
			if ( $control.val() === $control.attr("title") ) {
				$control.removeClass("placeholder");
				$control.val("");
			}
		}		
		
		
   	});

	function openinformed() {
		$('#keepinformed').dialog('open');
	}

	function closeinformed() {
		$('#keepinformed').dialog('close');
	}


/*
 * Chained - jQuery chained selects plugin
 *
 * Copyright (c) 2010 Mika Tuupola
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 */

(function($) {

    $.fn.chained = function(parent_selector, options) { 
        
        return this.each(function() {
            
            /* Save this to self because this changes when scope changes. */            
            var self   = this;
            var backup = $(self).clone();
            
            /* Handles maximum two parents now. */
            $(parent_selector).each(function() {
                $(this).bind("change", function() {
                    $(self).html($(backup).html());
                    

                    /* If multiple parents build classname like foo\bar. */
                    var selected = "";
                    $(parent_selector).each(function() {
                        selected += "\\" + $(":selected", this).val();
                    });
                    selected = selected.substr(1);

                    /* Also check for first parent without subclassing. */
                    /* TODO: This should be dynamic and check for each parent */
                    /*       without subclassing. */
                    var first = $(parent_selector).first();
                    var selected_first = $(":selected", first).val();
                
                    $("option", self).each(function() {
                        /* Remove unneeded items but save the default value. */
                        if (!$(this).hasClass(selected) && 
                            !$(this).hasClass(selected_first) && $(this).val() !== "") {
                                $(this).remove();
                        }                        
                    });
                
                    /* If we have only the default value disable select. */
                    if (1 == $("option", self).size() && $(self).val() == "") {
                        $(self).attr("disabled", "disabled");
                    } else {
                        $(self).removeAttr("disabled");
                    }
                
                });

                /* Force updating the children. */
                $(this).trigger("change");             

            });
        });
    };
    
    /* Alias for those who like to use more English like syntax. */
    $.fn.chainedTo = $.fn.chained;
    
})(jQuery);
