var mailregex = /^[0-9a-zA-Z][0-9a-zA-Z\._-]*@([0-9a-zA-Z-_]+\.)+([a-z]{2,6})$/;
var dateregex = /^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{4}$/;
var numregex = /^[0-9]+$/;

function popupNormal(sUrl, sWidth, sHeight)
{
	var popup;

	popup = window.open(sUrl, 'PressRelease','toolbar=yes,location=no,directories=no,status=no,menubar=no,scrollbars=yes,screenX=10,screenY=10,resizable=yes,width=' + sWidth + ',height=' + sHeight, true);
	if(navigator.appName.indexOf('Netscape') != -1)
		popup.focus();
}

function popupHelp(sUrl, sWidth, sHeight)
{
	var popup;

	popup = window.open(sUrl, 'PressRelease','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=auto,screenX=10,screenY=10,resizable=yes,width=' + sWidth + ',height=' + sHeight, true);
	if(navigator.appName.indexOf('Netscape') != -1)
		popup.focus();
}

function popupPicture(sUrl, sWidth, sHeight)
{
	var popup;
	//var sWidth=sWidth+10;
	//var sHeight=sHeight+10;
	
	popup = window.open(sUrl, 'PictureViewer','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,screenX=10,screenY=10,resizable=yes,width=' + sWidth + ',height=' + sHeight, true);
	if(navigator.appName.indexOf('Netscape') != -1)
		popup.focus();
}

function cms_login() {
	window.open("/cms/login.php", "login", "width=450,height=300,resizable=yes");
}

function konsole(konsurl,konswidth,konsheight) {
	kons = window.open(konsurl, "zoom", "width="+konswidth+",height="+konsheight+"location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizable=no'");
	if(kons.focus)kons.focus();
}

function mifi(missifi) {
	if (missifi!="") {
		alert("Bitte folgende Felder noch vollständig ausfüllen:\n " + missifi);
		return false;
	}
	return true;
}

function check_mailthispage() {
	var missing_fields="";
	if (!mailregex.test(document.forms[0].email_recipient.value)) missing_fields+="\n- Emailadresse Empfänger";
	if (!mailregex.test(document.forms[0].email_sender.value)) 	missing_fields+="\n- Emailadresse Absender";
	return mifi(missing_fields);
}

function check_eventvorschlag() {
	var missing_fields="";
	if (document.forms[0].event_title.value=="") 	missing_fields+="\n- Event Titel";
	if (document.forms[0].desc.value=="") 	missing_fields+="\n- Event Beschreibung";
	if (document.forms[0].event_type.options[document.forms[0].event_type.selectedIndex].value=="none") missing_fields+="\n- Event Typ";
	if (document.forms[0].organizer.value=="") 	missing_fields+="\n- Veranstalter Name/Firma";
	if (document.forms[0].address.value=="") 	missing_fields+="\n- Adresse";
	if (document.forms[0].phone.value=="") 	missing_fields+="\n- Telefon";
	return mifi(missing_fields);
}

function check_bnl() {
	var missing_fields="";
	if (document.forms[0].name.value=="") 	missing_fields+="\n- Name";
	if (document.forms[0].betrieb.value=="") 	missing_fields+="\n- Betrieb";
	if (document.forms[0].adresse.value=="") 	missing_fields+="\n- Adresse";
	if (!mailregex.test(document.forms[0].email.value)) 	missing_fields+="\n- eMail";
	return mifi(missing_fields);
}

function check_kundenanmeldung() {
	var missing_fields="";
	if (document.forms[0].name.value=="")		missing_fields+="\n- Name";
	if (document.forms[0].betrieb.value=="")	missing_fields+="\n- Betrieb";
	if (document.forms[0].adresse.value=="")	missing_fields+="\n- Adresse";
	if (!mailregex.test(document.forms[0].email.value)) 	missing_fields+="\n- eMail";
	if (document.forms[0].phone.value=="")		missing_fields+="\n- Telefon";
	if (document.forms[0].username.value=="")	missing_fields+="\n- gew. Benutzername";
	if (document.forms[0].password.value=="")	missing_fields+="\n- gew. Passwort";
	if (document.forms[0].password_conf.value=="")	missing_fields+="\n- Passwortbestätigung";
	if (document.forms[0].password.value != document.forms[0].password_conf.value) missing_fields+="\n- Passwort und Passwortbestätigung müssen identisch sein!";
	if (document.forms[0].password.value == document.forms[0].username.value) missing_fields+="\n- Benutzername und Passwort dürfen aus Sicherheitsgründen nicht identisch sein!";
	return mifi(missing_fields);
}

function printpage() {
	if (window.print)
		window.print();
	else
		alert("Leider unterstützt Ihr Browser diese Funktion nicht. Bitte aktivieren Sie die Druckfunktion Ihres Browsers manuell.");
}


function openSearch(id) {
	if (document.getElementById("openSearch").style.display != 'none') {
		document.getElementById("openSearch").style.display = 'none';
		document.getElementById("closeSearch").style.display = '';
		new Effect.SlideDown (id, {duration: 1});
	}
}			
function closeSearch(id) {
	if (document.getElementById("closeSearch").style.display != 'none') {
		document.getElementById("closeSearch").style.display = 'none';
		document.getElementById("openSearch").style.display = '';
		new Effect.SlideUp(id, {duration: 1});	
	}
}

function openSearchDetail(id,openId,closeId) {
	if (document.getElementById(openId).style.display != 'none') {
		document.getElementById(openId).style.display = 'none';
		document.getElementById(closeId).style.display = '';
		new Effect.SlideDown (id, {duration: 1.5});
		activeSD = 1;
	} else {
		activeSD = 0;
	}
}			
function closeSearchDetail(id,openId,closeId) {
	if (document.getElementById(openId).style.display == 'none') {
		document.getElementById(closeId).style.display = 'none';
		document.getElementById(openId).style.display = '';
		new Effect.SlideUp(id, {duration: 1});
		activeSD = 0;
		
	}
}

function toogleSearchDetails(id,openId,closeId) {
	if (document.getElementById(openId).style.display == 'none') {
		document.getElementById(closeId).style.display = 'none';
		document.getElementById(openId).style.display = '';
		document.getElementById(id).style.display = 'none';	
	}else{
		document.getElementById(closeId).style.display = '';
		document.getElementById(openId).style.display = 'none';
		document.getElementById(id).style.display = '';			
	}
}


// Script Source: CodeLifter.com
// Copyright 2003
// Do not remove this notice.

// SETUPS:
// ===============================

// Set the horizontal and vertical position for the popup

PositionX = 100;
PositionY = 100;

// Set these value approximately 20 pixels greater than the
// size of the largest image to be used (needed for Netscape)

defaultWidth  = 500;
defaultHeight = 500;

// Set autoclose true to have the window close automatically
// Set autoclose false to allow multiple popup windows

var AutoClose = true;

// Do not edit below this line...
// ================================
if (parseInt(navigator.appVersion.charAt(0))>=4){
	var isNN=(navigator.appName=="Netscape")?1:0;
	var isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;
}
var optNN='scrollbars=no,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY;
var optIE='scrollbars=no,width=150,height=100,left='+PositionX+',top='+PositionY;

function popImage(imageURL,imageTitle){
	if (isNN){imgWin=window.open('about:blank','',optNN);}
	if (isIE){imgWin=window.open('about:blank','',optIE);}
	with (imgWin.document){
		writeln('<html><head><title>Loading...</title><style>body{margin:0px;}</style>');writeln('<sc'+'ript>');
		writeln('var isNN,isIE;');writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
		writeln('isNN=(navigator.appName=="Netscape")?1:0;');writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
		writeln('function reSizeToImage(){');writeln('if (isIE){');writeln('window.resizeTo(300,300);');
		writeln('width=300-(document.body.clientWidth-document.images[0].width);');
		writeln('height=300-(document.body.clientHeight-document.images[0].height);');
		writeln('window.resizeTo(width,height);}');writeln('if (isNN){');       
		writeln('window.innerWidth=document.images["George"].width;');writeln('window.innerHeight=document.images["George"].height;}}');
		writeln('function doTitle(){document.title="'+imageTitle+'";}');writeln('</sc'+'ript>');
		if (!AutoClose) writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()">')
		else writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()" onblur="self.close()">');
		writeln('<img name="George" src='+imageURL+' style="display:block"></body></html>');
		close();		
	}
}

function checkDate()
{

	var year_month = document.form.checkin_year_month.value.split("-"); 
	var year_monthOut = document.form.checkout_year_month.value.split("-"); 
	
    var year = year_month[0];
    var month = year_month[1];
    var yearOut = year_monthOut[0];
    var monthOut = year_monthOut[1];
	var day = document.form.checkin_monthday.value;
    var feb = 28;
    if ( (year%4 == 0 && year%100 !=0) || year%400 == 0 )
    {
        var feb = 29;
    }
    var days = new Array(31, feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    document.form.checkin_monthday.options.length = days[month-1];
    
    for ( i = 0; i < days[month-1]; i++ )
    {
        document.form.checkin_monthday.options[i].text = i+1 + '.';
        document.form.checkin_monthday.options[i].value = i+1;
    }
	if ( parseInt(document.form.checkin_monthday.value) + 1 > document.form.checkin_monthday.options.length && monthOut == month )
	{
		document.form.checkout_year_month.value = year + '-' + (parseInt(month) + 1);
		document.form.checkout_monthday.value = 1;
	}
	else
	{
		if(year == yearOut && monthOut < month )
		{	
			document.form.checkout_monthday.value = parseInt(document.form.checkin_monthday.value) + 1;
			document.form.checkout_year_month.value = document.form.checkin_year_month.value;
		}		
		else if(year == yearOut && month == monthOut && parseInt(document.form.checkout_monthday.value) <= document.form.checkin_monthday.value  )
		{
			document.form.checkout_monthday.value = parseInt(document.form.checkin_monthday.value) +1;
		}
		else if(year == yearOut && month > monthOut )
		{
			alert(monthOut + '  ' + month);
			document.form.checkout_year_month.value = document.form.checkin_year_month.value;
		}				
		
	}
}

// Funktion CheckDate
// Wandelt - fast - beliebiges Datumsformat nach YYYY.MM.DD um.

function checkDateEx(id1, id2)
{
	var feb = 28;
	var year = new Date().getFullYear();
	
    if ( (year%4 == 0 && year%100 !=0) || year%400 == 0 )
    {
        var feb = 29;
    }
    var days = new Array(31, feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);	
	
	obj1 = document.getElementById(id1);	
	obj2 = document.getElementById(id2);
	
	
	// Config ===========
	date_trenner = '-';
	// End Config =======
	
	val = obj1.value;
	val2 = obj2.value;
	if (!val || (!val2))
		return;
	
	// 1-2 Ziffern
	// ein oder kein Slash oder Punkt
	// 1-2 Ziffern
	// ein oder kein Slash oder Punkt
	// 4 oder 2 Ziffern
	expression = /^([0-9]{4}|[0-9]{2})(\/|\.|-)?([0-9]{1,2})(\/|\.|-)?([0-9]{1,2})?$/;
	//alert(expression.exec(val));
	result = expression.exec(val);
	result2 = expression.exec(val2);


	if (result == null ) {
		// Invalid date!
		return;
	}
	
	t = result[5]; t2 = result2[5];
	m = result[3]; m2 = result2[3];
	y = result[1]; y2 = result2[1];
	
	//alert('montht:' + m + '  month2:' + m2 + ' day:' + t + ' day2: ' + t2);
	
	// Wenn Jahr fehlt, hinzufügen (FF: undefined; IE: Leerstring)
	if (typeof(y) == 'undefined' || y == '')
		y = new Date().getFullYear();

	
	if ( parseInt(t) + 1 > days[parseInt(m)-1]  && m2 == m )
	{
		m2 =  String(parseInt(m) + 1);
		t2 = '1';
	}
	else
	{		
		if(y == y2 && m2 < m )
		{	
			t2 = String(parseInt(t) + 1);
			m2 = m;
		}		
		else if(y == y2 && m >= m2 && parseInt(t2) <= parseInt(t) )
		{
			t2 = String(parseInt(t) + 1);
			m2 = m;
		}
	}	
	if (t.length == 1) t = '0' + t;
	if (t2.length == 1){; t2 = '0' + t2};
	if (m.length == 1) m = '0' + m;
	if (m2.length == 1) m2 = '0' + m2;
	if (y.length == 2) y = '20' + y;
	if (y2.length == 2) y2 = '20' + y2;	

	obj1.value = y + date_trenner + m + date_trenner + t;
	obj2.value = y2 + date_trenner + m2 + date_trenner + t2;
}