function CreateBookmarkLink(url,title)
{
    if (window.sidebar)
        window.sidebar.addPanel(title, url,"");
    else if (window.external)
        window.external.AddFavorite( url, title); 
    else if (window.opera && window.print)
        return true;
}

function setCategory(c)
{
	if (c == 1)
	{
		document.getElementById('month').disabled = false;
	}
	else
	{
		document.getElementById('month').selectedIndex = 0;
		document.getElementById('duration').selectedIndex = 0;
		document.getElementById('arrival_date').selectedIndex = 0;
		document.getElementById('month').disabled = true;
		document.getElementById('duration').disabled = true;
		document.getElementById('arrival_date').disabled = true;
	}
	
	GetDestinations(c);
}

function setMonth(m)
{
	if (m != '')
	{
		document.getElementById('duration').disabled=false;
		document.getElementById('duration').options[0].text=((language=="NL")?'Kies duur':'Bitte wählen Sie hier');
		
		var duration = document.getElementById('duration').options[document.getElementById('duration').selectedIndex].value;
		if (duration != '')
		{
			document.getElementById('arrival_date').options[0].text=((language=="NL")?'Even geduld aub':'Bitte kürz warten');
			
			var destination = document.getElementById('destination').options[document.getElementById('destination').selectedIndex].value;
			
			GetArrivalDates(document.getElementById('month').options[document.getElementById('month').selectedIndex].value, duration, destination);
		}
	}
	else
	{
		document.getElementById('duration').selectedIndex=0;
		document.getElementById('duration').options[0].text=((language=="NL")?'Kies maand':'Bitte wählen Sie hier');
		document.getElementById('duration').disabled=true;
		
		document.getElementById('arrival_date').selectedIndex=0;
		document.getElementById('arrival_date').options[0].text=((language=="NL")?'Kies eerst een maand en de duur':'Wählen Sie erst Monat danach Reisedauer');
		document.getElementById('arrival_date').disabled=true;
	}
}

function setDuration(dur)
{
	if (dur != '')
	{
		document.getElementById('arrival_date').options[0].text=((language=="NL")?'Even geduld aub':'Bitte kürz warten');
		
		var destination = document.getElementById('destination').options[document.getElementById('destination').selectedIndex].value;
		
		GetArrivalDates(document.getElementById('month').options[document.getElementById('month').selectedIndex].value, dur, destination);
		document.getElementById('arrival_date').disabled=false;
	}
	else
	{
		document.getElementById('arrival_date').selectedIndex=0;
		document.getElementById('arrival_date').options[0].text=((language=="NL")?'Kies eerst een maand en de duur':'Wählen Sie erst Monat danach Reisedauer');
		document.getElementById('arrival_date').disabled=true;
	}
}

function DoHttpRequest(url, callback_function, el)
{
    var http_request = false;
	
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) { }
        }
    }
	
    if (!http_request) {
        alert('Unfortunately you browser doesn\'t support this feature.');
        return false;
    }
	
    http_request.onreadystatechange = function() {
	
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
				eval(callback_function + "(http_request.responseXML, el)");
            } else {
                alert('There was a problem with the request.(Code: ' + http_request.status + ')');
            }
        }
    }
    http_request.open('GET', url, true);
    http_request.send(null);
}

function GetDestinations(c)
{
	DoHttpRequest('/?act=get_destinations&cat='+c, 'FillDestinations');
}

function FillDestinations(xml)
{
	var destinations = xml.getElementsByTagName('destination');
	
	var list = ["select", {id:'destination', name:'destination'}];
	
	list.push(['option', {value:''}, ((language=="NL") ? 'Kies een bestemming' : 'Wählen Sie Land')]);
	
	var len = destinations.length;
	
	for(i=0;i<len;i++)
	{
		if(destinations[0].textContent == undefined)
		{
			label = destinations[i].getElementsByTagName('acc_country_name');
			label = label[0].text;
			value = destinations[i].getElementsByTagName('acc_country_id');
			value = value[0].text;
			list.push(['option', {value:value}, utf8decode(label)]);
		}
		else
		{
			label = destinations[i].getElementsByTagName('acc_country_name');
			label = label[0].textContent;
			value = destinations[i].getElementsByTagName('acc_country_id');
			value = value[0].textContent;
			list.push(['option', {value:value}, utf8decode(label)]);
		}
	}
	
	var destinations = document.getElementById('destination');
	destinations.parentNode.removeChild(destinations);
	
	graft(document.getElementById("insert-destinations-here"), list);
}

function GetArrivalDates(m, dur, dest)
{
	DoHttpRequest('/?act=get_arrival_dates&month='+m+'&duration='+dur+'&destination='+dest, 'FillArrivalDates', 'fillarrivaldates');
}

function FillArrivalDates(xml, el)
{
	var xmlDoc = xml;
	
	var arrivaldates = xmlDoc.getElementsByTagName('arrivaldate');
	
	var list = ["select", {id:'arrival_date', name:'arrival_date'}];
	
	list.push(['option', {value:''}, ((language=="NL") ? 'Kies een aankomstdatum' : 'Wählen Sie Ankunfstdatum')]);
	
	var len = arrivaldates.length;
	
	for(i=0;i<len;i++)
	{
		if(arrivaldates[0].textContent == undefined)
		{
			label = arrivaldates[i].getElementsByTagName('label');
			label = label[0].text;
			value = arrivaldates[i].getElementsByTagName('value');
			value = value[0].text;
			list.push(['option', {value:value}, utf8decode(((language=="NL") ? 'rond '+label : 'um '+label+' herum'))]);
		}
		else
		{
			label = arrivaldates[i].getElementsByTagName('label');
			label = label[0].textContent;
			value = arrivaldates[i].getElementsByTagName('value');
			value = value[0].textContent;
			list.push(['option', {value:value}, utf8decode(((language=="NL") ? 'rond '+label : 'um '+label+' herum'))]);
		}
	}
	
	var arrivals = document.getElementById('arrival_date');
	arrivals.parentNode.removeChild(arrivals);
	
	graft(document.getElementById("insert-here"), list);
}

function utf8encode(string)
{
    string = string.replace(/\r\n/g,"\n");
    var utftext = "";
	
    for (var n = 0; n < string.length; n++)
	{
        var c = string.charCodeAt(n);
		
        if (c < 128)
		{
            utftext += String.fromCharCode(c);
        }
        else if((c > 127) && (c < 2048))
		{
            utftext += String.fromCharCode((c >> 6) | 192);
            utftext += String.fromCharCode((c & 63) | 128);
        }
        else
		{
            utftext += String.fromCharCode((c >> 12) | 224);
            utftext += String.fromCharCode(((c >> 6) & 63) | 128);
            utftext += String.fromCharCode((c & 63) | 128);
        }
    }
	
    return utftext;
}

function utf8decode(utftext)
{
    var string = "";
    var i = 0;
    var c = c1 = c2 = 0;
	
    while ( i < utftext.length )
	{
        c = utftext.charCodeAt(i);
		
        if (c < 128)
		{
            string += String.fromCharCode(c);
            i++;
        }
        else if((c > 191) && (c < 224))
		{
            c2 = utftext.charCodeAt(i+1);
            string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
            i += 2;
        }
        else
		{
            c2 = utftext.charCodeAt(i+1);
            c3 = utftext.charCodeAt(i+2);
            string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    }
	
    return string;
}

// graft() function
// Originally by Sean M. Burke from interglacial.com
// Closure support added by Maciek Adwent
//
// Modified by Kasper to support style attributes in IE
//
function graft (parent, t, doc)
{
    doc = (doc || parent.ownerDocument || document);
    var e;
	
    if(t == undefined) {
        throw complaining( "Can't graft an undefined value");
    } else if(t.constructor == String) {
        e = doc.createTextNode( t );
    } else if(t.length == 0) {
        e = doc.createElement( "span" );
        e.setAttribute( "class", "fromEmptyLOL" );
    } else {
        for(var i = 0; i < t.length; i++) {
            if( i == 0 && t[i].constructor == String ) {
                var snared;
                snared = t[i].match( /^([a-z][a-z0-9]*)\.([^\s\.]+)$/i );
                if( snared ) {
                    e = doc.createElement(   snared[1] );
                    e.setAttribute( 'className', snared[2] );
                    continue;
                }
                snared = t[i].match( /^([a-z][a-z0-9]*)$/i );
                if( snared ) {
                    e = doc.createElement( snared[1] );  // but no class
                    continue;
                }
				
                // Otherwise:
                e = doc.createElement( "span" );
                e.setAttribute( "class", "namelessFromLOL" );
            }
			
            if( t[i] == undefined ) {
                throw complaining("Can't graft an undefined value in a list!");
            } else if(  t[i].constructor == String ||
                                    t[i].constructor == Array ) {
                graft( e, t[i], doc );
            } else if(  t[i].constructor == Number ) {
                graft( e, t[i].toString(), doc );
            } else if(  t[i].constructor == Object ) {
                // hash's properties => element's attributes
                for(var k in t[i]) {
                    // support for attaching closures to DOM objects
                    if(typeof(t[i][k])=='function'){
                        e[k] = t[i][k];
                    } else {
						if( k == "style" )
							e.style.cssText = t[i][k]; // this bit modified by Kasper
						else
							e.setAttribute( k, t[i][k] );
                    }
                }
            } else {
                throw complaining( "Object " + t[i] +
                    " is inscrutable as an graft arglet." );
            }
        }
    }
	
    parent.appendChild( e );
    return e; // return the topmost created node
}

function complaining (s) { alert(s); return new Error(s); }


function changeStep(iStep)
{
	oStep = document.getElementById("steppor");
	oStep.innerHTML = '';

	for(i=1; i < 4; i++)
	{
		if(i != iStep)
		{
			var tag = document.createElement("div");
			tag.className = "hide_step_" + i;
			tag.id = "step_hider_" + i;
			oStep.appendChild(tag);
		}
	}
}

function getTabs(sTabsContainer)
{
	if(typeof sTabsContainer!='string') sTabsContainer='tabs2';
	
	oTabContainer=document.getElementById(sTabsContainer);
	
	if(!oTabContainer) return false;
	
	aElements=oTabContainer.getElementsByTagName("a");
	
	for(i=0;i<aElements.length;i++)
	{
		oHref=aElements[i];

		if(oHref.className=='hidden') oHref.style.display='none';

		if(i==0) oCurOpen=oHref;
		
		oHref.onclick=function(e)
		{
			e=window.event || e;
			var who=e.target || e.srcElement;
			loadTab(who);
			return false;
		}
		oHref.id="tab"+oHref.href.split('#')[1];
	}

	if (typeof window.location.href.split('#')[1] == 'string')
		openTab(window.location.href.split('#')[1]);
	else
		loadTab(oCurOpen);
}

function loadTab(newAObject)
{
	// close and hide the current open tab
	setTabProps(oCurOpen,'','none');
	
	if (document.getElementById('tabToLoad'))
	{
		document.getElementById('tabToLoad').value = newAObject.href.split('#')[1];
	}
	
	// open the new tab
	setTabProps(newAObject,'active-tab','block');

	oCurOpen=newAObject;

	if (sWebsiteMode != 'extranet')
	{
		changeStep(2);
	}

	return false;
}

// this function is to "manually" open tabs (now also being used to open up the "start-tab"
function openTab(sTabname)
{
	if(typeof sTabname!='string') return;
	return loadTab(document.getElementById("tab" + sTabname));
}

function setTabProps(oObject,sClass,sDisplay)
{
	oObject.className=sClass;
	sHref=oObject.href.split("#");
	oToFix=document.getElementById(sHref[1]);
	oToFix.style.display=sDisplay;
}

function InitTabs()
{
	getTabs('tabs2');
	if (sWebsiteMode == 'extranet') 
	{
		if (tabToLoad != '')
		{
			if (document.getElementById("tab"+tabToLoad))
			{
				loadTab(document.getElementById("tab"+tabToLoad));
			}
		}
	}
}
window.onload=InitTabs;
