/**
 * 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( ObjectName, ObjectNumber, FormName ) {
	this.Setup( ObjectName, ObjectNumber, FormName );
}

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

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

Calendar.prototype.setActiveDate = function( Date ) {
	
	var Input = document.getElementById( "ChosenDate" +  this.ObjectNumber	 );
	if ( Input ) {
		Input.value = Date;
		this.ActiveDate = Date;
		
		var Object = document.getElementById( this.ObjectName + '_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( this.ObjectName + '_Day_' + this.ActiveDate );
		if ( Object ) {
			Object.className = this.DefaultClassname;
		}
	}
}

Calendar.prototype.setDefaultDate = function() {
	var ObjectChosenDate = document.getElementById( "ChosenDate" + this.ObjectNumber );

	if( ObjectChosenDate ) {
		var Date = ObjectChosenDate.value;
		this.setDate( Date );
		
	}
}

Calendar.prototype.getCalendar = function( Month, Year ) {
	
	var httpObject = getHTTPObject();
	var ScriptURL = AbsPath + 'includes/SpecialPages/Calendar/Grid.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 = "O=" + this.ObjectName + "&N=" + this.ObjectNumber + "&Y=" + Year + "&M=" + Month;
		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 CalendarDiv = document.getElementById( this.ObjectName );
			CalendarDiv.innerHTML = response;
			this.setDefaultDate();
		} else {
			throw 'Not a valid year/month combination';
		}
		
	} else {
		throw 'HttpObject was not set!';
	}
	
	return false;
}

Calendar.prototype.loadDate = function( Date ){
	
	var ProgramLocation = AbsPath + 'includes/Components/Overzicht.php';  
 
	$.ajax({
		type: "POST",
		url: ProgramLocation,
		data: {Date: Date},
		contentType: "application/x-www-form-urlencoded; charset=UTF-8;",
		timeout: 5000,
		error: function(){
			alert( 'Error loading XML document' );
		},
		success: function( R ){
			var DIV = document.getElementById( 'ProgrammaOverzicht' );
			if ( DIV ) {
				if ( R != 'false' ) {
					//Gelukt, ik heb de HTML in var R
					//DIV.innerHTML = R;
					$( DIV ).html( R );
				} else {
					document.location.href = ProgramPath + '?Date='+Date;
				}
			} else {
				document.location.href = ProgramPath + '?Date='+Date;
			}
		}
	});
}

function CheckDate(Form, Field) {	
	var FormField = document.getElementById( Field );
	if( FormField.value ){
		return true;
	} else {
		alert( Error_DateNotSet );
		return false;
	}
}

function CompareDates(Form, Field1, Field2) {	
	
	var FormField1 = document.getElementById( Field1 );
	var FormField2 = document.getElementById( Field2 );
	if( FormField1 && FormField2 ){
		//eerste datum converteren naar date object
		var Date1 = FormField1.value;
		
		var DatePart1_Y = Date1.substring(6,10);
		var DatePart1_D = Date1.substring(0,2); // Jan-Dec=01-12
		var DatePart1_M = Date1.substring(3,5);
		
		var DateField1 = new Date( DatePart1_M + '/' +DatePart1_D + '/' + DatePart1_Y );
		//tweede datum converteren naar date object
		var Date2 = FormField2.value;
		
		var DatePart2_Y = Date2.substring(6,10);
		var DatePart2_D = Date2.substring(0,2); // Jan-Dec=01-12
		var DatePart2_M = Date2.substring(3,5);
		
		var DateField2 = new Date( DatePart2_M + '/' +DatePart2_D + '/' + DatePart2_Y );
		
	}
	if( DateField1 < DateField2 ){
		return true;
	} else {
		// convert 29-03-2010 -> 03/29/2010
		alert( Error_InvalidDate );
		return false;
	}
}