function addEvent(object, type, func) {
	if (object.addEventListener)
		object.addEventListener(type, func, false);
	else if (object.attachEvent)
		object.attachEvent("on" + type, func);
}

function externalLinks() {
    if (!document.getElementsByTagName) return;

    var anchors = document.getElementsByTagName("a");
    for (var i=0; i<anchors.length; i++) {
        var anchor = anchors[i];
        if (anchor.getAttribute("rel") == "external") {
            if (anchor.getAttribute("href")) {
                anchor.target = "_blank";
            }
        } else if (anchor.getAttribute("rel") == "internal") {
        } else if (anchor.getAttribute("href")) {
            if (anchor.getAttribute("href").toLowerCase().indexOf("http") == 0) {
                var aLink = anchor.getAttribute("href");
                if (aLink.substring(0,7).toLowerCase() == "http://")
                    aLink = aLink.substring(7);
                if (aLink.substring(0,8).toLowerCase() == "https://")
                    aLink = aLink.substring(8);
                    
                if (aLink.substring(0, window.location.host.length) != window.location.host) {
                    anchor.target = "_blank";
                }
            }
        }
    }
}
addEvent(window, "load", externalLinks);

function gaExternal(pageTracker) {
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i=0; i<anchors.length; i++) {
        var anchor = anchors[i];
        if (anchor.getAttribute("href")) {
            if (anchor.getAttribute("href").toLowerCase().indexOf("http") == 0) {
                var aLink = anchor.getAttribute("href");
                if (aLink.substring(0,7).toLowerCase() == "http://")
                    aLink = aLink.substring(7);
                if (aLink.substring(0,8).toLowerCase() == "https://")
                    aLink = aLink.substring(8);
                    
                if (aLink.substring(0, window.location.host.length) != window.location.host) {
                    anchor.setAttribute("gaLink", '/ext/' + aLink);
                    addEvent(anchor, "click", function(evt) { 
                        var l = gaExternal_link(evt).getAttribute("gaLink");
                        //setTimeout( function() {
                            pageTracker._trackPageview(l);
                        //}, 50);
                    });
                }
                else if (aLink.toLowerCase().substring(aLink.length - 8) == "/res.axd") {
                    anchor.setAttribute("gaLink", aLink.substring(window.location.host.length));
                    addEvent(anchor, "click", function(evt) {
                        var l = gaExternal_link(evt).getAttribute("gaLink");
                        //setTimeout( function() {
                        pageTracker._trackPageview(l);
                        //}, 50);
                    });
                }
            }
        }
    }
}
function gaExternal_link(evt){
	var e=evt.srcElement||evt.target;
	while (e.tagName&&(e.tagName.toLowerCase()!="a")){
		e=e.parentElement||e.parentNode;
	}
	return e;
}

function trim(str) {
	if (str.length == 0) return str;
	while(str.indexOf(" ") == 0) {
		if (str.length == 0) return str;
		str = str.substring(1);
	}
	while(str.lastIndexOf(" ") == str.length - 1) {
		if (str.length == 0) return str;
		str = str.substring(0, str.length - 1);
	}
	return str;
}

function replace(str, from, to) {
	var r = "";
	
	while (str.indexOf(from) != -1) {
		r += str.substring(0, str.indexOf(from)) + to;
		str = str.substring(str.indexOf(from) + from.length, str.length);
	}
	str =  r + str;
	return str;		
}

function checkTime(value) {
	var redatumtid = /(\d\d):(\d\d)/
	res = value.match(redatumtid)
	if (res == null || ((res[1] > 23 || res[1] < 0) || (res[2] > 59 || res[2] < 0))) {
		return false
	}
	return true;
}

function BrowserCheck() {
	var b = navigator.appName
	if (b=="Netscape") this.b = "ns"
	else if (b=="Microsoft Internet Explorer") this.b = "ie"
	else this.b = b
	this.v = parseInt(navigator.appVersion)
	this.ns = (this.b=="ns" && this.v>=4)
	this.ns4 = (this.b=="ns" && this.v==4)
	this.ns5 = (this.b=="ns" && this.v>=5)
	this.ns6 = (this.b=="ns" && this.v>=6)
	this.ie = (this.b=="ie" && this.v>=4)
	this.ie4 = (navigator.userAgent.indexOf('MSIE 4')>0)
	this.ie5 = (navigator.userAgent.indexOf('MSIE 5')>0)
	this.ie6 = (navigator.userAgent.indexOf('MSIE 6')>0)
	this.ie7 = (navigator.userAgent.indexOf('MSIE 7') > 0)
	this.ie8 = (navigator.userAgent.indexOf('MSIE 8') > 0)
	this.ie9 = (navigator.userAgent.indexOf('MSIE 9') > 0)
	if (this.ie5) this.v = 5
	if (this.ie6) this.v = 6
	this.min = (this.ns||this.ie)
	this.editorIe = (this.ie && !this.ie4);
}

// automatically create the "is" object
is = new BrowserCheck()
	
function setValue(obj, value)
{
	var obj2 = obj;
	obj = eval(obj);
	if (obj == null)
	{
		alert("Wrong input field name: '" + obj2 + "'!");
		return;
	}

	if (obj.length == null || obj.type == "select-one" || obj.type == "select-multiple")
	{
		setInputValue(obj, value);
	} 
	else
	{
		for (var i = 0; i < obj.length; i++)
		{
			setInputValue(obj[i], value);
		}
	}

}

function setInputValue(obj, value)
{
	if (obj.type == "select-one")
	{
		for (var i = 0; i < obj.options.length; i++)
		{
			if (obj.options[i].value == value)
			{
				obj.options[i].selected = true;
			}
		}
	}
	else if (obj.type == "select-multiple")
	{
		var selected = eval("new Array(null, " + value + ")");
		for (var i = 0; i < obj.options.length; i++)
		{
			for (var k = 1; k < selected.length; k++)
			{
				if (obj.options[i].value == selected[k])
				{
					obj.options[i].selected = true;
				}
			}
		}
	}	
	else if (obj.type == "text" || obj.type == "textarea" || obj.type == "hidden" || obj.type == "password")
	{
		obj.value = value;
	}
	else if (obj.type == "checkbox")
	{
		var selected = eval("new Array(null, " + value + ")");
		obj.checked = false;
		for (var k = 1; k < selected.length; k++)
		{
			if (obj.value == selected[k])
			{ 
				obj.checked = true;
			}
		}
	}
	else if (obj.type == "radio")
	{
		if (obj.value == value)
		{	
			obj.checked = true;
		}
	}

}

function getValue(obj)
{
	var obj2 = obj;
	obj = eval(obj);
	if (obj == null)
	{
		alert("Wrong input field name: '" + obj2 + "'!");
		return;
	}

	if (obj.length == null || obj.type == "select-one" || obj.type == "select-multiple")
	{
		return getInputValue(obj);
	} 
	else
	{
		var retStr = "";
		for (var i = 0; i < obj.length; i++)
		{
			var v = getInputValue(obj[i]);
			retStr += ((retStr.length == 0 || v.length == 0) ? "" : ", ") + v;
		}
		return retStr;
	}
}
function getInputValue(obj)
{
	if (obj.type == "select-one")
	{
		return obj.options[obj.selectedIndex].value;
	}
	else if (obj.type == "select-multiple")
	{
		var retStr = "";
		for (var i = 0; i < obj.options.length; i++)
		{
			if (obj.options[i].selected)
			{
				retStr += ((retStr.length == 0) ? "" : ", ") + obj.options[i].value;
			}
		}
		return retStr;
	}
	else if (obj.type == "text" || obj.type == "textarea" || obj.type == "hidden" || obj.type == "password")
	{
		return obj.value;
	}
	else if (obj.type == "checkbox" || obj.type == "radio")
	{
		if (obj.checked)
		{	
			return obj.value;
		}
	}
	return "";
}

function inputOnlyNumber(tf, useDecimal) {
    var k = event.keyCode;
    if (k == 9 || k == 13)
        return true;

    if ( event.ctrlKey ) {
        if (k == 67 || k == 82 || k == 86 || k == 88)
            return true;
        return false;
    }
    if ( event.shiftKey ) {
        if ((k >= 35 && k <= 40))
            return true;
        return false;
    }
    if (useDecimal && tf.value.indexOf(',') == -1 && tf.value.indexOf('.') == -1) {
        if (k == 188 || k == 190 || k == 110) // Accept comma and dots
            return true;
    }
    if ((k >= 48 && k <= 57) || (k >= 96 && k <= 105) || (k >= 35 && k <= 40) || k == 8 || k == 46 || k == 17)
        return true;
    return false;
} 
function editor_getDiv(obj, nest) {
	return getDiv(obj, nest);
}
function getDiv(obj, nest) {
	if (!nest) nest = 'document.';

	ie = (document.all)?1:0;
	ne = (document.layers)?1:0;
	dom =(document.getElementById)?1:0;

	if (dom)
		return document.getElementById(obj);
	else if(ie)
		return eval(obj);
	else
		return eval(nest + obj);
}

//  Function to create or update a cookie.
//    name - String object containing the cookie name.
//    value - String object containing the cookie value.  May contain
//         any valid string characters.
//    [expires] - Date object containing the expiration data of the cookie.  If
//         omitted or null, expires the cookie at the end of the current session.
//    [path] - String object indicating the path for which the cookie is valid.
//         If omitted or null, uses the path of the calling document.
//    [domain] - String object indicating the domain for which the cookie is
//         valid. If omitted or null, uses the domain of the calling document.
//    [secure] - Boolean (true/false) value indicating whether cookie
//         transmission requires a secure channel (HTTPS).  
//
//  The first two parameters are required.  The others, if supplied, must
//  be passed in the order listed above.  To omit an unused optional field,
//  use null as a place holder.  For example, to call SetCookie using name,
//  value and path, you would code:
//
//      SetCookie ("myCookieName", "myCookieValue", null, "/");
//
//  Note that trailing omitted parameters do not require a placeholder.
//
//  To set a secure cookie for path "/myPath", that expires after the
//  current session, you might code:
//
//      setCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);
//
function setCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}


//  Function to return the value of the cookie specified by "name".
//    name -    String object containing the cookie name.
//    returns - String object containing the cookie value,
//              or null if the cookie does not exist.
//
function getCookie(Name) {
	var search = Name + "="
	if (document.cookie.length > 0) {
		// if there are any cookies
		offset = document.cookie.indexOf(search)
		if (offset != -1) {
			// if cookie exists
			offset += search.length
			// set index of beginning of value
			end = document.cookie.indexOf(";", offset)
			// set index of end of cookie value
			if (end == -1)
				end = document.cookie.length
			return unescape(document.cookie.substring(offset, end))
		}
	}

	return null;
}
//  Function to delete a cookie. (Sets expiration date to start of epoch)
//    name -   String object containing the cookie name
//    path -   String object containing the path of the cookie to delete.
//             This MUST be the same as the path used to create the cookie, or
//             null/omitted if no path was specified when creating the cookie.
//    domain - String object containing the domain of the cookie to delete.
//             This MUST be the same as the domain used to create the cookie, or
//             null/omitted if no domain was specified when creating the cookie.
//
function deleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function setCookieEditor(name, value) {
	var exdate = new Date();
	var y = exdate.getYear();
	if (y < 1900) y += 1900;
	exdate = new Date(y+1, 5, 15, exdate.getHours(), exdate.getMinutes(), exdate.getSeconds());
	setCookie(name, value, exdate, ApplicationPath);
}

function popup( url, width, height, scrollbars, resizable, toolbar, menubar, location, status, x_name) {
	if (!x_name) x_name = "_blank";
	var win = window.open( url, x_name, "toolbar=" + ((toolbar) ? 1 : 0) + ",resizable=" + ((resizable) ? 1 : 0) + ",scrollbars=" + ((scrollbars) ? 1 : 0) + ",height=" + height + ",width=" + width + ",menubar=" + ((menubar) ? 1 : 0) + ",location=" + ((location) ? 1 : 0) + ",status=" + ((status) ? 1 : 0) + "" );
	win.focus();
}


// HTTPrequests
var http_request;
function editor_makeRequest(url, refFunction, postdata) { 
    if (window.XMLHttpRequest) { // Mozilla, Safari,... 
        http_request = new XMLHttpRequest(); 
    } else if (window.ActiveXObject) { // IE 
        http_request = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    http_request.onreadystatechange = refFunction; 
    if (postdata == null) {
        http_request.open('GET', url, true); 
        http_request.send(null); 
    } else {
        http_request.open('POST', url, true); 
        http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        http_request.send(postdata); 
    }
} 
/*
   function retFunction() { 
       if (http_request.readyState == 4) { 
           if (http_request.status == 200) { 
               alert(http_request.responseText); 
           } else { 
               alert('There was a problem with the request.'); 
           } 
       } 

   } 
*/
