/*
 * javascript base lib
 * @author: zhang.xiaoye@gmail.com
 * @version: $Id: base_lib.js 621 2007-05-17 07:40:13Z xiaoye $
 */
/**
 * template engine
 * @param {Object} a
 * @param {Object} b
 */
function WocalTemplate(a,b)
{
	if(!b)
	{
		a=a.replace(/>(\s+)</g,"><");
		a=a.replace(/\s{2,}/g," ")
	}
	this.D=[];
	this.Ee=0;
	this.Va={};
	var c=a.match(/\$\{\w+\}/g)||[];
	var d=0;
	for(var e=0;e<c.length;++e)
	{
		var f=c[e];
		var g=a.indexOf(f,d);
		if(d!=g)
		{
			this.D.push(a.substring(d,g))
		}
		d=g+f.length;
		f=f.substring(2,f.length-1);
		var h=this.Va[f];
		if(!h)
		{
			h=[];
			this.Va[f]=h;
			++this.Ee;
			this["put_"+f]=this.Mf(this,f)
		}
		h.push(this.D.length);
		this.D.push(undefined)
	}
	if(d!=a.length)
	{
		this.D.push(a.substring(d))
	}
	this.ld=0
}
WocalTemplate.prototype.Mf=function(a,b)
{
	return function(c){return a.put(b,c)}
};
WocalTemplate.prototype.put=function(a,b)
{
	var c=this.Va[a];
	if(this.D[c[0]]===undefined)
		++this.ld;
	if(c.length===1)
	{
		this.D[c[0]]=b
	}
	else
	{
		for(var d=0;d<c.length;++d)
		{
			this.D[c[d]]=b
		}
	}
};
WocalTemplate.prototype.toString=function()
{
	if(!this.R())
	{
		this.debug(false,"some keys are missing values: "+this.wg())
	}
	return this.D.join("")
};
WocalTemplate.prototype.wg=function()
{
	var a=[];
	for(var b in this.kg())
		a.push(b);
	return a.join()
};
WocalTemplate.prototype.eh=function(a)
{
	return a&&a in this.Va
};
WocalTemplate.prototype.lh=function()
{
	return this.ld
};
WocalTemplate.prototype.R=function()
{
	return this.ld==this.Ee
};
WocalTemplate.prototype.ih=function()
{
	var a={};
	for(var b in this.Va)
		a[b]=null;
	return a
};
WocalTemplate.prototype.kg=function()
{
	var a={};
	for(var b in this.Va)
	{
		if(this.D[this.Va[b][0]]===undefined)
		{
			a[b]=null
		}
	}
	return a
}
WocalTemplate.prototype.debug=function(a,b)
{
	if (a==false)
	{
		alert(b);
	}
}
function _CheckDate(str)
{
	var aStr = str.split(' ');
	var date = aStr[0].split('-');
	var newDate = new Date(date[0], date[1]-1, date[2]);
	if(newDate.getMonth()+1 != date[1] || newDate.getDate() != date[2] || newDate.getFullYear() != date[0] || date[0].length != 4)
	{
		return false;
	}
	return true;
}
function _ReloadPage()
{
	window.location.reload();
}
function newWindow(url,name,w,h,scroll,resize, additionalSettings)
{
	var left = (screen.width-w)/2;
	var top = (screen.height-h)/2;
	var settings  ='height='+h+',';
	    settings +='width='+w+',';
	    settings +='top='+top+',';
	    settings +='left='+left+',';
	if(scroll==true)
	{
	    settings += 'scrollbars,';
	}
	if(resize==true)
	{
		settings += 'resizable,';
	}
	win = window.open(url,name,settings+additionalSettings);
	win.focus();
}

function _log(msg)
{
	alert(msg)
}

function LTrim(str)
{
	var whitespace = new String(" tnr");
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(0)) != -1)
	{
		var j=0, i = s.length;
		while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
		{
			j++;
		}
		s = s.substring(j, i);
	}
	return s;
}