var ajaxCache = new Array();
var ajaxJSONCache = new Array();
var map = null;

var searchRootUrl = /([a-z\/_]+)scripts/;
searchRootUrl.exec(location.pathname);
var rootUrl = RegExp.$1;
var ajaxUrl = rootUrl + 'scripts/ajax_json.php?';


// format overlib
ol_bgcolor = '#0CC1E3';
ol_fgcolor = '#ffffff';
ol_closecolor = '#ffffff';

function isError(error) {
	if (error != null && error['PEARError'] != undefined && error['PEARError'] == true) {
		return true;
	}
	return false;
}

Array.prototype.remove = function(s) {
	for (i = 0;i < this.length;i++) {
		if (s == this[i]) this.splice(i, 1);
	}
}

Array.prototype.exists = function(s) {
	var exists = false;
	for (i = 0;i < this.length;i++) {
		if (s == this[i]) exists = true;
	}
	return exists;
}

function sprintf() {
	if(sprintf.arguments.length < 2 ) {
		return;
	}

	var data = sprintf.arguments[ 0 ];
	for (var k = 1;k < sprintf.arguments.length; ++k ) {
		switch( typeof( sprintf.arguments[ k ] ) ) {
			case 'string':
				data = data.replace( /%s/, sprintf.arguments[ k ] );
				break;
			case 'number':
				data = data.replace( /%d/, sprintf.arguments[ k ] );
				break;
			case 'boolean':
				data = data.replace( /%b/, sprintf.arguments[ k ] ? 'true' : 'false' );
				break;
			default:
				/// function | object | undefined
				break;
		}
	}
	return(data);
}

// register sprintf function for strings
if (!String.sprintf ) {
	String.sprintf = sprintf;
}

function getText(id, pid) {
	// TRANSLATIONS is loaded in javascript/jslang.php
	if (pid == undefined) {
		pid = 'framework';
	}

	var text = TRANSLATIONS[pid][id];
	if (text == undefined || text == '') {
    	// check if we find the text in frameworkcdbsweb
    	text = TRANSLATIONS['frameworkcdbsweb'];
    	if (text != undefined) text = TRANSLATIONS['frameworkcdbsweb'][id];
    	if (text == undefined || text == '') {
    		// check if we find the text in framework
    		text = TRANSLATIONS['framework'];
    		if (text != undefined) text = TRANSLATIONS['framework'][id];
    		if (text == undefined || text == '') {
    			// no text found
    			return '[MISSING: ' + id + '-' + pid + ']';
    		}
    	}
   	}

   	// replace \n in char \n so it breaks the line at this position
   	text = text.replace(/\\n/, '\n');

	return text;
}

function new_window (file) {
	win = window.open(file, "NewWindow","width=600,height=500,scrollbars=yes,resizable=no,screenX=100,screenY=50");
}

function ask(id, replacement) {
	var translation = getTranslation(id);
	if (replacement != 'undefined') {
		translation = String.sprintf(translation, replacement);
	}
	return confirm(translation);
}

function ajaxJSONCall(call, noCache) {
	if (ajaxJSONCache[call] == undefined || noCache == true) {
		var chunks = document.location.pathname.match(/([a-z0-9\/]*\/scripts\/)/i);
		var baseUrl = chunks[1] + "ajax_json.php?";
		var callUrl = baseUrl + call;
		ajaxJSONCache[call] = eval("(" + HTML_AJAX.grab(callUrl) + ")");
	}
	return ajaxJSONCache[call];
}

function geocode(form) {
//	var street = form.elements['street'];
//	var streetnumber = form.elements['streetnumber'];
//	var zip = form.elements['zip'];
//	var city = form.elements['city'];
//
//	var addressToGeocode = "";
//	if (zip.value != "") {
//		addressToGeocode += " " + zip.value;
//	}
//	if (city.value != "") {
//		addressToGeocode += " " + city.value;
//	}
//	if (street.value != "") {
//		addressToGeocode += " " + street.value;
//	}
//	if (zip.streetnumber != "") {
//		addressToGeocode += " " + streetnumber.value;
//	}

	addressToGeocode = "Wilhelm-Maybach-Straße 3, 70734 Fellbach";

	//if (form.elements['coordinates'] != undefined && city.value != "") {
		var gcg = new GClientGeocoder();
		gcg.getLatLng(addressToGeocode,
			function (point) {
				if (point != null) {
					//form.elements['coordinates'].value = "(" + point.y + "," + point.x + ")";
					prompt("C", "(" + point.y + "," + point.x + ")");
				}
			}
		);
	//}
}

function initializeMap() {
	var mapDiv = document.getElementById("map");
	mapDiv.style.display = 'block';

	if (map == null) {
		map = new GMap2(mapDiv);
		map.setCenter(new GLatLng(45.120053, 6.943359), 5);
		map.addControl(new GSmallMapControl());
	}
}

function createMarker(point, html, icon) {
	var marker = null;
	if (icon == undefined) {
		//var marker = new GMarker(point);
		var marker = new PdMarker(point);
	}
	else {
		//var marker = new GMarker(point, {icon: icon});
		var marker = new PdMarker(point, icon);
	}
	//var marker = new GMarker(point);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	});
	return marker;
}

function selectAll(form, fieldname) {
	if (form.elements[fieldname][0] != undefined && form.elements[fieldname].length > 0) {
		var checked = !(form.elements[fieldname][0].checked);
		for (i = 0;i < form.elements[fieldname].length;i++) {
			form.elements[fieldname][i].checked = checked;
		}
	}
}