/* ---------------------------- */
/* XMLHTTPRequest Enable */
/* ---------------------------- */
function createObject() {
	var request_type;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		request_type = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		request_type = new XMLHttpRequest();
	}
	return request_type;
}

var http = createObject();

/* -------------------------- */
/* LOGIN */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */
var nocache = 0;
function doLogin(phpfunc) {
	// Optional: Show a waiting message in the layer with ID ajax_response
	document.getElementById('login_response').innerHTML = "Lade...";
	// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
	var email = encodeURI(document.getElementById('useremail').value);
	var passwort = encodeURI(document.getElementById('passwort').value);
	// Set te random number to add to URL request
	nocache = Math.random();
	
	var url = 'action=login'+'&email='+email+'&passwort='+passwort+'&nocache = '+nocache;

	// Pass the login variables like URL variable
	http.open('post', phpfunc);
	http.onreadystatechange = loginReply;
	
	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", url.length);

	http.send(url);
}
function loginReply() {
	if(http.readyState == 4){
		var response = http.responseText;

		if(response == 0){
		// if login fails
			document.getElementById('login_response').innerHTML = 'EMail-Adresse oder Passwort nicht korrekt';
		} else {
			document.getElementById('login_response').innerHTML = '';
			login_form_onoff();
			login_mode_onoff();
			
			var login_status = document.getElementById('login_status');
			if(login_status != null)
				login_status.value = '1';
				
			//Objektdetails
			var merkdiv = document.getElementById('merkdiv');
			if(merkdiv)
				merkdiv.style.visibility = "visible";
		}
	}
}

function doLogout(phpfunc) {
	nocache = Math.random();
	
	var url = 'action=logout'+'&nocache = '+nocache;
	
	// Pass the login variables like URL variable
	http.open('post', phpfunc);
	http.onreadystatechange = logoutReply;
	
	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", url.length);

	http.send(url);
}
function logoutReply() {
	if(http.readyState == 4){
		login_mode_onoff();
		
		var login_status = document.getElementById('login_status');
		if(login_status != null)
			login_status.value = '0';
		
		//Objektdetails
		var merkdiv = document.getElementById('merkdiv');
		if(merkdiv)
			merkdiv.style.visibility = "hidden";
		
		// return to start page if merkliste is open
		var url = location.href;
		
		if(url.lastIndexOf('/merkliste') == url.length - 10 && url.lastIndexOf('/merkliste') != -1)
		{
			location = './';
			location.reload;
		}
	}
}

function doRemember(objid)
{
	document.getElementById('merkreply').innerHTML = 'Speichere Objekt...';
	
	nocache = Math.random();
	
	var url = 'action=remember'+'&objnr='+objid+'&nocache = '+nocache;
	
	http.open('post', 'ajax_functions.php5');
	http.onreadystatechange = rememberReply;
	
	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", url.length);

	http.send(url);
}
function rememberReply() {
	if(http.readyState == 4){	
		var response = http.responseText;
		
		if(response == 0){
			document.getElementById('fancybox-content').innerHTML = '<div id="merkreply">Objekt befindet sich bereits in Merkliste.</div>';
		} else {
			document.getElementById('fancybox-content').innerHTML = '<div id="merkreply">Objekt in Merkliste gespeichert.</div>';
		}
		window.setTimeout('$.fancybox.close()', 2500);
	}
}

function doCheckEmail()
{
	var email = document.getElementById('email').value;
	
	document.getElementById('no_email').className = "check_response";
	document.getElementById('no_email').innerHTML = "E-Mail-Adresse wird &uuml;berpr&uuml;ft...";
	
	nocache = Math.random();
	
	var url = 'action=checkEmail&email='+email+'&nocache = '+nocache;
	
	http.open('post', 'ajax_functions.php5', false);

	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", url.length);

	http.send(url);

	document.getElementById('no_email').className = "info";
	
	if(http.readyState == 4)
		return http.responseText;
	else
		return false;
}

function doCheckCaptcha()
{
	var captcha = document.getElementById('captcha_input').value;
	
	document.getElementById('no_captcha').className = "check_response";
	document.getElementById('no_captcha').innerHTML = "Sicherheitscode wird &uuml;berpr&uuml;ft...";
	
	nocache = Math.random();
	
	var url = 'action=checkCaptcha&captcha='+captcha+'&nocache = '+nocache;
	
	http.open('post', 'ajax_functions.php5', false);

	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", url.length);

	http.send(url);

	document.getElementById('no_captcha').className = "info";
	
	if(http.readyState == 4)
		return http.responseText;
	else
		return false;
}

function doSendPW(phpfunc)
{
	var email = document.getElementById('useremail').value;

	if(email == 'E-Mail Adresse')
		email = '';
	
	if(email == '')
		document.getElementById('sendreply').innerHTML = 'Bitte geben Sie unter "Login" Ihre E-Mail-Adresse an!';
	else
	{
		document.getElementById('sendreply').innerHTML = 'Passwort f&uuml;r "'+email+'" wird ermittelt...';

		nocache = Math.random();
		
		var url = 'action=sendPW&email='+email+'&nocache = '+nocache;

		http.open('post', phpfunc);
		http.onreadystatechange = sendPWReply;

		//Send the proper header information along with the request
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length", url.length);

		http.send(url);
	}
}

function sendPWReply()
{
	var email = document.getElementById('useremail').value;
	
	if(http.readyState == 4){	
		var response = http.responseText;

		if(response == 0){
			document.getElementById('fancybox-content').innerHTML = '<div id="sendreply">Die angegebene E-Mail-Adresse wurde nicht gefunden!</div>';
		} else {
			document.getElementById('fancybox-content').innerHTML = '<div id="sendreply">Ihre Zugangsdaten wurden soeben an "'+email+'" versendet.</div>';
		}
		window.setTimeout('$.fancybox.close()', 2500);
	}
}

