var lastAjaxResponse = '';
var divURLs = Array();

document.onkeydown = function() {
	if (document.all) // ie check
		if (event.ctrlKey && event.shiftKey && event.keyCode == 67) {
			var hWnd = window.open('?', 'ajaxConsole');
			hWnd.document.write(lastAjaxResponse);
		}
};

if (!tabIndexes)
	var tabIndexes = Array();

function loadDivURL(divName, url, onComplete) {

	if (tabIndex = tabIndexes[divName]) {
		divName = tabDivNames[tabIndex];
	}

	url += '&currentTab=' + divName + '&currentDiv=' + divName;

	divURLs[divName] = url;

	var ajax = new Ajax.Updater(divName, url + '&ajaxRequest=1', {
		onComplete : function(originalRequest) {
			lastAjaxResponse = originalRequest.responseText;
			if (onComplete)
				onComplete();
		},
		onLoading : onLoading(divName),
		evalScripts : true
	});
}

function ajaxUpdater(params) {
	var ajax = new Ajax.Updater(
			params.divId,
			params.page,
			{
				method : params.method,
				parameters : params.parameters,
				postBody : params.postBody,
				onComplete : params.onComplete,
				onSuccess : params.onSuccess,
				onLoading : function() {
					$(params.divId).innerHTML = '<img src="' + app_root + '/libraries/layout/images/spinner.gif" />';
				},
				evalScripts : params.evalScripts
			});
}

function loadDocument(url, onComplete) {
	var ajax = new Ajax.Request(url, {
		method : 'get',
		onComplete : onComplete
	});
}

function loadFrameURL(frameName, url) {
	window.top.frames[frameName].location.href = url;
}

function reloadDiv() {
	for (i = 0; i < arguments.length; i++) {
		divName = arguments[i];
		if (url = divURLs[divName]) {
			loadDivURL(divName, url);
		}
	}
}

function loadDivForm(form, divName) {
	var url = form.action + '&' + Form.serialize(form);
	loadDivURL(divName, url);
}

function postForm(form, onComplete) {
	form = $(form);
	updateFCKEditors(form);
	var postBody = Form.serialize(form);
	var ajax = new Ajax.Request(form.action + '&postForm=1&ajaxRequest=1', {
		method : 'post',
		postBody : postBody+'&AJAX=true',
		onComplete : function() {
			if (onComplete)
				onComplete();
		}
	});
}

function postData(action, postBody, onComplete) {
	var ajax = new Ajax.Request(action + '&postForm=1&ajaxRequest=1', {
		method : 'post',
		postBody : postBody,
		onComplete : function() {
			if (onComplete)
				onComplete();
		}
	});
}

function onLoading(divName) {
	if (div = $(divName)) {
		div.innerHTML = 'Loading ...';
	}
}

/*
 * Update fckeditors in given form. @author Mark Vermeltfoort
 */

function updateFCKEditors(form) {
	if (!window.FCKeditorAPI)
		return;

	var inputs = $A(Form.getInputs(form, 'hidden'));

	inputs.each(function(input) {
		var FCKe;
		if (FCKe = FCKeditorAPI.GetInstance(input.name))
			FCKe.UpdateLinkedField();
	});

	// if(form.updateFCKeditor){
	// for(i=0;i<form.updateFCKeditor.length;i++){
	// form.updateFCKeditor[i]();
	// }
	// }
}

function updateAutocompleteIDField(input, selected_item) {
	if (typeof (input) == 'string') {
		if (input = $(input)) {
			input.value = selected_item.id;
		}
	}
	if (input = $(input.id + '_id')) {
		input.value = selected_item.id;
	}
}

function resetAutocompleteIDField(element, entry) {
	if (typeof (element) != 'string') {
		element = element.id + '_id';
	}
	var values = entry.split('=');

	if ((input = $(element)) && values[1] == '') {
		input.value = 0;
	}

	return entry;
}

function copy_clip(text) {
	if (window.document.all) {
		window.clipboardData.setData("Text", text);
		return;
	} else {
		var flashcopier = 'flashcopier';
		if (!document.getElementById(flashcopier)) {
			var divholder = document.createElement('div');
			divholder.id = flashcopier;
			document.body.appendChild(divholder);
		}
		document.getElementById(flashcopier).innerHTML = '';
		var divinfo = '<embed src="'
				+ www_root
				+ 'libraries/layout/flash/_clipboard.swf" FlashVars="clipboard='
				+ encodeURIComponent(text)
				+ '" width="50" height="50" type="application/x-shockwave-flash"></embed>';
		document.getElementById(flashcopier).innerHTML = divinfo;
	}
}

var interval = 60 * 5; // 5 min

function keepAlive() {
	postData(app_root + '/app/keep_alive/');
}

new PeriodicalExecuter(keepAlive, interval);

function printObject(object) {
	var wnd = window.open('', 'print', '');
	wnd.document
			.write('<div style="padding-left: 5%;width: 600px;">' + object.innerHTML + '</div>');
	wnd.document.close();
	wnd.focus();
	wnd.print();
	wnd.close();
}

if (window.opener) {
	if (window.opener.updateElements) {
		window.opener.updateElements();
	}
}

function enterToTab(e, focusobject) {
	var keynum;
	if (window.event) // IE
	{
		keynum = e.keyCode;
	} else if (e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which;
	}
	if (keynum == 13 && (focusobject = $(focusobject))) {
		focusobject.focus();
		focusobject.select();
		return false;
	}
}

function addListener(element, type, expression, bubbling) {
	bubbling = bubbling || false;

	if (window.addEventListener) { // Standard
		element.addEventListener(type, expression, bubbling);
		return true;
	} else if (window.attachEvent) { // IE
		element.attachEvent('on' + type, expression);
		return true;
	} else
		return false;

}
function basename(path, suffix) { 	
	// Returns the filename component of the path   
	//  
	// version: 812.316     
	// discuss at: http://phpjs.org/functions/basename 
	// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 
	// +   improved by: Ash Searle (http://hexmen.com/blog/) 
	// +   improved by: Lincoln Ramsay 
	// +   improved by: djmix 
	// *     example 1: basename('/www/site/home.htm', '.htm');     
	// *     returns 1: 'home' 
	var b = path.replace(/^.*[\/\\]/g, ''); 
	
	if (typeof(suffix) == 'string' && b.substr(b.length-suffix.length) == suffix) { 
		b = b.substr(0, b.length-suffix.length);    
	} 
	return b; 
}


