/***
÷òîáû ïîìåñòèòü èíôîðìàöèþ â äèâ èñïîëüçóåì ñëåäóþùèé øàáëîí:
httpRequest(URI+"WidgetRegistry.ajax", "wgName="+wgName, setDIV, wg.Add_Inc, 'getResult');
httpRequest("/genet/interface/templates/widgetsbar.html", "id="+id, setDIV, wg.divWithText, 'DIV');	
ò.å. âûçûâàåì ôóíêöèþ ÑÅÒÄÈÂ ïåðåäàåì ïàðàìåòðû. òîæå ñàìîå äåëàåì åñëè íàì íóæíî ïðîñòî âûçâàòü êàêóþ-òî ôóíêöèþ
ïî îêí÷àíèþ ðàáîòû
***/

function httpRequest(url, parameters, onComplete, package, flag) {

    var http_request = false;
    try {
        http_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e1) {
        try {
            http_request= new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e2) {
            http_request = new XMLHttpRequest();
        }
    }

    if (!http_request) {
      	alert('Cannot create XMLHTTP instance');
      return false;
    }

    completeListener = function() {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
            	if (flag == 'DIV' || flag == 'getResult') {
                		onComplete(http_request, package, flag)
                } else {
                		if (onComplete != "null")
               			onComplete(package);
                }
            }
        }
    };

    http_request.onreadystatechange = completeListener;

    http_request.open('GET', url + "?" + parameters, true);

    http_request.send(null);
}

function setDIV(http_request, dataPackage, flag) {
    request_result = http_request.responseText;
    if (flag == 'getResult') {
    	    dataPackage(request_result);
    } else {
          var divElement = document.getElementById(dataPackage);
          divElement.innerHTML = request_result;             
    }
}

function Class() { };

Class.prototype.construct = function() { };

Class.extend = function(def) {

    var classDef = function() {
        if (arguments[0] !== Class) {
            this.construct.apply(this, arguments);
        }
    };

    var proto = new this(Class);
    var superClass = this.prototype;

    for (var n in def) {
        var item = def[n];
        if (item instanceof Function) item.$ = superClass;
                else classDef[n] = item;
        proto[n] = item;
    }

    classDef.prototype = proto;
    classDef.extend = this.extend;

    return classDef;
};

var URI = window.location.pathname.match(/^(.*)\/|$|\/$|\?/ )[1]+"/";

function getElementsByClassName(className, tag, elm){
	var testClass = new RegExp("(^|\\\\s)" + className + "(\\\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}

var mynavigator = {IE:false, Opera: false, FireFox: false, Other: false};

if (window.execScript) {
	mynavigator.IE = true;
} else if (document.height == 0) {
	mynavigator.FireFox = true;
} else if (window.opera) {
	mynavigator.Opera = true;	
} else if (document.all) {
	mynavigaror.IE = true;	
} else {
	mynavigator.Other = true;
}


function isArray()
{
  if (typeof arguments[0] == 'object')
  {  
    var criterion = arguments[0].constructor.toString().match(/array/i);
   return (criterion != null);  
  }
  return false;
}
function imgresize (img, imgMaxW, imgMaxH) {
	im = new Image();
	im.src = img.src;
raz = 0;
/*
	k1 = img.width / img.height;
	k2 = imgMaxW / imgMaxH;
	if (k1 < k2) {
			img.width = imgMaxW;
			img.height = imgMaxW * ;
	} else {
			img.width = imgMaxW;
			img.height = (imgMaxW * img.height) / img.width;
	}
	*/
	if (imgMaxW <= im.width && imgMaxH >= (im.height + raz) && imgMaxH != im.height) {
		k = im.width / imgMaxW;
		im.width = imgMaxW;
		im.height = Math.round(im.height / k);
	} else if (imgMaxH <= (im.height + raz) && imgMaxW >= im.width && imgMaxW != im.width) {
		k = imgMaxH / im.height;
		im.height = imgMaxH;
		im.width = Math.round(im.width / k);
	} else if (imgMaxW <= im.width && imgMaxH <= (im.height + raz) && (im.height + raz) >= im.width) {
		k = im.height / im.width;
		im.height = imgMaxH;
		im.width = Math.round(im.height / k);
	} else if (imgMaxW <= im.width && imgMaxH <= (im.height + raz) && (im.height + raz)<= im.width) {
		k = im.width / im.height;
		im.width = imgMaxW;
		im.height = Math.round(im.width / k);
	} else { 

	}
	
	img.width = im.width;
	img.height = im.height;
}

function trim (str) {
	str = str.replace(/^\s+/, '');
	for (var i = str.length - 1; i >= 0; i--) {
		if (/\S/.test(str.charAt(i))) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
}

 function radioTHD(r) {

      if (r.checked) { 
 	 r.checked = false;
      } else {
	 r.checked = true;
      }
    return true;
 }

	function randINT(int) {
		if (int) {
			ran_unrounded=Math.random()*int;
			return	ran_buff=Math.floor(ran_unrounded);
		}
	return false;
	}

function _urlencode(str) {
return escape(str).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
}

function $(id)
{
	return document.getElementById(id);
}

function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

function OuterHtml(element)
{
	 var c_element = element.cloneNode(true);
	 var parent = element.parentNode;
     var el = document.createElement(parent.tagName);
     el.appendChild(c_element);
     var html = el.innerHTML;
     return html;
}