
// telepark.cms 
// (c) by telepark

// open new window
function newWindow(url,name,width,height) {
	fenster = window.open (
	url,
	name, 					// name
	'toolbar=0' 			// tool bar 
	+',location=0' 			// address bar
	+',directories=0' 		// additional bars
	+',status=0' 			// status bar
	+',menubar=0' 			// menu bar
	+',scrollbars=1' 		// scroll bars
	+',resizable=1' 		// window resizable
	+',width=' + width 		// window width in pixels
	+',height=' + height 	// window height in pixels
	);
}

// get focus
function getfocus() {
	self.focus();
}

// create HttpRequest
function createHttpRequest() {
	var xmlhttp;
 	try {
 		xmlhttp = new XMLHttpRequest();
 	} catch (e) {
  		var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0',
  										'MSXML2.XMLHTTP.5.0',
  										'MSXML2.XMLHTTP.4.0',
  										'MSXML2.XMLHTTP.3.0',
  										'MSXML2.XMLHTTP',
  										'Microsoft.XMLHTP'
  										);
  		for (var i = 0; i<XmlHttpVersions.length && !xmlhttp; i++) {
  			try {
  				xmlhttp = new ActiveXObject(XmlHttpVersions[i]);
  			}
  			catch(e) {
  			}
  		}
 	}
 	if (!xmlhttp) {
 		alert('Error creating the XMLHttpRequest object.');
 	} else {
		return xmlhttp;
 	}
}

// loading animation
function loading(f) {
	if (f != true) {
		if (document.getElementById('status')) document.getElementById('status').style.display = 'none';
		else if (parent.document.getElementById('status')) document.getElementById('status').style.display = 'none';
	} else {
		if (document.getElementById('status')) document.getElementById('status').style.display = 'block';
		else if (parent.document.getElementById('status')) document.getElementById('status').style.display = 'block';
	}
}

// input field restriction
MS_restrict_field = function(formname, id_or_name, chars) {
	var obj = (document.getElementById && document.getElementById(id_or_name) != null)
			  ? document.getElementById(id_or_name) : ((document[formname][id_or_name] != null)
			  ? document[formname][id_or_name] : '');

	if(obj.type == "text" || obj.type == "textarea") {
		obj.timer = "";
		obj.chars = chars;
		obj.onkeypress = obj.onkeydown = function() {
			var self = this;
			controll = function() {
				for(var t='',x=0; x<self.value.length; ++x) {
					if(self.chars.indexOf(self.value.charAt(x))>-1) {
						t += self.value.charAt(x);
					}
				}
				self.value = t;
			};
			this.timer = setTimeout(controll,1);
		};
		obj.onkeyup = function() {
			clearTimeout(this.timer);
		};
	}
};

// PRAKTIKA
// ========

// reload praktika list (frontend)
function frontendReloadPraktikaList(userid,searchstring) {
	loading(true);
	
	var s = '';
	
	if (!searchstring || searchstring == "undefined" || searchstring == '') {
		if (document.getElementById('searchpraktikum')) {
			s = encodeURI(document.getElementById('searchpraktikum').value);
		}
	}
	else if (searchstring != '') {
		s = encodeURI(searchstring);
	}
	
	var xmlhttp = createHttpRequest();
	xmlhttp.open("GET", "admin/modules/sprungbrett/ajax/frontendReloadPraktikaList.php?userid=" + userid + "&searchstring=" + s, true);
	xmlhttp.onreadystatechange=function() {
	  // readyState==4 - meaning the load is complete
	  if (xmlhttp.readyState==4) {
		// only if "OK"
		var status = '';
		try {
			status = xmlhttp.status;
			if (status==200) {
			//alert(xmlhttp.responseText);
				eval(xmlhttp.responseText);
				loading();
			}
		}
		catch(e) {
			status = '';
			loading();
		}
	  }
	}
	xmlhttp.send(null);
}

// delete praktikum
function frontendDeletePraktikum(pracid,userid,searchstring,page) {
	loading(true);
	var c = confirm('Möchten Sie dieses Praktikum wirklich löschen?');
	if (c == true) {
		var xmlhttp = createHttpRequest();
		xmlhttp.open("GET", "admin/modules/sprungbrett/ajax/frontendDeletePraktikum.php?pracid=" + pracid + "&searchstring=" + searchstring + "&page=" + page, true);
		xmlhttp.onreadystatechange=function() {
		  // readyState==4 - meaning the load is complete
		  if (xmlhttp.readyState==4) {
			// only if "OK"
			var status = '';
			try {
				status = xmlhttp.status;
				if (status==200) {
				//alert(xmlhttp.responseText);
					eval(xmlhttp.responseText);
					loading();
				}
			}
			catch(e) {
				status = '';
				loading();
				if (page && page > 0) frontendReloadPraktikumSearchResults(page);
				else frontendReloadPraktikaList(userid,searchstring);
			}
		  }
		}
		xmlhttp.send(null);
	}
	else {
		loading();
	}
}

// toggle Kontaktart
function toggleKontaktart(art) {
	if (art == 1) {
		if (document.getElementById('public_contact') && document.getElementById('anonymous_contact')) {
			document.getElementById('public_contact').style.display = "block";
			document.getElementById('anonymous_contact').style.display = "none";
		}
	}
	else if (art == 2) {
		if (document.getElementById('public_contact') && document.getElementById('anonymous_contact')) {
			document.getElementById('public_contact').style.display = "none";
			document.getElementById('anonymous_contact').style.display = "block";
		}
	}
}

// delete praktikum
function frontendSetPraktikumStatus(pracid,searchstring,newstatus,userid) {
	loading(true);
	if (newstatus == 0) var question = 'Möchten Sie dieses Praktikum wirklich inaktiv setzen?';
	else if (newstatus == 1) var question = 'Möchten Sie dieses Praktikum wirklich aktiv setzen?';
	var c = confirm(question);
	if (c == true) {
		var xmlhttp = createHttpRequest();
		xmlhttp.open("GET", "admin/modules/sprungbrett/ajax/frontendSetPraktikumStatus.php?pracid=" + pracid + "&userid=" + userid + "&status=" + newstatus + "&searchstring=" + searchstring, true);
		xmlhttp.onreadystatechange=function() {
		  // readyState==4 - meaning the load is complete
		  if (xmlhttp.readyState==4) {
			// only if "OK"
			var status = '';
			try {
				status = xmlhttp.status;
				if (status==200) {
				//alert(xmlhttp.responseText);
					eval(xmlhttp.responseText);
					loading();
				}
			}
			catch(e) {
				status = '';
				loading();
				frontendReloadPraktikaList(userid,searchstring);
			}
		  }
		}
		xmlhttp.send(null);
	}
	else {
		loading();
	}
}

// reload praktika list
function frontendReloadPraktikumSearchResults(page) {
	loading(true);

	var xmlhttp = createHttpRequest();
	xmlhttp.open("GET", "admin/modules/sprungbrett/ajax/frontendReloadPraktikumSearchResults.php?page=" + page, true);
	xmlhttp.onreadystatechange=function() {
	  // readyState==4 - meaning the load is complete
	  if (xmlhttp.readyState==4) {
		// only if "OK"
		var status = '';
		try {
			status = xmlhttp.status;
			if (status==200) {
			//alert(xmlhttp.responseText);
				eval(xmlhttp.responseText);
				loading();
			}
		}
		catch(e) {
			status = '';
			loading();
		}
	  }
	}
	xmlhttp.send(null);
}

// toggle praktika search form
function togglePraktikaSearchForm() {
	if (document.getElementById('extendedSearchForm')) {
		if (document.getElementById('extendedSearchForm').style.display == "block") document.getElementById('extendedSearchForm').style.display = "none";
		else document.getElementById('extendedSearchForm').style.display = "block";
	}
}

// reset page session variable
function resetSessionPage() {
	//alert("resetSessionPage");
	var xmlhttp = createHttpRequest();
	xmlhttp.open("GET", "admin/modules/sprungbrett/ajax/resetPageSession.php", false);
	xmlhttp.onreadystatechange=function() {
	  // readyState==4 - meaning the load is complete
	  //alert(xmlhttp.readyState);
	  if (xmlhttp.readyState==4) {
		// only if "OK"
		var status = '';
		try {
			status = xmlhttp.status;
			if (status==200) {
			//alert(xmlhttp.responseText);
			//eval(xmlhttp.responseText);
			}
		}
		catch(e) {
			status = '';
		}
	  }
	}
	xmlhttp.send(null);
}

// MARKTPLATZ
// ==========

// reload marktplatz list (frontend)
function frontendReloadMarktplatzList(userid,searchstring) {
	loading(true);
	
	var s = '';
	
	if (!searchstring || searchstring == "undefined" || searchstring == '') {
		if (document.getElementById('searchmarktplatz')) {
			s = encodeURI(document.getElementById('searchmarktplatz').value);
		}
	}
	else if (searchstring != '') {
		s = encodeURI(searchstring);
	}
	
	var xmlhttp = createHttpRequest();
	xmlhttp.open("GET", "admin/modules/sprungbrett/ajax/frontendReloadMarktplatzList.php?userid=" + userid + "&searchstring=" + s, true);
	xmlhttp.onreadystatechange=function() {
	  // readyState==4 - meaning the load is complete
	  if (xmlhttp.readyState==4) {
		// only if "OK"
		var status = '';
		try {
			status = xmlhttp.status;
			if (status==200) {
			//alert(xmlhttp.responseText);
				eval(xmlhttp.responseText);
				loading();
			}
		}
		catch(e) {
			status = '';
			loading();
		}
	  }
	}
	xmlhttp.send(null);
}

// delete marktplatz
function frontendDeleteMarktplatz(id,userid,searchstring,page) {
	loading(true);
	var c = confirm('Möchten Sie diesen Marktplatzeintrag wirklich löschen?');
	if (c == true) {
		var xmlhttp = createHttpRequest();
		xmlhttp.open("GET", "admin/modules/sprungbrett/ajax/frontendDeleteMarktplatz.php?id=" + id + "&searchstring=" + searchstring + "&page=" + page, true);
		xmlhttp.onreadystatechange=function() {
		  // readyState==4 - meaning the load is complete
		  if (xmlhttp.readyState==4) {
			// only if "OK"
			var status = '';
			try {
				status = xmlhttp.status;
				if (status==200) {
				//alert(xmlhttp.responseText);
					eval(xmlhttp.responseText);
					loading();
				}
			}
			catch(e) {
				status = '';
				loading();
				if (page && page > 0) frontendReloadMarktplatzSearchResults(page);
				else frontendReloadMarktplatzList(userid,searchstring);
			}
		  }
		}
		xmlhttp.send(null);
	}
	else {
		loading();
	}
}

// set frontend marktplatz status
function frontendSetMarktplatzStatus(markid,searchstring,newstatus,userid) {
	loading(true);
	if (newstatus == 0) var question = 'Möchten Sie diesen Marktplatzeintrag wirklich inaktiv setzen?';
	else if (newstatus == 1) var question = 'Möchten Sie diesen Marktplatzeintrag wirklich aktiv setzen?';
	var c = confirm(question);
	if (c == true) {
		var xmlhttp = createHttpRequest();
		xmlhttp.open("GET", "admin/modules/sprungbrett/ajax/frontendSetMarktplatzStatus.php?markid=" + markid + "&userid=" + userid + "&status=" + newstatus + "&searchstring=" + searchstring, true);
		xmlhttp.onreadystatechange=function() {
		  // readyState==4 - meaning the load is complete
		  if (xmlhttp.readyState==4) {
			// only if "OK"
			var status = '';
			try {
				status = xmlhttp.status;
				if (status==200) {
				//alert(xmlhttp.responseText);
					eval(xmlhttp.responseText);
					loading();
				}
			}
			catch(e) {
				status = '';
				loading();
				frontendReloadMarktplatzList(userid,searchstring);
			}
		  }
		}
		xmlhttp.send(null);
	}
	else {
		loading();
	}
}

// reload marktplatz list
function frontendReloadMarktplatzSearchResults(page) {
	loading(true);

	var xmlhttp = createHttpRequest();
	xmlhttp.open("GET", "admin/modules/sprungbrett/ajax/frontendReloadMarktplatzSearchResults.php?page=" + page, true);
	xmlhttp.onreadystatechange=function() {
	  // readyState==4 - meaning the load is complete
	  if (xmlhttp.readyState==4) {
		// only if "OK"
		var status = '';
		try {
			status = xmlhttp.status;
			if (status==200) {
			//alert(xmlhttp.responseText);
				eval(xmlhttp.responseText);
				loading();
			}
		}
		catch(e) {
			status = '';
			loading();
		}
	  }
	}
	xmlhttp.send(null);
}

// toggle marktplatz search form
function toggleMarktplatzSearchForm() {
	if (document.getElementById('extendedSearchForm')) {
		if (document.getElementById('extendedSearchForm').style.display == "block") document.getElementById('extendedSearchForm').style.display = "none";
		else document.getElementById('extendedSearchForm').style.display = "block";
	}
}

function toggleMarktplatzKategorie(id) {
	loading(true);
	for (var i=1; i<=4; i++) {
		if (document.getElementById('angebot_kategorienid_' + i)) {
			document.getElementById('angebot_kategorienid_' + i).style.display = 'none';
		}
	}
	if (document.getElementById('angebot_kategorienid_' + id)) {
		document.getElementById('angebot_kategorienid_' + id).style.display = 'block';
	}
	loading();
}

// reload berufe list
function frontendReloadBerufeResults(searchstring) {
	loading(true);
	
	var s = '';
	
	if (!searchstring || searchstring == "undefined" || searchstring == '') {
		if (document.getElementById('searchberuf')) {
			s = encodeURI(document.getElementById('searchberuf').value);
		}
	}
	else if (searchstring != '') {
		s = encodeURI(searchstring);
	}
	
	var xmlhttp = createHttpRequest();
	xmlhttp.open("GET", "admin/modules/sprungbrett/ajax/frontendReloadBerufeList.php?searchstring=" + s, true);
	xmlhttp.onreadystatechange=function() {
	  // readyState==4 - meaning the load is complete
	  if (xmlhttp.readyState==4) {
		// only if "OK"
		var status = '';
		try {
			status = xmlhttp.status;
			if (status==200) {
			//alert(xmlhttp.responseText);
				eval(xmlhttp.responseText);
				loading();
			}
		}
		catch(e) {
			status = '';
			loading();
		}
	  }
	}
	xmlhttp.send(null);
}

// toggle berufe filter
function frontendToggleBerufeFilter(tid) {
	if (document.getElementById('searchberuf')) var searchstring = document.getElementById('searchberuf').value;
	else var searchstring = "";
	
	var xmlhttp = createHttpRequest();
	xmlhttp.open("GET", "admin/modules/sprungbrett/ajax/frontendToggleBerufeFilter.php?tid=" + tid, true);
	xmlhttp.onreadystatechange=function() {
	  // readyState==4 - meaning the load is complete
	  if (xmlhttp.readyState==4) {
		// only if "OK"
		var status = '';
		try {
			status = xmlhttp.status;
			if (status==200) {
			//alert(xmlhttp.responseText);
			frontendReloadBerufeResults(searchstring);
			}
		}
		catch(e) {
			status = '';
			frontendReloadBerufeResults(searchstring);
		}
	  }
	}
	xmlhttp.send(null);
}

// toggle search area
function toggleSearchArea(area) {
	if (area == 2) {
		if (document.getElementById('extendedSearchLink')) document.getElementById('extendedSearchLink').href="index.php?extendedmarktplatzsearch";
		if (document.getElementById('searchPlace')) document.getElementById('searchPlace').name="marktplatzsearch";
		if (document.getElementById('extendedsearchlabel')) document.getElementById('extendedsearchlabel').innerHTML="Marktplatz";
	}
	else if (area == 1) {
		if (document.getElementById('extendedSearchLink')) document.getElementById('extendedSearchLink').href="index.php?extendedpraktikasearch";
		if (document.getElementById('searchPlace')) document.getElementById('searchPlace').name="praktikasearch";
		if (document.getElementById('extendedsearchlabel')) document.getElementById('extendedsearchlabel').innerHTML="Praktika";
	}
}

// toggle gueltigkeit (praktika and marktplatz)
function toggleGueltigkeit(mode) {
	if (mode == "unbegrenzt") {
		if (document.getElementById('angebot_gueltigab')) document.getElementById('angebot_gueltigab').value = '';
		if (document.getElementById('angebot_gueltigbis')) document.getElementById('angebot_gueltigbis').value = '';
		if (document.getElementById('praktikum_zeitraum_start')) document.getElementById('praktikum_zeitraum_start').value = '';
		if (document.getElementById('praktikum_zeitraum_ende')) document.getElementById('praktikum_zeitraum_ende').value = '';
	} else if (mode == "datum") {
		if (document.getElementById('angebot_gueltigunbegrenzt') && (document.getElementById('angebot_gueltigab').value!="" || document.getElementById('angebot_gueltigbis').value!="")) {
			document.getElementById('angebot_gueltigunbegrenzt').checked = false;
		}
		if (document.getElementById('praktikum_zeitraum_unbegrenzt') && (document.getElementById('praktikum_zeitraum_start').value!="" || document.getElementById('praktikum_zeitraum_ende').value!="")) {
			document.getElementById('praktikum_zeitraum_unbegrenzt').checked = false;
		}
	}
}

function sortList(order,field,jsfunction,target) {
	var xmlhttp = createHttpRequest();
	target = target + "/toggleSort.php?field=" + field + "&order=" + order;
	xmlhttp.open("GET", target, true);
	xmlhttp.onreadystatechange=function() {
	  // readyState==4 - meaning the load is complete
	  if (xmlhttp.readyState==4) {
		// only if "OK"
		var status = '';
		try {
			status = xmlhttp.status;
			if (status==200) {
			//alert(xmlhttp.responseText);
			//frontendReloadBerufeResults();
			eval(jsfunction);
			}
		}
		catch(e) {
			status = '';
			eval(jsfunction);
		}
	  }
	}
	xmlhttp.send(null);
}

function loadApplicantForm() {
	var xmlhttp = createHttpRequest();
	target = "/sainterose/website/admin/ajax/applicants_form.php?email=" + document.getElementById('email').value;
	xmlhttp.open("GET", target, true);
	xmlhttp.onreadystatechange=function() {
	  // readyState==4 - meaning the load is complete
	  if (xmlhttp.readyState==4) {
		// only if "OK"
		var status = '';
		try {
			status = xmlhttp.status;
			if (status==200) {
			//alert(xmlhttp.responseText);
			//frontendReloadBerufeResults();
			eval(xmlhttp.responseText);
			}
		}
		catch(e) {
			status = '';
			eval(xmlhttp.responseText);
		}
	  }
	}
	xmlhttp.send(null);
}

function loginApplicant() {
	var xmlhttp = createHttpRequest();
	target = "/sainterose/website/admin/ajax/applicants_login.php?email=" + document.getElementById('email').value + "&pass=" + document.getElementById('pass').value;
	xmlhttp.open("GET", target, true);
	xmlhttp.onreadystatechange=function() {
	  // readyState==4 - meaning the load is complete
	  if (xmlhttp.readyState==4) {
		// only if "OK"
		var status = '';
		try {
			status = xmlhttp.status;
			if (status==200) {
			//alert(xmlhttp.responseText);
			//frontendReloadBerufeResults();
			eval(xmlhttp.responseText);
			}
		}
		catch(e) {
			status = '';
			eval(xmlhttp.responseText);
		}
	  }
	}
	xmlhttp.send(null);
}

function appFormFields() {
	var fields=['email','firstname','lastname','birthname','city','country','occupation','captcha'];
	return fields;
}

function appFreeFormFields() {
	var fields=['birthname','occupation'];
	return fields;
}

function in_array(search,arr) {
	for (var i in arr) {
		if (arr[i]==search) return true;
	}
	return false;
}

function clearApplicantForm() {
	var fields=appFormFields();
	for (var i=0; i<fields.length; i++) {
		if (document.getElementById(fields[i])) document.getElementById(fields[i]).value="";
	}
}

function checkAppFields() {
	var fields=appFormFields();
	var freeFields=appFreeFormFields();
	var empty=false;
	for (var i=0; i<fields.length; i++) {
		if (document.getElementById(fields[i]) && !in_array(fields[i],freeFields) && document.getElementById(fields[i]).value.replace(/^\s+|\s+$/g, '')=="") {
			empty=true;
			break;
		}
	}
	return !empty;
}

function assignCheckOnAppForm() {
	var fields=appFormFields();
	for (var i=0; i<fields.length; i++) {
		if (document.getElementById(fields[i])) document.getElementById(fields[i]).onkeyup=enableAppSubmit;
	}
	enableAppSubmit();
}

function enableAppSubmit() {
	var obj=document.getElementById('subAppForm');
	if (!obj) return;
	obj.disabled=!checkAppFields();
}

