//<![CDATA[
var $loginbox;
var $login;

var $bookings = new Array();
var $maxdays = 34;
var $day = 60*60*24;
var $parameter;
$parameter = new Object({
	bland : 'test',	
	kalstart_mon: '',
	kalstart_year: '',
	input_start: '',
	input_end: '',
	showCal: 'true'	
});

var $tool = 'setStart';
var $start = 0;
var $end = 0;
function getBigCal() {		
	$parameter['input_start'] = $($('zeitraumvon')).value;
	$parameter['input_end'] = $($('zeitraumbis')).value;	
	$parameter['ID'] = $($('cal_form_fewo_id')).value;
	$url = 'http://www.fewo-ferienwohnung-schwarzwald.de/getFewoCal_new.php';
	new Ajax.Updater('belegungskalcontainer', $url, {
		method: 'post',
		parameters: $parameter,
		evalScripts: true
	
	});

}

function countHpClicks($fewo_id) {
	//new Ajax.Request


}

function getSchoolHolidays($p_num) {	
	$parameter['bland'] = $p_num;
	getBigCal();

}

function doconvDate($p_tstamp) {
	if(!Object.isNumber($p_tstamp)) {
		return '';
	}
	
	if($p_tstamp < 1000) {
		return '';
	}
	var $temp1 = new Date(parseInt($p_tstamp)*1000);	
	$rday = String($temp1.getDate());
	$rmonth = String($temp1.getMonth()+1);
	$ryear = $temp1.getFullYear();	
	if($rday.length < 2) {
		$rday = '0'+$rday;
	}
	if($rmonth.length < 2) {
		$rmonth = '0'+$rmonth;
	}
	return $rday+'.'+$rmonth+'.'+$ryear;	
}

function handle($p_id) {
		$($('kalerrormsg')).hide();		
		if($tool == 'setStart') {			
			$($($p_id)).setStyle({ backgroundColor: 'yellow' });
			$start = $p_id;			
			$tool = 'setEnd';
			$($('tool_setStart')).setStyle( { backgroundColor: ''});
			$($('tool_setEnd')).setStyle( { backgroundColor: 'yellow'});
			if(parseInt($end) > parseInt($start)) {
				$to = colorPeriod(parseInt($start)+$day, parseInt($end));				
				$end = $to;								
			}
			if(parseInt($start) > parseInt($end)) {
				$end = 0;
				$($('zeitraumbis')).value = doconvDate($end);
			}
			
			$($('zeitraumvon')).value = doconvDate(parseInt($start));
			$($('zeitraumbis')).value= doconvDate(parseInt($end));
			
		}else if($tool == 'setEnd') {			
			var $from = parseInt($start);
			var $to = parseInt($p_id);
						
			
			if((($to-$from)/$day) > $maxdays) {
				$to = $from + $maxdays*$day;
			}

			
			if($to > $end) {				
				$to = colorPeriod($from+$day, $to);		
			}else{				
				unsetPeriod($from, $to);				
				$to = $end;
			}			

			$end = $to;
			$($('zeitraumbis')).value= doconvDate(parseInt($end));
			$($('zeitraumvon')).value = doconvDate(parseInt($start));
		}
}

function changeTool($p_tool_to) {	
	if($p_tool_to == 'setStart') {		
		$temp = $end;
		if($start != 0) {
			unsetPeriod(-$end, $start-$day);
		}
		$end = $temp;
		$start = 0;		
		$tool = $p_tool_to;
		$($('tool_setEnd')).setStyle( { backgroundColor: ''});
		$($('tool_setStart')).setStyle( { backgroundColor: 'yellow'});
	}else if($p_tool_to == 'Clear') {
		if($start != 0) {
			unsetPeriod(-$end, $start-$day);
		}
		$start = $end = 0;
		$($('tool_setEnd')).setStyle( { backgroundColor: ''});
		$($('tool_setStart')).setStyle( { backgroundColor: 'yellow'});
		$tool = 'setStart';
	}else if($start != 0) {
		$($('tool_setStart')).setStyle( { backgroundColor: ''});
		$($('tool_setEnd')).setStyle( { backgroundColor: 'yellow'});
		$tool = 'setEnd';
	}
	
}

function colorPeriod($p_from, $p_to) {
	$act = $p_from;
	$period_days = 0;
	while($act <= $p_to) {		
		if($bookings.indexOf(String($act)) != -1) {
			return $act-$day;
		}
	
		try {
			$($(String($act))).setStyle({ backgroundColor: 'yellow'});
		} catch (e) {}

		$act = $act+$day;
		
		
		$period_days++;
		if($period_days > $maxdays) {
			return $act-$day;
		}
	}
	return $act-$day;

}

function unsetPeriod($p_from, $p_to) {
	while($end > $p_to && $end > $p_from) {		
			try {
				$($(String($end))).setStyle({ backgroundColor: ''});
			} catch (e) {}				
		
		$end = $end-$day;
	}
}

window.onload = function() {
	if(typeof(load) == 'function') {
		load();
	}
	$loginbox = document.getElementById('loginbox');
	$login = document.getElementById('login');		
}

function toggleLogin() {		
	if($loginbox.style.display == 'none') {
		$loginbox.style.display = 'block';
	}else{
		$loginbox.style.display = 'none';
		$login.style.backgroundColor = '';
		$login.style.color = '#000000';			
	}			
}

function getRandom( min, max ){
	if( min > max ) {
	return( -1 );
	}
	if( min == max ) {
	return( min );
	}
	return( min + parseInt( Math.random() * ( max-min+1 ) ) );
}
function reloadCaptcha() {
	var codeString = '<img src="/homepagebaukasten/webseite/captcha/captcha_new.php?color1=FFFFFF&amp;color2=7489B4&amp;captchawidth=220&amp;captchaheight=50&amp;dummy=';
	codeString = codeString + getRandom(0,99999) + '">';
	$($('sicherheitsabfrage')).innerHTML = codeString;
	$($('sicherheitsabfrage')).appear({ duration: 1 });
}

//anfrage senden
function sendForm()  {
	//check ob iputs gemacht wurden
	var $filled = true;
	if(!Form.Element.present('name')) {
		$($('ername')).appear({ duration: 0.3});
		$($('name')).observe('focus', 
		function() { 
			$($('ername')).fade({ duration: 0.1});
			$($('name')).stopObserving('focus');

		} );
		$filled = false;
	}
	
	if(!Form.Element.present('email') || $($('email')).value.indexOf('@') === -1 || $($('email')).value.indexOf('.') === -1) {
		$($('eremail')).appear({ duration: 0.3});
		$($('email')).observe('focus', 
		function() { 
			$($('eremail')).fade({ duration: 0.1});
			$($('email')).stopObserving('focus');

		} );
		$filled = false;
	}
	/*
	new Ajax.Request('http://www.fewo-ferienwohnung-schwarzwald.de/homepagebaukasten/webseite/captcha_check.php?captloesung='+$($('captloesung')).value, {
		method:'get',
		onSuccess: function(transport) {
			captchacheck = transport.responseText;
		}
	});
	*/
	/*
	if (captchacheck==0) {
		$($('ercaptcha')).appear({ duration: 0.3});
		$filled = false;
	}
	*/
	
	if($filled) {
		showMessage('anfragemessage');
		$($('anfrageformular')).request({
		onComplete: function($p_transponder) {			
			$t_text = $p_transponder.responseText;
			$error = $t_text.substring($t_text.indexOf('&error=')+7, $t_text.indexOf('&message='));			
			$message = $t_text.substring($t_text.indexOf('&message=')+9, $t_text.indexOf('&regmessage='));
			$regmessage = $t_text.substring($t_text.indexOf('&regmessage=')+12, $t_text.length);
			
			if($error == 'false') {
				$($('anfragemessage')).empty();
				$($('anfragemessage')).update('<h1 class="headline fgreen">Anfrage erfolgreich gesendet!</h1>'+$message+'<p>&nbsp;</p>'+$regmessage);
				$($('btnsend')).empty();
				$($('btnsend')).update('Anfrage erfolgreich gesendet.');
				$($('btnsend')).onclick = function () { }
			}else{
				$($('anfragemessage')).empty();
				$($('anfragemessage')).update('<h1 class="headline fgreen">Fehler aufgetreten!</h1>'+$message+'<p>&nbsp;</p>'+$regmessage);
			}
			killMessage.$item = 'anfragemessage'
			$($('body')).observe('click', killMessage);	
			
		},
		onFailure : function() {				
				$($('anfragemessage')).innerHTML = 'Fehler bei der Daten&uuml;bertragung!<p>Bitte versuchen Sie es sp&auml;ter nochmal.</p><p>Klicken Sie hier um diese Nachricht zu schliessen</p>';
				killMessage.$item = 'anfragemessage'
				$($('body')).observe('click', killMessage);	
			}		
		});		
	}
	
}

function updateContent($p_obj) {
	
	if(!Form.Element.present('name')) {	
		$($('name')).value = $p_obj.aname;
	}
	if(!Form.Element.present('email')) {	
		$($('email')).value = $p_obj.aemail;
	}
	if(!Form.Element.present('telefon')) {	
		$($('telefon')).value = $p_obj.atelefon;
	}
	if(!Form.Element.present('strasse')) {	
		$($('strasse')).value = $p_obj.astrasse;
	}
	if(!Form.Element.present('ort')) {	
		$($('ort')).value = $p_obj.aort;
	}
	if(!Form.Element.present('plz')) {	
		$($('plz')).value = $p_obj.aplz;
	}
}
//]]> 
