﻿var urlVars = new Array();
var brIE = false;
var brIE8 = false;
var brFF = false;
var brIEVer = 0;
var pxpLogEnabled = true;

var PxXmlIndent = "  ";

var dsChar = ',';

var toRad = Math.PI / 180;
var toDeg = 180 / Math.PI;

var deleteRecordText = "Are you shure you want to delete selected record?";

function RadToDeg(an) {
	return an * toDeg;
}

function DegToRad(an) {
	return an * toRad;
}

function RadAngAbs(an) {
	var ra = an % (Math.PI * 2);
	if (ra < 0) {
		ra = (Math.PI * 2) + ra;
	}
	return ra;
}

function Round(val, ndp) {
	var cndp = 2;
	if (ndp != null) {
		cndp = ndp;
	}
	var mv = Math.pow(10, cndp);
	var rv = Math.round(val * mv) / mv;
	return rv;
}

function ChangeCSS(myclass, element, value) {
	var CSSRules
	if (document.all) {
		CSSRules = 'rules'
	}
	else if (document.getElementById) {
		CSSRules = 'cssRules'
	}
	for (var i = 0; i < document.styleSheets[0][CSSRules].length; i++) {
		if (document.styleSheets[0][CSSRules][i].selectorText.toLowerCase() == myclass.toLowerCase()) {
			document.styleSheets[0][CSSRules][i].style[element] = value
		}
	}
}

function StripForUrl(text) {
    var su = text;
    su = su.toLowerCase();
    su = su.replace(/š/gi, 's').replace(/[čć]/gi, 'c').replace(/ž/gi, 'z').replace(/đ/gi, 'd');
    su = su.replace(/[^\w\s]/gi, '').replace(/\s/gi, '_').replace(/\_+$/, '').replace(/\;/gi, '').replace(/\,/gi, '');
     return su;
}

function URLVar(vname, vvalue) {
    this.name = vname;
    this.value = vvalue;
}

function FillURLVars() {
    var ss = window.location.search + "&";
    ss = ss.replace(/\?/, '&')
    var uv = ss.split(/&/);
    if (uv.length > 0) {
        for (var i = 0; i < uv.length; i++) {
            if (uv[i] != "") {
                var vtmp = uv[i].split(/=/);
                var urlVar = new URLVar(vtmp[0], vtmp[1]);
                urlVars[urlVars.length] = urlVar;
            }
        }
    }
}

function GetURLVar(vname) {
    for (var i = 0; i < urlVars.length; i++) {
        var urlVar = urlVars[i];
        if (urlVar.name.toLowerCase() == vname.toLowerCase()) {
            return urlVar;
        }
    }
    return new URLVar(vname, "");
}

function $id(elid) {
    return document.getElementById(elid);
}

function SelectFirstEditor(edform) {
    if (edform == null) {
        return;
    }

    for (var i = 0; i < edform.elements.length; i++) {
        var cel = edform.elements[i];
        if (cel.type != "hidden" && cel.type != "submit") {
            cel.focus();
            cel.select();
            return;
        }
    }
}

function DetectBrowser() {
	var apv = navigator.appVersion.toLowerCase();
	var msieind = apv.indexOf("msie ");
	if (msieind >= 0) {
		var vei = apv.indexOf(";", msieind);
		if (vei >= 0) {
			var vs = apv.substr(msieind + 5, vei - msieind - 5);
			var bv = parseFloat(vs);
			if (bv >= 8) {
			    brIE8 = true;
			    brIE = true;
			} else {
				brIE = true;
				brFF = false;
			}
			brIEVer = bv;
		}
	} else {
		brIE = false;
		brFF = true;		
	}
}

function UpdateFormFields(edform) {
    for (var i = 0; i < edform.elements.length; i++) {
        var cel = edform.elements[i];
        if (cel.type != "hidden" && cel.type != "submit") {
            if (cel.alt) {
                if (cel.alt != "") {
                    if (cel.alt.toLowerCase() == "required") {
                        cel.style.backgroundColor = "#CBD6DE";
                    }
                }
            }
        }
    }
}

function ValidateForm(edform) {
    for (var i = 0; i < edform.elements.length; i++) {
        var cel = edform.elements[i];
        if (cel.type != "hidden" && cel.type != "submit") {
            if (cel.alt != "") {
                if (cel.alt.toLowerCase() == "required") {
                    if (cel.value == "") {
                        if (cel.parentNode.tagName.toLowerCase() == "td") {
                            var fname = cel.parentNode.parentNode.cells[0].innerHTML.replace(/:/, "");
                            alert("Required field [" + fname + "] not filled!");
                            cel.focus();
                            cel.select();
                            return false;
                        } else {
                            alert("All required fields not filled!");
                            return false;
                        }
                    }
                }
            }
        }
    }
    return true;
}


function CancelEvent(evt) {
    evt.cancel = true;
    evt.returnValue = false;
    evt.cancelBubble = true;
    if (evt.stopPropagation) evt.stopPropagation();
    if (evt.preventDefault) evt.preventDefault();
    return false;
}

function GetEventObject(ev) {
    if (ev == null) {
        if (typeof (event) == "object") {
            return event;
        }
        return null;
    }

    if (typeof(ev) == "undefined") {
        return event;
    } else {
        return ev;
    }
}

function IntToString(val, nz) {
    var tv = Math.abs(val);
    var rs = tv.toString();
    var sl = rs.length;
    for (var i = 0; i < (nz - sl); i++) {
        rs = "0" + rs;
    }
    if (val < 0) {
        rs = "-" + rs;
    }
    return rs;
}

function FloatToString(val, nz) {
    var rs = Round(val, nz).toString();
    
    if (nz == 0) {
        return IntToString(val, 0);
    }
    
    var dpi = rs.indexOf(GetDecimalSeparator());
    
    var nza = 0;
    if (dpi < 0) {
        nza = nz;
        rs += GetDecimalSeparator();
    } else {
        nza = nz - (rs.length - dpi - 1);
    }
        
    for (var i = 0; i < nza; i++) {
        rs += '0';
    }
    
    return rs;
}

function StringContainsLetters(testStr) {
    for (var i = 0; i < testStr.length; i++) {
        var cc = testStr.charCodeAt(i);
        if (cc < 48 || cc > 57) {
            if (cc != 44 && cc != 46 && cc != 45) {
                return true;
            }
        }
    }
    return false;
}

function DateToString(dat) {
    var ds = IntToString(dat.getFullYear(), 4) + "-";
    ds += IntToString(dat.getMonth() + 1, 2) + "-";
    ds += IntToString(dat.getDate(), 2);
    return ds;
}

function StrToInt(sval) {
    var i = -1;

    var ci = 0;
    var cc = '';
    var zi = 0;
    do {
        cc = sval.charAt(ci);
        ci++;
        if (cc == '0') {
            zi++;
        } else {
            break;
        }
    } while (cc = '0')

    var tvs = sval.substr(zi);
    i = parseInt(tvs);

    return i;
}

function GetDecimalSeparator() {
    var n = 1.1;
    n = n.toLocaleString().substring(1, 2);
    return n;
}

String.prototype.contains = function (ctext) {
    if (this.indexOf(ctext) >= 0)
        return true;
    return false;
};

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/, '');
};

String.prototype.trimStart = function () {
    return this.replace(/^\s*/, "");
}

String.prototype.trimEnd = function (val) {
    var v = "\\s";
    if (typeof (val) == "string") v = val;
    var re = new RegExp(v + '*$', 'ig');
    return this.replace(re, "");
}

String.prototype.trim = function () {
    return this.trimStart().trimEnd();
}

function CreateNewXml(txt) {
    try {
        if (brIE) {
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async = "false";
            xmlDoc.loadXML(txt);
            return xmlDoc;
        } else {
            parser = new DOMParser();
            xmlDoc = parser.parseFromString(txt, "text/xml");
            return xmlDoc;
        }
    }
    catch (e) {
        return null;
       }
      }

/**
* Create a new Document object. If no arguments are specified,
* the document will be empty. If a root tag is specified, the document
* will contain that single root tag. If the root tag has a namespace
* prefix, the second argument must specify the URL that identifies the
* namespace.
*/
NewXmlDocument = function(rootTagName, namespaceURL) {
	if (!rootTagName) rootTagName = "";
	if (!namespaceURL) namespaceURL = "";
	if (document.implementation && document.implementation.createDocument) {
		// This is the W3C standard way to do it
		return document.implementation.createDocument(namespaceURL, rootTagName, null);
	}
	else { // This is the IE way to do it
		// Create an empty document as an ActiveX object
		// If there is no root element, this is all we have to do
		var doc = new ActiveXObject("MSXML2.DOMDocument");
		// If there is a root tag, initialize the document
		if (rootTagName) {
			// Look for a namespace prefix
			var prefix = "";
			var tagname = rootTagName;
			var p = rootTagName.indexOf(':');
			if (p != -1) {
				prefix = rootTagName.substring(0, p);
				tagname = rootTagName.substring(p + 1);
			}
			// If we have a namespace, we must have a namespace prefix
			// If we don't have a namespace, we discard any prefix
			if (namespaceURL) {
				if (!prefix) prefix = "a0"; // What Firefox uses
			}
			else prefix = "";
			// Create the root element (with optional namespace) as a
			// string of text
			var text = "<" + (prefix ? (prefix + ":") : "") + tagname +
	  (namespaceURL
	   ? (" xmlns:" + prefix + '="' + namespaceURL + '"')
	   : "") +
	  "/>";
			// And parse that text into the empty document
			doc.loadXML(text);
		}
		return doc;
	}
};


ParseCreateXml = function(text) {
	if (typeof DOMParser != "undefined") {
		// Mozilla, Firefox, and related browsers
		return (new DOMParser()).parseFromString(text, "application/xml");
	}
	else if (typeof ActiveXObject != "undefined") {
		// Internet Explorer.
		var doc = NewXmlDocument();  // Create an empty document
		doc.loadXML(text);            // Parse text into it
		return doc;                   // Return it
	}
	else {
		// As a last resort, try loading the document from a data: URL
		// This is supposed to work in Safari. Thanks to Manos Batsis and
		// his Sarissa library (sarissa.sourceforge.net) for this technique.
		var url = "data:text/xml;charset=utf-8," + encodeURIComponent(text);
		var request = new XMLHttpRequest();
		request.open("GET", url, false);
		request.send(null);
		return request.responseXML;
	}
}


function FindXmlChild(xmlParent, name) {
    for (var i = 0; i < xmlParent.childNodes.length; i++) {
        var cc = xmlParent.childNodes[i];
        if (cc.nodeName == name) {
            return cc;
        }        
    }
    return null;
}

function GetEventTarget(ev) {
    return (ev.target) ? ev.target : ev.srcElement;
}

function PathEnd(cpath) {
	var tp = cpath;
	var li = tp.lastIndexOf('/');
	while (li == tp.length - 1) {
		li = tp.lastIndexOf('/');
		if (li == tp.length - 1) {
			tp = tp.substr(0, tp);
		}
	}
	return tp;
}

function IsDefined(variable)
{
	return eval('(typeof(' + variable + ') != "undefined");');
}

function RepeatChar(ch, c) {
	ch = ch.charAt(0);
	var rs = "";
	for (var i = 0; i < c; i++) {
		rs += ch;
	}
	return rs;
}

function RepeatStr(st, c) {
	var rs = "";
	for (var i = 0; i < c; i++) {
		rs += st;
	}
	return rs;
}

function gi(cnt) {
	return RepeatStr(PxXmlIndent, cnt);
}

function CancelJQEvent(evt) {
	if (typeof (evt) == "undefined")
		return;

	evt.preventDefault()
	evt.isDefaultPrevented()
	evt.stopPropagation()
	evt.isPropagationStopped()
	evt.stopImmediatePropagation()
	evt.isImmediatePropagationStopped()
}

DetectBrowser();

dcChar = GetDecimalSeparator();

PXPoint = function(xp, yp) {
	this.x = 0;
	this.y = 0;
	if (typeof (xp) != "undefined") {
		this.x = xp;
	}
	if (typeof (yp) != "undefined") {
		this.y = yp;
	}

	this.toString = function() {
		return "(" + this.x.toString() + ", " + this.y.toString() + ")";
	}

	this.Clone = function() {
		return new PXPoint(this.x, this.y);
	}

	this.Dot = function(v2) {
		return this.x * v2.x + this.y + v2.y;
	}

	this.Normalize = function() {
		var vl = Math.sqrt((this.x * this.x) + (this.y * this.y));
		if (vl != 0 && vl != 1) {
			this.x = this.x / vl;
			this.y = this.y / vl;
		}
	}

	this.Add = function(sp) {
		this.x += sp.x;
		this.y += sp.y;
	}

	this.Subtract = function(sp) {
		this.x -= sp.x;
		this.y -= sp.y;
	}
}

PXRect = function(x, y, w, h) {
	if (x != null && y != null) {
		this.p = new PXPoint(x, y);
	} else {
		this.p = new PXPoint(0, 0);
	}
	if (w != null && h != null) {
		this.s = new PXPoint(w, h);
	} else {
		this.s = new PXPoint(0, 0);
	}

	this.b = 0;
	this.r = 0;

	this._updateBR = function() {
		this.b = this.p.y + this.s.y;
		this.r = this.p.x + this.s.x;
	}


	this.SetPos = function(x, y) {
		this.p.x = x;
		this.p.y = y;
		this._updateBR();
	}

	this.SetSize = function(w, h) {
		this.s.x = w;
		this.s.y = h;
		this._updateBR();
	}

	this.ContainsPoint = function(pnt) {
		if (pnt.x >= this.p.x) {
			if (pnt.x <= this.r) {
				if (pnt.y >= this.p.y) {
					if (pnt.y <= this.b) {
						return true;
					}
				}
			}
		}

		return false;
	}

	this.toString = function() {
		return "[" + this.p.x.toString() + ", " + this.p.y.toString() + "]-[" + this.s.x.toString() + ", " + this.s.y.toString() + "]";
	}

	this.Clone = function() {
		return new PXRect(this.p.x, this.p.y, this.s.x, this.s.y);
	}

	this.Translate = function(x, y) {
		this.SetPos(this.p.x + x, this.p.y + y);
	}

	this._updateBR();
}

function PXPRectFromElement(el) {
	var x = 0;
	var y = 0;
	var w = 0;
	var h = 0;

	var tel = el;

	w = tel.offsetWidth;
	h = tel.offsetHeight;

	while (tel != null) {
		x += tel.offsetLeft;
		y += tel.offsetTop;
		tel = tel.offsetParent;
	}

	var er = new PXRect(x, y, w, h);
	return er;
}

PXPoly = function(pdata) {
	this.pps = new Array();
	this.pp = new PXPoint(0, 0);
	this.ps = new PXPoint(0, 0);

	if (pdata != null) {
		if (typeof (pdata.pos) != "undefined") {
			this.pp = pdata.pos;
		}
		if (typeof (pdata.points) != "undefined") {
			for (var pi = 0; pi < pdata.points.length; pi++) {
				var cp = pdata.points[pi];
				this.pps.push(cp);
				if (this.ps.x < cp.x) {
					this.ps.x = cp.x;
				}
				if (this.ps.y < cp.y) {
					this.ps.y = cp.y;
				}
			}
		}
	}

	this.pdiv = document.createElement("div");
	this.pdiv.style.position = "absolute";
	document.body.appendChild(this.pdiv);

	this.pjg = new jsGraphics(this.pdiv);

	this.UpdatePolyDiv = function() {
		this.pdiv.style.width = this.ps.x.toString() + "px";
		this.pdiv.style.height = this.ps.y.toString() + "px";
		this.pdiv.style.left = this.pp.x.toString() + "px";
		this.pdiv.style.top = this.pp.y.toString() + "px";

		this.pjg.clear();
		if (this.pps.length > 2) {
			var fp = this.pps[0];
			var cp = null;
			for (var ppi = 1; ppi < this.pps.length; ppi++) {
				var cpp = this.pps[ppi - 1];
				cp = this.pps[ppi];

				this.pjg.setColor("#ff0000");
				this.pjg.drawLine(cpp.x, cpp.y, cp.x, cp.y);
			}
			this.pjg.drawLine(cp.x, cp.y, fp.x, fp.y);
			this.pjg.paint();
		}

	}

	this.ClientContainsPoint = function(pt) {
		for (var c = false, i = -1, l = this.pps.length, j = l - 1; ++i < l; j = i)
			((this.pps[i].y <= pt.y && pt.y < this.pps[j].y) || (this.pps[j].y <= pt.y && pt.y < this.pps[i].y))
		&& (pt.x < (this.pps[j].x - this.pps[i].x) * (pt.y - this.pps[i].y) / (this.pps[j].y - this.pps[i].y) + this.pps[i].x)
		&& (c = !c);
		return c;
	}

	this.ContainsPoint = function(pt) {
		var tp = pt.Clone();
		pt.Subtract(this.pp);
		return this.ClientContainsPoint(pt);
	}

	this.UpdatePolyDiv();
}

function GetViewSize() {
    var viewportwidth;
    var viewportheight;
 
     // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
     
     if (typeof window.innerWidth != 'undefined')
     {
          viewportwidth = window.innerWidth,
          viewportheight = window.innerHeight
     }
     
    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

     else if (typeof document.documentElement != 'undefined'
         && typeof document.documentElement.clientWidth !=
         'undefined' && document.documentElement.clientWidth != 0)
     {
           viewportwidth = document.documentElement.clientWidth,
           viewportheight = document.documentElement.clientHeight
     }
     
     // older versions of IE
     
     else
     {
           viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
           viewportheight = document.getElementsByTagName('body')[0].clientHeight
     }

     return new PXPoint(viewportwidth, viewportheight);
}

Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  var rarr = this.push.apply(this, rest);
  
  if (typeof(rarr) == "number") 
    return this;
    
  return rarr;
};

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function OnListDeleteButtonClick(evt) {
    var ev = GetEventObject(evt);

    if (confirm(deleteRecordText) == false) {
        return false;
    } else {
        return true;
    }
}

String.prototype.endsWith = function(str)
{ return (this.match(str + "$") == str) }

function GetProps(pt) {
    var ptt = pt;
    var pr = new Array();

    var prrx = new RegExp(' ? ? ? ? ?([^"]*)="([^"]*) ? ? ? ? ?');
    var m = prrx.exec(pt);
    while (m != null) {
        if (m != null) {
            if (m.length == 3) {
                var vn = m[1].toLowerCase();
                var vv = m[2];

                pr[vn] = vv;
            }
            pt = pt.replace(m[0], "");
        }

        m = prrx.exec(pt);
    }

    return pr;
}

function EncodeHtml(htmlText) {
    var encodedHtml = encodedHtml = escape(htmlText);
    encodedHtml = encodedHtml.replace(/\//g, "%2F");
    encodedHtml = encodedHtml.replace(/\?/g, "%3F");
    encodedHtml = encodedHtml.replace(/=/g, "%3D");
    encodedHtml = encodedHtml.replace(/&/g, "%26");
    encodedHtml = encodedHtml.replace(/@/g, "%40");
    return encodedHtml;
}

function GetHSGHtml(has) {
    var gh = "";

    var id = has['id'];
    var ts = has['thumbsize'];
    if (ts == null)
        ts = "0";
    var is = has['imgsize'];
    if (is == null)
        is = "5";
    var cc = has['cols'];
    if (cc == null)
        cc = "3";
    var al = has['align'];
    if (al == null)
        al = "";
    var ma = has['max'];
    if (ma == null)
        ma = "0";

    var ccn = parseInt(cc) + 1;

    var ccol = 1;

    var cg = galls[id];

    gh = "<table cellspacing=\"2\" cellpadding=\"0\" border=\"0\" style=\"margin-left: auto; margin-right: auto; width: auto;\">";

    for (var ii = 0; ii < cg.length; ii++) {
        if (ccol % ccn == 1) {
            gh += "<tr>";
        }

        var ci = cg[ii];
        var ciu = "imgs/" + ci.id + "/" + ts + "/" + ci.name + ".jpg";
        gh += "<td>";
        gh += "<a class=\"highslide hsg_hs_a\" onclick=\"return hs.expand(this, { slideshowGroup: '" + id + "', captionText: '" + ci.name + "' })\" href=\"imgs/" + ci.id + "/" + is + "/" + ci.name + ".jpg\">";
        gh += "<img src=\"" + ciu + "\" alt=\"" + ci.name + "\" />";
        gh += "</a>";
        gh += "</td>";

        if (ccol % ccn == 0) {
            gh += "</tr>";
        }

        ccol++;
    }

    gh += "</table>";

    return gh;
}

function ParseHSG(cont) {
            var nc = cont;

            var hsgrx = new RegExp('<hsg([^>]*)>.*</hsg>');
            var m = hsgrx.exec(nc);
            if (m != null) {
                var hsgh = "";
                if (m.length > 1) {
                    var hsgt = m[1].replace(">", "").replace("<", "");
                    console.log("HSGT: " + hsgt);
                    var hsgat = GetProps(hsgt);

                    hsgh = GetHSGHtml(hsgat);
                }
                nc = nc.replace(m[0], hsgh);
            }

            return nc;
        }

        BrowserInfo = function () {
            this.type = "";
            this.btype = "";
            this.version = "";

            //document.write("App. Name: [" + navigator.appName + "]<br>\nUser Agent: [" + navigator.userAgent + "]");

            if (navigator.appName.indexOf('Microsoft') != -1) {
                this.type = 'IE';
                this.btype = 'IE';
            } else if (navigator.appName.indexOf('Netscape') != -1) {
                this.type = 'NS';

                if (navigator.userAgent.indexOf('Flock') != -1) {
                    this.btype = 'FL';
                } else if (navigator.userAgent.indexOf('Apple') != -1) {
                    this.btype = 'SA';
                } else if (navigator.userAgent.indexOf('Navigator') != -1) {
                    this.btype = 'NS';
                } else if (navigator.userAgent.indexOf('Mozilla') != -1) {
                    this.btype = 'FF';
                } else {
                    this.type = 'NS';
                }
            } else if (navigator.appName.indexOf('Opera') != -1) {
                this.type = 'OP';
                this.btype = 'OP';
            } else {
                this.type = 'IE';
                this.btype = 'IE';
            }


            if (this.version == "") {
                this.version = navigator.appVersion;
                var paren = this.version.indexOf('(');
                var whole_version = navigator.appVersion.substring(0, paren - 1);
                this.version = parseInt(whole_version);
            }
        }

        function FileNameStripExt(fn) {

            var fnn = fn;
            var ldi = fnn.lastIndexOf('.');
            if (ldi > 0) {
                fnn = fnn.replace(/\.{0,1}[^\.]*$/, "");
            } else if (ldi == 0) {
                fnn = "";
            }
            return fnn;
        }

        var brInfo = new BrowserInfo();

        function f_clientWidth() {
            return f_filterResults(
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
        }
        function f_clientHeight() {
            return f_filterResults(
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
        }
        function f_scrollLeft() {
            return f_filterResults(
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
        }
        function f_scrollTop() {
            return f_filterResults(
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
        }
        function f_filterResults(n_win, n_docel, n_body) {
            var n_result = n_win ? n_win : 0;
            if (n_docel && (!n_result || (n_result > n_docel)))
                n_result = n_docel;
            return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
        }



        function findParentWithAttribute(cn, attrname, attrval) {
            if (typeof (cn.hasAttribute) == "undefined")
                return null;

            if (cn.hasAttribute(attrname))
                return cn;

            if (cn.parentNode != null) 
                return findParentWithAttribute(cn.parentNode, attrname, attrval);
            
            return null;
        }

        function findParentWithClass(cn, classname) {
            if (typeof (cn) == "undefined")
                return null;
            
            if (cn == null)
                return null;

            if (typeof (cn.className) == "undefined")
                return null;

            if (cn.className.contains(classname))
                return cn;

            if (cn.parentNode != null)
                return findParentWithClass(cn.parentNode, classname);

            return null;
        }

        function findParentWithTag(cn, tagname) {
            if (typeof (cn.tagName) == "undefined")
                return null;

            if (cn.tagName.toUpperCase() == tagname.toUpperCase())
                return cn;

            if (cn.parentNode != null)
                return findParentWithTag(cn.parentNode, tagname);

            return null;
        }

        function findChildWithTag(cn, tagname) {
            if (typeof (cn.tagName) == "undefined")
                return null;

            if (cn.tagName.toUpperCase() == tagname.toUpperCase())
                return cn;

            if (typeof(cn.childNodes) != "undefined") {
                if (cn.childNodes.length > 0) {
                    for (var i = 0; i < cn.childNodes.length; i++) {
                        var fc = findParentWithTag(cn.childNodes[i], tagname);
                        if (fc != null)
                            return fc;
                    }
                }
            }

            return null;
        }

        function findChildWithClass(cn, classname) {
            if (typeof (cn) == "undefined") return null;
            if (cn == null) return null;

            if (typeof (cn.className) == "undefined")
                return null;

            if (cn.className.contains(classname))
                return cn;

            if (typeof(cn.childNodes) != "undefined") {
                if (cn.childNodes.length > 0) {
                    for (var i = 0; i < cn.childNodes.length; i++) {
                        var fc = findChildWithClass(cn.childNodes[i], classname);
                        if (fc != null)
                            return fc;
                    }
                }
            }

            return null;
        }

        function StringFromNull(strVal, defVal) {
            var rs = defVal;
            if (typeof (strVal) != "undefined") {
                if (strVal != null) {
                    return strVal.toString();
                }
            }
            return rs;
        }


        function GetEventMouseButton(e) {
            var b = -1;
            var ev = GetEventObject(e);
            if (brIE) {
                b = e.button - 1;
            } else {
                b = e.button;
            }
            return b;
        }

        if (typeof (console) == "undefined") {
            pxpLogEnabled = false;            
        } else {
            if (typeof(console.log) == "undefined") pxpLogEnabled = false;
        }


        function pxlog(msg) {
            if (pxpLogEnabled) {
                console.log(msg);                
            }
        }           
        

        function GetChildsWithClassName(cn, pn) {
            var chs = new Array();

            if (typeof (pn.className) != "undefined") {
                if (pn.className.contains(cn)) {
                    chs.push(pn);
                }
                if (pn.childNodes.length > 0) {
                    for (var i = 0; i < pn.childNodes.length; i++) {
                        var ccn = pn.childNodes[i];
                        var fcn = GetChildsWithClassName(ccn, cn);
                        if (fcn.length > 0) {
                            for (var ci = 0; ci < fcn.length; ci++) {
                                chs.push(fcn[ci]);
                            }
                        }
                    }
                }
            }

            return chs;
        }

        var getElementsByClassName = function (className, elm, tag) {
            if (document.getElementsByClassName) {
                getElementsByClassName = function (className, elm, tag) {
                    elm = elm || document;
                    var elements = elm.getElementsByClassName(className),
				nodeName = (tag) ? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
                    for (var i = 0, il = elements.length; i < il; i += 1) {
                        current = elements[i];
                        if (!nodeName || nodeName.test(current.nodeName)) {
                            returnElements.push(current);
                        }
                    }
                    return returnElements;
                };
            }
            else if (document.evaluate) {
                getElementsByClassName = function (className, elm, tag) {
                    tag = tag || "*";
                    elm = elm || document;
                    var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace) ? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
                    for (var j = 0, jl = classes.length; j < jl; j += 1) {
                        classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
                    }
                    try {
                        elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
                    }
                    catch (e) {
                        elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
                    }
                    while ((node = elements.iterateNext())) {
                        returnElements.push(node);
                    }
                    return returnElements;
                };
            }
            else {
                getElementsByClassName = function (className, elm, tag) {
                    tag = tag || "*";
                    elm = elm || document;
                    var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all) ? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
                    for (var k = 0, kl = classes.length; k < kl; k += 1) {
                        classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
                    }
                    for (var l = 0, ll = elements.length; l < ll; l += 1) {
                        current = elements[l];
                        match = false;
                        for (var m = 0, ml = classesToCheck.length; m < ml; m += 1) {
                            match = classesToCheck[m].test(current.className);
                            if (!match) {
                                break;
                            }
                        }
                        if (match) {
                            returnElements.push(current);
                        }
                    }
                    return returnElements;
                };
            }
            return getElementsByClassName(className, elm, tag);
        };


        function FindPreviousElementSibling(pn) {
            var pes = pn.previousSibling;
            if (typeof (pes) != "undefined") {
                if (pes != null) {
                    if (pes.nodeType == 1) {
                        return pes;
                    } else {
                        return FindPreviousElementSibling(pes);
                    }
                }
            }
            return null;
        }

        function FindNextElementSibling(pn) {
            var pes = pn.nextSibling;
            if (typeof (pes) != "undefined") {
                if (pes != null) {
                    if (pes.nodeType == 1) {
                        return pes;
                    } else {
                        return FindNextElementSibling(pes);
                    }
                }
            }
            return null;
        }

        function HideChildren(n) {
            for (var i = 0; i < n.childNodes.length; i++) {
                n.childNodes[i].style.display = "none";
            }
        }

        function ShowChildren(n) {
            for (var i = 0; i < n.childNodes.length; i++) {
                n.childNodes[i].style.display = "block";
            }
        }

        function getScrollTop(win, doc) {
            return f_filterResults(
		win.pageYOffset ? win.pageYOffset : 0,
		doc.documentElement ? doc.documentElement.scrollTop : 0,
		doc.body ? doc.body.scrollTop : 0
	);}

        function getScrollLeft(win, doc) {
            return f_filterResults(
		win.pageXOffset ? win.pageXOffset : 0,
		doc.documentElement ? doc.documentElement.scrollLeft : 0,
		doc.body ? doc.body.scrollLeft : 0
	);
}

function RemoveHTMLTags(htmlIn) {
//if (document.getElementById && document.getElementById("input-code")) {
    var strInputCode = htmlIn;
    /* 
    This line is optional, it replaces escaped brackets with real ones, 
    i.e. < is replaced with < and > is replaced with >
    */
    strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1) {
        return (p1 == "lt") ? "<" : ">";
    });
    return strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
    //alert("Output text:\n" + strTagStrippedText);
    // Use the alert below if you want to show the input and the output text
    //		alert("Input code:\n" + strInputCode + "\n\nOutput text:\n" + strTagStrippedText);	
//}
}

GetElsByClass = function (cls, el) {
    return getElementsByClassName(cls, el);
}

function bookmarksite(title, url) {
    if (window.sidebar) // firefox
        window.sidebar.addPanel(title, url, "");
    else if (window.opera && window.print) { // opera
        var elem = document.createElement('a');
        elem.setAttribute('href', url);
        elem.setAttribute('title', title);
        elem.setAttribute('rel', 'sidebar');
        elem.click();
    }
    else if (document.all)// ie
        window.external.AddFavorite(url, title);
}

function IncludeJS(filename) {
    var body = document.getElementsByTagName('body').item(0);
    script = document.createElement('script');
    script.src = filename;
    script.type = 'text/javascript';
    body.appendChild(script)
}

function getViewportHeight() {
    var height = window.innerHeight; // Safari, Opera
    var mode = document.compatMode;

    if ((mode || !$.support.boxModel)) { // IE, Gecko
        height = (mode == 'CSS1Compat') ?
            document.documentElement.clientHeight : // Standards
            document.body.clientHeight; // Quirks
    }

    return height;
}

function isElementVisible(el, complete) {
    var cv = true;
    if (typeof (complete) == "boolean")
        cv = complete;
    var vpH = getViewportHeight(),
            scrolltop = (document.documentElement.scrollTop ?
                document.documentElement.scrollTop :
                document.body.scrollTop);

    var $el = $(el),
                    top = $el.offset().top,
                    height = $el.height();

    if (cv) {
        if (scrolltop > (top) || scrolltop + vpH < top) {
            return false;
        } else if (scrolltop < (top)) {
            return true;
        }
    } else {
        if (scrolltop > (top + height) || scrolltop + vpH < top) {
            return false;
        } else if (scrolltop < (top + height)) {
            return true;
        }
    }
}

function GetEventKeyCode(e) {
    var ev = GetEventObject(e);
  if (ev.which == null)
     return String.fromCharCode(ev.keyCode);
  else if (ev.which != 0 && ev.charCode != 0)
      return String.fromCharCode(ev.which);
}

function nbool(val, defv) {
    if (typeof (val) == "boolean") return val;    
    if (typeof (defv) == "boolean") return defv;
    return null;
}

function nstr(val, defv) {
    if (typeof (val) == "string") return val;
    if (typeof (defv) == "string") return defv;
    return "";
}

function nisempty(val) {
    if (typeof (val) == "undefined") return true;
    
    if (val == null) return true;

    return false;
}

function mouseX(evt) {
    if (evt.pageX) return evt.pageX;
    else if (evt.clientX)
        return evt.clientX + (document.documentElement.scrollLeft ?
   document.documentElement.scrollLeft :
   document.body.scrollLeft);
    else return null;
}
function mouseY(evt) {
    if (evt.pageY) return evt.pageY;
    else if (evt.clientY)
        return evt.clientY + (document.documentElement.scrollTop ?
   document.documentElement.scrollTop :
   document.body.scrollTop);
    else return null;
}
