/**
 * This javascript-object is used in combination with the calendar script (PHP) to set a date (with javascript) in an hidden input box
 */
function Calendar() {
	this.Setup();
}

Calendar.prototype.Setup = function() {
	/*
	 * Setting the defaults
	 */
	this.ActiveDate = null;
	this.DefaultClassname = "Normal";
	this.ActiveClassname = "Selected";
	this.setDefaultDate();
}

Calendar.prototype.setDate = function( Date ) {
	var Input = document.getElementById( "ChosenDate" );
	if( Input ) {
		this.setPreviousDateInactive();
		this.setActiveDate( Date );
		this.getActivities( Date );
	} else {
		throw( "Calendar script could not be initialized" ); 
	}
}

Calendar.prototype.setActiveDate = function( Date ) {
	
	var Input = document.getElementById( "ChosenDate" );
	if ( Input ) {
		Input.value = Date;
		this.ActiveDate = Date;
		
		var Object = document.getElementById( 'oCall_Day_' + Date );
		if( Object ) {
			Object.className = this.ActiveClassname;
		}
	} else {
		throw( "This can't be happening! The input element was there before I started this function!" );
	}
}

Calendar.prototype.setPreviousDateInactive = function() {
	if( this.ActiveDate ) {
		var Object = document.getElementById( 'oCall_Day_' + this.ActiveDate );
		if ( Object ) {
			Object.className = this.DefaultClassname;
		}
	}
}

Calendar.prototype.setDefaultDate = function() {
	var ObjectChosenDate = document.getElementById( "ChosenDate" );
	if( ObjectChosenDate ) {
		var Date = ObjectChosenDate.value;
		this.setDate( Date );
	}
}

Calendar.prototype.getCalendar = function( Month, Year ) {
	
	var httpObject = getHTTPObject();
	var ScriptURL = AbsPath + 'includes/SpecialPages/Calendar/Calendar.php';
	
	if ( httpObject != null) {
		//Opening the connection
		httpObject.open( "POST", ScriptURL, false );
		httpObject.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
		
		//Setting up params and sending them
		var Params = "Y=" + Year + "&M=" + Month + "&T=" + Language;
		httpObject.send( Params );
		
		//Based on the ajax response we will determin if it went ok or not.
		var response = httpObject.responseText;
		
		if ( response !== 'false' ) {
			alert('ajax');
			var CalendarDiv = document.getElementById( 'Calendar' );
			CalendarDiv.innerHTML = response;
			this.setDefaultDate();
		} else {
			throw 'Not a valid year/month combination';
		}
	} else {
		throw 'HttpObject was not set!';
	}
	
	return false;
}

Calendar.prototype.getActivities = function( Date ) {
	
	var httpObject = getHTTPObject();
	var ScriptURL = AbsPath + 'includes/SpecialPages/Calendar/Activities.php';
	
	if ( httpObject != null) {
		//Opening the connection
		httpObject.open( "POST", ScriptURL, false );
		httpObject.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
		
		//Setting up params and sending them
		var Params = "Date=" + Date;
		httpObject.send( Params );
		
		//Based on the ajax response we will determin if it went ok or not.
		var response = httpObject.responseText;
		
		if ( response !== 'false' ) {
			var ActivitiesDiv = document.getElementById( 'Activities' );
			ActivitiesDiv.innerHTML = response;
		} else {
			throw 'Not a valid year/month/day combination';
		}
	} else {
		throw 'HttpObject was not set!';
	}
	
	return false;
}