function start(){
	if(userid==null){
		currency.selectedIndex=145;
		country.selectedIndex=225;
		display("login",true);
		display("register",false);
		display("loggedIn",false);
		display("modifyProfile",false);
		display("dataentry",false);
		display("explaination",true);
		display("statistics",false);
	}else{
		display("login",false);
		display("register",false);
		display("loggedIn",true);
		display("modifyProfile",false);
		loadCompositions();
		display("explaination",false);
		display("dataentry",true);
		display("statistics",true);
	}
	id("table").style.visibility="visible";
}

function forgotPassword(){
	if(signInEmail.value.length>0){
		ajaxXML("forgotPassword.php","email="+encodeURI(signInEmail.value),replyForgotPassword,true);
	}else{
		alert("Please enter your email in the Email-Field.");
	}
}

function replyForgotPassword(xml){
	var error=xml.getElementsByTagName("error");
	clearErrors();
	if(error.length==0){
		alert(xmlString(xml,"message"));
	}else{
		markErrors(error);
	}
}

function backToMenu(){
	display("login",false);
	display("register",false);
	display("loggedIn",true);
	display("modifyProfile",false);
}

function modify(){
	var s="";
	s+="email="+encodeURI(modEmail.value);
	s+="&password="+encodeURI(modPassword.value);
	s+="&rpassword="+encodeURI(modRPassword.value);
	s+="&salary="+encodeURI(modSalary.value);
	s+="&currency="+encodeURI(modCurrency.options[modCurrency.selectedIndex].text);
	var gender="";
	if(document.getElementById("modSexM").checked)gender="M";
	else if(document.getElementById("modSexF").checked)gender="F";
	s+="&gender="+gender;
	s+="&birthyear="+document.getElementById("modBirthyear").value;
	s+="&city="+encodeURI(modCity.value);
	s+="&country="+encodeURI(modCountry.value);
	ajaxXML("modifyProfile.php",s,replyModify,true);
}

function replyModify(xml){
	var error=xml.getElementsByTagName("error");
	clearErrors();
	if(error.length==0){
		display("login",false);
		display("register",false);
		display("loggedIn",true);
		display("modifyProfile",false);
		email=xmlString(xml,"email");
		password=xmlString(xml,"password");
		document.cookie='email='+email+'; expires=Thu, 2 Aug 2021 20:47:11 UTC; path=/';
		document.cookie='password='+password+'; expires=Thu, 2 Aug 2021 20:47:11 UTC; path=/';
		alert('The Profile has been updated.');
	}else{
		markErrors(error);
	}
}

function modifyProfile(){
	ajaxXML("getProfile.php",null,replyShowModifyProfile,true);
}

function replyShowModifyProfile(xml){
	modEmail.value=xmlString(xml,"email");
	modPassword.value=xmlString(xml,"password");
	modRPassword.value=modPassword.value;
	modSalary.value=xmlString(xml,"salary");
	selectCurrency(modCurrency,xmlString(xml,"currency"));
	var gender=xmlString(xml,"gender");
	if(gender=="M")document.getElementById("modSexM").checked=true;
	else if(gender=="F")document.getElementById("modSexF").checked=true;
	modBirthyear.value=xmlString(xml,"birthyear");
	modCity.value=xmlString(xml,"city");
	selectCountry(modCountry,xmlString(xml,"country"));
	display("login",false);
	display("register",false);
	display("loggedIn",false);
	display("modifyProfile",true);
}

function selectCurrency(field,cur){
	for(var a=0;a<field.options.length;a++){
		if(field.options[a].text==cur){
			field.selectedIndex=a;break;
		}
	}
}
function selectCountry(field,cou){
	for(var a=0;a<field.options.length;a++){
		if(field.options[a].value==cou){
			field.selectedIndex=a;break;
		}
	}
}

function backToSignIn(){
	display("login",true);
	display("register",false);
	display("loggedIn",false);
	display("modifyProfile",false);
}

function signIn(){
	var s="";
	s+="email="+encodeURI(signInEmail.value);
	s+="&password="+encodeURI(signInPassword.value);
	ajaxXML("login.php",s,replyLogin,true);
}

function logout(){
	ajaxXML("login.php","logout=true",replyLogout,true);
}
function replyLogout(xml){
	var error=xml.getElementsByTagName("error");
	clearErrors();
	if(error.length==0){
		display("login",true);
		display("register",false);
		display("loggedIn",false);
		display("modifyProfile",false);
		display("dataentry",false);
		display("explaination",true);
		display("statistics",false);
		
		id("regEmail").value="";
		id("regPassword").value="";
		id("regRPassword").value="";
		id("regSalary").value="";
		resetCombo("currency","USD");
		id("regSexM").checked=false;
		id("regSexF").checked=false;
		id("regBirthyear").value="";
		id("regCity").value="";
		resetCombo("country","US");

		id("modEmail").value="";
		id("modPassword").value="";
		id("modRPassword").value="";
		id("modSalary").value="";
		resetCombo("modCurrency","USD");
		id("modSexM").checked=false;
		id("modSexF").checked=false;
		id("modBirthyear").value="";
		id("modCity").value="";
		resetCombo("modCountry","US");
		
		id("compositionName").value="";
		
		document.getElementById("statistics").innerHTML=null;
		userid=null;
		email=null;
		password=null;
		document.cookie='userid=0; expires=Thu, 2 Aug 2000 20:47:11 UTC; path=/';
		document.cookie='email=0; expires=Thu, 2 Aug 2000 20:47:11 UTC; path=/';
		document.cookie='password=0; expires=Thu, 2 Aug 2000 20:47:11 UTC; path=/';
	}else{
		markErrors(error);
	}
}
function resetCombo(s,value){
	var sel=id(s);
	for(var a=0;a<sel.options.length;a++){
		if((sel.options[a].value)==value){
			sel.selectedIndex=a;
		}
	}
}
function markErrors(error){
	for(var a=0;a<error.length;a++){
		var f=error[a].getElementsByTagName("field")[0].firstChild.data;
		if(f=="alert")alert(error[a].getElementsByTagName("description")[0].firstChild.data);
		else{
			var field=document.getElementById(f);
			field.description=error[a].getElementsByTagName("description")[0].firstChild.data;
			field.className="error";
		}
	}	
}

function replyLogin(xml){
	var error=xml.getElementsByTagName("error");
	clearErrors();
	if(error.length==0){
		display("login",false);
		display("register",false);
		display("loggedIn",true);
		display("modifyProfile",false);
		userid=xmlString(xml,"userid");
		email=xmlString(xml,"email");
		password=xmlString(xml,"password");
		if(rememberme.checked){
			document.cookie='userid='+userid+'; expires=Thu, 2 Aug 2021 20:47:11 UTC; path=/';
			document.cookie='email='+email+'; expires=Thu, 2 Aug 2021 20:47:11 UTC; path=/';
			document.cookie='password='+password+'; expires=Thu, 2 Aug 2021 20:47:11 UTC; path=/';
		}
		loadCompositions();
		display("explaination",false);
		display("dataentry",true);
		display("statistics",true);
	}else{
		markErrors(error);
	}
}

function registerShow(){
	display("login",false);
	display("register",true);
	display("loggedIn",false);
	display("modifyProfile",false);
}

function register(){
	var s="";
	s+="email="+encodeURI(regEmail.value);
	s+="&password="+encodeURI(regPassword.value);
	s+="&rpassword="+encodeURI(regRPassword.value);
	s+="&salary="+encodeURI(regSalary.value);
	s+="&currency="+encodeURI(currency.options[currency.selectedIndex].text);
	var gender="";
	if(document.getElementById("regSexM").checked)gender="M";
	else if(document.getElementById("regSexF").checked)gender="F";
	s+="&gender="+gender;
	s+="&birthyear="+document.getElementById("regBirthyear").value;
	s+="&city="+encodeURI(regCity.value);
	s+="&country="+encodeURI(country.value);
	ajaxXML("register.php",s,replyRegister,true);
}

function replyRegister(xml){
	var error=xml.getElementsByTagName("error");
	clearErrors();
	if(error.length==0){
		display("login",false);
		display("register",false);
		display("loggedIn",true);
		display("modifyProfile",false);
		display("explaination",false);
		display("dataentry",true);
		userid=xmlString(xml,"userid");
		email=xmlString(xml,"email");
		password=xmlString(xml,"password");
		document.cookie='userid='+userid+'; expires=Thu, 2 Aug 2021 20:47:11 UTC; path=/';
		document.cookie='email='+email+'; expires=Thu, 2 Aug 2021 20:47:11 UTC; path=/';
		document.cookie='password='+password+'; expires=Thu, 2 Aug 2021 20:47:11 UTC; path=/';
		loadCompositions();
		display("explaination",false);
		display("dataentry",true);
	}else{
		markErrors(error);
	}
}

function clearErrors(){
	var inp=document.getElementsByTagName('input');
	for(var a=0;a<inp.length;a++){
		if(inp[a].helpDiv!=null){
			document.getElementsByTagName('body')[0].removeChild(inp[a].helpDiv);
			inp[a].className=null;
			inp[a].description=null;
			inp[a].helpDiv=null;
		}
	}
	inp=document.getElementsByTagName('span');
	for(var a=0;a<inp.length;a++){
		if(inp[a].helpDiv!=null){
			document.getElementsByTagName('body')[0].removeChild(inp[a].helpDiv);
			inp[a].className=null;
			inp[a].description=null;
			inp[a].helpDiv=null;
		}
	}
}

function showHelp(e,event){
	if(e.description==null)return;
	if(e.helpDiv==null){
		e.helpDiv=document.createElement("div");
		e.helpDiv.style.position="absolute";
		e.helpDiv.style.border="1px solid #000000";
		e.helpDiv.style.backgroundColor="#ffffff";
		e.helpDiv.style.top=(event.clientY+25)+"px";
		e.helpDiv.style.left=(event.clientX)+"px";
		e.helpDiv.innerHTML=e.description;
		document.getElementsByTagName('body')[0].appendChild(e.helpDiv);
	}else{
		e.helpDiv.style.top=(event.clientY+25)+"px";
		e.helpDiv.style.left=(event.clientX)+"px";
	}
	if(	e.helpDiv.style.visibility=="hidden")e.helpDiv.style.visibility="visible";
}
function hideHelp(e){
	if(e.helpDiv==null)return;
	e.helpDiv.style.visibility="hidden";
}

function showError(i,s){
	document.getElementById(i).className="error";
}
function clearError(id){
	display(id,false);
	e.innerHTML="";
}
//------------------------------------------------------------------------------------
function id(s){
	return document.getElementById(s);
}

function setCookie(){
	document.cookie='userid=5; expires=Thu, 2 Aug 2021 20:47:11 UTC; path=/'
}


function hitEnter(e){
	var keynum;
	if(window.event){
		keynum = e.keyCode;
	}else if(e.which){
		keynum = e.which;
	}
	return keynum==13;	
}
function xmlString(xml,element){
	if(xml.getElementsByTagName(element)[0].firstChild==null)return "";
	else return xml.getElementsByTagName(element)[0].firstChild.data;
}
function xmlInt(xml,element){
	if(xml.getElementsByTagName(element)[0].firstChild==null)return 0;
	else return parseInt(xml.getElementsByTagName(element)[0].firstChild.data);
}
function xmlFloat(xml,element){
	if(xml.getElementsByTagName(element)[0].firstChild==null)return 0;
	else return parseFloat(xml.getElementsByTagName(element)[0].firstChild.data);
}

function ajaxXML(url,vars,callbackFunction,synchronous){
	if (window.XMLHttpRequest) {
		var request = new XMLHttpRequest();
	}else{
		var request = new ActiveXObject("MSXML2.XMLHTTP.3.0");
	}
	if(synchronous==null)synchronous=false;
	request.open("POST", url,!synchronous);
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	if(!synchronous){
		request.onreadystatechange = function(){
			if (request.readyState == 4 && request.status == 200) {
//				alert((request.responseText));
				if(request.responseXML){
					callbackFunction(request.responseXML.documentElement);
				}
			}
		}
	}
	request.send(vars);
	if(synchronous){
//		alert((request.responseText));
		callbackFunction(request.responseXML.documentElement);
	}
}

//-------------------------------- displayElements ----------------------------
function display(r,b){
	r=document.getElementById(r);
	if(navigator.userAgent.indexOf("Firefox")!=-1){
		r.style.display= b ? '' : 'none';
	}else{
		r.style.display= b ? 'block' : 'none';
	}
}
  
function displayed(r){
	r=document.getElementById(r);
	if(navigator.userAgent.indexOf("Firefox")!=-1){
		if(r.style.display=='') return true;
		if(r.style.display=='none') return false;
	}else{
		if((r.style.display=='block')||(r.style.display=='')) return true;
		if(r.style.display=='none') return false;
	}
}

//------------------------ Formatieren von Zahlen ---------------------------------------------

function ignoreKey(event,keyArray){
	var keynum;
	if(window.event){
		keynum = event.keyCode;
	}else if(event.which){
		keynum = event.which;
	}
	if((keynum==null)||(keynum==9))return;
	
	for(var a=0;a<keyArray.length;a++){
		if(keynum==keyArray[a]){
			if(window.event){
				event.returnValue=false;
			}else if(event.which){
				event.preventDefault();
			}
			return;
		}
	}
}
function onlyNumberKeys(event){
	var keynum;
	if(window.event){
		keynum = event.keyCode;
	}else if(event.which){
		keynum = event.which;
	}
	if(((keynum>47)&&(keynum<58))||(keynum==8)||(keynum==null))return;
	
	if(window.event){
		event.returnValue=false;
	}else if(event.which){
		event.preventDefault();
	}
}

function numberText(tb,decimals,event){
	var keynum;
	if(window.event){
		keynum = event.keyCode;
	}else if(event.which){
		keynum = event.which;
	}
	if((keynum==null)||(keynum==9)||(keynum==16))return;

	tb.value=numberFormat(tb.value,decimals);
	return numberFormatReverse(tb.value);
}

function numberFormat(s,decimals){
	s+="";
	var res="",negative="";  
	if((s.length>0)&&(s.charAt(0)=="-")){
		negative="-";
		s=s.substring(1);
	}
	for(a=0;a<s.length;a++){
		if((s.charCodeAt(a)>47)&&(s.charCodeAt(a)<58)){
			res+=s.charAt(a);
		}
	}
	while(res.charAt(0)=="0"){
		res=res.substring(1);
	}
	s=res;res="";
	var sl=s.length;
	for(a=0;a<decimals-sl+1;a++){
		s="0"+s;
	}
	
	for(a=0;a<s.length;a++){
		if((a==decimals)&&(decimals>0))res="."+res;
		if((a>decimals)&&((a-decimals)-(Math.round((a-decimals)/3-0.5)*3)==0))res=","+res;
		res=s.charAt(s.length-1-a)+res;
	}
	return negative+res;
}

function numberFormatReverse(s){
	s+="";
	var res="";
	for(a=0;a<s.length;a++){
		if((s.charCodeAt(a)>44)&&(s.charCodeAt(a)<58)&&(s.charCodeAt(a)!=47)){
			res+=s.charAt(a);
		}
	}
	return parseFloat(res);
}

function openVideo(){
	openW("video.html","mycpi",805,490,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0')
}
function openW(mypage,myname,w,h,features) {
	if(screen.width){
		var winl=parseInt((screen.width-w)/2),wint=parseInt((screen.height-h)/2);
	}else{
		var winl=0,wint=0;
	}
	if (winl<0)winl=0;
	if (wint<0)wint=0;
	var settings = 'height='+h+',width='+w+',top='+wint+',left='+winl+','+features;
	win=window.open(mypage,myname,settings);
	win.window.focus();
}