/* 
	Everything that steers filtering is located within this file

	
	$Log: filter.js,v $
	Revision 1.12  2009/10/30 09:13:40  cvsosir
	resetSpecial added
	
	Revision 1.11  2009/07/22 09:10:28  cvsosir
	slider-fix
	
	Revision 1.10  2009/02/15 13:05:59  cvsosir
	Filter Bonuszertifikate Schwelle erreicht
	FogBugz: 253
	
	Revision 1.9  2009/01/09 13:17:16  cvsosir
	Fixed the snapshot -> kurstabellen problem
	FogBugz: 262
	

*/

var dontUpdateInReset = false;
/** This array should hold all sliders objects within the document **/
var sliders=new Array();
var Filter = {
	
	reset: function() {
		gl.log('=== Filter.reset() ===','i');
		dontUpdateInReset = true;
		this.resetCheckBoxes();        
		this.resetSliders();
		dontUpdateInReset = false;
		if( sliders.length>0 ) {
			sliders[0].updateFinished();
		}
		gl.log('=== THE END Filter.reset() ===','i');    
	},
	
	resetSpecial: function(ausnahme) {
		gl.log('=== Filter.reset() ===','i');
		dontUpdateInReset = true;
		this.resetCheckBoxesSpecial(ausnahme);        
		this.resetSliders();
		dontUpdateInReset = false;
		if( sliders.length>0 ) {
			sliders[0].updateFinished();
		}
		gl.log('=== THE END Filter.reset() ===','i');    
	},
	
	/** This function searchs for all checkboxes within the document and turns them off **/    
	resetCheckBoxesSpecial: function(ausnahme) {
		gl.log('=== Filter.resetCheckBoxes() ===','i');
		var inputs = $$('input');
		for(i=0;i<inputs.length;i++) {
			if (inputs[i].getAttribute('type') == 'checkbox') {
				//Uncheck except ausnahme
				if (inputs[i].getAttribute('name')!=ausnahme)
				{
					inputs[i].checked=false;
				}
			}
		}
		gl.log('=== THE END Filter.resetCheckBoxes() ===','i');
	},    
	
	resetCheckBoxes: function() {
		gl.log('=== Filter.resetCheckBoxes() ===','i');
		var inputs = $$('input');
		for(i=0;i<inputs.length;i++) {
			if(inputs[i].getAttribute('type') == 'checkbox') {
				inputs[i].checked=false;
			}
		}
		gl.log('=== THE END Filter.resetCheckBoxes() ===','i');
	},   
	
	/** This function searchs for all sliders and sets the values to 0 **/    
	resetSliders: function() {
		gl.log('=== Filter.resetSliders() ===','i');
		for (i=0;i<sliders.length;i++) {
			try {
				sliders[i].setValue(sliders[i].minimum, 0);
				if (sliders[i].handles.length == 2)
					sliders[i].setValue(sliders[i].maximum, 1);
			} catch(e) {
				gl.log(e,'e');
			}
		}
		gl.log('=== THE END Filter.resetSliders() ===','i');
	}
}
/**
 *  simple Oracle style isBlank / isNull check for strings
 */
function isBlank(value) {
	if (value)
		return value == '';
	
	return true;
}
/**
 *  Simple function to format an integer value for a slider
 *
 * @param slider the slider
 * @param value the current value (integer)
 * @param label the proposed label
 * @param isStartRange boolean indicates if this is the start, otherwise it's the end of the range
 */
function formatSliderIntegerValue(slider, value, label, isStartRange) {
	if (!isBlank(label))
		return label;
	
	return ""+value;
}
/**
 *  default renderer function for range sliders
 * 
 * @param slider the slider 
 * @param startPos the current start position of the range
 * @param endPos the current end position of the range
 * @param sliderHTML the slider html element 
 * @param sliderValueHTML the slider value html element
 */
function formatSliderIntegerRange(slider, startPos, endPos, sliderHTML, sliderValueHTML) {
	var text = slider.options.valuesLabelPrefix + ' ';
	var startVal = startPos;
	var endVal = endPos;
	
	if (slider.options.openStart && startPos == slider.minimum) {
		if (!isBlank(slider.options.openStartLabel))
			text += slider.options.openStartLabel;
		else if (!isBlank(slider.options.labelStart))
			text += slider.options.labelStart + slider.options.valuesLabelUnit;
		else
			text += ""+startPos + slider.options.valuesLabelUnit;
		startVal = -1;
	} else {
		text += ""+startPos + slider.options.valuesLabelUnit;
	}
	
	text += slider.options.valuesLabelSeperator;
	
	if (slider.options.openEnd && endPos == slider.maximum) { 
		if (!isBlank(slider.options.openEndLabel))
			text += slider.options.openEndLabel;
		else if (!isBlank(slider.options.labelEnd))
			text += slider.options.labelEnd;
		else
			text += ""+startPos + slider.options.valuesLabelUnit;
		endVal = -1;
	} else {
		text += ""+endPos + slider.options.valuesLabelUnit;
	}
	
	$(sliderValueHTML).innerHTML=text;
	$(sliderHTML).setAttribute( 'svalue', ''+startVal+'|'+endVal );
}
/**
 *  converts the given osiris date to a JS Date
 *
 * @param value the osiris date
 * @return the equivalent JS date
 */
function osirisToDate(value) {
  return new Date((value - 25569.0) * (24 * 60 * 60 * 1000)); 
}
/**
 * converts the given JS Date to an osiris date
 *
 * @param date the JS Date to convert
 * @return the equivalent osiris value
 */
function dateToOsiris(date) {
  return (date.getTime() / (24 * 60 * 60 * 1000)) + 25569.0;
}
/**
 *  adds the given parameters to the given date
 * 
 * @param date the original date to manupulate
 * @param days the days to add / substract
 * @param hours the hours to add / substract
 * @param minutes the minutes to add / substract
 * @param seconds the seconds to add / substract
 * @return a new date object
 */
function addToDate(date, days, hours, minutes, seconds) {
    var m_secs = date.getTime();
    var local_offset = date.getTimezoneOffset() * 60 * 1000;
    var utc_m_secs = m_secs + local_offset;
  
    if (days)
      utc_m_secs += days * 24 * 60 * 60 * 1000;
    if(hours)
      utc_m_secs += hours * 60 * 60 * 1000;
    if(minutes)
      utc_m_secs += minutes * 60 * 1000;
    if(seconds)
      utc_m_secs += seconds * 1000;
    return new Date(utc_m_secs);
}
function pad2(value) {
	var padded2 = parseInt(value, 10); 
	if (value < 10) 
		padded2 = "0" + 
	padded2; 
	return padded2; 
}
/**
 *  formats the given date into ISO 8601
 *
 * @param date the date to format
 * @return formatted string of the date
 */
function formatDateISO(date) {
	if (date) {
		var str = date.getFullYear() + "-" + pad2(date.getMonth() + 1) + "-" +pad2(date.getDate());
		return str;
	}
	return '';
}
/**
 *  formats the given date into ISO 8601
 *
 * @param date the date to format
 * @return formatted string of the date
 */
function formatDateDe(date) {
	if (date) {
		var str = pad2(date.getDate()) + "." + pad2(date.getMonth() + 1) + "." + (""+date.getFullYear()).substr(2);
		return str;
	}
	return '';
}
/**
 *  formats the given date into a locale specific format
 * 
 * @param date the date to format
 * @return formatted string of the date
 */
function formatDate(date) {
	return formatDateDe(date);
}
/**
 *  Simple function to format an integer value to a date output for a slider
 *
 * @param slider the slider
 * @param value the current value (integer)
 * @param label the proposed label
 * @param isStartRange boolean indicates if this is the start, otherwise it's the end of the range
 * @return the passed in value as formatted string
 */
function formatSliderDateValue(slider, value, label, isStartRange) {
	if (!isBlank(label))
		return label;
	
	var date = osirisToDate(value);
	return formatDate(date);
}
/**
 *  renderer function for range sliders to convert the values to dates
 * 
 * @param slider the slider 
 * @param startPos the current start position of the range
 * @param endPos the current end position of the range
 * @param sliderHTML the slider html element 
 * @param sliderValueHTML the slider value html element
 */
function formatSliderDateRange(slider, startPos, endPos, sliderHTML, sliderValueHTML) {
	var cacheKey = ''+startPos+"|"+endPos;
	
	// create cache on the fly
	if (typeof(slider.options.cache) == 'undefined') {
		slider.options.cache = new Object();
	}
	
	// now check if there is a cache entry
	if (slider.options.cache[cacheKey]) {
		var cachedData = slider.options.cache[cacheKey];
		
		$(sliderValueHTML).innerHTML= cachedData.text;
		$(sliderHTML).setAttribute( 'svalue', cachedData.svalue);
		return;
	}
	
	var text = slider.options.valuesLabelPrefix + ' ';
	var startVal = startPos;
	var endVal = endPos;
	var svalue = '';
	
	if (slider.options.openStart && startPos == slider.minimum) {
		if (!isBlank(slider.options.openStartLabel))
			text += slider.options.openStartLabel;
		else if (!isBlank(slider.options.labelStart))
			text += slider.options.labelStart + slider.options.valuesLabelUnit;
		else
			text += ""+formatDate(osirisToDate(startPos)) + slider.options.valuesLabelUnit;
		startVal = -1;
	} else {
		var startDate = osirisToDate(startPos);
		text += ""+formatDate(startDate) + slider.options.valuesLabelUnit;
	}
	
	text += slider.options.valuesLabelSeperator;
	
	if (slider.options.openEnd && endPos == slider.maximum) { 
		if (!isBlank(slider.options.openEndLabel))
			text += slider.options.openEndLabel;
		else if (!isBlank(slider.options.labelEnd))
			text += slider.options.labelEnd;
		else
			text += ""+formatDate(osirisToDate(endPos)) + slider.options.valuesLabelUnit;
		endVal = -1;
	} else {
		var endDate = osirisToDate(endPos);
		text += ""+formatDate(endDate) + slider.options.valuesLabelUnit;
	}
	
	if (startVal != -1)
		svalue = svalue+formatDateISO(osirisToDate(startVal));
	else
		svalue = svalue+'-1';
	
	svalue = svalue + '|';
	
	if (endVal != -1)
		svalue = svalue+formatDateISO(osirisToDate(endVal));
	else
		svalue = svalue+'-1';
	
	var cachedData = new Object();
	cachedData.text = text;
	cachedData.svalue= svalue;
	slider.options.cache[cacheKey] = cachedData;
	
	$(sliderValueHTML).innerHTML=text;
	$(sliderHTML).setAttribute( 'svalue', svalue);
}

/**
  @desc This function is used to open/close the filter boxes
  @param The element (openClose) that got clicked 
*/
function filterOpenClose(element)
{
	var value=$(element).innerHTML;
	
	if(value=='[ + ]')
	{
		$(element).innerHTML='[ - ]';
		//getting the second parent of this element
		var parent=$(element).ancestors()[1];
		//...from there search for filterBody
		var filterBody=parent.select('[id="filterBody"]')[0];
		//.. make this element visible
		filterBody.removeClassName('invisible');
	}
	else
	{
		$(element).innerHTML='[ + ]';
		//getting the second parent of this element
		var parent=$(element).ancestors()[1];
		//...from there search for filterBody
		var filterBody=parent.select('[id="filterBody"]')[0];
		//.. make this element invis
		filterBody.addClassName('invisible');		
	}
}
