/*
 * @purpose			:create wait calendar Div on center screen for select date and time  
 * @auther			:xp wang(xpw365@gmail.com)
 * @date			:2007-06-19
 * @dateModified		:2007-07-30
 * @licence			:use what ever you want, no guaranty!
 * @work:
 *
 * @xpCalendar(datetime, fmt)
 * @datetime	: datetime,if empty, using system time  
 * @fmt	: Date format: Australian =0 or Asian=1 or American=2;
 *
 *   
 *
 * @xpCalendar(date, fmt1, fmt2)
 *  @obj	: object itself
 *  @target	: target field, datetime,if empty, using system time  
 * @fmt1	: Date format: Australian =0 or Asian=1 or American=2;
 * @fmt2	: 1=have time;
 *  @beign	: calendar allow begin date
 *  @end	: calendar allow end date 
 *  @center	:show in screen center
 *	$noDisplay	: do not display @ first;
 *	need xpWait.js pack
 */
var xpCalendarObj;

function _xpCalendar(target,fmt1,fmt2,start,end,center,noDisplay,offDates){
	var d = new Date();
	var name = "xpCalendar"+d.getTime();
	var obj = 'xpGLOBALS["'+name+'"]';
	xpGLOBALS[name] = new xpCalendar(obj,target,fmt1?fmt1:0,fmt2?fmt2:0,start?start:0,end?end:0,center?center:0,noDisplay?noDisplay:0,offDates?offDates:0)
	return obj;
}
function  enMonth(mm){
	 	var en_month=['all','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
	 	return en_month[parseInt(mm)];
	 }
function xpCalendar(obj,target,fmt1,fmt2,start,end,center,noDisplay,offDates,markDates){
	//test 7
	var MONTH_DAYS = [31,28,31,30,31,30,31,31,30,31,30,31];
	var MONTHS = ["01", "02", "03", "04", "05", "06","07", "08", "09", "10", "11", "12"];
	var WEEKDAYS = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];	
	
	//background color:[ normal,today,weekend,  marked ]
	var COLORS = ['#fff','#dde','#e0eaef','#feb','#dfd'];
	var HIGH_COLOR= 'orange';
	var ON_COLOR= '#333';
	var OFF_COLOR= '#999';
	var TITLE_COLOR= '#779';
	var SELECT_COLOR= '#ffd';
	var BACK_COLOR= '#99f';
	var BORDER_COLOR= '#EFEFEF';
	/* calendar date size */
	var sizeX="28px;"
	var sizeY="20px;"
	this.array=[];

	//var calH=fmt2?315:277;
	//var	calW=254;
	var bHeight=20;
	var bWidth=18;
	
	this.obj=obj;
	this.displayOn=false;		//first time;
	this.center = (!center)?0:center;
	this.fmt1 = !fmt1?0:fmt1;
	this.fmt2 = !fmt2?0:fmt2;
	this.noDisplay = !noDisplay?0:1;
	
	this.tmp='';			//
	this.selectDate=0;		//no select yet;
	this.checkTime =function (i){ return (i<10)? ("0" + i):i;};
	this.dateFormat=function (yy,mm,dd)	{
		mm = MONTHS[mm];
		dd = this.checkTime(dd);
		var fd = [[dd,mm,yy],[yy,mm,dd],[mm,dd,yy]];
		var str  = fd[this.fmt1][0] + "-" +fd[this.fmt1][1] + "-"+fd[this.fmt1][2] ; 
		return str;
	}
	this.dateFormatmmyy=function (yy,mm,dd)	{
		mm = MONTHS[mm];
		dd = -1;
		var fd = [[dd,mm,yy],[yy,mm,dd],[mm,dd,yy]];
		var f0 = fd[this.fmt1][0]
		var f1 = fd[this.fmt1][1]
		var f2 = fd[this.fmt1][2]
		var ddd=new Array();
		if(f0!=-1) ddd.push(f0)
		if(f1!=-1) ddd.push(f1)
		if(f2!=-1) ddd.push(f2)
		var str  ='&nbsp;&nbsp;'+ ddd.join('-')+'&nbsp;&nbsp;'; 
		return str;
	}
	this.formatDate=function (d){
		d[0] *=1;
		d[1] *=1;
		d[2] *=1;
		var fd = [[d[2],d[1],d[0]],[d[0],d[1],d[2]],[d[2],d[0],d[1]]];
		var df  = fd[this.fmt1] ; 
		return df;
	}
	if(!fmt2) fmt2 = 0;
	this.today = function(t)	{ //t=0 return date; t=1; return time; t=2 return both together
		var date = new Date();
		var yy = date.getFullYear();
		var mm = date.getMonth();
		var dd = date.getDate();
		var hs = date.getHours();
		var ms = date.getMinutes();
		var ss = date.getSeconds();
		var str ='';
		if(t==0)
			str = this.dateFormat(yy,mm,dd);
		if(t>0)	
			str +=' '+hs +':'+ms+':'+ss;
		return str;	
	}

	this.cmp=function(x,y){ //compare date

		var a = x.split(' ');
		    a = this.formatDate(a[0].split('-'));
		var b =y.split(' ');
	//	alert(x +'='+ y+ 'b='+b[0])
		      b = this.formatDate(b[0].split('-'));
		a=a[0]+'-'+this.checkTime(a[1])+'-'+this.checkTime(a[2]);
		b=b[0]+'-'+this.checkTime(b[1])+'-'+this.checkTime(b[2]);
		if(a>b) return 1;
		if(a==b) return 0;
		return -1
	}
	
	this.target=target;
	if(this.target!=''){
		this.date=target.value;
	}

		
	if(this.date)
	{
		var ddd = this.date.split(' ');
		var dd = ddd[0].split('-');
		if(dd[0] && dd[1] && dd[2]){
			dd=this.formatDate(dd);
			this.date =new Date(dd[0]+"/"+dd[1]+"/"+dd[2]+" "+(ddd[1]?ddd[1]:''));	
		}
		else
			this.date = new Date();
	}else{
		this.date = new Date();
	}	
	
	
	this.yy = this.date.getFullYear();
	this.mm = this.date.getMonth();
	this.dd = this.date.getDate();
	this.hs = this.date.getHours();
	this.ms = this.date.getMinutes();
	this.ss = this.date.getSeconds();

	this.start = !start?this.dateFormat(2008,1,1):( start==1?this.today(0):start);

	this.end   = !end?this.dateFormat(2035,5,5): end;

	//this.display();
	/****************end **********/
	/* on calendar change month ,year call back to get offTime and offDate */
	this.onChangeCallBack=function(callBack){
		this.callBack = callBack;
	}
	/* on OK call back; without update field(element.value)  */
	this.onOkCallBack=function(callBack){
		this.okCallBack = callBack;
	}
/*	this.onMonthCallBack=function(callBack){
		this.monthCallBack = callback;
	}
	this.onYearCallBack=function(callBack){
		this.yearCallBack = callback;
	}
*/
	this.getDays=function(yyyy,mm){
		yyyy=yyyy?yyyy:this.yy
		mm=parseInt(mm?mm:this.mm)
	    if ((yyyy % 4) == 0 && ((yyyy % 100) !=0 || (yyyy % 400)==0))  MONTH_DAYS[1] = 29;
	    else  MONTH_DAYS[1] = 28;	//clear last time
	    return MONTH_DAYS[mm];
	}
	
	this.lastDay = function(dateStr){
		var d = dateStr.split('-');			
		if( 1 < d[0]  ) return ((d[0]-1)<10 ?'0'+(d[0]-1) :(d[0]-1))+'-'+d[1]+'-'+d[2]
		if(1==d[1]) return '31-12-'+(d[2]-1)
		d[1]--;
		d[0] = this.getDays(d[2],d[1]-1);
		return (d[0]<10? ('0'+d[0]) : d[0] )+'-'+ (d[1]<10?('0'+d[1]):d[1] )+'-'+d[2];
	}	
	
	this.nextDay = function(dateStr){
		var d = dateStr.split('-');			
		var msd = this.getDays(d[2],d[1]-1);
		d[0]++
		if(d[0] > msd) {
			d[0]=1;
			d[1] ++;
			if(d[1] ==12){
				d[1]=0
				d[2] ++;
			}
		}
		return d[0]+'-'+d[1]+'-'+d[2];
	}
	
	this.days = function(sfrom,sto){
		var sign = 1;
		if(this.cmp(sfrom,sto)==1) {
			sign=sto;
			sto=sfrom;
			sfrom=sign;
			sign=-1;
		}
		var n=0;
		while( this.cmp(sto,sfrom)==1){
			n++;
			sfrom = this.nextDay(sfrom)
		}
		
		return n*sign;
	}
	
	
	this.setDateArray=function(){
		this.days = this.getDays();
	 	var dayone = new Date(this.yy+"/"+(this.mm+1)+"/"+1);
		this.day = dayone.getDay();
		var today=new Date();
		var tyy = today.getFullYear();
		var tmm = today.getMonth();
		var tdd = today.getDate();	
		//alert(this.start);	
		//alert(this.dateFormat(tyy,tmm,tdd));
		//alert(this.cmp(this.start, this.dateFormat(tyy,tmm,tdd)));
		if(!this.offDates){ this.offDates=[];}
		//var row = Math.ceil((this.day+this.days)/7);
		var sc=0;
		var ic=0
		var on,date,type;
		for(i=0;i<6;i++)	{
			this.array[i]=[];
			for(j=0;j<7;j++)	{
				type=0;
				if(sc< this.day || ic>=this.days)  date = 0;
				else	date=1+ (ic++);					
					
				if(date==0) off=true
				if(this.cmp( this.dateFormat(this.yy,this.mm,date),this.start) < 0 ) off=true
				if(this.cmp( this.dateFormat(this.yy,this.mm,date),this.end  ) > 0 ) off=true
				if(this.offDates[date] ){ off=true;}
				
				if(j==0 || j==6) type=2;
				if(!off && this.markDates[date]) type=3;
						
				if(tyy==this.yy && tmm == this.mm && tdd == ic)	type=1;
										
				
				this.array[i][j]={date:date,type:type,off:off,select:false}	
				var off=false;
				sc++;	
			}	
		}
	}	
	
	this.display=function(){
		this.setDateArray();
		var theDiv ='<div style="margin:0; padding:0;width:auto;background:#fff;">'
					+'<table  cellspacing="0" cellpadding="0" style="width:auto;margin:0; padding:2px; border:5px #abd683 solid; font-size:10px;" ><tr>'
					+'<td><!--<input type="button" value="&#8592;" onclick=\''+obj+'.year(-1);\' style="cursor:pointer;border:0 transparent solid; background:transparent;margin:1px;padding:1px;height:'+bHeight+'px; width:'+bWidth+'px;" />'
					+'--></td><td><input type="button" value="&#8592;" onclick=\''+obj+'.month(-1);\' style="cursor:pointer;border:0 transparent solid; background:transparent;margin:1px;padding:1px;height:'+bHeight+'px; width:'+bWidth+'px;"  />'
					+'</td><td colspan="2"><div style="height:20px;width:'+2*sizeX+'px;padding:6px 0 0 0;margin:0px;color:#233D88;font-weight:900;font-size:11px;font-family:arial;">'+this.dateFormatmmyy(this.yy,this.mm,this.dd)+'</div>'
					+'</td><td><input type="button" value="&#8594;"  onclick=\''+obj+'.month(1);\' style="cursor:pointer;border:0 transparent solid; background:transparent;margin:1px;padding:1px;height:'+bHeight+'px; width:'+bWidth+'px;" />'
					+'</td><td><!--<input type="button" value="&#8594;" onclick=\''+obj+'.year(1);\' style="cursor:pointer;border:0 transparent solid; background:transparent;margin:1px;padding:1px;height:'+bHeight+'px; width:'+bWidth+'px;" />'
					+('--></td><td><input type="button" value="X" onclick=\''+obj+'.cancel();\' style="border:0px solid #bbb; cursor:pointer; background:transparent;margin:1px;padding:1px;height:'+bHeight+'px; width:'+bWidth+'px;" />')
					+'</td></tr>'
					//+'</table>'
					+'</div>'
				//	+'<table id="xpCalendarTable" border="0" cellspacing="0" cellpadding="0" style="margin:0;width:auto;">'
					+'<tr style="background:url(../images/bg_calendertop.gif) 0 100% repeat-x; border: 1px solid #dee0dd; border-bottom:2px #ddd solid">'
		for(i=0;i<7;i++)
			theDiv =theDiv 
					+ '<th >'+WEEKDAYS[i]+'</th>';			
		theDiv +='</tr>'

		//var row = Math.ceil((this.day+this.days)/7);
		var endline=0;
		for(i=0;i<6;i++)
		{
			theDiv +='<tr style="background:#fff;">'
			for(j=0;j<7;j++)
			{
				endline=endline?endline:0;
				theDiv +='<td style="margin:0;padding:1px;" ><div id="xpCalendarDate_'+i+'_'+j+'" '
					//	+(this.array[i][j].off?('onclick=\''+(this.array[i][j].off?'':obj+'.select('+i+','+j+')')+'; '+(fmt2?'':obj+'.ok();')+'\' '):'')
						+(this.array[i][j].off?'':(' onclick=\''+obj+'.select('+i+','+j+')' + '; '+(fmt2?'':obj+'.ok();')+'\' '))
						+' onmouseover=\''+(this.array[i][j].off?'':obj+'.mOver('+i+','+j+')')+'\' '
						+' onmouseout=\''+(this.array[i][j].off?'':obj+'.mOut('+i+','+j+')')+'\' ' 
						+' style="color:'+(this.array[i][j].off?OFF_COLOR:ON_COLOR)
						+';background:'+COLORS[this.array[i][j].type]
						+';width:'+sizeX+'px;height:'+sizeY+'px;border:1px #EBEEF3 solid;' 
						+' margin:0;padding:0;'
						+(this.array[i][j].off?'':'cursor:pointer;')
						+'"'
						+' valign="top">'
						+((this.array[i][j].date)?this.array[i][j].date:'&nbsp;')
						+'</div></td>';
				if(this.array[i][j].date == this.days){endline=100};		
						
			}	
			
			theDiv +='</tr>';
			i+=endline;
		}
		if(fmt2)
		{
			if(!this.offTimes) this.offTimes=[];
			theDiv +='<tr><td colspan="7" style="text-align:center;">'
			var select='&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<select name="xpTime" id="xpTime" >';
			for(i=0;i<24;i++)		
				for(j=0;j<12;j++)
				{
					if(!this.offTimes[i*12+j])
					{
						ic = this.checkTime(i)+":"+this.checkTime(j*5)+":00";
						select+='<option value="'+ic+'">'+ic+'</option>';
					}
				}
			select+='</select>';
			theDiv +=select;
		//		theDiv +='</td></tr>'
		//		theDiv +='<tr><td colspan="7" style="text-align:center;">'	
		
			theDiv +='&nbsp;&nbsp;<input type="button" class="button" value="ok" onclick=\''+obj+'.ok();\'  style="cursor:pointer;border:0 transparent solid; background:transparent;margin:1px;padding:2px;height:'+bHeight+'px; width:'+bWidth+'px;" />';
		//	theDiv +='&nbsp;&nbsp;<input type="button" class="button" value="&#x24dd;" onclick="'+obj+'.cancel();" style="cursor:pointer;border:0 transparent solid; background:transparent;margin:1px;padding:2px;height:'+bHeight+'px; width:'+bWidth+'px;"/>&nbsp;';
			theDiv +='</td></tr>'
		}

		theDiv +='</table>';
		var x=y=-1;

		this.speed=this.displayOn?1:350;

		if(!this.displayOn){
				
			if(!center)
			{
				var objXY = w$.findPositionXY(target);
				var objWH = w$.findSizeWH(target);
				x= 10 + objXY.x -200;
				
				y= 10 + objXY.y+objWH.h ; //border of box default
				this.wait = new xpWait(this.obj+'.wait',0.01,'#444');
			}
			else
			{
				this.wait = new xpWait(this.obj+'.wait');
			}
			//this.box = new xpBox(this.obj+'.box',x,y,false,false,5,"#fff",this.speed);
			this.box = popBox(x,y,false,false,5,"#fff",this.speed);
		}		
		
		this.box.setContent(theDiv);
		
		//this.box.innerID.style.zIndex = 2001
		//this.box.outerID.style.zIndex = 2000
		this.displayOn=true;
	}
	
	this.hide=function(){
		eval(this.obj+".box.hide()");
	}
	
	this.offDates=[];
	this.markDates=[];
	/*date not available */
	this.setOffDates=function(arr){
		this.offDates=arr;
	}

	if(offDates){ 
		var tmp = offDates.split(',');
		var offs=[]
		for(var i=0;i<tmp.length;i++){
			offs[tmp[i]]=200;
		}
		this.setOffDates(offs);
	}
	/* date has been marked */
	this.setMarkDates=function(arr){
		this.markDates=arr;
	}
	if(markDates){ 
		var tmp = markDates.split(',');
		var marks=[]
		for(var i=0;i<tmp.length;i++){
			marks[tmp[i]]=1;
		}
		this.setMarkDates(marks);
	}
	
	
	/* time block not a available */
	this.setOffTimes=function(arr){
		this.offTimes=arr;
		this.setDateArray();
	}
	this.mOver=function(i,j){
		if(!this.array[i][j].select && !this.array[i][j].off)
		{
			this.tmp = document.getElementById("xpCalendarDate_"+i+'_'+j);
			this.tmp.style.background= HIGH_COLOR ;
		}
	}
	this.mOut=function(i,j){
		if(!this.array[i][j].select && !this.array[i][j].off)
		{
			this.tmp = document.getElementById("xpCalendarDate_"+i+'_'+j);
			this.tmp.style.background = COLORS[this.array[i][j].type];
		}
	}
		
	this.select=function (i,j){
		if(this.selectDate)
		{
			this.tmp = document.getElementById("xpCalendarDate_"+this.selectDate.i+'_'+this.selectDate.j);
			this.tmp.style.background = COLORS[this.array[this.selectDate.i][this.selectDate.j].type];		
			this.array[this.selectDate.i][this.selectDate.j].select = false;
		}
		this.selectDate = {i:i,j:j};
		this.array[i][j].select = true;
		this.tmp = document.getElementById("xpCalendarDate_"+i+'_'+j);
		this.tmp.style.background = SELECT_COLOR;
		//alert(1);
	}
	this.year = function (x){
		this.yy += x;
		this.offDates=[];
		this.offTimes=[];
		if(this.callBack){eval(this.callBack)}
		else{	this.display();}	
	}
	this.month = function(x){
		if(this.mm+x<0)		{
			this.yy--;
			this.mm=11;
		}else{	
			if(this.mm+x >11){
				this.yy++;
				this.mm=0;
			}else{
				this.mm +=x;
			}
		}
		this.year(0);
		//cahnge call in this.year !!
	}

	this.ok=function(){
		if(this.okCallBack) //using call instead directly update field!
			setTimeout(this.okCallBack,1);
		else
			if(this.target && this.selectDate)	
				this.target.value=this.dateFormat(this.yy,this.mm,this.array[this.selectDate.i][this.selectDate.j].date)+(fmt2?(" "+document.getElementById('xpTime').value):'');
			if(this.target.onchange) this.target.onchange();	
		this.stop();
	}
	this.result=function(){
		return	 this.dateFormat(this.yy,this.mm,this.array[this.selectDate.i][this.selectDate.j].date)+(fmt2?(" "+document.getElementById('xpTime').value):'');
	}
	this.cancel=function(){
		//alert(1);
		this.stop();
	}
	this.stop = function (){
		this.box.stop();
		this.wait.stop();
		setTimeout("delete "+this.object,1)
	}
	//now start
	if(!this.noDisplay)
		this.display();
		
	return this;	
}

//var calendar 
/*
function xpttt()
{
	calendar = new xpCalendar('calendar','20-6-2007 5:22:12',0);

	var cont = document.getElementById('xpCalendar_container');
	cont.style.left="300px"

}
*/
/**
 * calendar
 *
 * date are formated like 1998-10-03 or 31-12-2005.. according to fmt;
 *{target:target,fmt:fmt,currentDates:currentDate,onDates:onDates,offDates:offDates,markedDates:markedDatas,center:center}
 * @param  $target:target element
 * @param  $fmt :date format  0 =asia(iso); 1=au; 2=us;
 * @param  $currentDate current date default = today
 * @param  $onDates : array of selectable dates 
 * @param  $offDates: array of none-selectable dates priority over on date; 
 * @param  $markedDatas : array of special marked dates 
 * @param  $center : if true, display in scewwn center; 
 * 
 */
function XPC(input)
{
//{target:target,fmt:fmt,currentDates:currentDate,onDates:onDates,offDates:offDates,markedDates:markedDatas,center:center})
	
	var name = '_calendar_'+Math.random();
	var div = w$.createDiv(name);
	var format = d.fmt?d.fmt:1;

	/** pre defined value **/
	var MONTH_DAYS = [31,28,31,30,31,30,31,31,30,31,30,31];
	var FORMAT = [[2,1,0], [0,1,2], [1,0,2]];	//==>TO LOCAL FORMAT dd-mm-yyyy
	var MONTHS = ["01", "02", "03", "04", "05", "06","07", "08", "09", "10", "11", "12"];
	var WEEKDAYS = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];	
	//background color:[ normal,today,weekend,  marked ]
	var COLORS = ['#fff','#dde','#e0eaef','#feb','#dfd'];
	var HIGH_COLOR= 'orange';
	var ON_COLOR= '#333';
	var OFF_COLOR= '#999';
	var TITLE_COLOR= '#779';
	var SELECT_COLOR= '#ffd';
	var BACK_COLOR= '#99f';
	var BORDER_COLOR= '#EFEFEF';
	/* calendar date size */
	var sizeX="28px;"
	var sizeY="20px;"	
	
	/** some common function **/
	
//	div.getDays=function(){
//	    if ((this.yy % 4) == 0 && ((this.yy % 100) !=0 || (this.yy % 400)==0))  MONTH_DAYS[1] = 29;
//	    return MONTH_DAYS[this.mm];
//	}	
	
	/** format input date string  to date object {dd,mm,yyyy} **/
	div.formating=function (date){
		var d = date.splite(date);
		var formula = FORMAT[format]
		return {dd:d[formula]};
	}

	div.today = function(date)	{

		date = new Date();
		var yy = date.getFullYear();
		var mm = date.getMonth();
		var dd = date.getDate();
		var str ='';
		if(t==0)
			str = this.dateFormat(yy,mm,dd);
		if(t>0)	
			str +=' '+hs +':'+ms+':'+ss;
		return str;	
	}	
	
	
	return div;
}



/** javascript date object **/ 
var xpDate={
	MONTH_DAYS : [0,31,28,31,30,31,30,31,31,30,31,30,31],

	date2object:function(date){
		var obj={ dd:date.getDate(), day:date.getDay(), yyyy:date.getFullYear(), hh:date.getHours(), ms:date.getMilliseconds(), mt:date.getMinutes(), mm:date.getMonth()+1, ss:date.getSeconds(), mseconds:date.getTime(), tzoon:date.getTimezoneOffset() };
		return obj;
	},
	today:function(){
		var date = new Date();
		return xpDate.date2object(date)
	},	
	differ:function(x,y){
		
	},
	monthDays:function(obj){
		var MonthDay = xpDate.MONTH_DAYS; 
		if ((obj.yyyy % 4) == 0 && ((obj.yyyy % 100) !=0 || (obj.yyyy % 400)==0))  MonthDay[2] = 29;
	    	else  MonthDay[2] = 28;	
	        	return MonthDay[obj.mm];		
	},
	nextYear:function(obj){
		obj.yyyy++;
		if(obj.mm == 2 && obj.dd == 29){
			obj.mm++;
			obj.dd=1;
		}
		return obj;
	},
	lastYear:function(obj){
		obj.yyyy--;
		if(obj.mm == 2 && obj.dd == 29){
			obj.mm++;
			obj.dd=1;
		}
		return obj;
	},
	nextMonth:function(obj){
		if(obj.mm == 12){
			obj = xpDate.nextYear(obj)
			obj.mm=1
		}else{
			obj.mm++
		}
		var msd = xpDate.monthDays(obj);
		if(obj.dd>msd) obj.dd = msd
		return obj
	},
	lastMonth:function(obj){
		if(obj.mm == 1){
			obj = xpDate.lastYear(obj)
			obj.mm=12
		}else{
			obj.mm--
		}
		var msd = xpDate.monthDays(obj);
		if(obj.dd>msd) obj.dd = msd
		return obj
	},
	nextDay:function(obj){
		var msd = xpDate.monthDays(obj);
		if(obj.dd == msd){
			obj = xpDate.nextMonth(obj)
			obj.dd=1
		}else{
			obj.dd++;
		}
		return obj;
	},
	lastDay:function(obj){
		if(obj.dd == 1){
			obj = xpDate.lastMonth(obj)
			obj.dd = xpDate.monthDays(obj);
		}else{
			obj.dd--;
		}
		return obj;
	},
	cmp:function(x,y){
		var a = " " + x.yyyy+w$.padNumber(x.mm,2)+w$.padNumber(x.dd,2)+w$.padNumber(x.hh,2)+w$.padNumber(x.mt,2)+w$.padNumber(x.ss,2)+w$.padNumber(x.ms,2)	
		var b = " " + y.yyyy+w$.padNumber(y.mm,2)+w$.padNumber(y.dd,2)+w$.padNumber(y.hh,2)+w$.padNumber(y.mt,2)+w$.padNumber(y.ss,2)+w$.padNumber(y.ms,2)		
	
		if(a>b) return 1;
		if(a==b) return 0;
		return -1	
	},
	stringCmp:function(x,y){
		return xpDate.cmp(xpDate.string2obj(x),xpDate.string2obj(y));
		
	},
	//time= 0 : only date, 1: with time, 2: time only, bit[2] ==1 (5,6) : with ms  
	obj2string:function(obj,time){
		time=time?time:0
		var dd=obj.dd<10 ? '0'+obj.dd : obj.dd
		var mm=obj.mm<10 ? '0'+obj.mm : obj.mm
		var date = dd+'-'+mm+'-'+obj.yyyy
		var hh=obj.hh<10 ? '0'+obj.hh : obj.hh
		var mt=obj.mt<10 ? '0'+obj.mt : obj.mt
		var ss=obj.ss<10 ? '0'+obj.ss : obj.ss
		var ms = time&4
		var dt = time&3;
		var str=''
		if(!time) str = date;
		if(dt==1)  str = date+' '+time;
		if(dt==2) str = time;
		
		if(ms) str = str+'.'+obj.ms
		return str
	},
	string2obj:function(str){
		var dt = str.split('.');	
		var d = dt[0].replace(/(\d{1,2})\-(\d{1,2})\-(\d\d\d\d)(.*)/g , "$3/$2/$1$4") 
//		var d = dt[0].replace('-',' ')
//alert(d); 
		var date = new Date(d);

		if(dt[1]) date.setMilliseconds(parseInt(dt[1]));
//alert('f-y='+date.getFullYear())
		return xpDate.date2object(date)		
	},
	nextDayString:function(str){
		return xpDate.obj2string(xpDate.nextDay(xpDate.string2obj(str)));
	},
	lastDayString:function(str){
		return xpDate.obj2string(xpDate.lastDay(xpDate.string2obj(str)));
	},
	lastMonthString:function(str){
		return xpDate.obj2string(xpDate.lastMonth(xpDate.string2obj(str)));
	},
	nextMonthString:function(str){
		return xpDate.obj2string(xpDate.nextMonth(xpDate.string2obj(str)));
	},
	nextYearString:function(str){
		return xpDate.obj2string(xpDate.nextYear(xpDate.string2obj(str)));
	},
	lastYearString:function(str){
		return xpDate.obj2string(xpDate.lastYear(xpDate.string2obj(str)));
	}

}
