//=======================================================================================
// add datepicker to date fields


function initializeMap(id) {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById(id));
		map.setUIToDefault(); 
		map.setCenter(new GLatLng(0, 0), 0);
		geocoder = new GClientGeocoder();
	} 
}
function showAddress(address, iconPath){
	var new_icon = new GIcon();
	new_icon.image = iconPath;
	new_icon.size = new GSize(16,16);
	new_icon.iconAnchor = new GPoint(8,9)  
	new_icon.infoWindowAnchor = new GPoint(7,7)  
	var opt;  
	opt = {};  
	opt.icon = new_icon;
	geocoder.getLatLng(
		    address,
		    function(point) {
		    	if (!point) {
		    		//alert(address + " not found");
		    	} else {
		    	map.setCenter(point, 13);
		    	var marker = new GMarker(point, opt);
		    	map.addOverlay(marker);
		    	//marker.openInfoWindowHtml(address);
		    }
		}
	);
}

function addJQDatePickers () {
//	alert("addDatePickers");
	jQuery.each($(".date"), function(index, value) {
//		alert("value " + value);
		$("#" + value.id).datepicker({showOn: 'button', buttonImage: pathprefix + '/images/calendar.png', buttonImageOnly: true});
		//$("#" + value.id).datepicker($.datepicker.regional['us']);
	 
//		   console.log("index", index, "value", value);
	});
}

//=======================================================================================
// utility method to get the 'cutter-url'

function getCutterURL (theURL, theCutID) {
	var anURL = null;
	if (theURL.indexOf("?") == -1) {
		anURL = theURL + "?client_request_ajaxcutid=" + theCutID;
	}
	else {
		anURL = theURL + "&client_request_ajaxcutid=" + theCutID;
	}
	return anURL;
}

//=======================================================================================
// utility to close a dialog

function __closeDialog() {
	$("#dialog").dialog("destroy");
	$("#dialog").dialog("close");
	$("#dialog").html("");
}

//=======================================================================================
//method used to open a modal form dialog

function openModalDialog (theDialogLink) {
	try{
		$("#dialog").dialog("destroy");		
		var myURL = getCutterURL(theDialogLink, 'content');
		$("#dialog").dialog("open");
//		alert(myURL);
		$("#dialog").load(myURL, {}, __internal_modaldialogcallback);
	}
	catch (error) {
		Console.log(error);
	}
}


function __internal_modaldialogcallback (responseText, textStatus, XMLHttpRequest) {
	replaceTextAreasByWYSIWYGEditorInID('dialog');
	
	addDatePickers();
	try {
		$("#dialog").dialog(
			{
				bgiframe: true,
				resizable: true,
				minHeight: 303, // sonst geht es im IE6 nicht, function _size in ui.dialog.js, zeile 431 geht sonst nicht
				height: 300,
				width: 600,
				modal: true,
				overlay: {
					backgroundColor: '#000',
					opacity: 0.5
				},
				buttons: {
					"Schliessen": function() {
						__closeDialog();
					}
				}
			}
		);
	}
	catch (error) {
		//alert("callback-error: " + error.number + ", " + error.description);
		Console.log(error);
	}
}

//=======================================================================================
//method used to open a modal form dialog

function openProgressPopup (theDialogLink) {
	try{
		$("#dialog").dialog("destroy");		
		var myURL = getCutterURL(theDialogLink, 'content');
		$("#dialog").dialog("open");
//		alert(myURL);
		$("#dialog").load(myURL, {}, __internal_modaldialogcallbackProgressPopup);
	}
	catch (error) {
		alert("Fehler: " + error);
	}
}


function __internal_modaldialogcallbackProgressPopup (responseText, textStatus, XMLHttpRequest) {
//	alert(responseText);
	replaceTextAreasByWYSIWYGEditorInID('dialog');
	addDatePickers();
	try {
		$("#dialog").dialog(
			{
				bgiframe: true,
				resizable: true,
				minHeight: 250, // sonst geht es im IE6 nicht, function _size in ui.dialog.js, zeile 431 geht sonst nicht
				height: 300,
				width: 600,
				modal: true,
				closable: true,
				overlay: {
					backgroundColor: '#000',
					opacity: 0.5
				}
			}
		);
	}
	catch (error) {
//		alert("callback-error: " + error.number + ", " + error.description);
	}
}


//=======================================================================================
// method used to open a modal form dialog

function openEditDialog (thePart2Replace, theLink, theDialogLink) {
	try{
		$("#dialog").dialog("destroy");
		$("#callbackURL").attr("val", theLink);
		$("#callbackTarget").attr("val", thePart2Replace);
		var myURL = getCutterURL(theDialogLink, 'content');
		$("#dialog").dialog("open");
//		alert(myURL);
		$("#dialog").load(myURL, {}, __internal_callback_2);
//		__internal_callback_2(null, null, null);
	}
	catch (error) {
//		alert("Fehler, letzter Schritt: " + step + ", error=" + error);
	}
}

function __internal_callback_2 (responseText, textStatus, XMLHttpRequest) {
//	alert(responseText);
	replaceTextAreasByWYSIWYGEditorInID('dialog');
	addDatePickers();
	try {
		$("#dialog").dialog(
			{
				bgiframe: true,
				resizable: true,
				minHeight: 400, // sonst geht es im IE6 nicht, function _size in ui.dialog.js, zeile 431 geht sonst nicht
				height: 600,
				width: 800,
				modal: true,
				overlay: {
					backgroundColor: '#000',
					opacity: 0.5
				}
			}
		);
	}
	catch (error) {
//		alert("callback-error: " + error.number + ", " + error.description);
	}
}

//=======================================================================================
// method used to open a docustore menu

function openMenuDialog (event, theHeight, title, theDialogLink) {
	try{
		size = title.length;
		if(size > 23) {
			title = title.substring(0, 15)+"..."+title.substring(size-4, size);
		}
		$("#docuMenu").dialog("destroy");		
		var myURL = getCutterURL(theDialogLink, 'content');
		$("#docuMenu").dialog("open");
		$("#docuMenu").attr("title", title);		
		$("#docuMenu").attr("height", theHeight);	
		$("#docuMenu").attr("position", new Array(new Number(event.clientX-160), new Number(event.clientY-40)));
		$("#docuMenu").load(myURL, {}, __internal_menu);
	  $("#docuMenu").attr("position", new Array(new Number(event.clientX-160), new Number(event.clientY-40)));
		//alert(event.clientX);
	}
	catch (error) {
		alert("Fehler: " + error);
	}
}

function __internal_menu (responseText, textStatus, XMLHttpRequest) {
	//alert(responseText);	
	replaceTextAreasByWYSIWYGEditorInID('docuMenu');
	addDatePickers();
	arr = $("#docuMenu").attr("position").toString().split(",");	
	try {
		$("#docuMenu").dialog(
			{
				position: [new Number(arr[0]), new Number(arr[1])],
				backgroundColor: '#ffffff',					
				padding:0,
				margin:0,				
				bgiframe: true,				
				resizable: false,
				minHeight: 100, // sonst geht es im IE6 nicht, function _size in ui.dialog.js, zeile 431 geht sonst nicht
				height: $("#docuMenu").attr("height"),
				width: 230,				
				modal: false				
			}
		);
	}
	catch (error) {
//		alert("callback-error: " + error.number + ", " + error.description);
	}
}

// =======================================================================================
// cancel a modal form dialog

function cancelFormDialog() {
	__closeDialog();
}

//=======================================================================================
//submit a modal form dialog and refresh part of the underlaying page

function submitFormDialog(formID) {
	writeBackFCK2textarea(formID);
	var dataString = $("#" + formID).serialize();
//	alert(dataString);
	$.ajax({
		type: "POST",
		url: pathprefix + "/boxalino/client/common/error.html?client_request_ajaxcutid=error",  
		data: dataString,
		success: function(thehtml) {
//			alert(thehtml);
//todo: auswerten des resultates
			$("#infoarea").html(thehtml);			
			var callbackURL = $("#callbackURL").attr("val");			
			var callbackTarget = $("#callbackTarget").attr("val");			
//			alert("callback url = " + callbackURL);
//			alert("callback target = " + callbackTarget);
			if (callbackTarget && callbackURL) {
				ajaxReplace(callbackTarget, callbackURL);
			}
			
			__closeDialog();
		}
	});  										
}

function submitFormDialog2(formID) {
	writeBackFCK2textarea(formID);
	var dataString = $("#" + formID).serialize();
//	alert(dataString);
	$.ajax({
		type: "POST",
		url: pathprefix + "/boxalino/client/common/error.html?client_request_ajaxcutid=error",  
		data: dataString,
		success: function(thehtml) {
			$("#infoarea").html(thehtml);
			if (!thehtml || thehtml.indexOf("errorMarker") < 0) {				
				var callbackURL = $("#callbackURL").attr("val");			
				var callbackTarget = $("#callbackTarget").attr("val");			
	//			alert("callback url = " + callbackURL);
	//			alert("callback target = " + callbackTarget);
				if (callbackTarget && callbackURL) {
					ajaxReplace(callbackTarget, callbackURL);
				}
				
				__closeDialog();
			}
		}
	});  										
}

//urlToCall: (pathprefix + url), this page will create for example an item an set the values

function submitFormDialogAndCallURL(formID, urlToCall) {
	writeBackFCK2textarea(formID);
	var dataString = $("#" + formID).serialize();
	//alert(dataString);
		
	$.ajax({
		type: "POST",
		url: urlToCall + "?client_request_ajaxcutid=error",  
		data: dataString,
		success: function(thehtml) {	
//todo: auswerten des resultates
			$("#infoarea").html(thehtml);	
			var callbackURL = $("#callbackURL").attr("val");
			var callbackTarget = $("#callbackTarget").attr("val");
		// alert("callback url = " + callbackURL);
	 // alert("callback target = " + callbackTarget);
			ajaxReplace(callbackTarget, callbackURL);
			__closeDialog();		
		}
	});  											
}


//=======================================================================================
//submit form without default action

function submitFormExceptDefaultAction (formID, targetID, theURL) {
//	var hiddenField = $("input[name='defaultAction']");
	var hiddenField = document.getElementById(formID + "_defaultAction");
	var origDA = "";
	if (hiddenField != null) {
		origDA = hiddenField.value;
		hiddenField.name="client_request_ajaxcutid";
		hiddenField.value="error";
	}
	writeBackFCK2textarea(formID);
	var dataString = $("#" + formID).serialize();
	if (hiddenField != null) {
		hiddenField.name = "defaultAction";
		hiddenField.value = origDA;
	}
//	alert(dataString);
	$.ajax({
		type: "POST",
		url: pathprefix + "/boxalino/client/common/error.html",  
		data: dataString,
		success: function(thehtml) {
//			alert(thehtml);
//todo: auswerten des resultates
//			$("#infoarea").html(thehtml);
//			alert("callback url = " + callbackURL);
//			alert("callback target = " + callbackTarget);
//			alert(targetID + "-->" + theURL);
			ajaxReplace(targetID, theURL);
		}
	});  
	
}

//=======================================================================================
//submit form without default action

function execAndUpdate (theFormID, theTargetID, theParams) {
//	var hiddenField = $("input[name='defaultAction']");
	var hiddenField = document.getElementById(theFormID + "_defaultAction");
	var origDA = "";
	if (hiddenField != null) {
		origDA = hiddenField.value;
		hiddenField.name="client_request_ajaxcutid";
		hiddenField.value="error";
	}
	writeBackFCK2textarea(theFormID);
	var dataString = $("#" + theFormID).serialize();
	if (hiddenField != null) {
		hiddenField.name = "defaultAction";
		hiddenField.value = origDA;
	}
//	dataString = encodeURIComponent(dataString);
//	alert(dataString);
	$.ajax({
		type: "POST",
		url: pathprefix + "/boxalino/client/common/error.html",  
		data: dataString,
		success: function(thehtml) {
//			alert(thehtml);
//todo: auswerten des resultates
//			$("#infoarea").html(thehtml);
//			alert("callback url = " + callbackURL);
//			alert("callback target = " + callbackTarget);
//			alert(targetID + "-->" + theURL);
			var refresh = $("#" + theTargetID).attr("refresh");
//			alert(refresh);
			refresh = refresh + "&" + theParams;
//			alert(refresh);
			ajaxReplace(theTargetID, refresh);
		}
	});
	return false;
}

//=======================================================================================
//submit form without default action

function confirmExecAndUpdate (theConfirmationDialogURL, theFormID, theTargetID, theParams) {
	$("#dialog").attr("the-form-id", theFormID);
	$("#dialog").attr("the-target-id", theTargetID);
	$("#dialog").attr("the-params", theParams);

	confirmActionWithFunction (theConfirmationDialogURL, _confirmExecAndUpdate);
}

function _confirmExecAndUpdate () {
	var theFormID = $("#dialog").attr("the-form-id");
	var theTargetID = $("#dialog").attr("the-target-id");
	var theParams = $("#dialog").attr("the-params");
//	var hiddenField = $("input[name='defaultAction']");
	var hiddenField = document.getElementById(theFormID + "_defaultAction");
	var origDA = "";
	if (hiddenField != null) {
		origDA = hiddenField.value;
		hiddenField.name="client_request_ajaxcutid";
		hiddenField.value="error";
	}
	writeBackFCK2textarea(theFormID);
	var dataString = $("#" + theFormID).serialize();
	if (hiddenField != null) {
		hiddenField.name = "defaultAction";
		hiddenField.value = origDA;
	}
//	alert(dataString);
	$.ajax({
		type: "POST",
		url: pathprefix + "/boxalino/client/common/error.html",  
		data: dataString,
		success: function(thehtml) {
//			alert(thehtml);
//todo: auswerten des resultates
//			$("#infoarea").html(thehtml);
//			alert("callback url = " + callbackURL);
//			alert("callback target = " + callbackTarget);
//			alert(targetID + "-->" + theURL);
			var refresh = $("#" + theTargetID).attr("refresh");
			refresh = refresh + "&" + theParams;
//			alert(refresh);
			ajaxReplace(theTargetID, refresh);
		}
	});
	return false;
}

//=======================================================================================
// process a modal form dialog and refresh part of the underlaying page

function executeHREFDialog(params) {
	var callbackURL = encodeURI($("#callbackURL").attr("val"));
	var callbackTarget = $("#callbackTarget").attr("val");
	var theurl = null;
	if (callbackURL.indexOf("?") == -1) {
		theurl = callbackURL + "?" + params;
	}
	else {
		theurl = callbackURL + "&" + params;
	}
	ajaxReplace(callbackTarget, theurl);
	__closeDialog();
}

//=======================================================================================
//method used to protect actions in hrefs

function confirmAction (thePart2Replace, theLink, theDialogLink) {
	$("#dialog").dialog("destroy");
	ajaxReplace('dialog', theDialogLink);
//	$("#dialog").dialog('open');
	$("#dialog").dialog(
			{
		bgiframe: true,
		resizable: false,
		height:250,
		minHeight: 250, // sonst geht es im IE6 nicht, function _size in ui.dialog.js, zeile 431 geht sonst nicht
		width:400,
		minWidth: 250,
		modal: true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		close: function () {
			__closeDialog();
		},
		buttons: {
			"Nein": function() {
			__closeDialog();
			},
			"Ja": function() {
//				alert("Lösche " + theLink.href);
//				alert("Lösche " + theLink);
//				alert(thePart2Replace + " --> " + theLink);
				ajaxReplace(thePart2Replace, theLink);
				__closeDialog();
			}
		}
	});
}

//=======================================================================================
//method used to protect actions in hrefs

function confirmActionWithFunction (theConfirmationDialogURL, theFunction) {
	$("#dialog").dialog("destroy");
	ajaxReplace('dialog', pathprefix + theConfirmationDialogURL);
//	$("#dialog").dialog('open');
	$("#dialog").dialog(
			{
		bgiframe: true,
		resizable: false,
		height:250,
		minHeight: 250, // sonst geht es im IE6 nicht, function _size in ui.dialog.js, zeile 431 geht sonst nicht
		width:400,
		minWidth: 250,
		modal: true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		close: function () {
			__closeDialog();
		},
		buttons: {
			"Nein": function() {
				__closeDialog();
			},
			"Ja": function() {
//				alert("Lösche " + theLink.href);
//				alert("Lösche " + theLink);
				theFunction();
				__closeDialog();
			}
		}
	});
}

//=======================================================================================
//method used to protect actions in hrefs

function confirmActionWithFunctionParam (theConfirmationDialogURL, theFunction, theParam) {
	$("#dialog").dialog("destroy");
	ajaxReplace('dialog', pathprefix + theConfirmationDialogURL);
//	$("#dialog").dialog('open');
	$("#dialog").dialog(
			{
		bgiframe: true,
		resizable: false,
		height:250,
		minHeight: 250, // sonst geht es im IE6 nicht, function _size in ui.dialog.js, zeile 431 geht sonst nicht
		width:400,
		minWidth: 250,
		modal: true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		close: function () {
			__closeDialog();
		},
		buttons: {
			"Nein": function() {
				__closeDialog();
			},
			"Ja": function() {
//				alert("Lösche " + theLink.href);
//				alert("Lösche " + theLink);
				theFunction(theParam);
				__closeDialog();
			}
		}
	});
}

//=======================================================================================
// helper method to replace a certain tag with the content of the given URL

function ajaxReplace (targetID, url) {
	var theurl = null;
	if (url.indexOf("?") == -1) {
		theurl = url + "?client_request_ajaxcutid=" + targetID;
	}
	else {
		theurl = url + "&client_request_ajaxcutid=" + targetID;
	}
//	alert(targetID + ", " + theurl);
	$("#" + targetID).load(theurl);
}




function ajaxReplaceWithAction (targetID, url, action) {
	var theurl = null;
	if (url.indexOf("?") == -1) {
		theurl = url + "?client_request_ajaxcutid=" + targetID;
	}
	else {
		theurl = url + "&client_request_ajaxcutid=" + targetID;
	}
//	alert(targetID + ", " + theurl);
//	alert(action);
	$("#" + targetID).load(theurl, null, action);
}



//============================================ Begin of BETA code
function pausecomp(millis)
{
	var date = new Date();
	var curDate = null;
	
	do { curDate = new Date(); }
	while(curDate-date < millis);
} 


var _loaded = false;

function _setLoaded() {
	_loaded = true;
}
function ajaxReplaceSynchron (targetID, url) {
	_loaded = false;
	ajaxReplaceWithAction(targetID, url, _setLoaded);
	var i = 0;
	while (!_loaded && i < 20) {
		pausecomp(100);
		i = i + 1;
	}
}
//============================================ End of BETA code

//=======================================================================================
// helper method to replace a certain tag with the content of the given URL
// this function allows to have different IDs of the part to be replaced and the part
// to use as the source from the page retrieved by the URL

function ajaxReplace2 (targetID, url, cutID) {
	var theurl = null;
	if (url.indexOf("?") == -1) {
		theurl = url + "?client_request_ajaxcutid=" + cutID;
	}
	else {
		theurl = url + "&client_request_ajaxcutid=" + cutID;
	}
//	alert(targetID + ", " + theurl);
	$("#" + targetID).load(theurl);
}


//=======================================================================================
// datepicker utility

function addDatePickers () {
	$("#adate").datepicker(
			{
				numberOfMonths: 3,
				showButtonPanel: true
			}
		);
}

