var gmap = new Array(), geocoder = null, addressMarker;

function number_format (number, decimals, dec_point, thousands_sep) {

 var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;

 var d = dec_point == undefined ? "," : dec_point;

 var t = thousands_sep == undefined ? "." : thousands_sep, s = n < 0 ? "-" : "";

 var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;

 return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");

}

function get_after_tax(_value, _after_tax, obj) {

 _value = number_format(_value.toString(_value).split(",").join("."), 2, '.', '');

 _value = number_format(_value / ((_after_tax / 100) + 1), 2, ',', '.');

 if (obj) document.getElementById(obj).value = _value;
 else return _value;

}

function isnumeric(sText) {

 var ValidChars = "0123456789.";
 var IsNumber = true;
 var Char;

 for (i = 0; i < sText.length && IsNumber == true; i++) { 

	Char = sText.charAt(i); 

	if (ValidChars.indexOf(Char) == -1) IsNumber = false;

 }

 return IsNumber;

}

function urlencode(s) { return escape(s).replace(/\+/g, '%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40'); }

function get_field_value(_field, url_encode) { var _value = document.getElementById(_field) ? document.getElementById(_field).value : false; return url_encode ? urlencode(_value) : _value; }

function load_module(n) { incJS("scripts/select_kategorie.php?sel=0&module_id=" + n); }

function open_popup(_lnk, w, h) {

 var l = (screen.availWidth - w) / 2, t = ((screen.availHeight - h) / 2) - 50;
 var win = window.open(_lnk, "", "width=" + w + ",height=" + h + ",left=" + l + ",top=" + t + ",scrollbars=yes,status=yes,dependent=yes");

}

function open_upload_window(m_id, e_id, i_id) { open_popup("acp/eintraege_upload_image.php?module_id=" + m_id + "&entry_id=" + e_id + "&image_id=" + i_id, 480, 320); }

function gallery_upload_window(e_id, i_id) { open_popup("acp/seiten_tpl_upload_image.php?menu_link_id=" + e_id + "&image_id=" + i_id, 480, 320); }

function open_entry_permission(m_id, k_id) { open_popup("acp/eintraege_permission.php?module_id="+ m_id +"&kategorie_id=" + k_id, 480, 320); }

function open_map_marker_window(m_id, e_id) { open_popup("acp/eintraege_map_marker.php?module_id=" + m_id + "&entry_id=" + e_id + "&street=" + get_field_value('street', 1) + "&housenumber=" + get_field_value('housenumber', 1) + "&zipcode=" + get_field_value('zipcode', 1) + "&city=" + get_field_value('city', 1) + "&country=" + get_field_value('country', 1), 520, 400); }

function load_google_map(address, div_map, _mark) {

 if (GBrowserIsCompatible()) {

	var div_id = !div_map ? "map" : div_map;

	var dmap = document.getElementById(div_id);

	function set_latlng(lat, lng) { with (document) { if (getElementById("lat")) getElementById("lat").value = lat; if (getElementById("lat")) getElementById("lng").value = lng; } }

	if (dmap) {

	 dmap.style.overflow = "hidden";

	 if (typeof(gmap[div_id]) != 'object') {

		gmap[div_id] = new GMap2(dmap);

		gmap[div_id].addControl(new GMapTypeControl());

		gmap[div_id].addControl(new GSmallMapControl());

		var isize = dmap.offsetWidth < dmap.offsetHeight ? dmap.offsetWidth / 3 : dmap.offsetHeight / 3;

		gmap[div_id].addControl(new GOverviewMapControl(new GSize(isize, isize)));

		if (_mark) GEvent.addListener(gmap[div_id], "click", function(overlay, point) { gmap[div_id].clearOverlays(); if (point) { var marker = new GMarker(point, {draggable: true}); gmap[div_id].addOverlay(marker); gmap[div_id].panTo(point); set_latlng(point.lat(), point.lng()); GEvent.addListener(marker, "dragend", function(point) { gmap[div_id].panTo(point); set_latlng(point.lat(), point.lng()) }) } });

	 }
	 else if (!_mark) gmap[div_id].clearOverlays();

	 if (typeof(address) != 'object') {

		if (!geocoder) var geocoder = new GClientGeocoder();

		if (geocoder) geocoder.getLatLng(address, function(point) { if (point) { gmap[div_id].setCenter(point, 13); var marker = new GMarker(point, _mark ? {draggable: true} : false); gmap[div_id].addOverlay(marker); if (_mark) GEvent.addListener(marker, "dragend", function(point) { gmap[div_id].panTo(point); set_latlng(point.lat(), point.lng()) }) } });

	 }
	 else {

		var gdir = new GDirections(gmap[div_id], document.getElementById(div_id + '_directions'));

		sTo = ''; for(x = 1; x < address.length; x++) sTo += " to: " + address[x] + " ";

		gdir.load("from: " + address[0] + sTo);

	 }

	}

 }

}

function generatePassword(l) {

 var keylist = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";

 var password = '';

 for (i = 0; i < l; i++) password += keylist.charAt(Math.floor(Math.random() * keylist.length));

 return password;

}
