timeOutID = 0;

function searchChangeStars() {
	if($('select#sterne option:selected').attr('value')) {
		$("span#superior").slideDown('slow');
	} else {
		$("span#superior").slideUp('slow');
	}
}

function searchChangeArea(area) {
	if($('div#' + area).css('display') == 'none') {
		//if(!$('div#' + area + 'Multibox').html()) {
			searchCall(area);
		//}
		$('div#' + area).slideDown('slow');
		$('a#' + area + 'Label').html('Auswahlliste&nbsp;schlie&szlig;en');
	} else {
		$('div#' + area).slideUp('slow');
		$('a#' + area + 'Label').html('Auswahlliste&nbsp;&ouml;ffnen');
	}
}

function searchCountSelected(area) {
	var selectedCount = 0;
	var allCount = 0;
	$('div#' + area + ' input').each(function() {
		if($(this).attr('checked')) {
			selectedCount++
		}
		allCount++
	});
	if(area=='rfw') {
		var singularLabel = 'Route';
		var pluralLabel = 'Routen';
	} else {
		//var singularLabel = 'Region';
		//var pluralLabel = 'Regionen';
		var singularLabel = '';
		var pluralLabel = '';
	}
	$('span#' + area + 'SelectedCount').html(selectedCount + ' ' + (selectedCount==1 ? singularLabel : pluralLabel));
	$('span#' + area + 'AllCount').html(allCount+'');
}

function searchOptions(area, areaDest) {
	$.get('search' + areaDest + '.php', 
		{search: $('select#' + area + ' option:selected').attr("value")},
		function(data){
			if(data) {
				$('select#' + areaDest).html(data);
				$('div#' + areaDest + 'Div').slideDown('slow');
			} else {
				$('div#' + areaDest + 'Div').slideUp('slow');
			}
		}
	);
}

function searchList(area) {
	if($('div#' + area).css('display') == 'none') {
		$('div#' + area + 'Multibox').html('<div style="width:100%; text-align:center; padding-top:20px;">Lade Daten...<br /><img src="/bub/images/loading.gif"></div>');
		$('div#' + area).slideDown('slow');
		$('a#' + area + 'Label').html('Auswahlliste schlie&szlig;en');
	}
	window.clearInterval(timeOutID);
	timeOutID = window.setTimeout("searchCall('"+area+"')", 2100);
}

function searchCall(area) {
	$('div#' + area + 'Multibox').html('<div style="width:100%; text-align:center; padding-top:20px;">Lade Daten...<br /><img src="/bub/images/loading.gif"></div>');
	$.get('/bub/search' + area + '.php', 
		{search: $('input#' + area + 'Input').attr('value')},
		function(data){
			$('div#' + area + 'Multibox').html(data);
			searchCountSelected(area)
		}
	);
	window.clearInterval(timeOutID);
}

function searchCheckZipForm() {
	var errorMsg = '';
	if(!$('form.zipForm input#zip').attr('value')) {
		errorMsg += 'Bitte das PLZ/Ort Feld ausf&uuml;llen!<br />';
	}
	if(errorMsg) {
		showError(errorMsg);
		return false;
	} else {
		showLoader();
		return true;
	}
}

function searchCheckTourForm() {
	var errorMsg = '';
	var foundCheckedCheckboxes = false;
	var foundCheckboxes = false;
	$('form.tourForm input').each(function() {
		if($(this).attr('type') == 'checkbox') {
			foundCheckboxes = true;
			if($(this).attr('checked')) {
				foundCheckedCheckboxes = true;
			}
		}
	});
	if(!foundCheckboxes) {
		errorMsg += 'Bitte einen Radfernweg eingeben bzw. aus der Liste ausw&auml;hlen!<br />';
	} else if(!foundCheckedCheckboxes) {
		errorMsg += 'Bitte mindestens einen Radfernweg ausw&auml;hlen!<br />';
	}
	if(errorMsg) {
		showError(errorMsg);
		return false;
	} else {
		showLoader();
		return true;
	}
}

function searchCheckExtendedForm() {
	var errorMsgs = '';
	
	//FILL CHECK
	var foundValue = false;
	$('form.extendedForm input').each(function() {
		if($(this).attr('type') == 'checkbox' && $(this).attr('checked')) {
			foundValue = true;
		}
		if($(this).attr('type') == 'text' && $(this).attr('value')) {
			foundValue = true;
		}
	});
	$('form.extendedForm select').each(function() {
		if($(this).attr('value')) {
			foundValue = true;
		}
	});
	if(!foundValue) {
		errorMsgs += 'Bitte etwas ausf&uuml;llen!<br />';
	}
	
	//UTM CHECK
	var utmzone = $('form.extendedForm input#utmzone').attr('value');
	var utmx = $('form.extendedForm input#utmx').attr('value');
	var utmy = $('form.extendedForm input#utmy').attr('value');
	if((utmzone || utmx || utmy) && (!utmzone || !utmx || !utmy)) {
		errorMsgs += 'Bitte ALLE UTM-Felder mit Zahlen-Werten f&uuml;llen!<br />';
	}
	if(utmx || utmy) {
		if(!parseFloat(utmx) && utmx) {
			errorMsgs += 'Das Feld "UTM-Rechtswert" muss eine Zahl enthalten!<br />';
		}
		if(!parseFloat(utmy) && utmy) {
			errorMsgs += 'Das Feld "UTM-Hochwert" muss eine Zahl enthalten!<br />';
		}
	}
	
	//ZIP CHECK
	var zip = $('form.extendedForm input#plz').attr('value');
	if (!zip) {
		;
	} else {
		if (zip || zip.charAt(0) == '0') {
			if(!parseInt(zip) && zip.charAt(0) != '0') {
				errorMsgs += 'Das Feld "PLZ" muss eine Zahl enthalten!<br />';
			}
		}
	}
	
	//ROOMS CHECK
	var rooms = $('form.extendedForm input#zimmer').attr('value');
	if(!parseInt(rooms) && rooms) {
		errorMsgs += 'Das Feld "Zimmer" muss eine Zahl enthalten!<br />';
	}
	
	//BEDS CHECK
	var beds = $('form.extendedForm input#betten').attr('value');
	if(!parseInt(beds) && beds) {
		errorMsgs += 'Das Feld "Betten" muss eine Zahl enthalten!<br />';
	}
	
	//PRICE CHECKS
	var price = $('form.extendedForm select#preis').attr('value');
	var from = $('form.extendedForm input#von').attr('value');
	var to = $('form.extendedForm input#bis').attr('value');
	if(	(price && !(to || from)) || ((to || from) && !price)) {
		errorMsgs += 'Bitte das Preis-Select-Men&uuml; und die "von"- und/oder "bis"-Felder ausf&uuml;llen!<br />';
	}

	if(parseInt(from) > parseInt(to)) {
		errorMsgs += 'Das Feld "von" muss kleiner als das Feld "bis" sein!<br />';
	} 
	if(from != '0' && !parseFloat(from) && from) {
		errorMsgs += 'Das Feld "von" muss eine Zahl enthalten!<br />';
	}
	if(to != '0' && !parseFloat(to) && to) {
		errorMsgs += 'Das Feld "bis" muss eine Zahl enthalten!<br />';
	}
	
	//RFW CHECKS
	var rfwentf = $('form.extendedForm input#rfwentf').attr('value');
	var land = $('select#land').attr('value');
	var raum = $('form.extendedForm input#naturraumInput').attr('value');
	rfwFound = 0;
	
	$('div#rfw input').each(function() {
		if($(this).attr('checked')) {
			rfwFound++
		}
	});
	if (rfwentf && rfwFound == 0) {
		errorMsgs += 'Bitte mindestens einen Radfernweg ausw&auml;hlen!<br />';
	}
	if((!rfwFound && !land) && !raum) {
		//errorMsgs += 'Bitte mindestens einen Radfernweg ausw&auml;hlen und die Entfernung ausf&uuml;llen!<br />';
		errorMsgs += 'Bitte mindestens ein Land ausw&auml;hlen!<br />';
	}
	if (rfwentf) {
		rfwentf = rfwentf.replace(/,/g, ".");
	}
	if(!parseFloat(rfwentf) && rfwentf) {
		if (rfwentf != '0') {
			errorMsgs += 'Das Feld "Entfernung" muss eine Zahl enthalten!<br />';
		}
	}

	//ERROR OR SUCCESS
	if(errorMsgs) {
		showError(errorMsgs);
		return false;
	} else {
		showLoader();
		return true;
	}
}

function showError(msg) {
	scroll(0,0)
	$('div#errorBox').slideUp('fast',function() {
		$('div#errorBox').html(msg);
		$('div#errorBox').slideDown('fast');
	});	
}

function showLoader() {
	if (navigator.appName.indexOf("Explorer") != -1) {
		$('select').each(function() {
			$(this).fadeOut('medium');
		});
		jQuery('.loadingImage').css("position", 'absolute');
		jQuery('.loadingImage').css("top", '280');
		scroll(0,0);
	}
	$('div.multibox').each(function() {
		$(this).slideUp('medium');
	});
	var masterHeight = document.getElementById('master').clientHeight
	var bodyHeight = document.getElementsByTagName('html')[0].clientHeight
	if(masterHeight > bodyHeight) {
		browserHeight = masterHeight;
	} else {
		browserHeight = bodyHeight;
	}
	jQuery('.loadingContainer').css("height", browserHeight)
	jQuery('.loadingContainer').fadeIn('medium');
}

function hideDiv(div) {
	$('div.' + div).slideUp('medium');
}

function showDiv(div) {
	$('div.' + div).slideDown('medium');
	$('div.' + div + ' input').each(function() {
		$(this).attr('checked', true);
	});	
}

function collapseDiv(area) {
	if($('div#' + area).css('display') == 'none') {
		$('div#' + area).slideDown('slow');
		$('a#' + area + 'Label').html('Ihre aktuellen Suchkriterien schlie&szlig;en');
	} else {
		$('div#' + area).slideUp('slow');
		$('a#' + area + 'Label').html('Ihre aktuellen Suchkriterien anzeigen');
	}
}
