parsedEditors = new Array();

function createEditor(edt)
{
	//alert(xinha_editors);
	//wait until previous editor has beet fully loaded
	if(xinha_editors != "[object Object]" && xinha_editors != "")
	{
		setTimeout("createEditor('"+edt+"')", 1000); //if not loaded, retry after some time
		return false;
	}
	
	if(parsedEditors.indexOf(edt) < 0 || parsedEditors.indexOf(edt) == null)
	{
		if(xinha_editors != "[object Object]")
			xinha_editors = xinha_editors.concat(edt);
		else
			xinha_editors = new Array(edt);

		xinha_init();	
		parsedEditors.push(edt);
	}
	return;
}

function toggleEditor(block, editor)
{
	toggle(block);
	createEditor(editor);
}

/************************************/
/* control height */
/************************************/
function fixHeight()
{
	var lay = document.getElementById("layout");
	var bot = document.getElementById("bottom");
	var height = 0;;
	
	height = document.body.parentNode.scrollHeight;
	if (height<window.innerHeight) height = window.innerHeight;

	//lay.style.height = (Height-20) + "px";
	//bot.style.top = height-35 + "px";
}

function toolTip(text)
{ 
	//Tip(text, ABOVE, true, BGCOLOR, "#FFFFCC", BORDERCOLOR, "#669933");
}

/************************************/
/* control marquee */
/************************************/
function scrollMarquee(slide)
{
	var obj = document.getElementById("scroller");
	
	if(slide)
		obj.start();
	else
		obj.stop();
}

/************************************/
/* XMLHttpRequest functions */
/************************************/
function changeLanguage(abs_head, lang)
{
	sendRequest("GET", abs_head+"/cl.php?"+lang, false);
	//window.location.reload();
	window.location = abs_head+"/";
}

function sendRequest(method, script, async)
{
	var xmlhttp=false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	 try {
	  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
	  try {
	   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (E) {
	   xmlhttp = false;
	  }
	 }
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
    
    if(xmlhttp	) {
		xmlhttp.open(method, script, async);
		xmlhttp.send(null);
	}
}
/************************************/
/* EOF XMLHttpRequest functions */
/************************************/

function checkLen(element, len)
{
	if (element.value.length>len)
	{
		 element.value = element.value.substring(0, len);
		 element.scrollTop = element.scrollHeight - element.clientHeight;
		 element.focus()
	}
}

function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}

function selAll(form)
{
	var chk = form.elements["f_sel_chk[]"];
	for(var i=1; i<chk.length; i++)
		chk[i].checked = chk[0].checked;	
}

function toggleArray(oHidden)
{
	for(var i=0; i<oHidden.length; i++)
	{
		toggle(oHidden[i]);
	}		
}

function toggle(element)
{
	var obj = document.getElementById(element);
	if(obj!=null)
	{
		if (obj.style.display=="none")
			obj.style.display = "";
		else
			obj.style.display = "none";
	}
}

function toggle_visibility(id, size)
{
	var obj = document.getElementById(id);
	alert(obj);

	if(obj.height == 0)
	{
		obj.height = size;
	}
	else if (obj.height>0)
	{
		obj.height = 0;
	}
}

function simple_toggle(element)
{
	var obj = document.getElementById(element);
	if(obj.innerHTML!="")
	{
		if (obj.style.display=="none")
		{
			obj.style.display = "";
		}
		else
		{
			obj.style.display = "none";
		}
	}
}


function swap_ico(img, img_path)
{
	if(img.name == "plus")
	{
		img.src = img_path + "folder_minus.png";
		img.name = "minus";
	}
	else
	{
		img.src = img_path + "folder_plus.png";		
		img.name = "plus";
	}
}

function popImage(img, w, h)
{
	window.open (img, "Latvijas Filmu Datu Bāze", "scrollbars=0,statusbar=0,toolbar=0,width="+(w+20)+",height="+(h+20));
}

function swapImage(img, path)
{
	img.src=path
}	

function unselectAll(obj)
{
	obj.selectedIndex=-1;
}

selectArray=new Array()

function addToSelect(frm, elm) {
		
		//alert(elm.selectedIndex);
	
    isFound=new Boolean()
    isFound=false
    var selectIndex = selectArray.length

    for (i=0;i<selectArray.length;i++)
    {
        if (selectArray[i]==elm.selectedIndex)
        {
            elm.options[selectArray[i]].selected=false
            selectArray[i]=null
            isFound=true
        }

    }

    if (!isFound)
        selectArray[selectIndex]=elm.selectedIndex

    for (i=0;i<selectArray.length;i++)
    {
        if (selectArray[i]!=null)
            elm.options[selectArray[i]].selected=true
    }

}

function eraseSelect() {
    selectArray=new Array()
}


var db = (document.body) ? 1 : 0;
var scroll = (window.scrollTo) ? 1 : 0;

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else {
    begin += 2;
  }
  var end = document.cookie.indexOf(";", begin);
  if (end == -1) end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function saveScroll() {
  if (!scroll) return;
  var now = new Date();
  now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
  var scr = getScrollXY();
  var x = scr[0];
  var y = scr[1];
  setCookie("xy", x + "_" + y, now);
}

function loadScroll() {
  if (!scroll) return;
  var xy = getCookie("xy");
  if (!xy) return;
  var ar = xy.split("_");
  if (ar.length == 2) scrollTo(parseInt(ar[0]), parseInt(ar[1]));
}
