

function make_wb_url(action, args){
    var url = "" + window.location.protocol + "//" + window.location.hostname + window.location.pathname + "?";
    var arg_value = "";
    var url_args = get_dayhistory_args();

    if (args.pg == undefined ) {
        url_args.pg = "1";
    } 
    else {
        url_args.pg = args.pg;        
    }

    for (var a in args) {
        switch (action) {
            case 'set':
            	if (typeof args[a] === 'object') {
            		url_args[a] = JSON.stringify(args[a]);
            	}
            	else {
        	        url_args[a] = args[a];
            	}
                break;
            case 'add':
            	if (typeof args[a] === 'object') {
            		arg_value = {};
            		if ((a in url_args) && url_args[a]) {
            			arg_value = jQuery.parseJSON(url_args[a]);
            		}
            		
            		for (k in args[a]) {
            			arg_value[k] = args[a][k];
            		}
            		url_args[a] = JSON.stringify(arg_value);
            	}
            	else {            		
	                if (url_args[a] != "") {
	                    url_args[a] += ",";    
	                }
		            url_args[a] += args[a];
            	}

                break;
            case 'remove':
            	if ((args[a]) && (typeof args[a] === 'object')) {
            		arg_value = {};
            		if ((a in url_args) && url_args[a]) {
            			arg_value = jQuery.parseJSON(url_args[a]);
            		}
            		else {
            			break;
            		}

            		// args = {prop:[v,v,v...],...}
            		// if args[a] is empty, remove a from arg_value
            		// else remove values from arg_value
            		// arg_value is the current parameter
            		for (p in args[a]) {            			
            			if (typeof arg_value[p] != 'array') {
            				arg_value[p] = [arg_value[p]];
            			}
            			if (!args[a][p]) {
            				delete arg_value[p]; 
            			}
            			else {
            				if (jQuery.type(args[a][p] == 'array')) {
		            			for (var i=0; i < args[a][p].length; i++) {
		            				if (typeof arg_value[p] == 'array') {
		            					arg_value[p] = arg_value[p].splice(arg_value[p].indexOf(args[a][p][i])) ;
		            				}
		            				else if (arg_value[p] == args[a][p][i]) {
		            					arg_value[p] = null;
		            				}
            					}
            				}
            				else { // no value to remove so set the parameter to be removed
								arg_value[p] = null;
	            			}            					
	        				if (!arg_value[p]) { // if all values where removed delete the property
	            				delete arg_value[p]; // remove the property        					
	        				}
            			} //if the object property has a value
            		}
            		// test if object is empty
            		var is_empty = true;
            		for (v in arg_value) {
            			is_empty = false;
            		}
            		if (arg_value && !is_empty) { // remove the parameter
	            		arg_value = JSON.stringify(arg_value);        			
            		}
            		else {
            			arg_value = null;
            		}
            		
            	} // end if the arg value is an object
            	else { // the arg value is not an object
	                if ((args[a] != null) && (args[a] != "")) {
	                    arg_value = url_args[a].replace(args[a], "");
	                    arg_value = arg_value.replace(/^,|,$/, ""); // WTF arg_value is empty
	                    arg_value = arg_value.replace(/,,/, ",");   // WTF arg_value is empty
	                }            		
            	}
                                
                if (!arg_value){ //}((arg_value == null) || (arg_value == "")) {
                    delete url_args[a]; // remove parameter
                }
                else {
	                url_args[a] = arg_value;            	
                }
                break;
            default:
                break;
        }
    }

    var url = "" + window.location.protocol + "//" + window.location.hostname + window.location.pathname + "?";
    for (var p in url_args) {
        
        if (url_args[p]){ //} (url_args[p] != null) && (url_args[p] != "")) {
            url += "&" + p + "=" + url_args[p];
        }
        
        
    }
    return encodeURI(url);

} // end make_wb_url


function day_suffix(day_digits) {

    var suffix = "";
    if (day_digits in {11:"",12:"",13:""}) { // these days are a special case
            suffix = "th";
    }
    else {
        switch (day_digits.charAt(day_digits.length-1)) {        
            case "1":
                suffix = "st";
                break;
            case "2":
                suffix = "nd";
                break;
            case "3":
                suffix = "rd";
                break;
                
            default:
                suffix = "th";
                break;
        }        
    }
    return suffix;
    
}


function make_day_str (day, format) {
    var arg_day = day;
    var d = new Date();
    var yr = parseInt(d.getFullYear());
    var is_year = false;
    if (arg_day && (arg_day != "")) {
        arg_day = $.trim(decodeURIComponent(arg_day).replace(/\//g,"-")); //normalize the string
//deb("cleaned:"+arg_day+":");        
        var day_parts = arg_day.split("-");
        if (day_parts.length > 2) {       // the year is specified
            is_year = true;
            // the following is because getFullYear prepends 20 for years before 70
            yr = parseInt(day_parts[2]); // for now assume year is the third part
            if ((yr > 10) && (yr < 100)) { 
                yr = 1900 + yr;
            }
            arg_day = day_parts[0] + "-" + day_parts[1] + "-" + yr;
//deb(":"+arg_day+":");        
            d = new Date.parseString(arg_day);                            
        }
        else { 
            // if no year is specified use a leap year to parse the day
            // otherwise Date may fail if day is 2-29
            d = new Date.parseString(arg_day+ "-"+"2000");    
        }
    }
    
//deb("format:"+format) ;   
    if (!format) {
        return d;
    }
    else if (format == "yyyy") {
        return ""+yr;
    }
    else {
        return d.format(format);
    }
}


