var mailregex = /^[0-9a-zA-Z][0-9a-zA-Z\._-]*@([0-9a-zA-Z-_]+\.)+([a-z]{2,4})$/;
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;
  }
  else 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&uuml;tzt Ihr Browser diese Funktion nicht! Bitte aktivieren Sie die Druckfunktion Ihres Browsers manuell!");
}

// gibt alle Elemente zurück, die eine bestimmte Klasse haben [Fotostory]
function getElementsByClassName(classname)  {
    var node = document.getElementsByTagName("body")[0];
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}

// blendet die Komponente mit der Klasse class="article_wrapper" mit der übergebenen Nummer ein, alle anderen aus!
function fotostory(sw) {
	nextprev = 0;
	if(sw == "next") {
		nextprev = document.getElementById('currentWrapper').value;
		nextprev++;
	} else if(sw == "previous") {
		nextprev = document.getElementById('currentWrapper').value;
		nextprev--;
	}
	
	var allWraps = getElementsByClassName("article_wrapper");
	
	if(allWraps[0]) {
		if(nextprev >(allWraps.length-1)) {
			nextprev = (allWraps.length-1);
		}
		
		if(nextprev<0) {
			nextprev = 0;
		} 
		
		var tp;
		
		for(i=0;i<allWraps.length;i++) {
			allWraps[i].style.display = 'none';
			if(i==nextprev) {
				tp = i;
			}
		}
			
		allWraps[(allWraps.length-1)-tp].style.display='block';
		
		document.getElementById('stor_all').innerHTML=allWraps.length;
		document.getElementById('stor_cur').innerHTML=tp+1;
		document.getElementById('currentWrapper').value=tp;
		
		// nextprev button
		if(tp==0) {
			document.getElementById('story_previous').style.display = 'none';
		} else {
			document.getElementById('story_previous').style.display = 'block';
		}
		
		if(tp==(allWraps.length-1)) {
			document.getElementById('story_next').style.display = 'none';
		} else {
			document.getElementById('story_next').style.display = 'block';
		}
	}
	scaleStoryImages();
}

// aktiviert den Fotostory Modus, falls es mindestens einen Fotostory Artikel in der Seite gibt!
function checkFotostory() {
	if(getElementsByClassName("fotostoryactivator").length>0) {
		document.getElementById("fotostory_navigation").style.display='block';
		fotostory("first");
	}
}

// skaliert sämtliche Bilder auf eine annehmbare Größe, mit Ausnahme des Header Image
function scaleStoryImages() {
	maxw = 540;
	maxh = 540;
	
  var imgs = document.getElementsByClassName("showpreview");
  
  for(i=0;i<imgs.length;i++) {
	  var ratio = maxh/maxw;
	  var img = imgs[i];
	  
	  if(img.getAttribute("id") != "lac_head_img") {
		  if (img.height/img.width > ratio){
		     // height is the problem
		    if (img.height > maxh){
		      img.width = Math.round(img.width*(maxh/img.height));
		      img.height = maxh;
		    }
		  } else {
		    // width is the problem
		    if (img.width > maxh){
		      img.height = Math.round(img.height*(maxw/img.width));
		      img.width = maxw;
		    }
		  }
	  }
	  img.setAttribute("class","showpreview ready");
  }
}

//Voreinstellungen für Anmeldefelder in Stammdaten Regattaerstellung. Verwendung in regatta_stammdaten.inc.php
function presetNormal(){
		jQuery('#presetjugend').attr('checked',false);
		jQuery('.formfield').attr('checked',false);
		NormalPresets = new Array(1,2,5,6,7,8,9,10,11,12,13,14,15,29,30,31,33,42,43,44,46,55,56,57,59,68,69,70,72,81,82,83,85,94);
		for (var i=0, j=NormalPresets.length; i<j; i++) {
			jQuery('#formfield_'+NormalPresets[i]).attr('checked',true);
		}
}

function presetJugend(){
		jQuery("#presetnormal").attr('checked',false);
		jQuery('.formfield').attr('checked',false);
		JugendPresets = new Array(1,2,5,7,8,9,10,11,12,13,14,15,17,18,20,21,22,23,24,25,26,29,30,31,33,39,40,42,43,44,46,52,53,55,56,57,59,65,66,68,69,70,72,78,79,81,82,83,85,91,92,94);
		for (var i=0, j=JugendPresets.length; i<j; i++) {
			jQuery('#formfield_'+JugendPresets[i]).attr('checked',true);
		}
}
