/* 
   * Last Update: May 6, 2008(Alex)

 * Description:
    - commonly used functions for the my lab pages. 

 * Dependencies:
   - Requires: jquery.js, jquery.cookie.js, common.js, my_lab_common.js
   - Used in: my_lab.js,  my_lab-popupExerciseParams.js
*/

/* filters a string of serialized params by adding the correct case, for later use, when saving the params as cookies */
function filterMyLabParamsCase(strLabParams)
{
	var labSensitiveCaseParams = ['workoutid', 'traineeid', 'AthleteTrainingProgramCacheDetailID', 'customworkoutexerciselookupid'];
	var labCorrectCaseParams = ['workoutId', 'traineeId', 'AthleteTrainingProgramCacheDetailID', 'CustomWorkoutExerciseLookupID'];

	var result = new String(strLabParams);
	for (var i=0; i< labSensitiveCaseParams.length; i++)
	{
		var filter = new RegExp(labSensitiveCaseParams[i], "ig");
		result = result.replace(filter, labCorrectCaseParams[i]);
	}
	return result;
}

/* returns the cookies used for the my lab pages. The cookie list is saved in the global myLabCookies array, defined in common.js */
function getMyLabCookies()
{
	var result = {};
	var ind = 0;
	for (var i=0; i< myLabCookies.length; i++)
	{
		var myLabCookie = jQuery.cookie(myLabCookies[i]);
		if (myLabCookie != null) 
		{
			ind +=1;
			result[myLabCookies[i]] = myLabCookie;
		}
	}
	if (ind == 0)
		return null;
		
	return result;
}

/* saves the my lab params passed as parameters  to cookies, after checking them against the global myLabCookies array, defined in common.js */
function saveMyLabCookies(queryParams, cookieOptions)
{	
	if (cookieOptions)
	{
		for (var param in queryParams)
		{
			if (jQuery.inArray(param, myLabCookies) != -1)
				jQuery.cookie(param, queryParams[param], cookieOptions);
		}
	}
	else
	{
		for (var param in queryParams)
		{
			if (jQuery.inArray(param, myLabCookies) != -1)
				jQuery.cookie(param, queryParams[param]);
		}
	}
}

/* returns the my lab query params, after setting the correct param names case 
     the returned result is in the form:
		{
			serializedParams : strSer, 
			params: {
					param1: param1val, 
					param2: param2val
					...
				   }
		}
   or null if no params are passed ;
*/
function getMyLabQueryParams()
{
	var result = null;
	var filteredCaseParamString = filterMyLabParamsCase(window.location);
	var strUrl = new String(filteredCaseParamString);	
	var strParams = new String(strUrl.match(/\?[a-z0-9&=_%\//-]+/i));
	
	if (strParams != 'null')
	{
		result = {
					serializedParams : '',
					params : {}
				};
				
		result['serializedParams'] = strParams.substr(1);
		
		var paramPairs = result['serializedParams'].split('&');
		for (var i=0; i< paramPairs.length; i++)
		{
			var strParamPair = new String(paramPairs[i]);
			var arrParamPair = strParamPair.split('=');
			
			if (jQuery.inArray(arrParamPair[0], myLabCookies) != -1)
			{
				result['params'][arrParamPair[0]] = arrParamPair[1];
			}
		}
		
		if (result['params'])
			result['serializedParams'] = getSerializedSimpleObject(result['params']);
		else
			return null;
	}
		
	return result;
}