/**
 * @author groening
 */

function writeHtmlEntities( str )
{
	retval = str;
	/* FIXME
	retval = str.replace( /ä/, "&auml;");
	retval = retval.replace( /ü/, "&uuml;" );
	retval = retval.replace( /ö/, "&ouml;" );
	retval = retval.replace( /Ä/, "&Auml;");
	retval = retval.replace( /Ü/, "&Uuml;" );
	retval = retval.replace( /Ö/, "&Ouml;" );
	retval = retval.replace( /ß/, "&szlig;" );
	*/
		
	return retval;
}


function setWindowTitle(box){
	title = 'Ferienhaus & Ferienwohnung {LANDNAME} buchen bei tui-ferienhaus.de';
	headlineOne = 'Suche Ferienhaus & Ferienwohnung{LANDNAME}';
	headlineTwo = 'Reiseziele{LANDNAME}';
	metadesc = 'Ferienaus & Ferienwohnung{LANDNAME} bequem online buchen bei tui-ferienhaus.de. Über 16.000 Ferienwohnungen & Ferienhäuser in 27 Ländern';
	
	if( document.getElementById('anlageart') && document.getElementById('anlageart').value == 'fh')
	{
		if (box.selectedIndex > 0) {
		
			landname = writeHtmlEntities(box[box.selectedIndex].text.replace(/\(.*\)/, ''));
			document.title = title.replace(/{LANDNAME}/, 'in ' + landname);
			if (document.getElementById('searchHeadlineOne')) {
				textlen = document.getElementById('searchHeadlineOne').firstChild.nodeValue.length;
				text = headlineOne.replace(/{LANDNAME}/, ' in ' + landname);
				document.getElementById('searchHeadlineOne').firstChild.replaceData(0, textlen, text);
			}
			if (document.getElementById('searchHeadlineTwo')) {
				textlen = document.getElementById('searchHeadlineTwo').firstChild.nodeValue.length;
				text = headlineTwo.replace(/{LANDNAME}/, ' in ' + landname);
				document.getElementById('searchHeadlineTwo').firstChild.replaceData(0, textlen, text);
			}
			var metas = document.getElementsByTagName('meta');
			var i = 0;
			for (i = 0; i < metas.length; i++) {
				if (metas[i].name == 'description') {
					metas[i].content = metadesc.replace(/{LANDNAME}/, ' in ' + landname);
					break;
				}
			}
			setObjectLinks();
			
		}
		else {
			document.title = title.replace(/{LANDNAME}/, '');
			if (document.getElementById('searchHeadlineOne')) {
				textlen = document.getElementById('searchHeadlineOne').firstChild.nodeValue.length;
				text = headlineOne.replace(/{LANDNAME}/, '');
				document.getElementById('searchHeadlineOne').firstChild.replaceData(0, textlen, text);
			}
			if (document.getElementById('searchHeadlineTwo')) {
				textlen = document.getElementById('searchHeadlineTwo').firstChild.nodeValue.length;
				text = headlineTwo.replace(/{LANDNAME}/, '');
				document.getElementById('searchHeadlineTwo').firstChild.replaceData(0, textlen, text);
			}
			var metas = document.getElementsByTagName('meta');
			var i = 0;
			for (i = 0; i < metas.length; i++) {
				if (metas[i].name == 'description') {
					metas[i].content = metadesc.replace(/{LANDNAME}/, '');
					break;
				}
			}
			/*
		 var countryobjects = document.getElementById( 'countryobjects' );
		 if( countryobjects )
		 {
		 do
		 {
		 knoten = countryobjects.firstChild;
		 if(knoten)
		 {
		 document.getElementById( 'countryobjects').removeChild(knoten);
		 }
		 }while( knoten );
		 }
		 */
			setObjectLinks();
		}
	}
}


function setWindowTitle2(box, formname){

	title = 'Ferienhaus und Ferienwohnung buchen bei tui-ferienhaus.de';
	headlineOne = 'Suche Ferienhaus & Ferienwohnung{LANDNAME}';
	headlineTwo = 'Reiseziele{LANDNAME}';
	metadesc = 'Ferienaus & Ferienwohnung{LANDNAME} bequem online buchen bei tui-ferienhaus.de. Über 16.000 Ferienwohnungen & Ferienhäuser in 27 Ländern';
	if (box.selectedIndex > 0) {
	
		landname = '';
		regionname = '';
		gebietname = '';
		ortname = '';
		
		
		if (box.name == 'searchdata[region_kz]') {
		
			landname = writeHtmlEntities(document.forms[formname].elements['searchdata[land_kz]'].options[document.forms[formname].elements['searchdata[land_kz]'].selectedIndex].text.replace(/\(.*\)/, ''));
			regionname = writeHtmlEntities(box[box.selectedIndex].text.replace(/\(.*\)/, ''));
			document.title = 'Ferienhaus ' + regionname + ' Ferienwohnung ' + landname + ' buchen bei tui-ferienhaus.de';
			
		}
		
		if (box.name == 'searchdata[area_kz]') {
		
			landname = writeHtmlEntities(document.forms[formname].elements['searchdata[land_kz]'].options[document.forms[formname].elements['searchdata[land_kz]'].selectedIndex].text.replace(/\(.*\)/, ''));
			regionname = writeHtmlEntities(document.forms[formname].elements['searchdata[region_kz]'].options[document.forms[formname].elements['searchdata[region_kz]'].selectedIndex].text.replace(/\(.*\)/, ''));
			gebietname = writeHtmlEntities(box[box.selectedIndex].text.replace(/\(.*\)/, ''));
			document.title = 'Ferienhaus ' + gebietname + ' Ferienwohnung ' + regionname + ' ' + landname + ' buchen bei tui-ferienhaus.de';
			
		}
		
		if (box.name == 'searchdata[ort_kz]') {
		
			landname = writeHtmlEntities(document.forms[formname].elements['searchdata[land_kz]'].options[document.forms[formname].elements['searchdata[land_kz]'].selectedIndex].text.replace(/\(.*\)/, ''));
			regionname = writeHtmlEntities(document.forms[formname].elements['searchdata[region_kz]'].options[document.forms[formname].elements['searchdata[region_kz]'].selectedIndex].text.replace(/\(.*\)/, ''));
			gebietname = writeHtmlEntities(document.forms[formname].elements['searchdata[area_kz]'].options[document.forms[formname].elements['searchdata[area_kz]'].selectedIndex].text.replace(/\(.*\)/, ''));
			ortname = writeHtmlEntities(box[box.selectedIndex].text.replace(/\(.*\)/, ''));
			document.title = 'Ferienhaus ' + ortname + ' Ferienwohnung ' + gebietname + ' ' + regionname + ' ' + landname + ' buchen bei tui-ferienhaus.de';
			
		}
		
		//landname = writeHtmlEntities(box[box.selectedIndex].text.replace( /\(.*\)/, ''));
		//document.title = title.replace( /{LANDNAME}/, 'in ' + landname);
		if (document.getElementById('searchHeadlineOne')) {
			textlen = document.getElementById('searchHeadlineOne').firstChild.nodeValue.length;
			text = headlineOne.replace(/{LANDNAME}/, ' in ' + landname);
			document.getElementById('searchHeadlineOne').firstChild.replaceData(0, textlen, text);
		}
		if (document.getElementById('searchHeadlineTwo')) {
			textlen = document.getElementById('searchHeadlineTwo').firstChild.nodeValue.length;
			text = headlineTwo.replace(/{LANDNAME}/, ' in ' + landname);
			document.getElementById('searchHeadlineTwo').firstChild.replaceData(0, textlen, text);
		}
		var metas = document.getElementsByTagName('meta');
		var i = 0;
		for (i = 0; i < metas.length; i++) {
			if (metas[i].name == 'description') {
				metas[i].content = metadesc.replace(/{LANDNAME}/, ' in ' + landname);
				break;
			}
		}
		setObjectLinks();
	}
	else {
		document.title = title.replace(/{LANDNAME}/, '');
		if (document.getElementById('searchHeadlineOne')) {
			textlen = document.getElementById('searchHeadlineOne').firstChild.nodeValue.length;
			text = headlineOne.replace(/{LANDNAME}/, '');
			document.getElementById('searchHeadlineOne').firstChild.replaceData(0, textlen, text);
		}
		if (document.getElementById('searchHeadlineTwo')) {
			textlen = document.getElementById('searchHeadlineTwo').firstChild.nodeValue.length;
			text = headlineTwo.replace(/{LANDNAME}/, '');
			document.getElementById('searchHeadlineTwo').firstChild.replaceData(0, textlen, text);
		}
		var metas = document.getElementsByTagName('meta');
		var i = 0;
		for (i = 0; i < metas.length; i++) {
			if (metas[i].name == 'description') {
				metas[i].content = metadesc.replace(/{LANDNAME}/, '');
				break;
			}
		}
		
		/*
		 var countryobjects = document.getElementById('countryobjects');
		 if (countryobjects) {
		 do {
		 knoten = countryobjects.firstChild;
		 if (knoten) {
		 document.getElementById('countryobjects').removeChild(knoten);
		 }
		 }
		 while (knoten);
		 }
		 */
		setObjectLinks();
	}
}

function setObjectLinks(){
	//splitarea();
	var obj_land_kz = document.getElementById('land_select');
	var obj_region_kz = document.getElementById('region_select');
	var obj_area_kz = document.getElementById('area_select');
	var param = '';
	var delim = '?';
	
	if (obj_land_kz) {
		if (obj_land_kz.selectedIndex > 0) {
			param = param + delim + 'land_kz=' + obj_land_kz[obj_land_kz.selectedIndex].value;
			delim = '&';
			if (obj_region_kz) {
				if (obj_region_kz.selectedIndex > 0) {
					param = param + delim + 'region_kz=' + obj_region_kz[obj_region_kz.selectedIndex].value;
					delim = '&';
					if (obj_area_kz) {
						if (obj_area_kz.selectedIndex > 0) {
							param = param + delim + 'area_kz=' + obj_area_kz[obj_area_kz.selectedIndex].value;
							delim = '&';
						}
					}
				}
			}
		}
	}
	if (param.length > 0) {
		
		var scriptpath = '/scripts/fh/updatecountryobjects.php' + param;
		var htmlhead = document.getElementsByTagName("head")[0];
		var script_id = "updatecobjects";
		
		if (document.getElementById(script_id)) {
			$('#' + script_id).remove();
		}
		
		var updatescript = document.createElement("script");
		updatescript.id = script_id;
		updatescript.src = scriptpath;
		htmlhead.appendChild(updatescript);
		
	}
	else {
		
		var countrylinks = document.getElementById('countrylinks');
		if (countrylinks) {
			do {
				knoten = countrylinks.firstChild;
				if (knoten) {
					document.getElementById('countrylinks').removeChild(knoten);
				}
			}
			while (knoten);
		}
	}
}
