$.getQueryString = function(name) 
{
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
    if (!results)
    { 
        return ''; 
    }

    return results[1] || '';
};

$.getPageName = function() 
{
    var path = window.location.pathname;
    var page = path.substring(path.lastIndexOf('/') + 1);   
    
    if(page.length == 0)
    {
        page = 'home';    
    }
    
    return $.capitalize(page.replace('.aspx',''));
}

$.capitalize = function(word) 
{
    return word.charAt(0).toUpperCase() + word.slice(1);
}

$.addWebAddress = function(webAddress) 
{
	var queryString = 'wa=' + webAddress
    
    function updateLink(href)
    {
        if(typeof href !=  'undefined' && 
           href.indexOf('http') < 0 && 
           href.toLowerCase().indexOf('javascript') < 0 && 
           href.toLowerCase().indexOf('mailto') < 0 && 
           href.toLowerCase().indexOf('void') < 0 && 
           href.toLowerCase().indexOf('#') < 0 && 
           href.indexOf('wa=') < 0)
        {
            return true;
        }
        return false;
    }
    
	// traverse all anchors
	$('a').each(function() 
	{
		var link = $(this);
        
        if(updateLink(link.attr('href')))
        {
            if(link.attr('href').indexOf('?') < 0)
            {
                link.attr('href', link.attr('href') + '?' + queryString);
            }
            else
            {
                link.attr('href', link.attr('href') + '&' + queryString);
            }
        }

	});
};

$.getTodaysDate = function()
{
    var d = new Date();

    return (d.getMonth() + 1) + '/' + d.getDate() + '/' + d.getFullYear();
};

$.getTomorrowsDate = function()
{
    var d = new Date();
    d.setDate(d.getDate()+1);
    
    /*if(canada)
    {
        return (d.getMonth() + 1) + '-' + (d.getDate()) + '-' + d.getFullYear();
    }
    else
    {*/
        return (d.getMonth() + 1) + '/' + (d.getDate()) + '/' + d.getFullYear();
    //}
};

$.addContent = function(code, selector) 
{
    $.ajax(
    { 
        type: "GET",   
        url: location.protocol + "//" + location.hostname + "/Content.aspx?code=" + code + "&" + location.search.substring(1),   
        async: false,
        cache: false,
        dataType: "text",
        success : function(data)
                  {
                      $(selector).append(data);
                  }
    });                   
};




$.replaceContent = function(code, selector) 
{
    $.ajax(
    { 
        type: "GET",   
        url: location.protocol + "//" + location.hostname + "/Content.aspx?code=" + code + "&" + location.search.substring(1),   
        async: false,
        cache: false,
        dataType: "text",
        success : function(data)
                  {
                      $(selector).replaceWith(data);
                  }
    })   
};

$.fn.exists = function()
{
    return this.length > 0;
};

$.isDefined = function(func)
{
    //return (typeof func != 'undefined');
    return eval("typeof " + func + " == 'function'")
}

$.iff = function(booleanExpression, trueValue, falseValue)
{
    if(booleanExpression)
    {
        return trueValue;           
    }
    else
    {
        return falseValue;
    }
}


$.ajaxRequest = function(functionName, parameters)
{
    var response = "";
    
    jQuery.ajax(
    { 
        type: "GET",   
        url: location.protocol + "//" + location.hostname + "/AjaxRequest.aspx?function=" + functionName +  "&" + parameters,   
        async: false,
        dataType: "text",
        success : function(r)
                  {
                      response = r;
                  }
    })    
    
    return response;
};

$.fn.disableLink = function(options)
{
    var defaults = 
    {
        showAlert: false,
        message: 'Links disabled on demo site'
    };
  
    // Extend our default options with those provided.
    var opts = $.extend(defaults, options);
    
    this.each(function() 
	{
	    $(this).removeAttr('onclick');
	    $(this).removeProp('onclick');
	    $(this).unbind('click')
	    
        $(this).click(function(e)
        {
            e.preventDefault();
            if(opts.showAlert)
            {
                alert(opts.message);   
            }
        });
   
	});
	
	return this;
};

$.getBoolean = function(variable) 
{
    var vtype;
    var toReturn;

    if(variable != null) 
    {
        switch(typeof(variable)) 
        {
            case 'boolean':
                vtype = "boolean";
                return variable;
                break;
        
            case 'number':
                vtype = "number";
                if(variable == 0)
                    toReturn = false;
                else toReturn = true;
                break;
        
            case 'string':
                vtype = "string";
                if(variable == "true")
                    toReturn = true;
                else if(variable == "false")
                    toReturn = false;
                else if(variable.length > 0)
                    toReturn = true;
                else if(variable.length == 0)
                    toReturn = false;
                break;

        }
        
        return toReturn; 
    }
}  

$.fn.addParameterToLink = function(options) 
{
	var defaults = 
    {

    };
  
    // Extend our default options with those provided.
    var opts = $.extend(defaults, options);
    
    
    
    function updateLink(href)
    {
        if(typeof href !=  'undefined' && 
           href.toLowerCase().indexOf('javascript') < 0 && 
           href.toLowerCase().indexOf('mailto') < 0 && 
           href.toLowerCase().indexOf('void') < 0 && 
           href.toLowerCase().indexOf('#') < 0 && 
           href.indexOf(opts.parameter + '=') < 0)
        {
            return true;
        }
        return false;
    }
    var parameter = opts.parameter + '=' + opts.value;
    
	this.each(function() 
	{
		var link = $(this);

        if(updateLink(link.attr('href')))
        {
            if(link.attr('href').indexOf('?') < 0)
            {
                link.attr('href', link.attr('href') + '?' + parameter);
            }
            else
            {
                link.attr('href', link.attr('href') + '&' + parameter);
            }
        }

	});
};

