/*
	Used some code from Lightbox JS: Fullsize Image Overlays 
	by Lokesh Dhakar - http://www.huddletogether.com

	For more information on this script, visit:
	http://huddletogether.com/projects/lightbox/
	
	Thanks
*/


function dbg (tag, caption, value) {
	if (
		(tag != 'ajax') &&
		(tag != 'tmp')
		)
	{
		return value;
	}
	_alert ('#' + tag + '# ' + caption + ': ' + value);
	return value;
}

function _alert(foo) {
  var arg;
  for (var i = 0; i < arguments.length; i++) {
    arg = arguments[i];
    if (window.console) window.console.log(arg);
    else if (window.opera) window.opera.postError(arg);
    else if (window.Log) {
      if (!window._logger)
        window._logger = new Log(Log.INFO, Log.popupLogger);
      window._logger.debug(arg);
    } else alert(arg);       
  }
}

function conlog (foo) {
  var arg;
  for (var i = 0; i < arguments.length; i++) {
    arg = arguments[i];
    if (window.console) window.console.log(arg);
  }
}

var isInternetExplorer = navigator.appName.indexOf("Microsoft") != -1;

function editor_DoFSCommand(command, id, args) {
	var editorObj = isInternetExplorer ? document.all.editor : document.editor;
	if ((command == 'update') || (command == 'FSCommand:update')) {
		var e = document.getElementById('e'+id);
		args = encodeURIComponent (args);
		e.value = args;
	}
}

function toggle_visibility (id)
{

	if (document.layers)
	{
		vista = (document.layers[id].visibility == 'hide') ? 'show' : 'hide'
		document.layers[id].visibility = vista;
	}
	else if (document.all)
	{
		vista = (document.all[id].style.visibility == 'hidden') ? 'visible'	: 'hidden';
		document.all[id].style.visibility = vista;
	}
	else if (document.getElementById)
	{
		vista = (document.getElementById(id).style.visibility == 'hidden') ? 'visible' : 'hidden';
		document.getElementById(id).style.visibility = vista;

	}
}

function toggle_display (id)
{
	if (document.layers)
	{
		current = (document.layers[id].display == 'none') ? 'block' : 'none';
		document.layers[id].display = current;
	}
	else if (document.all)
	{
		current = (document.all[id].style.display == 'none') ? 'block' : 'none';
		document.all[id].style.display = current;
	}
	else if (document.getElementById)
	{
		vista = (document.getElementById(id).style.display == 'none') ? 'block' : 'none';
		document.getElementById(id).style.display = vista;
	}
}

function set_element_visibility (element, visibility)
{
	element.style.visibility = visibility;
}

function listRowOver(o) { o.style.borderColor = '#ABA800'; o.className = 'lr green_border'; }

function listRowOut(o) { o.style.borderColor = ''; o.className = 'lr'}

function listNumber() {
	allNodes = document.getElementsByClassName("lr_number");
	for(i = 0; i < allNodes.length; i++) {
		allNodes[i].innerHTML = (i+1)+'.';
	}
}

function listArrows() {
	allNodes = document.getElementsByClassName("list_move_down");
	for(i = 0; i < allNodes.length; i++) {
		if (i < allNodes.length-1) {
			set_element_visibility (allNodes[i], 'visible');
		} else {
			set_element_visibility (allNodes[i], 'hidden');
		}
	}
	allNodes = document.getElementsByClassName("list_move_up");
	for(i = 0; i < allNodes.length; i++) {
		if (i > 0) {
			set_element_visibility (allNodes[i], 'visible');
		} else {
			set_element_visibility (allNodes[i], 'hidden');
		}
	}
}

function open_fullscreen (url, title) {
	window.open(url,title,"width="+(screen.availWidth-0)+",height="+(screen.availWidth-40)+",scrollbars=yes,top=0,left=0,toolbar=no,location=no,directories=no,menubar=no,resizable=no,status=no");
}

function open_fullscreen_no_scroll (url, title) {
	window.open(url,title,"width="+(screen.availWidth-0)+",height="+(screen.availWidth-40)+",scrollbars=no,top=0,left=0,toolbar=no,location=no,directories=no,menubar=no,resizable=no,status=no");
}

function open_window (url, title, woptions) {
	window.open(url, title, woptions);
}

function ExternalInterfaceManager()
{
	this.registerMovie = function (movieName) {
		if(!window.fakeMovies) {
			window.fakeMovies = new Array();
		}
		window.fakeMovies[window.fakeMovies.length] = movieName;
	}
	this.initialize = function () {
		if(document.all) {
			if(window.fakeMovies) {
				for(i=0;i<window.fakeMovies.length;i++) {
					window[window.fakeMovies[i]] = new Object();
				}
				window.onload = initializeExternalInterface;
			}
		}
	}
}

function initializeExternalInterface()
{
	for(i=0;i < window.fakeMovies.length;i++) {
		var movieName = window.fakeMovies[i];
		var fakeMovie = window[movieName];
		var realMovie = document.getElementById(movieName);

		for(var method in fakeMovie) {
			realMovie[method] = function() {
				flashFunction = "<invoke name=\"" + method.toString() + "\" returntype=\"javascript\">" + __flash__argumentsToXML(arguments, 0) + "</invoke>";this.CallFunction(flashFunction);
			}
		}

		window[movieName] = realMovie;
	}
}

function submitenter(myfield,e)
{
	var keycode;
	if (window.event) {
		keycode = window.event.keyCode;
	} else if (e) {
		keycode = e.which;
	} else {
		return true;
	}

	if (keycode == 13) {
		myfield.form.submit();
		return false;
	} else {
	   return true;
	}
}

function setCheckboxes( container_id, state ) {
    var checkboxes = document.getElementById(container_id).getElementsByTagName('input');

    for ( var i = 0; i < checkboxes.length; i++ ) {
        if ( checkboxes[i].type == 'checkbox' ) {
            checkboxes[i].checked = state;
        }
    }

    return true;
}

function getSerializedTrueCheckboxes( container_id ) {
	
    var checkboxes = document.getElementById(container_id).getElementsByTagName('input');
	var ret = new String ();

    for ( var i = 0; i < checkboxes.length; i++ ) {
        if ( checkboxes[i].type == 'checkbox' ) {
			if ( checkboxes[i].checked ) {
				ret += ','+checkboxes[i].id.replace ('list_cb_', '');
			}
        }
    }
	ret = ret.substr(1);

	return ret;
}

function deleteCheckboxes ( container_id, form_id, input_id ) {
	var ids = new String ();
	ids = getSerializedTrueCheckboxes (container_id);
	if (ids != '') {
		if (confirm ('Do you really want delete selected items?')) {
			$('input_hidden_action').value = 'multicheck_delete';
			$(input_id).value = ids;
//			alert ($(input_id).value);
//			alert ('id_'+form_id);
			$('id_'+form_id).submit ();
		}
	}
}

function colorizeAsDefaultCheckboxes ( container_id, form_id, input_id ) {
	var ids = new String ();
	ids = getSerializedTrueCheckboxes (container_id);
	if (ids != '') {
		if (confirm ('Do you really want colorize selected items as default?')) {
			$('form_action').value = 'multicheck_colorize_as_default';
			$(input_id).value = ids;
//			alert ($(input_id).value);
//			alert ('id_'+form_id);
			$('id_'+form_id).submit ();
		}
	}
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

function addLoadEvent(func) {
  var oldonload = window.onload;
//conlog (oldonload);
  if (typeof window.onload != 'function') {
//alert ('clean');
//conlog (func);
    window.onload = func;
  } else {
//alert ('adding');
//conlog (oldonload);
//conlog (func);
    window.onload = function() {
      oldonload();
      func();
    }
  }
}