﻿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 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 $$(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);} 
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,i=0; a<arguments.length; a++){
		if(arguments[a] instanceof Array){
			for(var j=0; j<arguments[a].length; j++){
				val = val.replace(new RegExp("\\{" + i++ + "\\}", "g"), arguments[a][j]);
			}
		} else {
			val = val.replace(new RegExp("\\{" + i++ + "\\}", "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 dialog(id, subject) {
	var mask_id = 'mask';
	var mask = $('mask_id');
	if (!mask) {
		mask = document.createElement('div');
		mask.id = mask_id;
		//document.body.appendChild(mask);
	}
	mask.style.height = document.body.offsetHeight + 'px';
	
	var dg = $(id);
	if (!dg) {
		dg = document.createElement('div');
		dg.id = id;
		dg.className = 'dialog';
		dg.style.height='auto';
		dg.innerHTML = '<div id="subject"></div><div id="content" style="margin:0px;"></div><span id="footer"></span>';
		extractobj(dg, 'div');
		dg.subject.innerHTML = '<span class=l><b>' + subject + '</b></span>';
		document.body.appendChild(dg);
	}
	 
	dg.content.innerHTML = '<div style="padding:30px;">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 = function() {		
		dg.style.visibility = mask.style.visibility = 'hidden';
		return false;
	};
	return dg;
}
