var browser = navigator.appName;

function Div_Toggle(item) {
  obj=document.getElementById(item);
  visible=(obj.style.display!="none");
  image=document.getElementById("img_" + item);
  if (visible) {
    obj.style.display="none";
    image.src="./images/div_closed.gif";
  } else {
    obj.style.display="block";
    image.src="./images/div_opend.gif";
  }
}

function checkdate(input){
  if(input.value=="")
    return true;
  var validformat="/^\d{2}\.\d{2}\.\d{4}$/"; //Basic check for format validity
  var returnval=false;
  var datValue = input.value;
  if (datValue.match(validformat == null))
    alert("Bitte geben Sie das Datum so wie im Beispiel gezeigt ein!");
  else { //Detailed check for valid date ranges
    var monthfield=input.value.split(".")[1];
    var dayfield=input.value.split(".")[0];
    var yearfield=input.value.split(".")[2];
    var dayobj = new Date(yearfield, monthfield-1, dayfield);
    if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
      alert("Ungültiger Tag, Monat oder Jahr eingetragen. Bitte korrigieren Sie das Datum.");
    else
      returnval=true;
  }
  if (returnval==false) 
    input.select();
  return returnval
}

function confirmDelete(sURLDelete, sURLNot, sText) {
  if(confirm(sText))
    location.href=sURLDelete;
  else
    location.href=sURLNot;
}

function PopupWindow(sURL) {
  F = window.open(sURL,"Popup","width=680,height=500,scrollbars=yes,resizable=yes");
}

function PopupWindow800(sURL) {
  F = window.open(sURL,"Popup","width=840,height=700,scrollbars=yes,resizable=yes");
}

function PopupWindowFull(sURL) {
  F = window.open(sURL,"Popup","fullscreen=yes,scrollbars=yes");
}

function countCharacters(what) {
  var str = new String(what.value);
  var len = str.length;
  var showstr = len + " Zeichen verwendet";
  var viewer = document.getElementById("charcounter");
  viewer.innerHTML = showstr;
}


function submitForm( formName )
{
	if ( document.forms[formName] )
	{
		var isValidated = true;
		if(formName == 'loginForm')
			isValidated = validateLoginForm(formName);
    if(formName == 'newsletterForm')
      isValidated = validateNewsletterForm(formName);
		if(isValidated)
			document.forms[formName].submit();
	}
}

// Validate the mydata form
function validateMydataForm(formName) {
  if(checkdate(document.forms[formName].birthday))
  {
    submitForm(formName);
  }
  else
  {
    alert("Es wurde kein gültiger Geburtstag eingetragen!");
  }
}


// Validate the newsletter form
function validateNewsletterForm(formName) {
    
	if(document.forms[formName].subfirstname.value.length < 3 && document.forms[formName].cancelfirstname.value.length < 3) 
	{
		alert("Bitte geben Sie Ihren Vornamen ein");
		document.forms[formName].subfirstname.focus();
		return false;	
	}
	if(document.forms[formName].subsurname.value.length < 3 && document.forms[formName].cancelsurname.value.length < 3) 
	{
		alert("Bitte geben Sie Ihren Nachnamen ein");
		document.forms[formName].subsurname.focus();
		return false;	
	}
	if(!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.forms[formName].subemail.value) && 
		!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.forms[formName].cancelemail.value))
	{
		alert("Bitte geben Sie eine korrekte eMail Adresse ein");
		document.forms[formName].subemail.focus();
		return false;	
	}	
	if(document.forms[formName].subfirstname.value.length > 2)
	{
		if(!document.forms[formName].confirm.checked)
		{
			alert("Bitte kreuzen Sie die Einwilligung an");		
			return false;
		}
	}
	return true;
}

function submitAndCloseForm( formName )
{
	if ( document.forms[formName] )
		document.forms[formName].submit();
	self.close();
}

function submitAndCloseContact(formName)
{
	if(validateContactForm(formName))
	{
		if ( document.forms[formName] )
			document.forms[formName].submit();
		alert("Vielen Dank f\374r Ihre Mitteilung welche wir so bald als m\366glich beantworten werden.");
		self.close();
	}
}

// Validate the login form
function validateLoginForm(formName) {

	if(document.forms[formName].username.value.length < 3) 
	{
		alert("Bitte geben Sie Ihren Namen ein");
		document.forms[formName].contactname.focus();
		return false;	
	}
	if(document.forms[formName].userpwd.value.length < 3) 
	{
		alert("Bitte geben Sie Ihr Kennwort ein");
		document.forms[formName].userpwd.focus();
		return false;	
	}
/*
	if(document.forms[formName].seccode.value.length < 3) 
	{
		alert("Bitte geben Sie den Securitykode ein");
		document.forms[formName].seccode.focus();
		return false;	
	}
*/
	return true;
}


// Validate the contact form
function validateContactForm(formName) {
	if(document.forms[formName].contactname.value.length < 3) 
	{
		alert("Bitte geben Sie Ihren Namen ein");
		document.forms[formName].contactname.focus();
		return false;	
	}
	if(document.forms[formName].contactemail.value.length < 3) 
	{
		alert("Bitte geben Sie Ihre eMail Adresse ein");
		document.forms[formName].contactemail.focus();
		return false;	
	}
	if(document.forms[formName].subject.value.length < 3) 
	{
		alert("Bitte geben Sie eine Betreffzeile ein");
		document.forms[formName].subject.focus();
		return false;	
	}
	if(document.forms[formName].message.value.length < 3) 
	{
		alert("Bitte geben Sie eine Mitteilung ein");
		document.forms[formName].message.focus();
		return false;	
	}
	if(!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.forms[formName].contactemail.value)){
		alert("Bitte geben Sie eine korrekte eMail Adresse ein");
		document.forms[formName].contactemail.focus();
		return false;	
	}	
	return true;
}

function resetForm( formName )
{
	if ( document.forms[formName] )
		document.forms[formName].reset();
}

function setFormField( formName, field, value )
{
	if ( document.forms[formName] )
  {
		document.forms[formName].elements[field].value = value;
  }
}

// Set the mnuAction hidden fields of the mainform
function setFormActions(value1, value2, value3, value4 )
{
	if ( document.forms['mainform'] ) 
	{
		document.forms['mainform'].elements['mAct1'].value = value1;
		document.forms['mainform'].elements['mAct2'].value = value2;
		document.forms['mainform'].elements['mAct3'].value = value3;
		document.forms['mainform'].elements['mAct4'].value = value4;
	}
}
// Set the mnuAction hidden fields of the mainform and submit the form
function setFormActionsSubmit(value1, value2, value3, value4 )
{
	if ( document.forms['mainform'] )
	{
		document.forms['mainform'].elements['mAct1'].value = value1;
		document.forms['mainform'].elements['mAct2'].value = value2;
		document.forms['mainform'].elements['mAct3'].value = value3;
		document.forms['mainform'].elements['mAct4'].value = value4;
		document.forms['mainform'].submit();
	}
}

function randomPassword(length)
{
  chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  pass = "";
  for(x=0;x<length;x++)
  {
    i = Math.floor(Math.random() * 62);
    pass += chars.charAt(i);
  }
  return pass;
}

function MM_swapImgRestore() { 
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() {
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

