// JavaScript document

function is_msie(){
	var browser = "";
	browser = navigator.appName;	
	return (browser == 'Microsoft Internet Explorer') ? 1 : 0;
}

function findPosX(obj){
	var curleft = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj){
	var curtop = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function getPage(page_id, anchor, anchor_type)
{
	var add_anchor	= (anchor && anchor_type) ? '#'+anchor : '';
	var add_admin	= (location.search.indexOf("admin=1") != -1) ? '&admin=1' : '';
	var url = front_end_action + '?cmsPage=' + page_id + '&sid=' + sid + add_admin + add_anchor;
	location.href = url;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function IfEnter(e)
{
	var keyCode = 0;
	if (e.keyCode) {
		keyCode = e.keyCode;
	} else if (e.which) {
		keyCode = e.which;
	}
	return (keyCode == 13) ? true : false;
}

function getForm() {
	var f = document.forms['kernel_form'];
	return f;
}

function submit_form()
{
	if (typeof(getForm().onsubmit) == "function") {
		getForm().onsubmit();
	}
	if (default_lang_prefix == lang_prefix)
		getForm().elements['t'].value = lang_prefix + getForm().elements['ct'].value;
	else
		getForm().elements['t'].value = getForm().elements['ct'].value.replace(/^\//, '');
	
	getForm().action = location.href;
	getForm().submit();
	getForm().target = '';
	//getForm().t.value = t;	
}

function setPage(prefix, page)
{
	document.kernel_form.elements[prefix + 'page'].value = page;
	document.kernel_form.elements[prefix + 'action_grid'].value = 'gotoPage';
	submit_form();
}

function openwincms( url, name, w, h)
{
	openImagePopup( url, name, w, h);
}

function openImagePopup( url, name, w, h)
{
	var win = null;
	
/*
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
*/
	name = name.replace(/\s/g, '_');
	
	var settings = 'height=200,width=200,scrollbars=no,resizable=no,status=no';
	win = window.open( '' , name , settings );
	var win_content = '<html>\n<head>\n\t<title>Image</title>\n'
						+ '<style type="text/css">\n'
						+ 'body {\n'
						+ '\toverflow: hidden;\n'
						+ '}\n'
						+ '</style>\n'
						+ '<script type="text/javascript" language="javascript">\n'
						+ 'var NS = (navigator.appName=="Netscape") ? true : false;\n'
						+ 'function initImagePopup() {\n'
						+ '\tiWidth = (NS) ? window.innerWidth : document.body.clientWidth;\n'
						+ '\tiHeight = (NS) ? window.innerHeight : document.body.clientHeight;\n'
						+ '\tim_w = document.images[0].width;\n'
						+ '\tim_h = document.images[0].height;\n'
						+ '\tiWidth = im_w - iWidth;\n'
						+ '\tiHeight = im_h - iHeight;\n'
						+ '\tself.resizeBy(iWidth, iHeight);\n'
						+ '\tself.moveTo(screen.width/2 - Math.floor(im_w/2), screen.height/2 - Math.floor(im_h/2));\n'
						+ '};\n'
						+ '</script>\n'
						+ '</head>\n\n'
						+ '<body onload="initImagePopup()" leftmargin="0" topmargin="0" marginheight="0" marginwidth="0">\n'
						+ '<a href="javascript:;" onclick="javascript:self.close(); return false;">\n'
						+ '<img src="'+url+'" border="0" alt="Close popup" title="Close popup">\n'
						+ '</a>\n'
						+ '</body>\n</html>';
	win.document.write( win_content );
	win.document.close();
	win.focus();
}

function addEvent(obj, evType, fn){
    if (obj.addEventListener){
        obj.addEventListener(evType, fn, false);
        return true;
    } else if (obj.attachEvent){
        var r = obj.attachEvent('on'+evType, fn);
        return r;
    } else {
        return false;
    }
}

function jump_to_link(select_obj)
{
	var selected_opt = select_obj.options[select_obj.selectedIndex]
	if (selected_opt.value != '' && selected_opt.value.indexOf('http://') != -1)
		window.location = selected_opt.value;
}

function show_props(obj, objName, line_end, filter_str, search_in_value) {
	if (!line_end) line_end = "\n";
	if (typeof(search_in_value) == 'undefined') search_in_value = true;
	var result = "Show properties for object "+objName;
	if(filter_str){
		reg = new RegExp (filter_str, "i");
		result += ' and search string "'+filter_str+'"';
	}
	result +=':'+line_end+line_end;
	i = 0;
	for (var prop in obj) {
		if (obj[prop] != null){
			if (prop == 'currentStyle' || prop == 'filters' || prop == 'behaviorUrns' || prop == 'attributes' || prop == 'childNodes') continue;
			prop_value = obj[prop].toString();
		}
		else if(!isNaN(obj[i])) 
			prop_value = obj[i].toString();
		else
			continue;
		if(!filter_str || prop.toString().search(reg) != -1 || (search_in_value && prop_value.search(reg) != -1 ))
			result += objName + "." + prop + " = " + prop_value + line_end;
		i++;
	}
	return result;
}

function show_props_in_window(obj, objName, line_end, filter_str, search_in_value)
{
	var settings = 'height=500,width=500,scrollbars=yes';
	var win = window.open( '' , 'Show_Props_Window' , settings );
	var win_content = '<pre>'+show_props(obj, objName, line_end, filter_str, search_in_value)+'</pre>';
	win.document.write( win_content );
	win.document.close();
	win.focus();
}

function popup(
	location, target, width, height,
	resizable, scrollbars, status, menubar, toolbar
)
{
	var str = '';
	if (width > 0 && height > 0) {
		str = str + 'width=' + width + ',height=' + height + ',';
	}
	var resizable_str  = resizable ? 'yes' : 'no';
	var scrollbars_str = scrollbars ? 'yes' : 'no';
	var status_str     = status  ? 'yes' : 'no';
	var menubar_str    = menubar ? 'yes' : 'no';
	var toolbar_str    = toolbar ? 'yes' : 'no';
	target = target.replace(/\s/g, '_');
	window.open(
		location,
		target,
		str +
		'resizable='  + resizable_str + ',' +
		'scrollbars=' + scrollbars_str + ',' +
		'status='     + status_str + ',' +
		'menubar='    + menubar_str + ',' +
		'toolbar='    + menubar_str
	);
}

// Ajax

function getXMLHttpRequest()
{
	var http;
	try {
		http = new XMLHttpRequest();
	}
	catch(e){
		var msxml = [
			'MSXML2.XMLHTTP.3.0',
			'MSXML2.XMLHTTP',
			'Microsoft.XMLHTTP'
		];
		for (var i=0, len = msxml.length; i < len; ++i){
			try {
				http = new ActiveXObject(msxml[i]);
				break;
			}
			catch(e) {}
		}
	}
	return http;
}

var asyncRequest = function()
{
	function handleReadyState(o, callback){
		var poll = window.setInterval(function(){
			if (o && o.readyState == 4){
				window.clearInterval(poll);
				if (callback){
					callback(o);
				}
			}
		},
		50);
	}
	var http;
	try {
		http = new XMLHttpRequest();
	}
	catch(e){
		var msxml = [
			'MSXML2.XMLHTTP.3.0',
			'MSXML2.XMLHTTP',
			'Microsoft.XMLHTTP'
		];
		for (var i=0, len = msxml.length; i < len; ++i){
			try {
				http = new ActiveXObject(msxml[i]);
				break;
			}
			catch(e) {}
		}
	}
	return function(method, uri, callback, postData) {
		method = method.toUpperCase();
		http.open(method, uri, true);	// true for asynchronous
		if (typeof callback != "undefined")
			handleReadyState(http, callback);
		if (method == 'POST'){
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		}
		http.send(postData || null);
		return http;
	};
}();

function getCaretPosition(input)
{
	var result = { start: 0, end: 0 };
	if (input.setSelectionRange)
	{
		result.start = input.selectionStart;
		result.end = input.selectionEnd;
	}
	else if (document.selection && document.selection.createRange)
	{
		var range = document.selection.createRange();
		var r2 = range.duplicate();
		result.start = 0 - r2.moveStart('character', -100000);
		result.end = result.start + range.text.length;
	}
	return result;
}

function setCursorPosition(field, pos)
{
	if (field.createTextRange) {
		var range = field.createTextRange();
		range.collapse(true);
		range.moveEnd('character', pos);
		range.moveStart('character', pos);
		range.select();
	}
	else if (field.selectionEnd) {
		field.selectionEnd = pos;
	}
}
/* * * * * * * * * * * * * * * * */
function cScroller () {//states: 1 - down , -1 - up , -2 - right , 2 - left
	this.objects = {};
	this.speed = 10;

	this.init = function (objID,state) {
		var obj = {};
		obj.obj = document.getElementById(objID);
		obj.state = state;
		obj.timer = null;
		obj.maxVertScroll = parseInt(obj.obj.scrollHeight) - parseInt(obj.obj.offsetHeight);
		obj.maxHorizScroll = (parseInt(obj.obj.scrollWidth) - parseInt(obj.obj.offsetWidth));
		this.objects[objID] = obj;
	}

	this.down = function (id,timer) {
		if (this.objects[id] == undefined) {
			this.init(id,1);
		}
		var obj = this.objects[id];
		if (timer == undefined) {
			obj.state = 1;
		}

		if ((obj.maxVertScroll > obj.obj.scrollTop) && (obj.state == 1)) {
			obj.obj.scrollTop = (obj.obj.scrollTop + this.speed);
			obj.timer = setTimeout(this.down.bind(this,id,true),100);
		}

	}

	this.right = function (id,timer) {
		if (this.objects[id] == undefined) {
			this.init(id,-2);
		}
		var obj = this.objects[id];
		if (timer == undefined) {
			obj.state = -2;
		}

		if ((obj.maxHorizScroll > obj.obj.scrollLeft) && (obj.state == -2)) {
			obj.obj.scrollLeft = (parseInt(obj.obj.scrollLeft) + this.speed);
			obj.timer = setTimeout(this.right.bind(this,id,true),100);
		}

	}


	this.up = function (id,timer) {
		if (this.objects[id] == undefined) {
			this.init(id,-1);
		}
		var obj = this.objects[id];
		if (timer == undefined) {
			obj.state = -1;
		}

		if ((obj.obj.scrollTop > 0) && (obj.state == -1)) {
			obj.obj.scrollTop = (parseInt(obj.obj.scrollTop) - this.speed);
			if (obj.obj.scrollTop < 0) obj.obj.scrollTop = 0;
			obj.timer = setTimeout(this.up.bind(this,id,true),100);
		}
	}


	this.left = function (id,timer) {
		if (this.objects[id] == undefined) {
			this.init(id,2);
		}
		var obj = this.objects[id];
		if (timer == undefined) {
			obj.state = 2;
		}

		if ((obj.obj.scrollLeft > 0) && (obj.state == 2)) {
			obj.obj.scrollLeft = (parseInt(obj.obj.scrollLeft) - this.speed);
			if (obj.obj.scrollLeft < 0) obj.obj.scrollLeft = 0;
			obj.timer = setTimeout(this.left.bind(this,id,true),100);
		}
	}

	this.stop = function (id) {
		if (this.objects[id]) {
			clearTimeout(this.objects[id].timer);
			this.objects[id].state = 0;
		}
	}
}

function update_checkbox(cb, cb_hidden)
{
	cb_hidden.value = cb.checked ? 1 : 0;
}
