/** 
    @desc Here you find all function used for the "Kursliste". 
	
	$Log: kursliste.js,v $
	Revision 1.10  2009/07/10 05:36:50  cvsosir
	kurslistenbugfix
	
	Revision 1.9  2009/03/03 14:55:24  cvsosir
	Filter IntradayEmissionen
	FogBigz Case 1134
	
	Revision 1.8  2009/02/15 13:05:59  cvsosir
	Filter Bonuszertifikate Schwelle erreicht
	FogBugz: 253
	
	Revision 1.7  2009/01/12 10:01:22  cvsosir
	Fixed the Neuemissionen back error
	Fogbugz 406
	    
    
**/

/** 
 *  This function can be used to get a new kursliste. It reads the new filter and updates the insite storage accordingly.
 *  after that it cares for retreiving a new kurliste
 * 	@param filter A filter string like (Sort=4/1,2/0°PerPage=15)
 *	@param changeHistory boolean true when  the history should be replaced. Default is true
 **/
function GetKursliste(filter,changeHistory) {
	gl.log('== getKursliste(' + filter + ',' + changeHistory + ') ==','i');
	
	//for default is true...
	if (typeof(changeHistory) == 'undefined') 
	{
		changeHistory=true;
	} 	
	arNewFilter = new Array(); 
	//e.g. 	Sort=0/0,4/1
	//		PerPage=15
	//		CurentPage=1
	var arAllFilters=filter.split('°');
	for(i=0;i<arAllFilters.length;i++) {
		gl.log('keysAndValues:' +  arAllFilters[i] ,'i'); 
		//e.g. 	Sort
		//		0/0,4/1
		var arSingleFilter=arAllFilters[i].split('=');
		var filterKey='';
		var filterValue='';
		//check if its a key value pair
		if(arSingleFilter.length!=2) {
			gl.log('Error: The given filter "' + arSingleFilter + '" has the wrong format (key=value)','i');	
		} else {
			filterKey=arSingleFilter[0];
			filterValue=encodeURIComponent(arSingleFilter[1]);
			updateStorage(filterKey,filterValue);
		}
	}	
	
	
	updateKursliste(changeHistory);
	
	gl.log('== END getKursliste() ==','i');
}

function UpdateCallPutCheckbox() {

 	//reading oType (optional)
 	try {
	 	var call=$('call').checked;
	 	var put=$('put').checked;
	 	if(call==true) {
			call=1;
	 	} else {
	 		call=0;
	 	}
		
	 	if(put==true) {
	 		put=1;
	 	} else {
	 		put=0;
	 	}
	 	
	 	filterKey='oType';
	 	filterValue=call+'/'+put;
 		updateStorage(filterKey,filterValue);
 	} catch (e) {
 		gl.log(e);
 	}

}

/** 
 *  This function is called when the user changes the sorting by the help of the sorting box
 *  It gets the values of the combo boxes and the option fields and updates the storage accordingly...<b> 
 *
 **/
function GetKurslisteFromSortBox() {
	gl.log('=== GetKurslisteFromSortBox() ===','i');
	var filterKey='Sort';
	var filterValue='';
	var filterCol=0;
	var filterSortation=0;
	
	try {
		filterCol=$('listSort1').selectedIndex;
		filterSortation=$('listSort1Order').value;
		filterValue=filterCol + '/' +filterSortation;
		
		filterCol=$('listSort2').selectedIndex;
		filterSortation=$('listSort2Order').value;
		filterValue=filterValue + ',' + filterCol + '/' +filterSortation;
		updateStorage(filterKey,filterValue);
		
 	} catch(e) {
		gl.log(e);
	}
	
    UpdateCallPutCheckbox();
	
	//reading SWXListed Filter (optional)
 	try {
	 	var swxlisted=$('swxlisted').checked;
	
	 	filterKey='SWXListed';
	 	filterValue='0';
	 	
	 	if(swxlisted==true) {
	 		filterValue='1';
	 	}
	 
 		updateStorage(filterKey,filterValue);
 	} catch (e) {
 		gl.log(e);
 	}
	
	//reading filter schwelle (optional)
 	try {
	 	var filterSchwelle=$('filterSchwelle').checked;
	
	 	filterKey='filterSchwelle';
	 	filterValue='0';
	 	
	 	if(filterSchwelle==true) {
	 		filterValue='1';
	 	}
	 	
 		updateStorage(filterKey,filterValue);
 	} catch (e) {
 		gl.log(e);
 	}
	
	//reading filter intradayEmission(optional)
 	try {
		var filterIntraday=$('filterIntraday').checked;
	
	 	filterKey='filterIntraday';
	 	filterValue='0';
	 	
	 	if(filterIntraday==true) {
	 		filterValue='1';
	 	}
	 	
 		updateStorage(filterKey,filterValue);
 	} catch (e) {
 		gl.log(e);
 	}
	
	
 	updateKursliste(true);
	
	gl.log('=== END GetKurslisteFromSortBox() ===','i');
}
/** 
 *  This function can be used to update the current storage
 * @param filterKey A valid key like Sort, PageName, PerPage,...
 * @param filterValue A value to set for that filter criteria
 **/
function updateStorage(filterKey,filterValue) {
	gl.log('== updateStorage(' + filterKey + ',' + filterValue +') ==','i');
	//when the storage doesnt exists...
	if (typeof(storage) == 'undefined') { 	
		//create one
		createStorage();
	}
	gl.log('storage old:' + filterKey + '=' + storage[filterKey] + ' new value: ' + filterValue);
	storage[filterKey]=filterValue;
	//this function cares for switching the sort box according to the sorting of the columns;
	changeSortbox(filterKey,filterValue);
	gl.log('=== THE END updateStorage(' + filterKey + ',' + filterValue +') ===','i');
}

/** 
 * This function updates the kursliste dependent of the CURRENT site storage 
 * @param changeHistory boolean indicates if the current browser history entry should be replaced with a new one
 **/
function updateKursliste(changeHistory)
{
	gl.log('=== updateKursliste('+changeHistory+') ===','i');
	
	
	if (changeHistory) {
		var type = "InitKursliste";
		var sessionId = GLOBALSESSIONID;
		var params = "sessionId="+sessionId+"&"+getStorageParams();
		// replace the current history string accordingly
		historyReplace('CallEx',type+'$'+params);
	}
	
	var params= getStorageParams();
	var url='!ProcessPage';
	var ajaxHelper = new AjaxHelper();	
	ajaxHelper.publicSend(url,'CallType=GetKursliste&'+params,'auswahl');
	
	gl.log('=== THE END updateKursliste() ===','i');
}

/** 
 * This function creates a new storage (can be used to clear it too) 
 **/
function createStorage() {
	gl.log('=== createStorage() ===');
	storage = new Array(); 
	gl.log('=== THE END createStorage() ===');
}
/** 
 * This function cares for switching the sort box according to the sorting of the columns
 **/
function changeSortbox(filterKey,filterValue) {
	gl.log('=== changeSortbox(' + filterKey+ ',' + filterValue+ ') ===','i');
	//when the sorting gets changed, update the sortbox to reflect the current sorting
	if(filterKey=='Sort') {
		try {
			filterValue=unescape(filterValue);
			
			var columnsFilter=filterValue.split(',');
			var colFilter=columnsFilter[0].split('/');			
			
			$('listSort1').selectedIndex=colFilter[0];
			$('listSort1Order').selectedIndex=colFilter[1];
			
		} catch(e) {
			gl.log('Warning:' + e);
		}
	}
	gl.log('=== THE END changeSortbox(' + filterKey+ ',' + filterValue+ ') ===','i');
}
/** This method returns everything inside the storage as a parameter list
@return The storage as parameter list (Sort=1/0;2/1&PerPage=15....) */
function getStorageParams() {
	var params='';
	for (var i in storage) {
		//check the type cause other wise the functions of the array object are used here too
		if(typeof(storage[i])=='string' && i !='______array')
		{
			params=params +i + '=' + storage[i] +'&'; 	
		}
	}
	
	//find the last & sign
	var last=params.lastIndexOf('&');
	//and delete it
	params=params.slice(0,last);
	
	return params;
}
/** Can be used to display the current storage in a logger window **/
function showStorage() 
{		
	gl.log('=== showStorage() ===');
	
	if (typeof(storage) != 'undefined') { 
		for (var i in storage) {
			//check the type cause other wise the functions of the array object are used here to
			if(typeof(storage[i])=='string' && i !='______array') {
				gl.log('key is: ' + i + ', value is: ' + storage[i]);
			}			
		}
	} else {
		gl.log('The storage is not defined!','w');
	}
	gl.log('=== THE END showStorage() ===','i');
}

