/*
// File written by Jochen "Khuri" Höhmann <khuri@khuris.com>
// Copyright 2010
//
// File        : shared.js
// Begin       : 2010.01.26 17:46:02
// Last Update : 2010.07.22 13:31:36
*/

// <![CDATA[
var request = null;
var clientbrowser = navigator.userAgent.toLowerCase();
var is_ie = ((clientbrowser.indexOf("msie") != -1) && (clientbrowser.indexOf("opera") == -1));
var is_nav = ((clientbrowser.indexOf('mozilla')!=-1) && (clientbrowser.indexOf('compatible') == -1));
var ie_version = getInternetExplorerVersion();

function getInternetExplorerVersion() {
	// From MSDN
	// Returns the version of Internet Explorer or a -1
	// (indicating the use of another browser).
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer') {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

// Handle onload events
function addonload(func) {
  var oldonload = window.onload;
  if(typeof window.onload != 'function') {
    window.onload = func;
  }
  else {
    window.onload = function() {
      if(oldonload) {
        oldonload();
      }
      func();
    }
  }
}

// Statusbox
function statusbox_close() {
	document.getElementById('statusbox').style.visibility='hidden';
	document.getElementById('statusbox').style.display='none';
}

/* Go somewhere else */
function goto(where,wwindow) {
	if(wwindow != 0 && typeof wwindow != "undefined") {
		if(where != 0) {
			if(wwindow.closed == true || !wwindow.document) {
				wwindow = window.open(where,wwindow);
			}
			wwindow.location.href=where;
		}
		wwindow.focus();
	}
	else {
		window.location.href=where;
	}
}

/* Confirm stuff */
function confirm_check(id) {
	check_opt = confirm("Sind Sie sicher, dass Sie diese Option markieren wollen?");
	document.getElementById(id).checked = (check_opt) ? true : false;
}
function confirm_action(what,where) {
	checki = confirm(what);
	if(checki == true) {
		goto(where);
	}
}

/* Calendar functions */
function calendar_draw(box,iswitch,year,month) {
	document.getElementById('calendar_'+box).value = document.getElementById('calendar_'+box).value.replace(/^\s*(S)\s*$/, '$1');
	if(calendar_checkdate(document.getElementById('calendar_'+box).value)) {
		if(document.getElementById('calendar_'+box).value != '') {
			var fieldvalue = document.getElementById('calendar_'+box).value.replace(/(19|20)?([0-9]{2})[\- \/.](1[012]|0?[1-9])[\- \/.](3[01]|[12][0-9]|0?[1-9])(?:[\- \/.]([01][0-9]|2[0-4])[:.]([0-5][0-9]))?/ig,"$1$2-$3-$4-$5-$6").split("-");
			if(!cdate) {
				var cdate = new Date(fieldvalue[0],(fieldvalue[1] -1),1);
			}
		}
		// only check display setting if required
		if(iswitch > 0 && iswitch < 3) {
			var cblock = (iswitch == 1) ? document.getElementById('calendarbox_'+box).style.display : "block";
			if(document.getElementById('calendarbox_on')) {
				document.getElementById('calendarbox_on').style.display="none";
			}
			if(document.getElementById('calendarbox_off')) {
				document.getElementById('calendarbox_off').style.display="none";
			}
			document.getElementById('calendarbox_'+box).style.display=(cblock == "block") ? "none" : "block";
			if(iswitch == 2) {
				return;
			}
		}
		// only 3 possible options, existing date is valid, invalid or the click is simply a date switch
		if(year > 0 && month >= 0) {
			var cdate = new Date(year,month,1);
		}
		if(iswitch < 3) {
			if(!cdate) {
				var cdate = new Date(new Date().getFullYear(),new Date().getMonth(),1);
			}
			var month_array = new Array("Januar","Februar","M&auml;rz","April","Mai","Juni","Juli","August","September","Oktober","November","Dezember");
			var cday = cdate.getDate();
			var cmonth = cdate.getMonth();
			var cyear = cdate.getFullYear();
			var cwday = cdate.getDay();
			cwday = (cwday == 0) ? 7 : cwday;
			// calculate days of month
			var cmdays = 31;
			while(cmonth != new Date(cyear,cmonth,cmdays).getMonth()) {
				cmdays--;
				if(cmdays == 0) break;
			}
			if(cblock != "block") {
				// clean calendar box and start drawing, and because i'm lazy it's all innerHTML only
				var cbox = '';
				cbox += '<table class="calendar" cellpadding=0 cellspacing=1><thead><tr>';
				cbox += '<th class="switch" onclick="calendar_draw(\''+box+'\',0,'+(((cmonth -1) > -1) ? cyear : (cyear -1))+','+(((cmonth -1) > -1) ? (cmonth -1) : 11)+');">&lt;&lt;<\/th>';
				cbox += '<th colspan=5>'+month_array[cmonth]+' '+cyear+' <span>';
				cbox += '<a onclick="calendar_draw(\''+box+'\',0,'+(cyear +1)+','+(cmonth)+');">+<\/a>|<a onclick="calendar_draw(\''+box+'\',0,'+(cyear -1)+','+(cmonth)+');">-<\/a>';
				cbox += '<\/span><\/th>';
				cbox += '<th class="switch" onclick="calendar_draw(\''+box+'\',0,'+(((cmonth +1) < 12) ? cyear : (cyear +1))+','+(((cmonth +1) < 12) ? (cmonth +1) : 0)+');">&gt;&gt;<\/th>';
				cbox += '<\/tr><\/thead>';
				cbox += '<tbody><tr>';
				// Weekday names
				for(i=0;i<7;i++) {
					cbox += '<th>'+"MDMDFSS".substr(i,1)+'<\/th>';
				}
				cbox += '<\/tr><tr>';
				// Weeks and days
				for(i=1;i<=42;i++) {
					// do markings
					showclass = '';
					showclass += (i-cwday +1 == new Date().getDate() && cmonth == new Date().getMonth() && cyear == new Date().getFullYear()) ? ' today' : '';
					showclass += (i%7==0) ? ' sunday' : '';
					showclass += ((fieldvalue) ? ((i-cwday +1 == fieldvalue[2] && cmonth == (fieldvalue[1] -1) && cyear == fieldvalue[0]) ? ' markday' : '') : '');
					cbox += '<td class="'+showclass+(((i-cwday +1 >0) && (i-cwday +1<=cmdays))?'':' inactive')+'" '+(((i-cwday +1 >0) && (i-cwday +1<=cmdays))?'onclick="this.parentNode.parentNode.lastChild.firstChild.firstChild.value=\''+cyear+'-'+((cmonth +1 < 10) ? '0' : '')+(cmonth +1)+'-'+((i-cwday +1 < 10) ? '0' : '')+(i-cwday +1)+'\';this.parentNode.parentNode.lastChild.firstChild.childNodes[1].select();this.parentNode.parentNode.lastChild.firstChild.childNodes[1].focus();calendar_markday(this);"' : '')+'>'+(((i-cwday>=0) && (i-cwday<cmdays)) ? (i-cwday +1) : '&nbsp;')+'<\/td>';
					cbox += (i%7==0 && i<36) ? '<\/tr><tr>' : '';
					// terminate box if we're about to draw an empty row
					if(i%7==0 && (i-cwday +1>=cmdays)) {
						break;
					}
				}
				cbox += '<\/tr><tr>';
				// create the timefields
				cbox += '<th colspan=3><input type="hidden" name="calendar_date" value="'+((fieldvalue) ? (fieldvalue[0]+'-'+fieldvalue[1]+'-'+fieldvalue[2]) : cyear+'-'+((cmonth+1 < 10) ? '0'+cmonth+1 : cmonth+1)+'-'+((cday < 10) ? '0'+cday : cday))+'">';
				cbox += '<input type="text" size="2" maxlength="2" class="courier" name="calendar_hour" value="'+((fieldvalue) ? ((fieldvalue[3] != '') ? fieldvalue[3] : 15) : 15)+'" tabindex=1> : <input type="text" size="2" maxlength="2" class="courier" name="calendar_min" value="'+((fieldvalue) ? ((fieldvalue[4] != '') ? fieldvalue[4] : '00') : '00')+'" tabindex=2><\/th>';
				cbox += '<th colspan=4><button type="button" class="set" onclick="calendar_adddate(\''+box+'\',this.parentNode.parentNode.firstChild.childNodes[0].value+\' \'+this.parentNode.parentNode.firstChild.childNodes[1].value+\':\'+this.parentNode.parentNode.firstChild.childNodes[3].value);" tabindex=3>setzen</button><\/th>';
				// end the table
				cbox += '<\/tr><\/table>';
				document.getElementById('calendarbox_'+box).innerHTML = cbox;
			}
		}
	}
}
function calendar_adddate(box,cstring) {
	document.getElementById('calendar_'+box).value=cstring;
	calendar_draw(box,2);
}
function calendar_markday(who) {
	for(i=1;i<=6;i++) {
		for(z=0;z<who.parentNode.parentNode.childNodes[i].childNodes.length;z++) {
			if(who.parentNode.parentNode.childNodes[i].childNodes[z].tagName.toLowerCase() == "td") {
				newclass = who.parentNode.parentNode.childNodes[i].childNodes[z].className.replace(/markday/ig, "");
				who.parentNode.parentNode.childNodes[i].childNodes[z].className = newclass+((who.parentNode.parentNode.childNodes[i].childNodes[z].innerHTML == who.innerHTML) ? " markday" : "");
			}
		}
	}
}
function calendar_checkdate(cstring) {
	if(/^(?:(19|20)?([0-9]{2})[\- \/.](1[012]|0?[1-9])[\- \/.](3[01]|[12][0-9]|0?[1-9])(?:[\- \/.]([01][0-9]|2[0-4])[:.]([0-5][0-9])))$/i.test(cstring) || cstring == "") {
		return true;
	}
	alert("Ihr Datum ist leider ungültig.\nBitte verwenden Sie folgendes Format:\nJJJJ-MM-TT HH:MM");
	return false;
}

// Object fading
var banner_fadespeed = 50;
var banner_fademultiply = 20;
var banner_targetimg;
var fadelock = 0;

function opac(i,init) {
	fadelock = 1;
	var elmnt = banner_targetimg.style;
	elmnt.opacity = (i / banner_fademultiply);
	elmnt.MozOpacity = (i / banner_fademultiply);
	elmnt.KhtmlOpacity = (i / banner_fademultiply);
	elmnt.filter = "alpha(opacity="+(i * (1 / banner_fademultiply * 100))+")";
	if(init > 0) {
		if(init == 2) {
			for(i=0;i<=banner_fademultiply;i++) {
				setTimeout("opac("+i+",0);",i * banner_fadespeed);
			}
		}
		else {
			for(i=banner_fademultiply;i>=0;i--) {
				setTimeout("opac("+i+",0);",(banner_fademultiply - i) * banner_fadespeed);
			}
		}
	}
	if(i == banner_fademultiply) {
		fadelock = 0;
		return true;
	}
}


// AJAX functions
if(window.XMLHttpRequest) {
	request = new XMLHttpRequest();
}
else if(window.ActiveXObject) {
	try {
		request = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e) {
		try {
			request = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e) {
			// no ajax support
		}
	}
}
// ]]>