﻿var st_utilities = {
        included_files: new Array(),
        PRODUCTION: "production",
        STAGE: "stage",
        DEV: "dev",
        LOCALHOST: "localhost"
};


/**
  * Adds a trim function to the standard javascript string
  * @return returns the trimmed string
  */
String.prototype.trim = function ()
{
    return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
};

/**
 * Includes a javescript source file in the curernt page so that its functions
 * can be accessed
 * @param script_filename the url of the js file to include
 */
st_utilities.include = function(script_filename)
{
    // If the given script has already been included, return false
    for (var i = 0; i < st_utilities.included_files.length; i++)
    {
        if (st_utilities.included_files[i] == script_filename) return false;
    }

    // Otherwise, include the script
    var head = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_filename);
    head.appendChild(js);

    // Record inclusion for future reference and return true
    st_utilities.included_files[st_utilities.included_files.length] = script_filename;
    return true;
}

/**
 * Add the given function to the list of functions being called on page load.
 * @param new_function a reference to the function to be called on load
 */
st_utilities.addOnLoad = function(new_function)
{
    var oldOnLoad = window.onload;
    window.onload = function ()
    {
        if(typeof oldOnLoad == 'function') oldOnLoad();
        new_function();
    }
}

/**
 * Gets the real left relative to the body
 * @param element a reference to the DOM element or it's id
 * @return the x position of the left of the element in pixels
 */
st_utilities.getAbsoluteLeft = function (element)
{
    if (typeof element == 'string')
        element = document.getElementById(element);
    var xPos = element.offsetLeft;
    var tempelement = element.offsetParent;
    while (tempelement != null) {
        xPos += tempelement.offsetLeft;
        tempelement = tempelement.offsetParent;
    }
    return xPos;
}

/**
 * Gets the real top relementative to the body
 * @param element a reference to the DOM element or it's id
 * @return the y position of the top of the element in pixels
 */
st_utilities.getAbsoluteTop = function (element)
{
    if (typeof element == 'string')
        element = document.getElementById(element);
    var yPos = element.offsetTop;
    var tempelement = element.offsetParent;
    while (tempelement != null) {
        yPos += tempelement.offsetTop;
        tempelement = tempelement.offsetParent;
    }
    return yPos;
}

/**
 * Gets the height of the current page in pixels
 * @return the height of the current page in pixels
 */
st_utilities.getPageHeight = function()
{
    if( window.innerHeight && window.scrollMaxY ) // Firefox
    {
        return window.innerHeight + window.scrollMaxY;
    }
    else if( document.body.scrollHeight > document.body.offsetHeight ) // all but Explorer Mac
    {
        return document.body.scrollHeight;
    }
    else // works in Explorer 6 Strict, Mozilla (not FF) and Safari
    {
        return document.body.offsetHeight + document.body.offsetTop;
    }
}

/**
 * Gets the width of the current page
 * @return the width of the current page in pixels
 */
st_utilities.getPageWidth = function()
{
    if( window.innerHeight && window.scrollMaxY ) // Firefox
    {
        return window.innerWidth + window.scrollMaxX;
    }
    else if( document.body.scrollHeight > document.body.offsetHeight ) // all but Explorer Mac
    {
        return document.body.scrollWidth;
    }
    else // works in Explorer 6 Strict, Mozilla (not FF) and Safari
    {
        return document.body.offsetWidth + document.body.offsetLeft;
    }
}

/**
 * Gets the height of the iframe frame in pixels
 * @param frame a reference to the frame or the id of the frame whose size to get
 * @return the height of frame in pixels
 */
st_utilities.getFramePageHeight = function(frame)
{
    if (typeof frame == 'string')
        frame = document.getElementById(frame);

    if( frame.contentDocument ) // Firefox
        return frame.contentDocument.body.scrollHeight;

    if (frame.contentWindow.document && frame.contentWindow.document.body.scrollHeight && frame.contentWindow.document.body.scrollHeight > frame.contentWindow.document.body.offsetHeight) //ie5+ syntax
        return frame.contentWindow.document.body.scrollHeight;

    return frame.contentWindow.document.body.offsetHeight + frame.contentWindow.document.body.offsetTop;

}

st_utilities.getParentIFrame = function()
{
    var iframes = top.document.getElementsByTagName('iframe');
    for (var i=0; i < iframes.length; i++) {
        var iframe = iframes[i];
        var doc = iframe.contentDocument || iframe.contentWindow;
        if (doc == document)
            return iframe;
    }
    return undefined;
}

st_utilities.callParentIFrameOnload = function()
{
    if (top != window) {
        var iframe = st_utilities.getParentIFrame();
        if (iframe.onload)
            iframe.onload();
    }
}


/**
 * Resizes the height of frame to fit the contained page
 * @param frame the frame to resize or its id
 */
st_utilities.resizeIFrameToFitPageHeight = function(frame)
{
    if (typeof frame == 'string')
        frame = document.getElementById(frame);
    var height = st_utilities.getFramePageHeight(frame);

    if ( frame.contentDocument)
        frame.height = height;
    else frame.style.height = height;


}

/**
 * Sets the element's display to "block".
 * @param element a reference to the DOM element, or a string with the
 * id of the element to show
*/
st_utilities.show = function (element)
{
    if ( element.style != undefined)
    {
        element.style.display = "block";
    }
    else if (typeof element == 'string')
    {
        var el = document.getElementById(element)
        if ( el != undefined)
            el.style.display = "block";
    }
}

/**
 * Sets the element's display to "none".
 * @param element a reference to the DOM element, or a string with the
 * id of the element to show
*/
st_utilities.hide = function (element)
{
    if ( element.style != undefined)
    {
        element.style.display = "none";
    }
    else if (typeof element == 'string')
    {
        var el = document.getElementById(element)
        if ( el != undefined)
            el.style.display = "none";
    }
}


/**
 * Hides all direct children of the element with the given id or reference
 * @param el the id of the element or a reference to the element whose children to hide
*/
st_utilities.hideChildren = function (el)
{
    if (typeof el == 'string')
        el = document.getElementById(el);

    if (el == undefined)
        return;
    var children = el.childNodes;
    if (children == undefined)
        return;
    for (i = 0; i < children.length; i++)
    {
        var child = children[i];
        st_utilities.hide(child);
    }
}

/**
 * Changes the class of an element given its id or a reference to it
 * @param el the id of the element or a reference to the element whose class
 *      to change
 * @param className the name of the class to change the element's class to
 */
st_utilities.changeClass = function(el, className)
{
    if (typeof el == 'string')
        el = document.getElementById(el);
    if (el == undefined)
	    return;
    el.className = className;
}

/**
 * Changes CSS style of all direct children of the element with the given id or reference to class with name childClassName
 * @param el the id of the element or a reference to the element whose children you want to change
 * @param childClassName the name of the CSS class that you want to assign to the child element
*/
st_utilities.changeChildClass =  function (el, childClassName)
{
    if (typeof el == 'string')
        el = document.getElementById(el);
    if (el == undefined)
        return;
    var children = el.childNodes;
    if (children == undefined)
        return;
    for (i = 0; i < children.length; i++)
    {
        var child = children[i];
        if (child.className != undefined)
        {
            child.className = childClassName;
        }
    }
}

/**
 * returns an associative array of all query string parameters.
 * keys or parameters containing '+' are normalized to ' '
 */
st_utilities.getQueryMap = function ()
{
    var arr = new Array();
    var qs = location.search.substring(1, location.search.length);
    if (qs.length == 0)
        return arr;
    qs = qs.replace(/\+/g, ' ');
    var args = qs.split('&');
    for (var i=0; i < args.length; i++)
    {
        var pair = args[i].split('=');
        var key = unescape(pair[0]);
        arr[unescape(pair[0])] = (pair.length == 2) ? pair[1] : null;
    }
    return arr;
}


//////////////////// WINDOW FUNCTIONS /////////////////////////////////////////////
/*
a good size for a lrg window is 690x480
scr, menu, stat, tool are set as either 0 (no) or 1 (yes)
name can be set to 0 as well unless you specifically need to have it named
*/

/**
 * Opens a popup window. Parameters are passes as a closure
 * @param url the url to display in the popup, REQUIRED
 * @param name the name for the popup window. Defaults to "_blank"
 * @param width the width of the window. Should not be used if widthPct is used
 * @param height the height of the window. Should not be used if heightPct is used
 * @param widthPct the percentage of the screen width to make the window width. Should not be used if width is used
 * @param heightPct the percentage of the sceen height to the the window height. Should not be uses if height is used
 * @param top the top coordinate of the window in pixels. Defaults to 0
 * @param left the left coordinate of the window in pixels. Defaults to 0
 * @param scrollbars whether or not to display scrollbars. Defautls to yes
 * @param resizable whether or not the window is to be resizable. Defaults to yes
 * @param menubar whether or not to display the menubar. Defaults to no
 * @param status whether or not to display a status bar. Defaults to no
 * @param toolbar whether or not to display a toolbar. Defaults to no
 * @return returns a reference to the window
 */
st_utilities.openWin = function (args)
{
    if ( !args.name )
        args.name = "_blank";

    if ( !args.resizable)
        args.resizable = "yes";
    var params = "resizable=" + args.resizable;

    if ( args.width )
        params += ",width=" + args.width;
    else if ( args.widthPct )
        params += ",width=" + (screen.availWidth * args.widthPct);

    if ( args.height )
        params += ",height=" + args.height;
    else if (args.heightPct )
        params += ",height=" + (screen.availHeight * args.heightPct);

    params += ",scrollbars=";
    if ( args.scrollbars )
        params += args.scrollbars;
    else params += "yes";

    params += ",menubar=";
    if ( args.menubar )
        params += args.menubar;
    else params += "no";

    params += ",status=";
    if ( args.status )
        params += args.status;
    else params += "no";

    params += ",toolbar=";
    if ( args.toolbar )
        params += args.toolbar;
    else params += "no";

    params += ",top=";
    if ( args.top )
        params += args.top;
    else params += "0";

    params += ",left=";
    if ( args.left )
        params += args.left;
    else params += "0";

    var win = window.open(args.url, args.name, params);
    if ( win )
        win.focus();

    return win;

}
/////////////////// END WINDOW FUNCTIONS /////////////////////////////////////////////////


////////////////// XMLHTTP/AJAX FUNCTIONS //////////////////////////////////////////////
/** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08        **
 ** Code licensed under Creative Commons Attribution-ShareAlike License      **
 ** http://creativecommons.org/licenses/by-sa/2.0/                           **/
/**
  * XMLHttp request wrapper method. Based on XHConn.
  * Takes in a CLOSURE containing the following parameters:
  * @param url the url to access. REQUIRED
  * @param method the HTTP method to use. GET and POST are supported. Defaults to GET
  * @param onLoad a callback function for when the url is successfully loaded
  * @param postQueryString the query string to use when making a POST request.
  *         This should be left out when making a GET request
  * @param onError a callback function for when the url is not successfully loaded
  * @return true if there are no unexpected errors, false otherwise
  *
  * Example:
  * var fnOnLoad = function(xmlhttpobj) {alert(xmlhttpobj.responseText);};
  * st_utilities.xmlHttp({url : 'http://www.google.com' , onLoad : fnOnLoad});
  */
st_utilities.xmlHttp = function(args)
{
    var xmlhttp, bComplete = false;
    if (!args)
        return false;

    if ( !args.url)
        return false;
    try
    {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
        try
        {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                xmlhttp = new XMLHttpRequest();
            }
            catch (e)
            {
                xmlhttp = false;
            }
        }
    }

    if (!xmlhttp) return false;

    bComplete = false;
    if ( !args.method)
        args.method = "GET";
    else args.method = args.method.toUpperCase();

    try
    {
        if (args.method == "GET")
        {
            xmlhttp.open(args.method, args.url, true);
            args.postQueryString = "";
        }
        else
        {
            xmlhttp.open(args.method, args.url, true);
            xmlhttp.setRequestHeader("Method", "POST " + args.url
                    + " HTTP/1.1");
            xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        }

        xmlhttp.onreadystatechange = function()
        {
            if (xmlhttp.readyState == 4 && !bComplete)
            {
                bComplete = true;
                if ( xmlhttp.status == 200)
                {
                    if (args.onLoad) args.onLoad(xmlhttp);
                }
                else
                {
                    if (args.onError) args.onError(xmlhttp);
                }
            }
        };
        xmlhttp.send(args.postQueryString);
    }
    catch(z)
    {
        return false;
    }
    return true;
}

/**
 * Replaces innerHTML of element with contents of url
 * @param url the url to load into the element
 * @param element either the id of the element or a reference to the element
 *      in which to write the loaded html
 */
st_utilities.ajaxElement = function(url, element)
{
    if (typeof element == 'string')
        element = document.getElementById(element);

    var onLoad = function(ret) { if (element) {element.innerHTML = ret.responseText;} };
    st_utilities.xmlHttp({url : url, onLoad : onLoad});
}

////////////////// END XMLHTTP/AJAX FUNCTIONS //////////////////////////////////////////////


///////////////////////////LEGACY FUNCTIONS ///////////////////////////////////

function openWin(file,w,h,scr,menu,stat,tool,name)
{
	var win = window.open(file,name,"scrollbars="+scr+",resizable=yes,menubar="+menu+",status="+stat+",toolbar="+tool+",width="+w+",height="+h+",screenx=0,screeny=0,top=0,left=0");
    if ( win )
        win.focus();
}

/*
This code resizes the popup window to height and width defined by
the available screen resolution. This can be a percentage of the screen
by using the aw, ah variables.
*/
function openWinRes(file,wpct,hpct,scr,menu,stat,tool,name)
{

    var screenRes = getScreenRes(80);
    var width;
    var height;

    if ( screenRes == "1280x1024")
    {
      width = 1200;
      height = 500;
    }
    else if ( screenRes == "1152x864")
    {
      width = 1072;
      height = 500;
    }
    else if ( screenRes == "1024x768")
    {
      width = 944;
      height = 500;
    }
    else
    {
      width = 740;
      height = 500;
    }

    var aw = screen.availWidth * wpct;
    var ah = screen.availHeight * hpct;
    var winRes = window.open(file,name,"scrollbars="+scr+",resizable=yes,menubar="+menu+",status="+stat+",toolbar="+tool+",width="+width+",height="+height+",screenx=0,screeny=0,top=0,left=0");
    winRes.focus();
}


function getScreenRes(errorMargin)
{
    var screenX = screen.availWidth;

    if ( screenX >= (1280-errorMargin))
        return "1280x1024";

    if (screenX >= (1152-errorMargin))
        return "1152x864";

    if ( screenX >= (1024-errorMargin))
        return "1024x768";

    return "800x600";

}

//////////////////////// END LEGACY FUNCTIONS ///////////////////////////////////

///////////////////////// INCLUDED SWF OBJECT ///////////////////////////////////
/**
 * SWFObject v1.5: Flash Player detection and embed - http://blog.deconcept.com/swfobject/
 *
 * SWFObject is (c) 2007 Geoff Stearns and is released under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 */
if(typeof deconcept=="undefined"){var deconcept=new Object();}if(typeof deconcept.util=="undefined"){deconcept.util=new Object();}if(typeof deconcept.SWFObjectUtil=="undefined"){deconcept.SWFObjectUtil=new Object();}deconcept.SWFObject=function(_1,id,w,h,_5,c,_7,_8,_9,_a){if(!document.getElementById){return;}this.DETECT_KEY=_a?_a:"detectflash";this.skipDetect=deconcept.util.getRequestParameter(this.DETECT_KEY);this.params=new Object();this.variables=new Object();this.attributes=new Array();if(_1){this.setAttribute("swf",_1);}if(id){this.setAttribute("id",id);}if(w){this.setAttribute("width",w);}if(h){this.setAttribute("height",h);}if(_5){this.setAttribute("version",new deconcept.PlayerVersion(_5.toString().split(".")));}this.installedVer=deconcept.SWFObjectUtil.getPlayerVersion();if(!window.opera&&document.all&&this.installedVer.major>7){deconcept.SWFObject.doPrepUnload=true;}if(c){this.addParam("bgcolor",c);}var q=_7?_7:"high";this.addParam("quality",q);this.setAttribute("useExpressInstall",false);this.setAttribute("doExpressInstall",false);var _c=(_8)?_8:window.location;this.setAttribute("xiRedirectUrl",_c);this.setAttribute("redirectUrl","");if(_9){this.setAttribute("redirectUrl",_9);}};deconcept.SWFObject.prototype={useExpressInstall:function(_d){this.xiSWFPath=!_d?"expressinstall.swf":_d;this.setAttribute("useExpressInstall",true);},setAttribute:function(_e,_f){this.attributes[_e]=_f;},getAttribute:function(_10){return this.attributes[_10];},addParam:function(_11,_12){this.params[_11]=_12;},getParams:function(){return this.params;},addVariable:function(_13,_14){this.variables[_13]=_14;},getVariable:function(_15){return this.variables[_15];},getVariables:function(){return this.variables;},getVariablePairs:function(){var _16=new Array();var key;var _18=this.getVariables();for(key in _18){_16[_16.length]=key+"="+_18[key];}return _16;},getSWFHTML:function(){var _19="";if(navigator.plugins&&navigator.mimeTypes&&navigator.mimeTypes.length){if(this.getAttribute("doExpressInstall")){this.addVariable("MMplayerType","PlugIn");this.setAttribute("swf",this.xiSWFPath);}_19="<embed type=\"application/x-shockwave-flash\" src=\""+this.getAttribute("swf")+"\" width=\""+this.getAttribute("width")+"\" height=\""+this.getAttribute("height")+"\" style=\""+this.getAttribute("style")+"\"";_19+=" id=\""+this.getAttribute("id")+"\" name=\""+this.getAttribute("id")+"\" ";var _1a=this.getParams();for(var key in _1a){_19+=[key]+"=\""+_1a[key]+"\" ";}var _1c=this.getVariablePairs().join("&");if(_1c.length>0){_19+="flashvars=\""+_1c+"\"";}_19+="/>";}else{if(this.getAttribute("doExpressInstall")){this.addVariable("MMplayerType","ActiveX");this.setAttribute("swf",this.xiSWFPath);}_19="<object id=\""+this.getAttribute("id")+"\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" width=\""+this.getAttribute("width")+"\" height=\""+this.getAttribute("height")+"\" style=\""+this.getAttribute("style")+"\">";_19+="<param name=\"movie\" value=\""+this.getAttribute("swf")+"\" />";var _1d=this.getParams();for(var key in _1d){_19+="<param name=\""+key+"\" value=\""+_1d[key]+"\" />";}var _1f=this.getVariablePairs().join("&");if(_1f.length>0){_19+="<param name=\"flashvars\" value=\""+_1f+"\" />";}_19+="</object>";}return _19;},write:function(_20){if(this.getAttribute("useExpressInstall")){var _21=new deconcept.PlayerVersion([6,0,65]);if(this.installedVer.versionIsValid(_21)&&!this.installedVer.versionIsValid(this.getAttribute("version"))){this.setAttribute("doExpressInstall",true);this.addVariable("MMredirectURL",escape(this.getAttribute("xiRedirectUrl")));document.title=document.title.slice(0,47)+" - Flash Player Installation";this.addVariable("MMdoctitle",document.title);}}if(this.skipDetect||this.getAttribute("doExpressInstall")||this.installedVer.versionIsValid(this.getAttribute("version"))){var n=(typeof _20=="string")?document.getElementById(_20):_20;n.innerHTML=this.getSWFHTML();return true;}else{if(this.getAttribute("redirectUrl")!=""){document.location.replace(this.getAttribute("redirectUrl"));}}return false;}};deconcept.SWFObjectUtil.getPlayerVersion=function(){var _23=new deconcept.PlayerVersion([0,0,0]);if(navigator.plugins&&navigator.mimeTypes.length){var x=navigator.plugins["Shockwave Flash"];if(x&&x.description){_23=new deconcept.PlayerVersion(x.description.replace(/([a-zA-Z]|\s)+/,"").replace(/(\s+r|\s+b[0-9]+)/,".").split("."));}}else{if(navigator.userAgent&&navigator.userAgent.indexOf("Windows CE")>=0){var axo=1;var _26=3;while(axo){try{_26++;axo=new ActiveXObject("ShockwaveFlash.ShockwaveFlash."+_26);_23=new deconcept.PlayerVersion([_26,0,0]);}catch(e){axo=null;}}}else{try{var axo=new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");}catch(e){try{var axo=new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");_23=new deconcept.PlayerVersion([6,0,21]);axo.AllowScriptAccess="always";}catch(e){if(_23.major==6){return _23;}}try{axo=new ActiveXObject("ShockwaveFlash.ShockwaveFlash");}catch(e){}}if(axo!=null){_23=new deconcept.PlayerVersion(axo.GetVariable("$version").split(" ")[1].split(","));}}}return _23;};deconcept.PlayerVersion=function(_29){this.major=_29[0]!=null?parseInt(_29[0]):0;this.minor=_29[1]!=null?parseInt(_29[1]):0;this.rev=_29[2]!=null?parseInt(_29[2]):0;};deconcept.PlayerVersion.prototype.versionIsValid=function(fv){if(this.major<fv.major){return false;}if(this.major>fv.major){return true;}if(this.minor<fv.minor){return false;}if(this.minor>fv.minor){return true;}if(this.rev<fv.rev){return false;}return true;};deconcept.util={getRequestParameter:function(_2b){var q=document.location.search||document.location.hash;if(_2b==null){return q;}if(q){var _2d=q.substring(1).split("&");for(var i=0;i<_2d.length;i++){if(_2d[i].substring(0,_2d[i].indexOf("="))==_2b){return _2d[i].substring((_2d[i].indexOf("=")+1));}}}return "";}};deconcept.SWFObjectUtil.cleanupSWFs=function(){var _2f=document.getElementsByTagName("OBJECT");for(var i=_2f.length-1;i>=0;i--){_2f[i].style.display="none";for(var x in _2f[i]){if(typeof _2f[i][x]=="function"){_2f[i][x]=function(){};}}}};if(deconcept.SWFObject.doPrepUnload){if(!deconcept.unloadSet){deconcept.SWFObjectUtil.prepUnload=function(){__flash_unloadHandler=function(){};__flash_savedUnloadHandler=function(){};window.attachEvent("onunload",deconcept.SWFObjectUtil.cleanupSWFs);};window.attachEvent("onbeforeunload",deconcept.SWFObjectUtil.prepUnload);deconcept.unloadSet=true;}}if(!document.getElementById&&document.all){document.getElementById=function(id){return document.all[id];};}var getQueryParamValue=deconcept.util.getRequestParameter;var FlashObject=deconcept.SWFObject;var SWFObject=deconcept.SWFObject;
////////////////////////// END INCLUDED SWF OBJECT //////////////////////////////