var xgAllCalendars = new Array();

function xgDate(){
		this.Label																			="mydate Label";
		this.LabelWidth																	=-1;
		this.ParentObject																=document.body;
		this.paramName																="";
		this.Draw																			=xgd_draw;
		this.setVisible																	=xgd_setvisible;
		this.setDate																		=xgd_setdate;
		this.tabIndex																		=0;
}

function xgd_setdate(strDate){
	var tdate = strDate;
	
	var _year = tdate.substring(0,tdate.indexOf("-"));
	tdate = tdate.substring(tdate.indexOf("-")+1);
	if ( (parseInt(_year)==0) || (parseInt(_year)<0) ) {
		return false;
	}
	var _month = tdate.substring(0,tdate.indexOf("-"));
	if ((parseInt(_month)==0) || (parseInt(_month)>12)) {
		return false;
	}
	tdate = tdate.substring(tdate.indexOf("-")+1);
	if (tdate.indexOf(" ")>-1){
		tdate = tdate.substring(0,tdate.indexOf(" "));
	}
	var _day = tdate;
	if ( (parseInt(_day)==0) || (parseInt(_day)>31 ) ) {
		return false;
	}
	strDate = strDate.substring(strDate.indexOf(" ")+1);
	
	this._year.value = _year;
	this._month.value = _month;
	this._day.value = _day;
	this._year.onchange();
	this._year.onkeyup();
	this._time.value = strDate;
	this._calendar.setDate(this._year.value,this._month.value,this._day.value,this._time.value);
}

function xgd_draw(dLeft,dTop,dWidth,dHeight){

		var _label = document.createElement("DIV");
		this.ParentObject.appendChild(_label);
		_label.className = "xg_date_label";
		this.Label = replaceStr(this.Label," ","&nbsp;");
		_label.innerHTML = this.Label;
		_label.style.width = 1; 
		_label.style.position = "absolute"; _label.style.left = 1; _label.style.top = parseInt(dHeight)/2 - parseInt(_label.offsetHeight)/2;
		if (this.LabelWidth == -1){
			this.LabelWidth = _label.offsetWidth;
		}else{
			_label.style.width = this.LabelWidth;
		}

		
		var _dsurface = document.createElement("DIV");
		this.ParentObject.appendChild(_dsurface);
		_dsurface.className = "xg_date_surface";
		_dsurface.style.position = "absolute"; _dsurface.style.left = this.LabelWidth; _dsurface.style.top = 1;
		_dsurface.style.width = dWidth - this.LabelWidth;
		_dsurface.style.height = dHeight ;

		var _top = 1;
		var _day = document.createElement("INPUT");	
		_day.style.position = "absolute"; _day.style.left = 1;_day.style.top = _top;
		_day.className = "xg_date_day"; _day.style.width = 16; _day.maxLength = 2;
		_dsurface.appendChild(_day);
		_day.tabIndex = this.tabIndex;

		var _s1 = document.createElement("DIV");
		_s1.style.position = "absolute"; _s1.style.left = 16; _s1.style.top = _top; _s1.style.width = 5;
		_s1.className = "xg_date_day";_s1.innerHTML = "/";
		_dsurface.appendChild(_s1);

		var _month = document.createElement("INPUT");	
		_month.style.position = "absolute"; _month.style.left = 20;_month.style.top = _top;
		_month.className = "xg_date_day"; _month.style.width = 16; _month.maxLength = 2;
		_dsurface.appendChild(_month);
		_month.tabIndex = this.tabIndex+1;

		var _s2 = document.createElement("DIV");
		_s2.style.position = "absolute"; _s2.style.left = 35; _s2.style.top = _top; _s2.style.width = 5;
		_s2.className = "xg_date_day";_s2.innerHTML = "/";
		_dsurface.appendChild(_s2);

		var _year = document.createElement("INPUT");	
		_year.style.position = "absolute"; _year.style.left = 39;_year.style.top = _top;
		_year.className = "xg_date_day"; _year.style.width = 30; _year.maxLength = 4;
		_dsurface.appendChild(_year);
		_year.tabIndex = this.tabIndex+2;

		var _time = document.createElement("INPUT");	
		_time.style.position = "absolute"; _time.style.left = 65;_time.style.top = _top;
		_time.className = "xg_date_day"; _time.style.width = 50; _time.maxLength = 8;
		_dsurface.appendChild(_time);
		_time.tabIndex = this.tabIndex+3;

		var _parameter = document.createElement("INPUT");
		_parameter.type="hidden";
		_parameter.style.position = "absolute"; _parameter.style.left = 0; _parameter.style.top = 0;
		_parameter.name = this.paramName;
		_parameter.id = this.paramName;
		this._parameter = _parameter;
		this._day = _day; this._month = _month;this._year = _year;this._time = _time;
		_dsurface.appendChild(_parameter);

		var _calendar = new xgCalendar();	
		_calendar.SurfaceParent = this.ParentObject;
		_calendar.Draw(dLeft+this.LabelWidth,xg.util.getTop(this.ParentObject)+dHeight+2,200,180);
		_calendar.hide();

		this._calendar = _calendar;

		_time.onchange = function(){
				try{
						var _tm = _time.value;
						var _h = _tm.substring(0,2);
						alert(_h);
				}catch(e){
					this.value = "00:00:00";
				}
		}

		_day.onkeydown = function(){		
			if (event.shiftKey == true){
				return false;
			}
			if ((event.keyCode == 8) || (event.keyCode == 37) || (event.keyCode == 39) || (event.keyCode == 46) || (event.keyCode == 9)){

			}else
			if ( ((event.keyCode < 48 ) || (event.keyCode > 57)) && (  (event.keyCode < 96 ) || (event.keyCode > 105))) {
				return false;
			}
		}


		_month.onkeydown = function(){		
			if (event.shiftKey == true){
				return false;
			}
			if ((event.keyCode == 8) || (event.keyCode == 37) || (event.keyCode == 39) || (event.keyCode == 46) || (event.keyCode == 9)){

			}else
			if ( ((event.keyCode < 48 ) || (event.keyCode > 57)) && (  (event.keyCode < 96 ) || (event.keyCode > 105))) {
				return false;
			}
		}

		_year.onkeydown = function(){		

		  if (event.keyCode == 13) { _year.onchange() }
			if (event.shiftKey == true){
				return false;
			}
			if ((event.keyCode == 8) || (event.keyCode == 37) || (event.keyCode == 39) || (event.keyCode == 46) || (event.keyCode == 9)){

			}else
			if ( ((event.keyCode < 48 ) || (event.keyCode > 57)) && (  (event.keyCode < 96 ) || (event.keyCode > 105))) {
				return false;
			}
		}

		_day.onkeyup = function(){
			if (event.keyCode == 9) return;
				if (_day.value.length == _day.maxLength){
					if (_day.value > 31){
					_day.value = "";
					_day.focus();
				}else{
					_month.focus();
					_month.select();
					_parameter.value = _year.value+"-"+_month.value+"-"+_day.value+" 00:00:00";
				}
				}
		}

		_month.onkeyup = function(){
			if (event.keyCode == 9) return;
				if (_month.value.length == _month.maxLength){
					if (_month.value > 12){
						_month.value = "";
						_month.focus();
					}else{
						//verific numarul maxim de zile
						var _max = _calendar.getMaxDays(_month.value,_year.value);
						if (_day.value > _max){
							_day.value = _max;
							_day.focus();
							_day.select();
							return;
						}
						_year.focus();
						_year.select();
						_parameter.value = _year.value+"-"+_month.value+"-"+_day.value+" 00:00:00";
					}
				}
		}

	  _year.onchange = function(){
					
					var _max = _calendar.getMaxDays(_month.value,_year.value);
						if (_day.value > _max){
							_day.value = _max;
							_day.focus();
							_day.select();						
						}
						_calendar.setDate(_year.value,_month.value,_day.value);
	  }

		_year.onkeyup = function(){
			_parameter.value = _year.value+"-"+_month.value+"-"+_day.value+" 00:00:00";
		}

		var d_button = new xgButton();
		d_button.ParentObject = this.ParentObject;
		 d_button.Label=escape("<img src="+xg.coreLocation+"imgs/combobox/down.png>");
		d_button.Draw(dLeft + dWidth -18,dTop+2,17,dHeight-2);

		var _obj = this;

		d_button.onclick = function(){

			if (_calendar.isVisible()){
				d_button.setOn(false);
				_calendar.hide();
			}else{
				d_button.setOn(true);
				var _left = parseInt(xg.util.getLeft(_obj.ParentObject))+parseInt(_obj.LabelWidth);
				var _top = parseInt(xg.util.getTop(_obj.ParentObject))+20;
				var _dw = _left + 220;
				if (document.body.clientWidth < _dw){
					_left = document.body.clientWidth-250;
				}
				_calendar.show(_left,_top);
			}
		}

		_calendar.onblur = function(){
				_calendar.hide();
				d_button.setOn(false);
		}

		_calendar.onchange = function(_dd,_dm,_dy){
				var mydate = _calendar.getDate();
				if (!_dd) _dd = mydate.day;
				if (!_dm) _dm = mydate.month;
				if (!_dy) _dy = mydate.year;
				_day.value = _dd;
				_month.value = _dm;
				_year.value = _dy;
				_parameter.value = _year.value+"-"+_month.value+"-"+_day.value+" 00:00:00";
				d_button.onclick();
				_day.focus();
		}
}

function xgd_setvisible(vis){
		if (vis == false){
			this.ParentObject.style.visibility = "hidden";
		}else{
			this.ParentObject.style.visibility = "visible";
		}
}


function xgCalendarDate(){
		this.day					=1;
		this.month				=1;
		this.year					=1;
}


function xgCalendar(day,month,year){
		var _d = new Date();
		
		month = parseInt(month);
		day = parseInt(day);
		if (!day){ day = _d.getDate()};
		if (!month){ month = _d.getMonth()+1};
		if (!year){ year = _d.getYear()};

			this.day																				=day;
			this.month																			=month;
			this.year																				=year;

			this.dayName																	= new Array ("L","M","Mi","J","V","S","D")
			this.dayName_long															= new Array("Duminica","Luni","Marti","Miercuri","Joi","Vineri","Sambata");
			this.monthName																=	new Array("Ianuarie","Februarie","Martie","Aprilie","Mai","Iunie","Iulie","August","Septembrie","Octombrie","Noiembrie","Decembrie");
			this.aNumDays																	= Array (31,0,31,30,31,30,31,31,30,31,30,31)
			this.getMonthName															=xgc_getmonthname;
			this.getMaxDays																=xgc_getmaxdays;
			this.ObjectIndex																=xgAllCalendars.length;
			this.getDate																		=xgc_getdate;
			this.setDate																		=xgc_setdate;
			this.hide																				=xgc_hide;
			this._visible																		=false;
			this.show																			=xgc_show;
			this.isVisible																		=xgc_isvisible;
			this._drawed																		=false;
			this.SurfaceParent															=null;

			xgAllCalendars[xgAllCalendars.length]						=this;
			this.Draw																			=xgc_draw;		
}


function xgc_getmaxdays(monthno,_year){
	var _maxd = parseInt(this.aNumDays[monthno-1]);
	if (_maxd == 0){
		_maxd = 28;
	}
	if (((_year/4 - Math.floor(_year/4)) == 0) && (monthno-1 == 1)) {
			_maxd = 29;
		}
	return _maxd;
}

function xgc_getmonthname(mno){
	return this.monthName[mno-1];
}


function xgc_draw(dLeft,dTop,dWidth,dHeight){	
			var _sframe = document.createElement("IFRAME");_sframe.src = "about:blank";
			_sframe.frameBorder =0;_sframe.scrolling="no";
			if (xg.browser.sName == "ie"){
				document.body.insertBefore(_sframe);
			}else{
				document.body.appendChild(_sframe);
			}
			_sframe.style.position="absolute";_sframe.style.left = dLeft;_sframe.style.top = dTop; _sframe.style.width = dWidth;_sframe.style.height = dHeight;
			_sframe.allowTransparency  = true;
			_sframe.id = "xg_calendar_surface"+this.id;
			_sframe.style.zIndex = 100001;
			var _width = parseInt(_sframe.style.width)-2;
			var _height = parseInt(_sframe.style.height)-2;
			var _iw = _width-16;
			var _ih = _height-16;
			this._iw = _iw;
			this._ih = _ih;
			 var th ="";
				th+="<table id='_shadow_table' border='0' cellspacing='0' cellpadding='0' style='position:absolute;width:100%;height:"+_height+"px'>";
				th+="<tr height='8' >";
				th+="<td align='right' valign='bottom'>";
				th+="<div style='border:solid red 0px;width:8px;height:8px;overflow:hidden;background-image:url("+xg.coreLocation+"/imgs/shadows/shadow_left_top_corner.png);'></div>";
				th+="</td>";
				th+="<td style='background-image:url("+xg.coreLocation+"/imgs/shadows/shadow_top_border.png);background-repeat:repeat-x;overflow:hidden;'>";
				th+="</td>";
				th+="<td style='width:8px;overflow:hidden;'><div style='border:none;width:8px;height:8px;overflow:hidden;background-image:url("+xg.coreLocation+"/imgs/shadows/shadow_right_top_corner.png);'></div></td>";
				th+="</tr>";
				th+="<tr height='"+_ih+"'>";
				th+="<td style='width:8px;background-image:url("+xg.coreLocation+"/imgs/shadows/shadow_left_border.png);background-repeat:repeat-y'></td>";
				th+="<td align='left' valign='top' style='width:"+_iw+"px;background-image:url("+xg.coreLocation+"/imgs/calendar/background.jpg);'>";
						//cred ca aici vine continutul
						th+="<div id='_mysurface' style='position:absolute;width:100%;height:100%;'>&nbsp;</div>";
				th+="</td>";
				th+="<td style='background-image:url("+xg.coreLocation+"/imgs/shadows/shadow_right_border.png);background-repeat:repeat-y'></td>";
				th+="</tr>";
				th+="<tr height='8'>";
				th+="<td align='right' valign='top' style=''>";
				th+="<div style='width:8px;height:8px;overflow:hidden;background-image:url("+xg.coreLocation+"/imgs/shadows/shadow_left_bottom_corner.png);'></div></td>";
				th+="<td style='background-image:url("+xg.coreLocation+"/imgs/shadows/shadow_bottom_border.png);background-repeat:repeat-x'></td>";
				th+="<td>";
				th+=" <div style='width:8px;height:8px;overflow:hidden;background-image:url("+xg.coreLocation+"/imgs/shadows/shadow_right_bottom_corner.png);'></div></td>";
				th+="</tr></table>";
			 
			_sframe.contentWindow.document.write(th);
			_sframe.contentWindow.document.close();
			_sframe.contentWindow.document.body.style.backgroundColor = "transparent";
			if (xg.browser.sName == "ie"){
					_sframe.contentWindow.document.body.style.margin=0;
//					_sframe.contentWindow.document.createStyleSheet(xg.serverLocation+'/css/xg-date.css');
			}
			else{
				_sframe.contentDocument.body.style.margin = 0;
			}
			this._sframe = _sframe;
			_sframe.style.visibility = "visible";

			var _surface = _sframe.contentWindow.document.getElementById("_mysurface");
			_surface.style.left = 18;
			_surface.style.top = 8;
			_surface.innerHTML = _constructCalendar(this);
			this._surface = _surface;
				this._visible = true;
				this._drawed = true;
				this._sframe.focus();

		var _myobj = this;
		_sframe.onblur = function(){
			_myobj.hide();
		}
}//draw

function _changeCalendar(_m,_y,oi){
	var _pd = xgAllCalendars[oi];
	_pd.month = parseInt(_m)+1;
	_pd.year = parseInt(_y);
	_pd._surface.innerHTML = _constructCalendar(_pd);

}

function _constructCalendar(obj){
		var shtml="";
		shtml+="<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\">";
		shtml+="<tr>";
			shtml+="<td><select style='font-size:10px;width:80px;' onchange='window.parent.window._changeCalendar(this.value,"+obj.year+","+obj.ObjectIndex+")'>";
				for (var i=0;i<obj.monthName.length;i++ ){
						if (i != obj.month-1){
							shtml+="<option value='"+i+"'>"+obj.monthName[i]+"</option>";
						}else{
							shtml+="<option value='"+i+"' selected >"+obj.monthName[i]+"</option>";
						}
				}
				shtml+="</select>";
			shtml+="</td>";
			shtml+="<td><select style='font-size:10px;width:60px;' onchange='window.parent.window._changeCalendar("+(obj.month-1)+",this.value,"+obj.ObjectIndex+")'>";
			for (var i=0;i<100;i++){
					var _yx = 1950+i;
					if (_yx != obj.year){
					shtml+="<option value='"+_yx+"'>"+_yx+"</option>";
					}else{
					shtml+="<option value='"+_yx+"' selected>"+_yx+"</option>";
					}
			}
			shtml+="</select>";
			shtml=="</td>";
		shtml+="</tr>";
		shtml+="</table>";
	 shtml+="<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
	shtml+="<tr>";
	for (var i=0;i<obj.dayName.length;i++){
		shtml+="<td align=\"center\" valign=\"middle\" style=\"font-family:Lucida,Lucida Grande,Trebuchet Ms;font-size:11px;cursor:default;font-weight:bolder;background-color:transparent;padding:2px;color:silver;\">";
			shtml+="<span>";
			shtml+=obj.dayName[i];
			shtml+="</span>";
		shtml+="</td>";
	}
	shtml+="</tr>";
	var y = obj.year; var m = obj.month-1;
	var _d = new Date();
	var _day = _d.getDay();
	var _td = _d.getDate();
	var _totaldays = obj.aNumDays[m];
	if (_totaldays == 0){
		_totaldays =28;
		if ((y/4 - Math.floor(y/4)) == 0){
			_totaldays = 29;
		}
	}

	var day1 = new Date(y,m,1);
	day1 = day1.getDay();

	if (day1==0)day1=7;
	var stop = false;
	var k=0;
	var r=0;
	var day=1;
	
	while (!stop){
		if (r==0){
			shtml+="<tr>";
		}
		shtml+="<td align='center' valign='middle' style='padding-top:2px;font-family:Lucida Grande,tahoma,verdana,arial,helvetica;font-size:11px'>";
		if (k>=day1-1){
			var _yeardate = new Date().getYear();

			if (xg.browser.sName != "ie") _yeardate = 1900+_yeardate;
			if ((day ==_td) && (new Date().getMonth() == m) && (_yeardate == y)){
				shtml+="<div style=\"cursor:pointer;background-color:#E4E4E4;width:24px;height:18px;background-image:url('"+xg.coreLocation+"imgs/calendar/day.jpg')\" _defaultClass=\"curDate\" onclick=\"window.parent.window._pdSelectDay(this,"+obj.ObjectIndex+")\">";
				}else if (day == 1){
					if (r < 5){
						shtml+="<div style=\"cursor:pointer;background-color:#E4E4E4;width:24px;height:18px;background-image:url('"+xg.coreLocation+"imgs/calendar/firstday.jpg')\" _defaultClass=\"firstday\" onclick=\"window.parent.window._pdSelectDay(this,"+obj.ObjectIndex+")\">";
					}else if (r == 6){
						shtml+="<div style=\"cursor:pointer;background-color:#E4E4E4;width:24px;height:18px;background-image:url('"+xg.coreLocation+"imgs/calendar/fwd.jpg')\" _defaultClass=\"fwd\" onclick=\"window.parent.window._pdSelectDay(this,"+obj.ObjectIndex+")\" >";
					}else{
						shtml+="<div style=\"cursor:pointer;background-color:#E4E4E4;width:24px;height:18px;background-image:url('"+xg.coreLocation+"imgs/calendar/firstweekendday.jpg')\" _defaultClass=\"firstweekendday\" onclick=\"window.parent.window._pdSelectDay(this,"+obj.ObjectIndex+")\">";
					}
				}else if (day == _totaldays){
					if (r < 5){
							shtml+="<div class=\"lastday\" _defaultClass=\"lastday\" onclick=\"window.parent.window._pdSelectDay(this,"+obj.ObjectIndex+")\" >";
					}else{
							shtml+="<div class=\"lastweekendday\" _defaultClass=\"lastweekendday\" onclick=\"window.parent.window._pdSelectDay(this,"+obj.ObjectIndex+")\" >";
					}
				}else{
					if (r<5){
						if (r==0){
						shtml+="<div style=\"cursor:pointer;background-color:#E4E4E4;width:24px;height:18px;background-image:url('"+xg.coreLocation+"imgs/calendar/firstday.jpg')\" _defaultClass=\"firstday\" onclick=\"window.parent.window._pdSelectDay(this,"+obj.ObjectIndex+")\" >";
						}else{
						shtml+="<div style=\"cursor:pointer;background-color:#E4E4E4;width:24px;height:18px;background-image:url('"+xg.coreLocation+"imgs/calendar/day.jpg')\" _defaultClass=\"day\" onclick=\"window.parent.window._pdSelectDay(this,"+obj.ObjectIndex+")\" >";
						}
					}else{
						if (r != 6){
							shtml+="<div style=\"cursor:pointer;background-color:gray;width:24px;height:18px;background-image:url('"+xg.coreLocation+"imgs/calendar/weekend.jpg')\"  _defaultClass=\"weekend\" onclick=\"window.parent.window._pdSelectDay(this,"+obj.ObjectIndex+")\" >";
						}else{
								shtml+="<div style=\"cursor:pointer;background-color:#E4E4E4;width:24px;height:18px;background-image:url('"+xg.coreLocation+"imgs/calendar/lastweekendday.jpg')\"  _defaultClass=\"_lastweekendday\" onclick=\"window.parent.window._pdSelectDay(this,"+obj.ObjectIndex+")\" >";
						}
					}
				}
				if ((day ==_td) && (new Date().getMonth() == m) && (_yeardate == y)){
					shtml+="<B>"+day+"</b>";
				}else
				if ((day ==obj.day) && (new Date().getMonth() == m) && (_yeardate == y)){
					shtml+="<i>"+day+"</i>";
				}else{
					shtml+=day;
				}
				shtml+="</div>";
		day++;
		}else{
			shtml+="&nbsp;";
		}
		shtml+="</td>";
		k++;
		r++;
		if (r==7){
			shtml+="</tr>";
			r=0;
		}
		if (day==_totaldays+1) stop= true;
	}
	shtml+="</table>";
	return shtml;
}	


function xgc_getdate(){
	var curc = new xgCalendarDate();
	curc.day = this.day;
	curc.month = this.month;
	if (xg.browser.sName != "ie") this.year = 1900+this.year;
	curc.year = this.year;
	if (parseInt(curc.day)<10){
		curc.day = "0"+curc.day;
	}

	if (parseInt(curc.month)<10){
		curc.month = "0"+curc.month;
	}
		return curc;
}//getdate

function xgc_setdate(_year,_month,_day){
			this.year = parseInt(_year);
			this.day = parseInt(_day);
			this.month = parseInt(_month);
			try{
			this._dayC.innerHTML = parseInt(_day);
			this._monthC.innerHTML = this.getMonthName(_month);
			this._yearC.innerHTML = _year;
			this._c3.innerHTML = _constructCalendar(this);
			}catch(e){
			}
	}

function xgc_hide(){
		this._sframe.style.visibility = "hidden";
		this._visible = false;
}

function xgc_show(dLeft,dTop){
		this._sframe.style.visibility = "visible";
		//this._sframe.style.left = xg.util.getLeft(this.SurfaceParent);
		//this._sframe.style.top = xg.util.getTop(this.SurfaceParent);
		if ((dLeft) && (dTop)){
			this._sframe.style.left = dLeft;
			this._sframe.style.top = dTop;
		}
		this._sframe.focus();
		this._visible = true;
}

function xgc_isvisible(){
	if (this._sframe.style.visibility == "visible"){
		return true;
	}else{
	return false;
	}
}

function _pdSelectDay(obj,oi){
	var _pd = xgAllCalendars[oi];
	try{
		var _dd = removeAllTags(obj.innerHTML);
		var _dm = _pd.month;
		var _dy = _pd.year;
		if (parseInt(_dd)<10){ _dd = "0"+_dd;}
		if (parseInt(_dm)<10){ _dm = "0"+_dm;}
		_pd.day = parseInt(removeAllTags(obj.innerHTML));
		_pd.onchange(_dd,_dm,_dy);
	}catch(e){}
		_pd._sframe.style.visibility = "hidden";
}

