/*********************************************
		
			MISC FUNCTIONS
		
**********************************************/

var e = window.encodeURIComponent ? encodeURIComponent : escape;
var uE = window.decodeURIComponent ? decodeURIComponent : unescape;

function addLoadEvent(func)
{
  var oldonload = window.onload;
  if (typeof window.onload != "function") {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

//Get all the elements of the given classname of the given tag.
function getElementsByClassName(classname,tag) {
	if(!tag) tag = "*";
	var anchs =  document.getElementsByTagName(tag);
	var total_anchs = anchs.length;
	var regexp = new RegExp("\\b" + classname + "\\b");
	var class_items = new Array()

	for(var i=0;i<total_anchs;i++) { //Go thru all the links seaching for the class name
		var this_item = anchs[i];
		if(regexp.test(this_item.className)) {
			class_items.push(this_item);
		}
	}
	return class_items;
}



function dbl_click (inp) {
	if (navigator.appName == "Microsoft Internet Explorer") { inp.onclick(); }
}

function getElement(id) {
	if (document.getElementById) {
		return document.getElementById(id);
	}
	else if (document.all) {
		return document.all[id];
	}
	else if (document.layers) {
		return document.layers[id];
	}
}

/*********************************************
		
			AJAX FUNCTIONS
		
**********************************************/

var strResDataErr = new String("Tekkis tõrge!\nTundmatu vastus serverilt, palun proovige mõne hetke pärast uuesti.\nID: [:ID:] ([:INFO:])");

function isValidXMLObject (res) { // V 2.0
	try {
		if (typeof res.getElementsByTagName("isError")[0] != "undefined") {
			return true;
		}
	} catch (e) {
		//
	}
	return false;
}

function isValidEmail (str) {
	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

function functionName (res) { // V 2.2
	var longstring = new String(res);
	var brokenstring = longstring.split(" ");
	return brokenstring[1];
}

function isValidResource (res) { // V 2.1
	return (typeof res == "undefined" || res == null) ? false : true;
}

// convert all characters to lowercase to simplify testing
var agt = navigator.userAgent.toLowerCase();
var appVer = navigator.appVersion.toLowerCase();

// *** BROWSER VERSION ***

var is_minor = parseFloat(appVer);
var is_major = parseInt(is_minor);
var is_opera = (agt.indexOf("opera") != -1);
var iePos  = appVer.indexOf('msie');
var is_ie   = ((iePos!=-1) && (!is_opera));
var is_ie7up = (is_ie && (agt.indexOf("msie 7.") != -1));

// threadsafe asynchronous XMLHTTPRequest code V 2.4
var TAVersion = "2.4";

function requestData (url, data, callback, dType, cApply) {
    resXML = (dType == "XML" ? true : false);
    // we use a javascript feature here called "inner functions"
    // using these means the local variables retain their values after the outer function
    // has returned. this is useful for thread safety, so
    // reassigning the onreadystatechange function doesn\'t stomp over earlier requests.
    function ajaxBindCallback() {
        if (ajaxRequest.readyState == 4) {
            if (ajaxRequest.status == 200) {
                if (ajaxCallback) {
                    if (resXML) {
                        eval(ajaxCallback + "(ajaxRequest.responseXML.documentElement, ajaxApply)");
                    } else {
                        eval(ajaxCallback + "(ajaxRequest.responseText, ajaxApply)");
                    }
                } else {
                    alert("No callback defined!");
                }
            } else {
                alert("There was a problem retrieving the XML data!\n\nStatus: " + ajaxRequest.statusText + " (" + ajaxRequest.status + ")\n\nResponse:\n" + ajaxRequest.responseText);
            }
        }
    }
    
    // use a local variable to hold our request and callback until the inner function is called...
    var ajaxRequest = false;
    var ajaxCallback = callback;
    var ajaxApply = cApply;
    
    // bind our callback then hit the server...
    ajaxRequest = getHTTPObject(resXML);
    
    if (!ajaxRequest) {
        return false;
    } else {
        ajaxRequest.onreadystatechange = ajaxBindCallback;
        try {
            ajaxRequest.open("POST", url, true);
        } catch (e) {
            return false;
        }
        ajaxRequest.setRequestHeader("X-Requested-With", "XMLHttpRequest");
        ajaxRequest.setRequestHeader("X-Prototype-Version", TAVersion);
        ajaxRequest.setRequestHeader("Connection", "close");
        ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        ajaxRequest.send(data);
        return true;
    }
    return false;
}

function getHTTPObject (resXML) {
	var xO = false;
	if (typeof XMLHttpRequest != "undefined") {
		try {
			xO = new XMLHttpRequest();
			if (resXML) {
				xO.overrideMimeType("text/xml");
			} else {
				xO.overrideMimeType("text/html");
			}
			return xO;
		} catch (e) {
			if (is_ie7up) {
				xO = new XMLHttpRequest();
				return xO;
			} else {
				xO = false;
			}
		}
	} else {
		var msxmls = new Array(
			"Msxml2.XMLHTTP",
			"Msxml2.XMLHTTP.5.0",
			"Msxml2.XMLHTTP.4.0",
			"Msxml2.XMLHTTP.3.0",
			"Microsoft.XMLHTTP"
		);
		for (var i = 0; i < msxmls.length; i++) {
			var msxml = msxmls[i]; 
			try {
				xO = new ActiveXObject(msxml);
				return xO;
			} catch(e) {
				xO = false;
			}
		}
		
	}
	return xO;
}




/*********************************************
		
			          MENU
		
**********************************************/

function findObj(n, d) {
  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=findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

var hideTimer=false;
function show_hide (cur, mid, act) {
	var obj,oid,i=0;
	if(hideTimer){clearTimeout(hideTimer);}
	do{
		oid="subMenu" + i;
		obj=findObj(oid);
		if(obj!=null) {
			if(i==cur){
				if(act=="show"){
					obj.style.display="";
				}else{
					hideTimer=setTimeout("hide_all()", 500);
				}
			}else{
				obj.style.display="none";
			}
		}
	}while (++i&&obj!=null);
}

function hide_all () {
	var obj,oid,i=0;
	do{
		oid="subMenu" + i;
		obj=findObj(oid);
		if(obj!=null) {
			obj.style.display="none";
		}
	}while (++i&&obj!=null);
}
