// ==============================================================
// telepark.cms

// This Software is copyright (c) 2007 by telepark, 
// Inh. Patrick Thomas, www.telepark.de. 
// All rights reserved. 

// You may not modify, extend, alter, reverse engineer or emulate
// the functionality, or create derivative works of the 
// Software in parts or it's entirety without the prior
// written consent of telepark.
// ==============================================================

// reload map list
function reloadMapList(searchstring,cw) {
	loading(true);
	var s = '';
	
	document.getElementById('mapAction').style.display = 'block';
	document.getElementById('mapActionLabel').style.display = 'block';
	
	if (!searchstring || searchstring == "undefined" || searchstring == '') {
		if (document.getElementById('searchmap')) {
			s = encodeURI(document.getElementById('searchmap').value);
		}
	}
	else if (searchstring != '') {
		s = encodeURI(searchstring);
	}
	//if (cw == "newsletterdata") target = "../modules/sprungbrett/ajax/reloadNewsletterList.php";
	//else target = "../ajax/reloadNewsletterList.php";
	
	var target = domain + "/modules/map/ajax/reloadMapList.php";
	
	var xmlhttp = createHttpRequest();
	xmlhttp.open("GET", target + "?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();
				if (cw && cw!='' && cw!="undefined") {
					fenster.close();
				}
			}
		}
		catch(e) {
			status = '';
			loading();
			if (cw && cw!='' && cw!="undefined") {
				fenster.close();
			}
		}
	  }
	}
	xmlhttp.send(null);
}

// select regbez
function selectRegBez(id) {
	var xmlhttp = createHttpRequest();
	var target = domain + "/modules/map/ajax/selectRegBez.php";
	xmlhttp.open("GET", target + "?id=" + id, 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);
			reloadMapList('');
			}
		}
		catch(e) {
			status = '';
			reloadMapList('');
		}
	  }
	}
	xmlhttp.send(null);
}

// change regbez map
function mapChangeRegBezImg(id,editid) {
	var xmlhttp = createHttpRequest();
	var target = domain + "/modules/map/ajax/changeRegBezMap.php";
	xmlhttp.open("GET", target + "?id=" + id + "&editid=" + editid, 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) {
				eval(xmlhttp.responseText);
				toggleBlocks("block");
			}
		}
		catch(e) {
			status = '';
		}
	  }
	}
	xmlhttp.send(null);
}

// display or hide blocks
function toggleBlocks(status) {
	document.getElementById('regbezmap').style.display = status;
	document.getElementById('xcord1').style.display = status;
	document.getElementById('xcord2').style.display = status;
	document.getElementById('ycord1').style.display = status;
	document.getElementById('ycord2').style.display = status;
	document.getElementById('textpos1').style.display = status;
	document.getElementById('textpos2').style.display = status;
}

// confirm map action
function confirmMapAction(mode) {
	loading(true);
	if (mode == "delete") {
		var c = confirm("Sollen die ausgewählten Arbeitskreise wirklich gelöscht werden?");
	}
	if (c == true) {
		return true;
	} else {
		loading();
		return false;
	}
}


document.onmousemove = getPosition; 
var fontwidth = 4;
var x_ratio;
var y_ratio;

// get mouse position within regbez image
function getPosition(e) { 
    if (document.getElementById("regbezimage")) {
		x = mx = (document.all) ? window.event.x + document.body.scrollLeft : e.pageX; 
	    y = my = (document.all) ? window.event.y + document.body.scrollTop : e.pageY; 
	    
	   var p = document.getElementById('regbezimage');
	   while (p != null) {
	    	x -= p.offsetLeft;
	    	y -= p.offsetTop;
	    	p = p.offsetParent;
	    }
 
	    x -= document.getElementById("regbezimage").offsetLeft; 
	    y -= document.getElementById("regbezimage").offsetTop; 
    }
} 

// display mouse position
function saveMousePosition() {
	// set cords
	document.getElementById('map_x').value = (document.all) ? x-2 : x;
	document.getElementById('map_y').value = (document.all) ? y-2 : y;
	// position dot
	var dot = document.getElementById('dot');
	dot.style.display = "block";
	dot.style.left = (document.all) ? x-2 : x;
	dot.style.top = (document.all) ? y-2 : y;
	// calculate ratio
	x_ratio = mx/document.getElementById('map_x').value;
	y_ratio = my/document.getElementById('map_y').value;
	// unlock fields
	//document.getElementById('map_x').readOnly = false;
	//document.getElementById('map_y').readOnly = false;
	// position title
	document.getElementById('title').style.display = "block";
	updateTitle();
}

// update dot position
function updateDot() {
	var x = document.getElementById('map_x').value*x_ratio;
	var y = document.getElementById('map_y').value*y_ratio;

	var dot = document.getElementById('dot');
	dot.style.display = "block";
	dot.style.left = (document.all) ? x-2 : x;
	dot.style.top = (document.all) ? y-2 : y;
	updateTitle();
}

// update title position
function updateTitle() {
	// position dot
	var x = document.getElementById('dot').style.left.replace(/px/g,"");
	var y = document.getElementById('dot').style.top.replace(/px/g,"");

	// position title
	var title = document.getElementById('title');
	var ak = document.getElementById('map_title').value;
	var pos = document.getElementById('map_pos').value;
	title.innerHTML = ak;
	var chars = ak.length;
	
	x = parseInt(x);
	y = parseInt(y);
	
	switch (pos) {
		// top
		case "1": y -= 12; x -= (chars/2*fontwidth); break;		// center
		case "2": y -= 12; x -= 0; break;						// right
		case "3": y -= 12; x -= chars*fontwidth; break;			// left
		// middle
		case "4": y -= 0; x -= chars/2*fontwidth; break;		// center
		case "5": y -= 0; x -= 0; break;						// right
		case "6": y -= 0; x -= chars*fontwidth; break;			// left
		// bottom
		case "7": y += 5; x -= chars/2*fontwidth; break;		// center
		case "8": y += 5; x -= 0; break;						// right
		case "9": y += 5; x -= chars*fontwidth; break;			// left
	}
	
	title.style.left = x;
	title.style.top = y;
}

// toggle map
function toggleMap(id) {
	if (id && id != 'undefined' && id!=0) {
		toggleBlocks("none");
		document.getElementById('mapcell').style.height="0px";
	} else {
		toggleBlocks("block");
		document.getElementById('mapcell').style.height=document.getElementById('regbezmap').style.height;
	}
}

// redirect to subaccount form
function loadSubaccountForm(label,regbez,editid) {
	if (editid != 0) {
		document.location.href = "../../subaccounts/admin/subaccountsdata.php?editid=" + editid;
	} else {
		document.location.href = "../../subaccounts/admin/subaccountsdata.php?label=" + label + "&regbez=" + regbez;
	}
}

// update select subaccount
function updateSelectSubaccount(id) {
	//if (document.getElementById('createmap').checked != true) return;
	var xmlhttp = createHttpRequest();
	var target = domain + "/modules/map/ajax/updateSelectSubaccount.php";
	xmlhttp.open("GET", target + "?id=" + id, 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);
			}
		}
		catch(e) {
			status = '';
		}
	  }
	}
	xmlhttp.send(null);
}

// toggle subaccount list
function toggleSubaccountList() {
	if (document.getElementById('subaccountlist')) {
		if (document.getElementById('subaccount').checked == true) {
			document.getElementById('subaccountlist').style.display = "block";
		} else {
			document.getElementById('subaccountlist').style.display = "none";
		}
	}
}
