/*ihoapm@gmail.com*/
function getObj(id)
{
	return document.getElementById(id);
}

function offsetLeft(o){
	var i = 0;
	while(o!=null && o.offsetParent!=null){
		i += o.offsetLeft;
		o = o.offsetParent;
	}
	return i + (o ? o.offsetLeft : 0);
}

function offsetTop(o){
	var i = 0;
	while (o!=null && o.offsetParent!=null){
		i += o.offsetTop;
		o = o.offsetParent;
	}
	return i + (o ? o.offsetLeft : 0);
}

function setFocus(controlName){	
	try{
		var ctrl = document.getElementsByName(controlName);
		if(ctrl) ctrl = ctrl[0];
		else ctrl = document.getElementById(controlName);
		if(ctrl.type=="text"){
			ctrl.focus();
			ctrl.value = ctrl.value + "";
			ctrl.select();
		}
		else{
			ctrl.focus();
		}
	}catch(e){}
}

function _preloadImages(){
	if(arguments.length){
		var s = "";
		for(i=0; i<arguments.length; ++i){
			s += "<img src='images/" + arguments[i] + "' style='width:1px;'/>";
		}
		odiv = document.createElement("div");
		odiv.style.visibility = "hidden"; odiv.style.height = "1px";
		odiv.innerHTML = s;		
		document.body.appendChild(odiv);
	}	
}

/*--------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------*/
function StrTrim(str){
	var s = LStrTrim(str);
	return RStrTrim(s);
}

function LStrTrim(str){		
	var s = new String(str);		
	if(s.length==0)
		return "";		
	if(s.charCodeAt(0)==32){	
		s = LStrTrim(s.substring(1,s.length));
	}
	return s;
}

function RStrTrim(str){		
	var s = new String(str);		
	if(s.length==0)
		return "";		
	if(s.charCodeAt(s.length-1)==32){				
		s = RStrTrim(s.substring(0,s.length-1));
	}
	
	return s;
}

function IsEmailAddress(strEmail){
	var re = /^([a-zA-Z_]+)([a-zA-Z0-9._%-]+)@[\w0-9.-]+\.[\w]{2,4}$/;
	return strEmail.match(re);
}

/*---date ---*/
function isLeapYear(year){
	return !(year%4 || (!(year%100) && year%400))
}

function validateDate(m, d, y){
	m = parseFloat(m); d = parseFloat(d); y = parseInt(y);
	if(d<1) return false;
	switch(m){
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12: return d<=31;
		case 4:
		case 6:
		case 9:
		case 11: return d<=30;
		case 2: return isLeapYear(y) ? (d<=29) : (d<=28);			
	}
	return false;
}
function isDateStr(s){
	re = /^[\d]{1,2}[-\/][\d]{1,2}[-\/][\d]{3,4}$/;
	if(!s.match(re)) return false;
	s = s.replace(/\//g, "-");
	parts = s.split("-");
	if(parts.length==3)
		return validateDate(parts[0], parts[1], parts[2]);
	return false;
}
function jsRoller(id, left)
{
	//props
	this.containerId = "js_roll_" + id;
	this.roller = document.getElementById(id);
	this.container = null;
	this.started = false;
	this.rollId = 0;
	this.dy = 1;
	this.delta = 0;
	this.itemsCount = 0;
	
	//self pointer
	jsRoller.prototype._pthis = this;
	
	//public methods
	//add item: image, title, id
	this.addItem = function(image, url, title){
		var oitem = document.createElement("<div>");
		var cx = 200;
		oitem.align="center";				
		oitem.innerHTML = "<a href='" + url + "' title=\"" + title + "\"><img src='" + image + "' style='border:0px;'></a><div style='height:2px;'></div>";
		this.container.appendChild(oitem);
		
		//resize the roller
		if(!this.itemsCount){
			this.roller.style.width = parseInt(this.roller.childNodes[0].offsetWidth) + "px";					
		}
		this.roller.style.height = parseInt(this.roller.childNodes[0].offsetHeight) + "px";
		++this.itemsCount;
	}
	
	this.setTop = function(y){
		this.roller.style.top = y;
	}//setTop
	
	//stop timeout
	this.startRoller = function(){
		if(this.roller.style.display != "block")
			this.roller.style.display = "block";
		if(this.rollId){
			clearTimeout(this.rollId);
			this.rollId = 0;
			this.started = false;
		}		
		this.rollId = setTimeout("jsRoller.prototype._runRolling()", 300);
	}//stop			
				
	//rolling
	jsRoller.prototype._runRolling = function(){
		var ptr = jsRoller.prototype._pthis;
		var roll = ptr.roller;
		var height = parseInt(roll.childNodes[0].offsetHeight);
		var top = parseInt(roll.style.top);
		var scrollTop = document.body.scrollTop;
		var endY =  scrollTop + (document.body.clientHeight)/2 - height/2;
		
		if(!ptr.started)
		{
			if(top < scrollTop - height)
				top = scrollTop - height + 10;
			else if(top > scrollTop + document.body.clientHeight)
				top = scrollTop + document.body.clientHeight + 10;
			ptr.started = true;
			ptr.delta = Math.abs(top - endY);
			ptr.dy = top>endY ? -1 : 1;
		}
		
		ptr.dy = -parseInt(((top - endY)*20)/ptr.delta);
		if(ptr.dy==0)
			ptr.dy = (top>endY ? -1:1);
		var done = false;
		if(top==endY)
			done = true;
		else
		{
			if(ptr.dy<0)
				done = (top+ptr.dy)<endY;
			else if(ptr.y>0)
				done = (top+ptr.dy)>endY;
		}
		
		if(done)
		{
			clearTimeout(ptr.rollId);
			this.started = false;
			return;
		}				
		
		var dis = Math.abs(top - endY);			
		var timeout = parseInt(1000 / dis);//parseInt(ptr.delta/Math.abs(top - endY));			
		roll.style.top = top + ptr.dy; roll.style.border = "0px solid #EEEEEE";
		ptr.rollId = setTimeout("jsRoller.prototype._runRolling()", timeout>=100 ? 100 : timeout);				
	}//end rolling
	
	this.hide = function(){
		this.roller.style.display = "none";
	}
	
	////////////////////////////////////////////////////////
	//initialize
	this._initialize = function(){
		this.roller.innerHTML = "<table align='center' cellspacing='0' cellpadding='0'><tr><td id='" + this.containerId + "' align='center'></td></tr></table>"; //make sure that content is empty :-)
		this.container = document.getElementById(this.containerId);
		this.setTop(1);
		if(window.screen.availWidth>800){
			this.roller.style.display = "block";
			this.startRoller();			
		}
	}	
	//call init
	this.roller.style.left = left;
	this._initialize();
}//end jsRoller////////////////////////////////////////////////////////


