﻿/**********************************************************
 Initialize OSI library and miscellaneous low-level functions
**********************************************************/
if (typeof(google) != "undefined") {
    // Load using https
    // Load most recent 1.5.x version of jQuery
    google.load("jquery", "1.5");
    // Load most recent 1.8.x version of jQueryUI
    google.load("jqueryui", "1.8");
    // Load most recent 3.3.x version of YUI
    google.load("yui", "3.3");
}

// The YUI global object is declared in OSI.RSWebNET.js right
// before its first usage. This allows time for the YUI library 
// to be loaded.

// String
// Add functionality to String object.
/********************************************************************
Remove whitespace from begining of string.
********************************************************************/
String.prototype.ltrim = function()
{
	//Match spaces at beginning of text and replace with a null string
	return this.replace(/^\s+/,'');
}
/********************************************************************
Remove trailing whitespace from string.
********************************************************************/
String.prototype.rtrim = function()
{
	//Match spaces at end of text and replace with a null string
    return this.replace(/\s+$/,'');
}
/********************************************************************
Remove leading and trailing whitespace from string.
********************************************************************/
String.prototype.trim = function()
{
    return this.ltrim().rtrim();
}

/********************************************************************
String format function similar to way Format works in c#.
Based on http://community.hdri.net/blogs/ray_blog/archive/2006/02/27/5.aspx
Example:
	var firstName = 'Ray';
	var lastName = 'Houston';
	
	var hello = String.format('Hello. My name is {0} {1}.', firstName, lastName);
outputs
	Hello. My name is Ray Houston.	
********************************************************************/
String.format = function()
{
	if (arguments.length == 0)
		return null;

	var str = arguments[0];

	for (var i=1;i<arguments.length;i++)
	{
		var re = new RegExp('\\{' + (i-1) + '\\}','gm');

		str = str.replace(re, arguments[i]);
	}

	return str;
}

/*******************************************************************************
 Represents an empty string.
 Example:
    var emptyString = String.empty;
*******************************************************************************/
String.empty = "";


/*
    See http://constc.blogspot.com/2008/07/undeclared-undefined-null-in-javascript.html
*/
/**
* Is specified object undefined?
*/
function isUndefined(obj) {
    var u; // undefined variable

    return obj === u;
}
/**
* Is the specified object have a VALUE of null or is undefined?
*/
function isNull(obj) { return obj == null; }
/**
* Is the specified object non-null and defined?
*/
function isNotNull(obj) { return obj != null; }
/**
* Does the specified object have a VALUE of null?
* 
*/
function isNullValue(obj) {
    return isNull(obj) && !isUndefined(obj);
}

function isBoolean(obj) { return typeof(obj) == "boolean"; }
function isFunction(obj) { return typeof(obj) == "function"; }
function isNumber(obj) { return typeof(obj) == "number"; }
function isString(obj) { return typeof(obj) == "string"; }

// Create OSI "namespace"
function OSI() {
}

OSI.addStylesheet = function(href) {
    // jQuery not initialized yet
    var headID = document.getElementsByTagName("head")[0];         
    var cssNode = document.createElement('link');
    cssNode.type = 'text/css';
    cssNode.rel = 'stylesheet';
    cssNode.href = href;
    headID.appendChild(cssNode);
}

OSI.addScriptLink = function(src) {
    // jQuery not initialized yet
    var scripts = document.getElementsByTagName("script");
    for (var i=0; i<scripts.length; i++) {
        if (scripts[i].src.match(src))
            // Already included
            return ;
    }
    document.write('<script src="' + src +
        '" type="text/javascript"></script>');
}

// Set jQuery UI theme
// http://blog.jqueryui.com/2010/05/jquery-ui-181/
if (isNull(OSI.jqueryui_theme))
    if (typeof(google) != "undefined") 
        // Default theme
        OSI.jqueryui_theme = "https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/cupertino/jquery-ui.css";
    else
        OSI.jqueryui_theme = "scripts/jqueryui/1.8.1/themes/cupertino/jquery-ui.css";
        
OSI.addStylesheet(OSI.jqueryui_theme);
OSI.addScriptLink("scripts/OSI.js");
OSI.addScriptLink("scripts/OSI.Modal.js");
// Always include logging component
OSI.addScriptLink("scripts/OSI.Debug.Logging.js");


