// JavaScript Document

NS6 = (document.getElementById&&!document.all);
IE = (document.all);
NS = (navigator.appName=="Netscape" && navigator.appVersion.charAt(0)=="4");


//add wrap method to prototype Element object. In a later prototypejs version warp is already implemented
Element.addMethods({
	wrap: function(element, entityName, entityAttributes) {
		element = $(element);
		
		var wrapper = document.createElement(entityName);
		
		for(var key in entityAttributes)
		{
			var attribute = document.createAttribute(key);
			attribute.nodeValue = entityAttributes[key];
			
			wrapper.setAttributeNode(attribute);
		}
		
		element.parentNode.replaceChild(wrapper, element);
		wrapper.appendChild(element);
		
		return Element.extend(wrapper);
	},
	fetchStyles: function(element, keys) {
		var style = '';
		
		if(typeof keys == 'undefined')
		{
			keys = {
				'float':'',
				'margin':'', 
				'margin-top':'', 
				'margin-right':'', 
				'margin-bottom':'', 
				'margin-left':'', 
				'width':'', 
				'height':''
			};
		}
		
		for(var key in keys)
		{
			var value = element.getStyle(key);
			
			if((typeof value != 'undefined') && ('' != value))
			{
				style += key+':'+value+';';
			}
		}
		
		style += 'position:relative;top:0;left:0;overflow:hidden;display:block;';
		
		return style;
	}
});


// document ready
Event.observe(window, 'load', function(event){
	$$('a.accumulativeRequest').each(function(elem){
		Event.observe(elem, 'click', function(event){
			var offerId = $(event.target).readAttribute('rel');
			new Ajax.Request('ajax.php?page=page_merkzettel&req_offer=' + offerId,
				{	method: 'get',
					onSuccess: function(transport){
						if((200 === transport.status) && ('OK' === transport.statusText))
						{
							window.location.reload();
							return false;
						}
					}
				});
			return false;
		});
	});
	
	$$('a.accRequest').each(function(elem){
		Event.observe(elem, 'click', function(event){
			new Ajax.Updater('useradmin', 'ajax.php?page=page_acc_request', {
				method: 'get',
				onSuccess: function(transport){
					if((200 === transport.status) && ('OK' === transport.statusText))
					{
						$('useradmin').setStyle({'background-image': 'none'});
					}
				}
			});
			return false;
		});
	});
	
	$$('img[cp]').each(function(element){
		var wrapper = element.wrap('span', {'style': element.fetchStyles()});
		element.setStyle({
			'margin':0,
			'margin-top':0,
			'margin-right':0,
			'margin-bottom':0,
			'margin-left':0
		});

		var anchor = document.createElement('a');
		var attribute;
		
		attribute = document.createAttribute('href');
		attribute.nodeValue = '#';
		anchor.setAttributeNode(attribute);
		
		attribute = document.createAttribute('class');
		attribute.nodeValue = 'copyright';
		anchor.setAttributeNode(attribute);		
		
		attribute = document.createAttribute('title');
		attribute.nodeValue = element.readAttribute('cp');
		anchor.setAttributeNode(attribute);		
		
		anchor.innerHTML = 'Q';
		
		wrapper.appendChild(anchor);
	});	
});

//Mauskoordinaten
var docEl = (
             typeof document.compatMode != "undefined" && 
             document.compatMode        != "BackCompat"
            )? "documentElement" : "body";
var gblX = 0;
var gblY = 0;

function getwinY(){
	if (NS6)
			winY = window.pageYOffset;
	if (IE)
			//winY = document.documentElement.scrollTop;
			winY = window.event.screenY;
	if (NS)
			winY = window.pageYOffset;
	
	window.alert(winY);
	return winY;
	}

var infowin_visible = false;

var spElement = new Array()
function spFetchElement(id, win){
	
	if(!win)
		win = document;
		
	if(spElement[id]){
		return spElement[id];	
		}
	if (win.getElementById){
		spElement[id] = win.getElementById(id);
		}
	else if (win.all){
		spElement[id] = win.all[id];
		}
	else if (win.layers){
		spElement[id] = win.layers[id];
		}
	else if (win.forms[id]){
		spElement[id] = win.forms[id];
		}
	
	if(!spElement[id]){
		spElement[id] = win.forms[id];
		}

		
	return spElement[id];
	}

function _get_frame(frame_name){
	
	eval("var obj = parent." + frame_name);
	
	return obj;
	
	//return document.frames[frame_name];
	}


function _tbhover(objekt, color){
	if(color)
		objekt.style.backgroundColor=color;
	else
	objekt.style.backgroundColor='#F9E0AC';
	}

function _tbhoverout(objekt, color){
	//objekt.style.removeAttribute("backgroundColor", false);

	if(color)
		objekt.style.backgroundColor=color;
	else
		objekt.style.backgroundColor='#FCF3DC';
	}

function _faddanker(formname,name, v_link){
		
	if(!v_link)
		v_link = '';
	
	var element;
	element = spFetchElement(formname);
	
	//if(!element)
	//	element = document.forms[0];		
	
	element.action=v_link+'#'+name;
	
	}

function _del(text, v_link){
	if(window.confirm(text))
		location.href= v_link;
	}

function _del_form(text, v_link){
	if(window.confirm(text)){
		document.all.hotel_add.action = v_link;
		document.all.hotel_add.submit();
		}
	}

function _del_page(text, v_link){
	if(window.confirm(text)){
		location.href = v_link;
		}
	}

function _popup(v_link, width, height, switches, name){
	
	var add = '';
	
	if(!switches) switches = '';
	if(!name) name = 'popup_standart';
	
	
	if(switches.indexOf('s') != -1)
		add = add + ', scrollbars=yes';
	if(switches.indexOf('f') != -1)
		add = add + ', fullscreen=yes';
	if(switches.indexOf('v') != -1){
		width = screen.width;
		height = screen.height;
		add = add + ',status=yes,menubar=yes,toolbar=yes, scrollbars=yes, location=yes';
	}

	win = window.open(v_link, name, 'width='+width+',height='+height+',top=150,left=150' + add);
	win.focus();
	}

function _from_popup(v_link, close_window){
	
	if(close_window){
		opener.parent.location.href = v_link;
		self.close();
		return true;
		}

	var zahl = Math.floor(Math.random()*100);
	_popup(v_link, 800, 600, 'v', 'a' + zahl);
	self.focus();
	}

function _waitpopup(){
	
	var win = spFetchElement('waitlayer');

	if (NS6)
			winY = window.pageYOffset;
	if (IE)
			winY = document.documentElement.scrollTop;
	if (NS)
			winY = window.pageYOffset;
		
	wl_top =  winY;

	win.style.top = winY;
	win.style.visibility = 'visible';
	win.style.left = (screen.width - 1024) / 2;
	}



function f_preis_change(id, preis){
	
	var alter_preis = spFetchElement('gesammtpreis_db');
	var preis_ausgabe = spFetchElement('gesammtpreis');
	var feld = spFetchElement('zl['+id+']');
	var neuer_preis = 0;
	
	if(!feld.checked){
		preis = preis*-1;
		}
		
	neuer_preis = (alter_preis.value/10)*10 + preis;
	
	alter_preis.value = neuer_preis;
	
	preis_ausgabe.value = Math.round(neuer_preis/0.01)*0.01;
	}

function _kalender(){
		
	_popup('popup/kalender.php?anreise=' + spFetchElement('anreise').value + '&abreise=' + spFetchElement('abreise').value, 365, 230, '', 'popup_kalender');
		
	}

function _kalender_set(field, v_value){
	win = opener.parent.document;
	spFetchElement(field, win).value = v_value;

	window.close();
	}

function _form(formname, win, nr){
	
	if(!nr)
		nr = 0;
	if(!win)
		win = document;

	element = spFetchElement(formname, win);
	if(!element)
		return win.forms[nr];		

	return element;
	}

function _cms_bild(seite_element_id, nr, seite_id){
	_popup('popup.php?sub=cms_bild&seite_element_id=' + seite_element_id + '&nr=' + nr + '&seite_id=' + seite_id + '&bild_show=1&selected=bild', 760, 600, 's', 'cms_bild')
	}

function _cms_bild_update(seite_element_id, nr, seite_id, name){
	win = opener.parent.document;
	spFetchElement('p' + seite_element_id+'_'+nr, win).value = name;
	spFetchElement(seite_element_id + '_' +  nr + '_no', win).value = '0';
	_form('cmseladd', win, 0).action = '?save_posts&id=' + seite_id;
	_form('cmseladd', win, 0).submit();
	window.close();
	}
function _cms_no_pic(seite_element_id, nr){
	spFetchElement('p' + seite_element_id + '_' +  nr).value = 'images/cms/no_pic.gif';
	}


function _kal_anreise(object, win){
	
	var parts = object.value.split('.');
	
	var month_days = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	var i = 0; var day = parts[0]*1; var month = parts[1]*1; var year = parts[2]*1;
	while(i <14){
		
		day = day + 1;
		
		if(day > month_days[(month-1)]){
				month = month + 1;
				day = 1;
				}
		
		if(month > 12){
				month = 1;
				year = year + 1;
				}
		
		i = i + 1;
		}
	
	if(day < 10) day = '0' + day;
	if(month < 10) month = '0' + month;
	
	var vback = day + '.' + month + '.' + year;
	spFetchElement('sabreise', win).value = vback;
	}


function _angebot_bild_popup(){
	
	var woerter = spFetchElement('angebotbild').src.split("/");
	var pic_name = woerter[(woerter.length-1)];
	
	
	
	spFetchElement('angebotbildorg').src = spFetchElement('org_path').value + pic_name;
	var win = spFetchElement('angebotbildlayer');
	
	wl_top = get_winY();

	win.style.visibility = 'visible';
	win.style.top = get_winY();
	win.style.left = (screen.width - 1024) / 2;
	}

function _angebot_bild_popup_close(){
	var win = spFetchElement('angebotbildlayer');

	win.style.visibility = 'hidden';
	}

function _angebot_bild_big(bild){
	spFetchElement('angebotbild').src = bild;
	}

function _kal_move(month, day){
	objekt = spFetchElement('nav_day_' + day);
	objekt.style.backgroundColor='#cccccc';
	objekt = spFetchElement('nav_month_' + month);
	objekt.style.backgroundColor='#cccccc';

	}

function _kal_move_out(month, day){
	objekt = spFetchElement('nav_day_' + day);
	objekt.style.backgroundColor='#DCBA8A';
	objekt = spFetchElement('nav_month_' + month);
	objekt.style.backgroundColor='#DCBA8A';

	}


var mouse_is_down = false;
var X = 0;
var Y = 0;
var masterX = 0;
var masterY = 0;
var masterHand = 1;

function _kal_mouse_down(){
	l = spFetchElement('mauslayer');
	l.style.left = event.clientX;
	l.style.top = event.clientY;
	l.style.width = 1;
	l.style.height = 1;
	mouse_is_down = true;
	X = event.clientX;
	Y = event.clientY;
	
	}

function _kal_mouse_up(){
	
	mouse_is_down = false;

	l = spFetchElement('mauslayer');
	l.style.visibility = 'hidden'

	newX = event.clientX;
	newY = event.clientY;
	
	l.style.width = newX - X;
	l.style.height = newY - Y;
	
	if(newX - X < masterHand && newY - Y < masterHand){



		var i_master = 1;
		while(i_master < 7){
			var i = 1;
			while(i < 32){
				if((masterX + ((i-1)*20)) < X && ((i)*20) > (X-masterX) && (masterY + ((i_master-1)*21)) < Y && (masterY + (i_master*21)) > Y ){
					
					if(feld = spFetchElement('nav_day_' + i_master + '_' + i) ){
						
						if(feld.style.backgroundColor == '#ff0000')
							feld.style.removeAttribute("backgroundColor", false);
						else
							feld.style.backgroundColor = '#ff0000';
						}
					}
				/*
				else
					if(feld = spFetchElement('nav_day_' + i_master + '_' + i) )
						feld.style.removeAttribute("backgroundColor", false);
				*/
				i = i + 1;
				}
			i_master = i_master + 1;
			}




		}
}

function _kal_mouse_move(){

	newX = event.clientX;
	newY = event.clientY;

	if(mouse_is_down && newX - X > masterHand && newY - Y > masterHand){
	
	l = spFetchElement('mauslayer');
	l.style.visibility = 'visible';	
		
	var i_master = 1;
	while(i_master < 7){
		var i = 1;
		while(i < 32){
			if((masterX + ((i-1)*20)) < newX && ((i)*20) > (X-masterX) && (masterY + ((i_master-1)*21)) < newY && (masterY + (i_master*21)) > Y ){
				if(feld = spFetchElement('nav_day_' + i_master + '_' + i) )
					feld.style.backgroundColor='#FF0000';
				}
			/*
			else
				if(feld = spFetchElement('nav_day_' + i_master + '_' + i) )
					feld.style.removeAttribute("backgroundColor", false);
			*/
			i = i + 1;
			}
		i_master = i_master + 1;
		}

		l.style.width = newX - X;
		l.style.height = newY - Y;
	}
}

function _info(id)
{
	_info_on(id);
}

var showPopup		= true;
var timoutInstance	= null;

function _info_on(id)
{
	if(showPopup)
	{
		showPopup = false;
		timoutInstance = setTimeout('_info_on_timeout("'+id+'");', 1000);
	}
}

function _info_on_timeout(id)
{
	clearTimeout(timoutInstance);
	if(showPopup)
	{
		return false;
	}
	
	var win = spFetchElement('info');
	win.style.visibility = 'visible';
	var bounds = getBounds();
	var scrollPos = getScrollXY();
	if(!infowin_visible)
	{
		var divWidth = 300 ;
		var divHeight = 300 ;
		var posNewX;
		var posNewY;
		if (bounds.width < gblX + divWidth)
		{//horizontale Positionierung des Popups prüfen
			//+15 um das Div innerhalb evtl. vorhandener Scrollbars zu halten
			posNewX = (gblX - (divWidth +15 - (bounds.width - gblX)));
		}
		else
		{
			posNewX = gblX;
		}
		if (bounds.height + scrollPos.vertical< gblY + divHeight)
		{//vertikale Positionierung des Popups prüfen
			//+18 um das Div innerhalb evtl. vorhandener Scrollbars zu halten
			posNewY = gblY - (gblY +divHeight +18 - (scrollPos.vertical + bounds.height));
		}
		else
		{
			posNewY = gblY;
		}
		posNewX += 1;
		posNewY += 1;
		if (document.getElementById) 
		{
			document.getElementById('info').style.left  = posNewX + "px";
			document.getElementById('info').style.top = (posNewY)+ "px";
		}
		else if (document.all) 
		{// Stand 2010 sollte diese Bedingung in aktuellen Browsern nicht mehr treffen
			document.all.info.style.left = Ereignis.clientX;
			document.all.info.style.top = Ereignis.clientY;
		}
		 //_get_frame('info_frame').location.href = 'info_window.php?id=' + id;	

		 new Ajax.Updater('info', 'info_window.php?id=' + id, {
			 method: 'get',
			 onSuccess: function(data){
			 	infowin_visible = true;
			 }
		 });
	}
}

function _info_out(){
	clearTimeout(timoutInstance);
	showPopup = true;
	var win = spFetchElement('info');
	win.style.visibility = 'hidden';
	infowin_visible = false;
}

function _preis(preis){
	var fixed = preis.toFixed(2);
	return fixed.replace('.',',');
	}

function _buchung_popup(id){
	_popup('popup/info_res.php?id=' + id,380, 340, 'c', 'info_res');
	}
	
function _gallery_pic(v_link, caption){
		_popup(v_link,800, 600, 'c', 'gallery_pic');
		}

function _info_res(id){
		_popup('',400, 400, 'c', 'info_res');
		}

function _task_do(id){
	new Ajax.Updater('task_layer', 'task_do.php?id=' + id, { method: 'get' });
	}

function _ajax_load(name1){
	$(name1).innerHTML = "<center><BR><img src='../../../images/global/ajax-loader.gif' align='absmiddle'> &nbsp; <B>Bitte warten</B></center><BR><BR>";
	}

function init_mousemove() {
    if(document.layers) document.captureEvents(Event.MOUSEMOVE);
    document.onmousemove =	dpl_mouse_pos;
}

function dpl_mouse_pos(e) {

    gblX    =  e? e.pageX : window.event.x;
	gblY    =  e? e.pageY : window.event.y;

	if (document.all && !document.captureEvents) {
	    gblX    += document[docEl].scrollLeft;
	    gblY    += document[docEl].scrollTop;
    }
    	
    //if (document.layers) routeEvent(e);
}

function faq(id){
	window.alert(id);
	}

function _ablauf_finish(){
	window.alert('Bitte schließen Sie erst diesen Schritt ab');
	}

function _trenner(v_link){
	if(v_link.indexOf('?') != -1)
		return v_link + '&';
	else
		return v_link + '?';
	}

function getBounds() 
{
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) 
	{
	//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	}
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
	{
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
	{
	//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return {width: myWidth,height: myHeight};
}


function toggleVisibility(id, visibleStyle)
{
	if(typeof visibleStyle == 'undefined')
	{
		visibleStyle = 'block';
	}
	
	var element = document.getElementById(id);
	
	if(null != element)
	{
		element.style.display = ('none' == element.style.display) ?  visibleStyle : 'none';
	}
	
	return false;
}

function getScrollXY() 
{
    var scrOfX = 0, scrOfY = 0;
 
    if( typeof( window.pageYOffset ) == 'number' ) 
    {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
	} 
    else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) )
    {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    }
    else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) 
    {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
	return { vertical: scrOfY, horizontal: scrOfX };
}

function checkBackendUser(form)
{
	var form = spFetchElement(form);
	
	var selector = new Selector('#user_username');
	var elements = selector.findElements(form);
	
	if(0 < elements.length)
	{
		var name = elements[0].value.toUpperCase();
		
		if(name.startsWith('VW-H') || name.startsWith('VW-A'))
		{
			form.action = 'http://vw-systems.de/admin/login.php';
			form.target = '_blank';
		}
	}
	
	form.submit();
}