var theDoc = null;

if (!Array.prototype.push) Array.prototype.push = new Function ('elem','var index=this.length;this[index]=elem;return index;');

var app = navigator.appName;
var ver = parseInt(navigator.appVersion.substring(0,1));
var is_ie4 = ((app == "Microsoft Internet Explorer") && (ver >= 4)); //browser_check && ((document.all)    ? true : false);
var is_ns  = ((app == "Netscape") && (ver >= 3)); //browser_check && ((document.layers) ? true : false);
var browser_check = is_ie4 || is_ns;

function MIN(a,b){return(a<b)?a:b;};
function MAX(a,b){return(a>b)?a:b;};
function BOUND(a,min,max){return(a<min)?min:(a>max)?max:a;};

function RAW_COPY(id,app){var res="";var ttt=document.all[id];if(!ttt)return res;if(app)ttt.insertAdjacentHTML("BeforeEnd",app);res=ttt.outerHTML;ttt.style.visibility="hidden";return res;};
function CreateElement(type,id,style,cls,parent){if(!theDoc)theDoc=document;var res=theDoc.createElement(type);if(!res)return null;res.id=id;if(style)ApplyStyles(res,style);if(cls)res.className=cls;if(parent)parent.appendChild(res);return res;};
function ApplyStyles(obj,styles){var temp=styles;while(temp.length>0){var i=temp.indexOf(":");var style_name=temp.substring(0,i);var ii=style_name.indexOf("-");if(ii!=-1) {var ttt=style_name.substring(0,ii);ttt+=style_name.substring(ii+1,ii+2).toUpperCase();ttt+=style_name.substring(ii+2,style_name.length);style_name=ttt;};var j=temp.indexOf(";");var value=temp.substring(i+1,j);temp=temp.substring(j+1,temp.length);eval('obj.style.'+style_name+'='+'"'+value+'"');};return true;};

function nop() { event.cancelBubble=true; event.returnValue=false; return false; };

//** array functions
function getFirstElem(obj)
{
	if (obj instanceof Array)
		return obj[0];
	else
		return obj;
};
function formArray(obj)
{
	var res=new Array();
	try {
		if (obj instanceof Array || (String(obj.length) != "undefined" && obj.length != 0)) {
			for (var i = 0; i < obj.length; i++) {
				res[i] = obj[i];
			};
		} else {
			res.length = 1;
			res[0] = obj;
		};
	} catch(err) {
		res.length = 1;
		res[0] = obj;
	};
	return res;
};

//** single-element DOM functions
function findElement(parent,tagName,id){return (is_ie4) ? getFirstElem(parent.all[id]) : findNamedItem(parent.getElementsByTagName(tagName),id); };
function findNamedItem(nodeList,id)
{
	for (var i = 0; i < nodeList.length; i++) {
		var node = nodeList.item(i);
		if (node.id == id || node.name == id) return node;
	};
	return null;
};

//** multiple-element DOM functions
function findElements(parent,tagName,id){return (is_ie4) ? formArray(parent.all[id]) : findNamedItems(parent.getElementsByTagName(tagName),id); };
function findNamedItems(nodeList,id)
{
	var result = new Array();
	for (var i = 0; i < nodeList.length; i++) {
		var node = nodeList.item(i);
		if (node.id == id || node.name == id) result.push(node);
	};
	return result;
};

function ToggleCheckbox2(oElem,inputId,imgId0,imgId1,newValue,fnProcessFn)
{
	if (typeof(oElem) != "object") oElem = findElement(document,"SPAN",String(oElem));
	if (!oElem) return;
	var oInput = findElement(oElem,"INPUT",inputId);
	var oImg0 = findElement(oElem,"IMG",imgId0);
	var oImg1 = findElement(oElem,"IMG",imgId1);

	var value = oInput.value;
	if (String(newValue) == "undefined") newValue = -1;
	var setValue = (newValue == -1) ? (!(value != "0")) : (newValue != false && newValue != "0");

	if (!setValue) {
		oImg0.style.display = "inline";
		oImg1.style.display = "none";
		oInput.value = "0";
	} else {
		oImg0.style.display = "none";
		oImg1.style.display = "inline";
		oInput.value = "1";
	};

	var result = (oInput.value == "1");

	if (fnProcessFn) {
		if (typeof(fnProcessFn) == "function") {
			fnProcessFn(result,oElem.id);
		} else if (typeof(fnProcessFn) == "object") {
      fnProcessFn.process (result);
		};
	};
	return result;
};

//** AUX. functions
function GetNumber(num,digits)
{
	var res = String(num);
	while (res.length < digits) {
		res = "0" + res;
	};
	return res;
};
function RoundPrec(fltNum,prec)
{
	prec = Math.pow(10,prec);
	return Math.round(fltNum * prec) / prec;
};

//** URL functions
function ConcatURL(baseURL,concatURL)
{
	var res = String(baseURL), toConcat=String(concatURL);
	if(toConcat.length>0 && toConcat!="" && toConcat!="undefined") {
		var app_sign = (res.indexOf("?")!=-1) ? "&" : "?";
		if ((toConcat.charAt(0) == "?") || (toConcat.charAt(0) == '&'))
			toConcat = toConcat.substring(1,toConcat.length);

		res += app_sign + toConcat;
	};
	return res;
};
function SetURLValue(baseURL,paramName,paramValue)
{
	var url = String(baseURL);
	var res = "";
	var testRG = new RegExp("[?&]?("+paramName+"=[^&]*)(&|$)","g");
	if (url.search(testRG) != -1) {
		res = url.replace(RegExp.$1,paramName+"="+paramValue);
	} else {
		res = ConcatURL(baseURL,paramName+"="+paramValue);
	};
	return res;
};


// hope your browser supports reg-exps
function Trim(str)
{
	str = str.replace(/^\s+/g,"");
	str = str.replace(/\s+$/g,"");
	return str;
};

function CheckEmail(str)
{
	var rgEmail = /^[0-9a-z_\-\.]+@([0-9a-z_\-]+\.)*[0-9a-z_\-]{2,}$/i;
	return str.match(rgEmail) != null;
};

function testLoginValue(str)
{
	var rg = /^[a-z0-9_\-!]+$/gi;
	return (str != "" && str.length >= 4 && str.length < 64 && (str.match(rg) != null));
};


function ProcessTextField_Multiline(val)
{
	if (!val) return "";
	// temp. solution

	val = val.replace(/&/gi,"&amp;");
	val = val.replace(/</gi,"&lt;");
	val = val.replace(/>/gi,"&gt;");
	return val;
};


/////////////////////////////////////////////////////////////////////
// from MSDN
// Create a cookie with the specified name and value.
// The cookie expires in a year
function SetCookie(sName, sValue)
{
	var date = new Date();
	date.setFullYear(date.getFullYear()+1);
	document.cookie = sName + "=" + escape(sValue) + "; expires=" + date.toGMTString();
};
// Retrieve the value of the cookie with the specified name.
function GetCookie(sName)
{
	// cookies are separated by semicolons
	var aCookie = document.cookie.split("; ");
	for (var i=0; i < aCookie.length; i++) {
	// a name/value pair (a crumb) is separated by an equal sign
	var aCrumb = aCookie[i].split("=");
	if (sName == aCrumb[0])
		return unescape(aCrumb[1]);
	};

	// a cookie with the requested name does not exist
	return "";
};
// Delete the cookie with the specified name.
function DelCookie(sName)
{
	document.cookie = sName + "=; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
};

// strings - массив спряженных словоформ
//    0 - '0'
//    1 - '1', 21, 31, 41, ...
//    2 - '2..4', 22-24, 32-34, ...
//    3 - '5', 25, 35, 45, ...
//    4 - '6-10,11-19,20', 26-30, 36-40, ...
// template:
// [/*0*/"",/*1*/"",/*2*/"",/*5*/"",/*6*/""]
// <xsl:value-of select="xmlUtils:conjugate_rus(@number,'0','1','2','5','6')"/>
function conjugate_rus(num,string0,string1,string2,string5,string6)
{
	if (num < 0) num = -num;
	num %= 100;
	if (num == 0) return string0;
	if (num >= 11 && num <= 19) return string6;
	num %= 10;
	if (num == 1) return string1;
	if (num >= 2 && num <= 4) return string2;
	if (num == 5) return string5;
	if (num >= 6 || num == 0) return string6;
	return string0;
}

