/*-------------------------------------------------------------------------
	Function: 		showLogin
	Parameter:		none
	Return:			none
	Description:	This function shows or hides the Login-mask
-------------------------------------------------------------------------*/
function showLogin()
{
	if(document.getElementById("tbl_login").style.visibility == "hidden")
	{
		document.getElementById("tbl_login").style.visibility = "";
		frmLogin.Login.focus();
	}
	else
		document.getElementById("tbl_login").style.visibility = "hidden";
}


/*-------------------------------------------------------------------------
	Function: 		showChilds
	Parameter:		strID -> ID of the element
					intNr -> Nr of the element
	Return:			none
	Description:	This function shows or hides a sub-menue
-------------------------------------------------------------------------*/
function showChilds(strId, intNr)
{
	if(document.getElementById(strId).className == "parent")
		document.getElementById(strId).className = "parentnoline";
	else
		document.getElementById(strId).className = "parent";
	
	for(i=1; i <= intNr; i++)
		if(document.getElementById(strId + "_" + i).style.display == "none")			
			document.getElementById(strId + "_" + i).style.display = "";
		else
			document.getElementById(strId + "_" + i).style.display = "none";
}


/*-------------------------------------------------------------------------
	Function: 		login
	Parameter:		none
	Return:			none
	Description:	This function checks, if the loginfields are filled.
					If ok, then the entries will be submitted, else the 
					user gets an error message
-------------------------------------------------------------------------*/
function login()
{
	blnTest = false;
	blnError = false;
	if(frmLogin.Login.value == "")
	{
		blnError = true;
		alert("Sie müssen eine Kennung eingeben.");
	}
				
	if(frmLogin.Passwort.value == "" && blnError == false)
	{
		blnError = true;
		alert("Sie müssen ein Passwort eingeben.");
	}
	
	if(blnError == false && blnTest == false)
		frmLogin.submit();
	else
		alert("Test-Template");
}


/*-------------------------------------------------------------------------
	Function: 		trim
	Parameter:		stringToTrim -> String to be trimed
	Return:			none	
	Description:	This function cuts blanks, etc. in front of and behind
					the regular text
-------------------------------------------------------------------------*/
function trim(stringToTrim) 
{
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}


/*-------------------------------------------------------------------------
	Function: 		ltrim
	Parameter:		stringToTrim -> String to be trimed
	Return:			none	
	Description:	This function cuts blanks, etc in front of a regular 
					text
-------------------------------------------------------------------------*/
function ltrim(stringToTrim) 
{
	return stringToTrim.replace(/^\s+/,"");
}


/*-------------------------------------------------------------------------
	Function: 		rtrim
	Parameter:		stringToTrim -> String to be trimed
	Return:			none	
	Description:	This function cuts blanks, etc behind the regular text
-------------------------------------------------------------------------*/
function rtrim(stringToTrim) 
{
	return stringToTrim.replace(/\s+$/,"");
}


/*-------------------------------------------------------------------------
	Function: 		checkEmail
	Parameter:		str
	Return:			true  -> valid email
					false -> invalid email	
	Description:	This function checks, if the fiels of the contactform
					are filled.
					If ok, then the entries will be submitted, else the 
					user gets an error message
-------------------------------------------------------------------------*/
function checkEmail(str)
{
	var at 	= "@";
	var dot = ".";
	var lat	= str.indexOf(at);
	var lstr= str.length;
	var ldot= str.indexOf(dot);
	
	if (str.indexOf(at) == -1)
	   return false;

	if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr)
	   return false;

	if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr)
	    return false;

	 if (str.indexOf(at, (lat+1)) != -1)
	    return false;

	 if (str.substring(lat-1, lat) == dot || str.substring(lat+1, lat+2) == dot)
	     return false;
		 
	 if (str.indexOf(dot, (lat+2)) == -1)
	    return false;

	 if (str.indexOf(" ") != -1)
	    return false;
	 

	return true;	
}


/*-------------------------------------------------------------------------
	Function: 		checkContactForm
	Parameter:		none
	Return:			true  -> Check succeeded
					false -> Check failed	
	Description:	This function checks, if the fiels of the contactform
					are filled.
					If ok, then the entries will be submitted, else the 
					user gets an error message
-------------------------------------------------------------------------*/
function checkContactForm()
{
	blnError	= false;
	strName		= trim(frmContact.Name.value);
	strEmail 	= trim(frmContact.Email.value);
	strText		= trim(frmContact.Text.value);
	// strCaptcha	= trim(frmContact.Confirm.value);
	
	if(strName == "")
	{
		alert("Bitte geben Sie einen Namen ein!");
		frmContact.Name.focus();
		return false;
	}	
	
	if(checkEmail(strEmail) == false)
	{
		alert("Bitte geben Sie eine gültige Emailaddresse ein!");
		frmContact.Email.focus();
		return false;
	}
	
	if(strText == "")
	{
		alert("Bitte geben Sie eine Nachricht ein!");
		frmContact.Text.focus();
		return false;
	}	
	
	/*
	if(strCaptcha == "")
	{
		alert("Bitte bestätigen Sie Ihre Eingaben indem Sie den dargestellten Code in das daneben stehende Feld eintragen!");
		frmContact.Confirm.focus();
		return false;
	}	
	*/
	
	return true;	
}


/*-------------------------------------------------------------------------
	Function: 		submitForm
	Parameter:		none
	Return:			none
	Description:	This submits the contact form
-------------------------------------------------------------------------*/
function submitForm()
{
	blnSubmit = checkContactForm();
	
	if(blnSubmit)
		frmContact.submit();
}


/*-------------------------------------------------------------------------
	Function: 		submitForm
	Parameter:		none
	Return:			none
	Description:	displays a warning-msg awaiting confirmation before 
					deleting s.th.
-------------------------------------------------------------------------*/
function confirmDeletion(strPrefix, strToDelete, strHref)
{
	blnDelete = confirm("Wollen Sie wirklich " + strPrefix + " " + makeDataConformForHTML(strToDelete) + " löschen?");
	
	if(blnDelete)
		location.href = strHref;
}


/*-------------------------------------------------------------------------
	Function: 		isFilled
	Parameter:		strToCheck -> string which should be checked
	Return:			true  -> strToCheck has more than 1 char
					false -> strToCheck is empty
	Description:	This submits User Setting Form
-------------------------------------------------------------------------*/
function isFilled(strToCheck, intMinLength)
{
	str = trim(strToCheck);
	if(strToCheck.length < intMinLength)
		return false;
	
	return true;
}


/*-------------------------------------------------------------------------
	Function: 		saveUserSetting
	Parameter:		none
	Return:			false -> when mandatory entries were missing
	Description:	This submits User Setting Form after validation
-------------------------------------------------------------------------*/
function saveUserSetting()
{
	if(!isFilled(frmEditUser.Nachname.value, 3))
	{
		alert("Bitte tragen Sie einen Nachnamen ein.");
		return false;
	}
	
	if(!isFilled(frmEditUser.Vorname.value, 3))
	{
		alert("Bitte tragen Sie einen Vornamen ein.");
		return false;
	}
	
	if(!isFilled(frmEditUser.Login.value, 3))
	{
		alert("Bitte tragen Sie einen Loginnamen ein.");
		return false;
	}
	
	if(!isFilled(frmEditUser.Passwort.value, 8))
	{
		alert("Bitte tragen Sie ein Passwort mit mindestens 8 Zeichen ein.");
		return false;
	}
	
	// if an email address has been entered, then check it
	strEmail = trim(frmEditUser.Email.value);
	if(strEmail != '')
	{
		if(checkEmail(strEmail) == false)
		{
			alert("Bitte geben Sie eine gültige Emailaddresse ein!");
			frmEditUser.Email.focus();
			return false;
		}
	}
	
	frmEditUser.submit();
	
}


/*-------------------------------------------------------------------------
	Function: 		saveTerm
	Parameter:		none
	Return:			false -> when mandatory entries were missing
	Description:	This submits term Form
-------------------------------------------------------------------------*/
function saveTerm()
{
	if(trim(frmEditTerm.Zeit.value) == '' || trim(frmEditTerm.Zeit.value) == '00:00')
	{
		alert("Bitte geben Sie eine Uhrzeit an.");
		return false;
	}
	
	if(trim(frmEditTerm.Titel.value) == '')
	{
		alert("Bitte tragen Sie eine Überschrift ein.");
		return false;
	}
	
	if(trim(frmEditTerm.Text.value) == '')
	{
		alert("Bitte tragen Sie eine Beschreibung ein.");
		return false;
	}
	
	frmEditTerm.submit();
}


/*-------------------------------------------------------------------------
	Function: 		validateCheckbox
	Parameter:		strName -> name of the checkbox
	Return:			true  -> if checkbox's name is Administrator or 
							 Bearbeiter
					false -> if checkbox's name isn't Administrator or 
							 Bearbeiter
	Description:	This function check, whether the current checkbox name
					is Administrator or Bearbeiter. If Administrator was 
					clicked and	Bearbeiter is checked, then uncheck 
					Bearbeiter and set check to Administrator
-------------------------------------------------------------------------*/
function validateCheckbox(strName)
{
	if(strName == "Administrator" && frmEditUser.Bearbeiter.checked == true)
	{
		alert("Ein Bearbeiter kann entweder ein Administrator oder ein Bearbeiter sein.")
		frmEditUser.Bearbeiter.checked = false;
		return true;
	}
	
	if(strName == "Bearbeiter" && frmEditUser.Administrator.checked == true)
	{
		alert("Ein Bearbeiter kann entweder ein Administrator oder ein Bearbeiter sein.")
		frmEditUser.Administrator.checked = false;
		return true;
	}
	
	return false;
}


/*-------------------------------------------------------------------------
	Function: 		showTerm
	Parameter:		strDate -> show appointments
	Return:			nothing
	Description:	Open index.php and show appointment list
-------------------------------------------------------------------------*/
function showTerm(strDate, strToDo)
{
	if(strDate != "")
		open("index.php?appdate=" + strDate + "&todo=" + strToDo, "_self");
	
		
}


/*-------------------------------------------------------------------------
	Function: 		loginKeypress
	Parameter:		Ereignis -> Keypress event
	Return:			nothing
	Description:	Calls function login after pressing ENTER on keyboard
-------------------------------------------------------------------------*/
function loginKeypress(Ereignis) {
  if (!Ereignis)
    Ereignis = window.event;
  if (Ereignis.which) {
    Tastencode = Ereignis.which;
  } else if (Ereignis.keyCode) {
    Tastencode = Ereignis.keyCode;
  }

	if(Tastencode == 13)
		login();
}


/*-------------------------------------------------------------------------
	Function: 		makeDataConformForHTML
	Parameter:		strToCheck
	Return:			strReplaced
	Description:	This function replaces special characters in strings
					which could cause errors in JavaScript
-------------------------------------------------------------------------*/
function makeDataConformForHTML(strToCheck)
{
	strReplaced = strToCheck.replace(/#/g, "'");
	
	return strReplaced;

}


/*-------------------------------------------------------------------------
	Function: 		addBranch
	Parameter:		intOptionID -> ID of the branch
	Return:			none
	Description:	this function adds a single branch to a user's branchlist
-------------------------------------------------------------------------*/
function addBranch(intOptionID)
{
	if(intOptionID == -1 && document.frmEditUser.branch_available.length > 0)
		intOptionID = 0;
	
	if(document.frmEditUser.branch_available.length > 0)
	{
		len = document.frmEditUser.branch_assigned.length;
		neu = new Option(frmEditUser.branch_available.options[intOptionID].text, frmEditUser.branch_available.options[intOptionID].value, false, true);
 		document.frmEditUser.branch_assigned.options[len] = neu;
	
		document.frmEditUser.branch_available.options[intOptionID] = null;

		if(document.frmEditUser.branch_available.length >= 1)
			document.frmEditUser.branch_available.options[0].selected = true ;
			
		assignBranch();
	}
}


/*-------------------------------------------------------------------------
	Function: 		deleteBranch
	Parameter:		intOptionID -> ID of the branch
	Return:			none
	Description:	this function deletes a single branch to a user's 
					branchlist
-------------------------------------------------------------------------*/
function deleteBranch(intOptionID)
{
	if(intOptionID == -1 && document.frmEditUser.branch_assigned.length > 0)
		intOptionID = 0;

	if(document.frmEditUser.branch_assigned.length > 0)
	{
		len = document.frmEditUser.branch_available.length;
		neu = new Option(frmEditUser.branch_assigned.options[intOptionID].text, frmEditUser.branch_assigned.options[intOptionID].value, false, true);
	 	document.frmEditUser.branch_available.options[len] = neu;
		
		document.frmEditUser.branch_assigned.options[intOptionID] = null;
	
		if(document.frmEditUser.branch_assigned.length >= 1)
			document.frmEditUser.branch_assigned.options[0].selected = true ;
			
		assignBranch();
	}
}


/*-------------------------------------------------------------------------
	Function: 		assignBranch
	Parameter:		none
	Return:			none
	Description:	this function adds all selected user branches to a 
					the field 'branch_to_assign'. This entries in that field 
					will be saved in the end
-------------------------------------------------------------------------*/
function assignBranch()
{
	frmEditUser.branch_to_assign.value = '';
	for(i = 0; i < document.frmEditUser.branch_assigned.length; i++)
		if(i == 0)
			frmEditUser.branch_to_assign.value = document.frmEditUser.branch_assigned.options[i].value;
		else
			frmEditUser.branch_to_assign.value = frmEditUser.branch_to_assign.value + '|' + document.frmEditUser.branch_assigned.options[i].value;
}


/*-------------------------------------------------------------------------
	Function: 		moveAllBranches
	Parameter:		blnAdd -> if true, move all branches to user's list
						   -> if false, delete all branches from user's list	
	Return:			none
	Description:	this function adds all or deletes all branches 
-------------------------------------------------------------------------*/
function moveAllBranches(blnAdd)
{
	if(blnAdd == true)
	{
		for(k = 0; k < document.frmEditUser.branch_available.length; k++)
		{
			len = document.frmEditUser.branch_assigned.length;
			neu = new Option(frmEditUser.branch_available.options[k].text, frmEditUser.branch_available.options[k].value, false, true);
	 		document.frmEditUser.branch_assigned.options[len] = neu;		
		}
		
		for(k = document.frmEditUser.branch_available.length; k >= 0 ; k--)
			document.frmEditUser.branch_available.options[k] = null;
		
	}
	else
	{
		for(k = 0; k < document.frmEditUser.branch_assigned.length; k++)
		{
			len = document.frmEditUser.branch_available.length;
			neu = new Option(frmEditUser.branch_assigned.options[k].text, frmEditUser.branch_assigned.options[k].value, false, true);
		 	document.frmEditUser.branch_available.options[len] = neu;	
		}
		
		for(k = document.frmEditUser.branch_assigned.length; k >= 0 ; k--)
			document.frmEditUser.branch_assigned.options[k] = null;
	}
	
	
	assignBranch();
}


/*-------------------------------------------------------------------------
	Function: 		saveBranch
	Parameter:		none
	Return:			true  -> submit form
					false -> mandatory fields missing	
	Description:	this function first checks the mandatory branch fields.
					when validation is successful, then submit form
-------------------------------------------------------------------------*/
function saveBranch()
{

	blnError		= false;
	strBranchName	= trim(frmEditBranch.branch.value);
	strLastName		= trim(document.getElementsByName('1')[0].value);
	strFirstName	= trim(document.getElementsByName('2')[0].value);
	strEmail 		= trim(document.getElementsByName('5')[0].value);
	
	// check mandatory fields 
	if(strBranchName == "")
	{
		alert("Bitte tragen Sie einen Namen für die neue Sparte ein!");
		frmEditBranch.branch.focus();
		return false;
	}	
	
	if(strLastName == "")
	{
		alert("Bitte tragen Sie den Nachnamen des Ansprechpartners ein!");
		document.getElementsByName('1')[0].focus();
		return false;
	}	
	
	if(strFirstName == "")
	{
		alert("Bitte tragen Sie den Vornamen des Ansprechpartners ein!");
		document.getElementsByName('2')[0].focus();
		return false;
	}	
	
	if(strEmail == "")
	{
		alert("Bitte tragen Sie die Email-Addresse des Ansprechpartners ein!");
		document.getElementsByName('5')[0].focus();
		return false;
	} 
	else 
	{
		if(checkEmail(strEmail) == false)
		{
			alert("Bitte geben Sie eine gültige Emailaddresse ein!");
			document.getElementsByName('5')[0].focus();
			return false;
		}
	}
	
	frmEditBranch.submit();
}



/*-------------------------------------------------------------------------
	Function: 		openDownload
	Parameter:		strPage -> the page containing the "force"-download-script 
	Return:			none
	Description:	This function opens a new window (for download) with a
					minimum size
-------------------------------------------------------------------------*/
function openDownload(strPage)
{
	window = open(strPage, '_blank', 'height=1,width=1,toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no,directories=no,location=no');
	window.close();
}

