﻿var w3c = (document.getElementById)?true:false;
var agt = navigator.userAgent.toLowerCase();
var ie  = ((agt.indexOf("msie")!=-1)&&(agt.indexOf("opera")==-1)&&(agt.indexOf("omniweb")==-1));
var ie5 = (w3c&&ie)?true:false;
var ns6 = (w3c&&(navigator.appName=="Netscape"))?true:false;
var op8 = (navigator.userAgent.toLowerCase().indexOf("opera")==-1)?false:true;

function getOffset(o) {var l=0;var t=0;if(ie5||ns6||op8){var p=o;while(p!=null&&p!=document.body){l+=p.offsetLeft;t+=p.offsetTop;if(!ns6){var lw=parseInt(p.currentStyle.borderLeftWidth);var tw=parseInt(p.currentStyle.borderTopWidth);if(lw>0)l+=lw;if(tw>0)t+=tw;}p=p.offsetParent;}}else{l=o.offsetLeft;t=o.offsetTop;}o.offsetLeft2=l;o.offsetTop2=t;return o;}
function addHandler(o,e,ne) {if(!o)return;var tmp=o[e];var args=Array.prototype.slice.call(arguments,3);o[e]=function(){if(ne)ne.apply(o,args);if(tmp)tmp.apply(o);};}

function gc() {if(CollectGarbage)setTimeout(CollectGarbage,1);}

function o(s) {document.write(s);}
function $(n) {return document.getElementById(n);}
function $$(n) {return document.getElementsByName(n);}
function request(name) {var url=document.location.href;if(url.indexOf('?')!=-1){url=url.split('?');url=url[url.length-1].split('&');for(var i=0;i<url.length;i++){var v=url[i].split('=');if(v[0]==name)return v[1];}}}

var setTimeout222=window.setTimeout;
window.setTimeout=function(fref,delay) {if(typeof fref=='function'){var args=Array.prototype.slice.call(arguments,2);var f=(function(){fref.apply(null,args);});return setTimeout222(f,delay);}return setTimeout222(fref,delay);} 

Date.prototype.toString = function(f) {
	function tempfunc(opo, pos) { var val = ''; opo = String(opo);
		for(var a = 1; a < arguments.length; a ++) { 
		var chr = opo.charAt(arguments[a] - 1); val += chr; } return val;
	}

	if(!f) f = 'yyyy-MM-dd HH:mm:ss';
	var h12 = this.getHours() > 12 ? (this.getHours() - 12) : this.getHours()
	var tmp = {
		'yyyy' : this.getYear(),
		'yy'   : tempfunc(this.getYear(), 3, 4),
		'MM'   : (this.getMonth() < 9 ? '0' : '') + (this.getMonth() + 1),
		'M'	: this.getMonth() + 1,
		'dd'   : (this.getDate() < 10 ? '0' : '') + this.getDate(),
		'd'	: this.getDate(),
		'hh'   : (h12 < 10 ? '0' : '') + h12,
		'h'	: h12,
		'HH'   : (this.getHours() < 10 ? '0' : '') + this.getHours(),
		'H'	: this.getHours(),
		'mm'   : (this.getMinutes() < 10 ? '0' : '') + this.getMinutes(),
		'm'	: this.getMinutes(),
		'ss'   : (this.getSeconds() < 10 ? '0' : '') + this.getSeconds(),
		's'	: this.getSeconds()
	};

	for (var p in tmp) f = f.replace(new RegExp('\\b' + p + '\\b', 'g'), tmp[p]);
	return f;
}

Date.prototype.getWeekey = function() {
	return def.week[this.getDay()];
}

Number.prototype.round = function(r) {
	r = typeof(r) == 'undefined' ? 1 : r;
	var rv = String(this);
	var io = rv.indexOf('.');
	var ri = io == -1 ? '' : rv.substr(io + 1, r);
	var le = io == -1 ? (rv + '.') : rv.substr(0, io + 1 + r);
	for(var a = ri.length ; a < r ; a ++) le += '0';
	return le;
}

String.Empty = '';
String.prototype.trim = function() {
	return this.ltrim().rtrim();
}
String.prototype.ltrim = function() {
	return this.replace(/^ +([^ ]*)/g, '$1');
}
String.prototype.rtrim = function() {
	return this.replace(/([^ ]*) +$/g, '$1');
}

String.prototype.format = function() {
	var val = this.toString();
	for(var a = 0 ; a < arguments.length ; a ++) val = val.replace(new RegExp("\\{" + a + "\\}", "g"), arguments[a]);
	return val;
}

String.prototype.getLength = function() {
	return this.replace(/([\u0391-\uFFE5])/ig,'11').length;
}

String.prototype.parseDate = function() {
	/^(\d{1,4})\-(\d{1,2})\-(\d{1,2})( (\d{1,2})(:(\d{1,2})(:(\d{1,2}))?)?)?$/gi.exec(this);
	return new Date(
	RegExp.$1 ? parseInt(RegExp.$1, 10) : 0,
	RegExp.$2 ? parseInt(RegExp.$2, 10) - 1 : 0,
	RegExp.$3 ? parseInt(RegExp.$3, 10) : 0,
	RegExp.$5 ? parseInt(RegExp.$5, 10) : 0,
	RegExp.$7 ? parseInt(RegExp.$7, 10) : 0,
	RegExp.$9 ? parseInt(RegExp.$9, 10) : 0);
}

String.prototype.toFloat = function(Round) {
	return /^[+\-]?\d+(\.\d+)?$/g.test(this) == false ? 0 : parseFloat(this);
}

String.prototype.toInteger = function(Default) {
	var temp = this.replace(/[^\d]/g, '');
	return temp == '' ? (Default ? Default : 0) : parseInt(temp);
}

String.prototype.toJavaString = function() {
	return this.replace(/\\/g, '\\\\').replace(/'/g, "\\'").replace(/\n/g, '\\n').replace(/\r/g, '');
}

String.prototype.toTitleString = function() {
	return this.replace(/"/g, '&quote;').replace(/\n/g, '&#13;');
}

String.prototype.toHtmlString = function() {
	return this.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n/g, '<br>').replace(/\t/g, '&nbsp;&nbsp;').replace(/ /g, '&nbsp;').replace(/"/g, '&quot;');
}

function extractobj(obj) {
	var parms = Array.prototype.slice.call(arguments, 1);
	parms.push('span');
	for(var a = 0; a < parms.length; a++) {
		var ts = obj.getElementsByTagName(parms[a]);
		for(var b = 0; b < ts.length; b++) if (ts[b].id) obj[ts[b].id] = ts[b];
	}
}

function setCookie(name, value)
{
	var expires = new Date(new Date().getTime() + (100 + 30) * 24 * 60 * 60 * 1000);
	document.cookie = name + "=" + escape(value) + ";expires=" + expires.toGMTString();
}

function getCookie(name)
{
	var a_start, an_end, text;
	if (document.cookie)
	{
		text = document.cookie.toString();
		a_start = document.cookie.indexOf(name + "=");
		if (a_start < 0) return '';
		text = text.substr(a_start + name.length + 1, text.length);
		if (text.indexOf(';') != -1) text = text.substr(0, text.indexOf(';'));
		return unescape(text);
	}
	else return '';
}

function dialog(id, subject) {
	var dg = $(id);
	if (!dg) {
		dg = document.createElement('div');
		dg.id = id;
		dg.className = 'dialog';
		dg.innerHTML = '<div class="table"><dl class="left-right" style="cursor:pointer;"><dt id="subject" style="color:white;"></dt>\
<dd><span style="text-align:center;background:#fff;padding:0px 6px 0px 6px;margin-right:6px;border:1px solid #ccc;cursor:pointer;">х</span></dd></dl>\
<div id="content"></div>\
<div id="footer"></div></div>';
		extractobj(dg, 'dt', 'div');
		document.body.appendChild(dg);
	}
	dg.subject.innerHTML = subject || '&nbsp;';
	dg.content.innerHTML = '<div style="padding:30px;">' + def.loading + '</div>';
	
	dg.style.top = (document.body.clientHeight - dg.offsetHeight) / 2 + 'px';
	dg.style.left = (document.body.offsetWidth - dg.offsetWidth - document.body.scrollLeft) / 2 + 'px';
	
	dg.style.visibility = ''
	dg.subject.onclick = dg.subject.parentNode.onclick = function() {
		dg.style.visibility = 'hidden';
		return false;
	};
	return dg;
}
