

var expiration = '30';
var urlParams = document.URL.split('?')[1]; //window.parent.location.search.replace('?', '');
var urlParams_cookie_name= "URL_Params";

manageCookieForURLParams = function() {
	

	function getCookie(name) {
		var value = "; " + document.cookie;
		var parts = value.split("; " + name + "=");
		if (parts.length == 2)
			return parts.pop().split(";").shift();
	}
	
	function setCookieObj(name, value, days) {        
		var cookie = decodeURIComponent(getCookie(urlParams_cookie_name));             
        
		if((cookie == undefined) || (cookie == null) || (cookie == '') || (cookie == 'undefined')){			
			cookie = setCookie(name, value, days);
		}else{			
			var map = cookieDataMap(cookie);			
			var newValue = populateNewCookieValue(value, map);															
			cookie = setCookie(name,  newValue, days);						
		}
		
        return cookie;
    }
		
	function setCookie(name, value, days) {		
		var host = window.location.hostname.split('.');
		host.shift();
		var currentdomain = host.join('.');		
		
		if ((days === undefined) || (days === null) || (days === '')) {
			//var cookieValue = name + '=' +  '{"data": "'+ doformat(value) +'"}; path=/';
			var cookieValue = name + '=' +  doformat(value) +'; domain='+currentdomain+'; path=/';
			document.cookie=cookieValue;
		}else{
			
			// day based expiration
			var now = (new Date()).getTime();
			var exp = new Date(now + parseInt(days) * (24 * 60 * 60 * 1000));
			exp = exp.toGMTString();						
			
			//var cookieValue = name + '=' +  '{"data": "'+ doformat(value) +'","date":' + now + ',"days":' + days + '}; expires=' + exp + "; path=/";
			var cookieValue = name + '=' +  doformat(value) +'; expires=' + exp + '; domain='+currentdomain +'; path=/';
			//alert('setCookieObj:cookieValue:'+cookieValue);
			document.cookie=cookieValue;			
		} 		
		
		return decodeURIComponent(getCookie(name));		
	}	
        
	function cookieDataMap(obj){
		var data = obj.split('&');
		var map = {}; 
		var hash = [];
		for(var i = 0; i < data.length; i++){
			hash = data[i].split('=');
			map[hash[0].toUpperCase()] = hash[1];				
		}
		return map;
	}   
	
	function populateNewCookieValue(value, map){
		var vals = value.split('&');
		var hash = [];
		var newMap = map;
		
		for(var i = 0; i < vals.length; i++){	
			hash = vals[i].split('=');
			var key = hash[0].toUpperCase();
			var val = hash[1];
			if((map[key] === undefined) || (map[key] === null) || (map[key] === '')){
				//new param
				newMap[key] = hash[1];					
			}else {
				//existing param with new value
				if(map[key] !== hash[1]){
					newMap[key] = hash[1];								
				}else{
					newMap[key] = map[key];
				}
			}							
		}
		var newValue; 
		
		for (key in newMap) {
			if(newValue === undefined || newValue === null || newValue === ""){
				newValue = key + '=' + newMap[key];
			}else{
				newValue = newValue + '&' + key + '=' + newMap[key];
			}
		}
		
		return newValue;
	}
	
	function doformat(obj){					
		var data = obj.split('&');
		var hash = [];
		var newValue;
		var first = true;
		for(var i = 0; i < data.length; i++){
			hash = data[i].split('=');
			if(first){
				newValue = hash[0].toUpperCase() + '=' + hash[1];
				first = false;
			}else{
				newValue = newValue + '&' + hash[0].toUpperCase() + '=' + hash[1];
			}
		}
		return escape(newValue);
	}

	if (urlParams !== undefined && urlParams !== '') {
		
		if (!(expiration != undefined && expiration != null && expiration != '')) {
			expiration = 30;
		} 
		setCookieObj(urlParams_cookie_name, urlParams, expiration);	
	}
};
