var mn=['January','February','March','April','May','June','July','August','September','October','November','December'];
var dim=[31,0,31,30,31,30,31,31,30,31,30,31];
var week=['Su','Mo','Tu','We','Th','Fr','Sa'];
var avail_days=[7,1,2,3,4,5,6];

var CalendarObjects = new Array();
function isIE()
{
	return navigator.appName == 'Microsoft Internet Explorer';
}

function BuildCalendar( InputID, DivID, e ){
	var cdc1 = new Calendar( InputID, InputID ); cdc1.build( DivID );
	var div = document.getElementById( DivID );

	div.style.top = e.clientY + document.documentElement.scrollTop + 'px';
	div.style.left= e.clientX - 258 + 'px';
	
	//var left = window.innerWidth/2 - 125;
	//div.style.left = left+'px';
	//div.style.top = '200px';
	div.style.display = 'block';
}

function CloseCalendar( DivID )
{
	if ( isIE() )
		var div = window.top.document.getElementById ( DivID );
	else
		var div = document.getElementById( DivID );
		
	div.style.display = 'none';
}

function select_day( prefix, day ){
	window.top.CalendarObjects[ prefix ].select_day( day, window );
}

function swap_style( prefix, day, obj, is_over ){
	if ( window.top.CalendarObjects[ prefix ].d != day ){
		obj.className = is_over ? 'overed' : '';
	}
}

function change_month( prefix, sel ){
	window.top.CalendarObjects[ prefix ].change_month( sel.options[ sel.selectedIndex ].value, window );
}

function change_year( prefix, sel ){
	window.top.CalendarObjects[ prefix ].change_year( sel.options[ sel.selectedIndex ].value, window );
}

function change_hours( prefix, sel ){
	window.top.CalendarObjects[ prefix ].change_hour( sel.options[ sel.selectedIndex ].value, window );
}

function change_minut( prefix, sel ){
	window.top.CalendarObjects[ prefix ].change_minut( sel.options[ sel.selectedIndex ].value, window );
}

function Calendar( prefix, outfield, show_hours ){
	this._prefix = prefix;
	this._outfield = outfield;
	this._show_hours = show_hours;
	this.d = 0;
	this.m = 0;
	this.y = 0;
	this.h = 0;
	this.s = 0;
	this.divID = 0;
	
	window.top.CalendarObjects[ prefix ] = this;
	
	this.set_show_hours = function (){
		this._show_hours = show;
	}
	
	this.get_day = function (){
		return this.d;//_get_object('_day').value;
	}
	
	this.get_month = function( window ){
		var sel =  this._get_object('_mouths', window );
		return sel.options[ sel.selectedIndex ].value;
	}
	
	this.get_year = function( window ){
		var sel =  this._get_object('_years', window);
		return sel.options[ sel.selectedIndex ].value;
	}
	
	this.get_hour = function( window ){
		var sel =  this._get_object('_hours', window);
		return sel.options[ sel.selectedIndex ].value;
	}
	
	this.get_minut = function( window ){
		var sel =  this._get_object('_minut', window);
		return sel.options[ sel.selectedIndex ].value;
	}
	
	this._normNumber = function ( num ){
		return String( num ).length==1 ? '0' + num : num;
	}
	
	this._get_object = function ( name, window ){
		return window.document.getElementById( this._prefix+name );
	}
	
	this.get_out_obj_value = function(){
		var obj = eval( "document.getElementById('"+this._outfield+"')" );
		return obj.value;
	}
	
	this.set_out_obj_value = function( value, window ){
		var obj = eval( "document.getElementById('"+this._outfield+"')" );
		obj.value = value;
	}
	
	this._build_out_date = function( window ){
		var m = this.get_month( window );
		var d = this.get_day( window );
		var y = this.get_year( window );
		
		var str=y+'-'+this._normNumber( m )+'-'+this._normNumber( d );

		if ( this._show_hours ){
			var h = this.get_hour();
			var s = this.get_minut();
			str +=' '+this._normNumber( h )+':'+this._normNumber( s );
		}
		
		this.set_out_obj_value( str, window.top );
	}
	
	this._rebuild_days = function( window ){
		var m = this.get_month( window );
		var d = this.get_day( window );
		var y = this.get_year( window );
		
		var dstr = this._build_day_table();
		var div = this._get_object( 'id_calendar', window );
		div.innerHTML=dstr;

		this._build_out_date( window );
	}
	
	this._rebuild_months = function( window ){
		var m = this.get_month( window );
		var y = this.get_year( window );
		var mobj=this._get_object( '_mouths', window );
		this._rebuild_days( window );
	}
	
	this._build_year_combo = function(){
		var str = '<select id='+this._prefix+'_years onchange="change_year( \''+this._prefix+'\', this )">'
		for( i=2005; i < 2010; i++ ){
			str+='<option value='+i+' '+( this.y == i ? 'selected' : '' )+' >'+i;
		}
		str += '</select>';
		return str;
	}
	
	this._build_month_combo = function(){
		var str='<select id='+this._prefix+'_mouths onchange="change_month( \''+this._prefix+'\', this )">';
		for( i = 1; i <= 12; i++ ){
			str+='<option value='+(i)+' '+( this.m  == i ? 'selected' : '' )+' >'+mn[ i-1 ];
		}
		str+='</select>';
		return str;
	}
	
	this._build_hours_combo = function(){
		var str='<select id="'+this._prefix+'_hours" onchange="change_hours( \''+this._prefix+'\', this )">';
		for( i = 0; i < 24; i++ ){
			str+='<option value='+(i)+' '+( this.h  == i ? 'selected' : '' )+' >'+this._normNumber( i );
		}
		str+='</select>';
		return str;
	}
	
	this._build_minut_combo = function(){
		var sd = ( this.s/10 - Math.floor( this.s/10 ) ) * 10;
		var s = sd <5 ? Math.floor( this.s/10 ) *10 : Math.floor( this.s/10 ) *10 + 5;
		var str='<select id="'+this._prefix+'_minut" onchange="change_minut( \''+this._prefix+'\', this )">';
		for( i = 0; i < 60; i+= 5 ){
			str+='<option value='+(i)+' '+( s  == i ? 'selected' : '' )+' >'+this._normNumber( i );
		}
		str+='</select>';
		return str;
	}
	
	this._build_day_table = function(){

		var str='<div id="'+this._prefix+'id_calendar">';
		str+='<table class="daycalendar" cellspacing="0"><tr>';
		for( i=0; i<7; i++ ){
			str+='<th>'+week[i]+'</th>';
		}
		str+='</tr>';
		var fdate=new Date(this.y, this.m-1, 1);
		str += '<tr>';

		var startDay = fdate.getUTCDay() + 1 + 1;
		if ( startDay == 0 )
			startDay = 7;
			  
		for(i=0;i<startDay-1;i++) {
			str += '<td>&nbsp;</td>';
		}

		var j=0; var c=0;
		var today=new Date();
		for( i=0, j = startDay-1, c=startDay; i<dim[ this.m-1 ]; i++, j++, c++){
			if( j != 0 && j % 7 == 0 ){
				c=1;
				str+="</tr><tr>";
			}
			astr='&nbsp;<a href="javascript:void();" id="'+this._prefix+'href'+i+'" onclick="javascript:select_day( \''+this._prefix+'\', '+(i+1)+' );return false;">'+(i+1)+'</a>&nbsp;';
			str += '<td id="'+this._prefix+'tdday'+( i+1 )+'" '+( this.d==i+1 ? 'class="selected"' : 'class="notselected"' )+' onclick="javascript:select_day( \''+this._prefix+'\', '+(i+1)+' );return false;" onmouseover="swap_style( \''+this._prefix+'\', '+(i+1)+', this ,1 );" onmouseout="swap_style( \''+this._prefix+'\', '+(i+1)+', this , 0 );" >'+(i+1)+'</td>';
		}
		for( i=j;; i++ ){
			if( i%7 == 0 ) break;
			str+='<td>&nbsp;</td>';
		}
		str+='</tr>';
		str+='</table>';
		str+='</div>';
		return str;
	}
	
	this.select_day = function( day, window ){
		window.document.getElementById( this._prefix+'tdday'+Math.abs( this.d ) ).className='notselected';
		this.d = day;
		window.document.getElementById( this._prefix+'tdday'+Math.abs( this.d ) ).className='selected';
		this._build_out_date( window );
		CloseCalendar( this.divID );
	}
	
	this.change_month = function ( month, window ){
		this.m = month;
		this._rebuild_months( window );
		//this._build_out_date();
	}
	
	this.change_year = function ( year, window ){
		this.y = year;
		var oD = new Date(this.y, 1, 1);
		dim[1]=(((oD.getFullYear()%100!=0)&&(oD.getFullYear()%4==0))||(oD.getFullYear()%400==0))?29:28;
		this._rebuild_months( window );
		//this._build_out_date();
	}
	
	this.change_hour = function ( hour ){
		this.h = hour;
		//this._build_out_date();
	}
	
	this.change_minut = function ( minut ){
		this.s = minut;
		//this._build_out_date();
	}
	
	this.build_today = function(){
		var todaydate=new Date();
		this.m=todaydate.getMonth()+1;
		this.y=todaydate.getFullYear();
		this.d=todaydate.getDate();
		this.h=todaydate.getHours();
		this.s=todaydate.getMinutes();
		//this.set_out_obj_value( this.y+'-'+this._normNumber( this.m )+'-'+this._normNumber( this.d )+' '+this._normNumber( this.h )+':'+this._normNumber( this.s ) );
	}
	
	this.build = function( divID ){
		this.divID = divID;
		var date = this.get_out_obj_value();
		if ( date != '' ){
			try {
				var r=new RegExp( /(\d{4})?-(\d{1,2})?-(\d{1,2})?(\s(\d{1,2})?:(\d{1,2})?)?/g );
				var parts = r.exec( date );
				this.y=parts[1];
				this.m=parts[2];
				this.d=parts[3];
				if ( parts[5] )
					this.h = parts[5];
				if (parts[6])
					this.s = parts[6];
			} catch(e) {
				this.build_today()
			}
		} else {
			this.build_today()
		}
		
		var oD = new Date(this.y, 1, 1);
		dim[1]=(((oD.getFullYear()%100!=0)&&(oD.getFullYear()%4==0))||(oD.getFullYear()%400==0))?29:28;
		
		var out = '<table class="calendar"><tr>';
		out += '<td> Year: '+this._build_year_combo()+'</td>';
		out += '<td> Month: '+this._build_month_combo()+'</td>';
		if ( this._show_hours ){
			out += '<td> Hours: '+this._build_hours_combo()+'</td>';
			out += '<td> Minutes: '+this._build_minut_combo()+'</td>';
		}
		out += '</tr><tr><td colspan='+( this._show_hours ? 4 : 2 )+'>';
		out += this._build_day_table();
		var closeStr = '<a href="javascript:void(0)" onclick="javascript:CloseCalendar( \''+divID+'\' ); return false">Close</a>';
		out += '</td></tr><tr><td colspan='+( this._show_hours ? 4 : 2 )+' align="center">'+closeStr+'</td></tr></table>';


		var div = window.top.document.getElementById( divID );
		if ( isIE() )
		{
			var iFrame = document.getElementById( 'CalendarIFrame' );
			iFrame.style.zIndex = "999999";
			iFrame.style.height = '200px';
            iFrame.style.width  = '250px';
			iFrame.style.display = 'block';
			iFrame.contentWindow.document.body.innerHTML = '<div id="' + divID + '">' + out + '</div>';
			div.style.background = 'none';
			div.style.border	 = '0px';
		}
		else
		{
			div.innerHTML = out;
		}
	}
}

