/*************************************************************
START: Function to open any link in a brand new pop-up window
*************************************************************/
function MM_openBrWindow(theURL, winName, features) {
    pop = window.open(theURL, winName, features);
    if (pop == null)
        alert('Unable to display pop-up window. Please disable any pop-up blockers.\n\nSee About Us / FAQs for more information.');
    else
        pop.focus();
}
/*************************************************************
END: Function to open any link in a brand new pop-up window
*************************************************************/

/*************************************************************
START: Function to track external links and documents linked directly
*************************************************************/
///<param name="path">Path to log in the GA. This will appear in the reports</param>
function GoogleLog(path) {
    if (path == "") return;
    try {
        pageTracker._trackPageview(path);
        //logUser();
    }
    catch (ex) { }
}
/*************************************************************
END: Function to track external links and documents linked directly
*************************************************************/

/**************************************************************
START: Functions and Variables related to logging the user GUID as a USer Defined Value
***************************************************************/
//XmlHttpRequest object to handle the UserGUID logging asynchronously
var XmlHttp = null;

//Fucntion to create a AJAX request and log the user guid
function logUser() {
    try {
        XmlHttp = new XMLHttpRequest();
    }
    catch (ex) {
        XmlHttp = GetXmlHttpRequestObject();
    }

    //If the HTTPRequest object is failed to create, then do not even attempt to get the user guid
    if (XmlHttp == null)
        return;
    
    //generating a ramdomnumber to avoid output caching of the user identity webpage
	var dt = new Date();
	var dynamicRandomValue = dt.getHours() + "." + dt.getMinutes() + "." + dt.getSeconds() + "." + dt.getMilliseconds() + "." + Math.random();
	var appName = window.location.pathname.split('/')[1].toLowerCase();
	try {
	    XmlHttp.open("GET", "/" + appName + "/Brochure/GetUserGUID.ashx?param=" + dynamicRandomValue, true);
	    XmlHttp.onreadystatechange = setGoogleUser;
	    XmlHttp.send(null);
	}
	catch (ex) { 
	    //Needs to be logged or it can be skipped?
	}
}

//Tries to create a valid XMlHttpRequest object if any is available
function GetXmlHttpRequestObject() {
    var httpRequestObj = new Array();
    httpRequestObj.push('Msxml2.XMLHTTP');
    httpRequestObj.push('Msxml2.XMLHTTP.5.0');
    httpRequestObj.push('Msxml2.XMLHTTP.4.0');
    httpRequestObj.push('Msxml2.XMLHTTP.3.0');
    httpRequestObj.push('Microsoft.XMLHTTP');

    for (var i = 0; i < httpRequestObj.length; i++) {
        try {
            return new ActiveXObject(httpRequestObj[i]);
        }
        catch (ex) { }
    }
    return null;
}

//function to handle when the AJAX request returns the user guid's value
//this logs the user guid to GA (Google Analytics) for this page
function setGoogleUser() {
    try {
        /*XmlHTTPRequest object's ReadyStates**/
        //0 Not initialized
        //1 Open
        //2 Sent
        //3 Received
        //4 Loaded
        if (XmlHttp.readyState == 4) {

            /**Valid XmlHttpRequest status to read the data from**/
            //200 - OK
            if (XmlHttp.status == 200) {
                //alert(XmlHttp.responseText);
                pageTracker._setVar(XmlHttp.responseText);
            }
        }
    }
    catch (ex) { }
}
/**************************************************************
END: Function related to logging the user GUID as a USer Defined Value
***************************************************************/


/**************************************************************
START: Function to track a file download only for the files hosted in our server
***************************************************************/
///<Summary>
///Function to log a file download linked only from our server
///<Summary>
function logFileDownload(hrefPath) {
    hrefPath = hrefPath.toLowerCase();
    var logPath = "/files";
    var protocol = window.location.protocol		//http or https
    var servername = window.location.hostname;		//DEVWEB or TESTWEB or WWW.IHI.ORG
    var relativeFilePath = ''; 				//relative path of the current page
    var relPathName = window.location.pathname; 	//holds the relative location of the current page

    var tempLen = relPathName.length;
    //the relative path sometimes does not ends with a back slash. If it does not, add a back slash at the end
    if (relPathName.substring(tempLen - 4, tempLen).toLowerCase() != ".htm" && relPathName.substring(tempLen - 1, tempLen) != "/")
        relPathName = relPathName + "/";

    //prepare the relative path on the server for the current page
    var filePath = relPathName.split('/');
    for (i = 1; i < filePath.length - 1; i++) {
        relativeFilePath = relativeFilePath + "/" + filePath[i].toLowerCase();
    }

    var origUrl = '';
    //if the href url contains gateway.aspx, then get the original url of the target file
    if (hrefPath.indexOf("gateway.aspx?") != -1 || hrefPath.indexOf("download.aspx?") != -1) {
        var paramName = 'target';
        if (hrefPath.indexOf("download.aspx?") != -1)
            paramName = 'file';
    
        var temp = hrefPath.substr(hrefPath.indexOf("?"));

        if (temp.indexOf('&') != -1) {
            //Multiple parameters
            paramsArr = temp.split('&');
            for (i = 0; i < paramsArr.length; i++) {
                var tempArr = paramsArr[i].split('=');
                if (tempArr[0] == paramName) {
                    origUrl = tempArr[1]; 
                    hrefPath = unescape(tempArr[1]); 
                }
            }
        }
        else {
            //single parameter
            var tempArr = temp.split('=');
            origUrl = tempArr[1];
            hrefPath = unescape(tempArr[1]);
        }
    }
    
    //Logging a link to a media file hosted on Akamai
    if (hrefPath.indexOf("/ihi.download.akamai.com/") != -1) {
        //Akamai media file link
        pageTracker._trackPageview(logPath + "/akamai" + hrefPath.substr(hrefPath.lastIndexOf("/49884/") + 6));
        return;
    }
    
    //specified whether it is a relative url or a complete url
    var isFullURL = (hrefPath.indexOf('//') != -1);

    //full path of the url is mentioned or a relative path is given
    if (isFullURL) {
        //only log if the resource is linked only to an internal resource. Do not log any of the outside resources
        if (hrefPath.indexOf(servername) != -1) {

            //if it is a link directly to a resource in the Resource Manager, 
            //then log the resource with the relative path of the current page
            if (hrefPath.toLowerCase().indexOf("/nr/rdonlyres/") == -1) {
                var tempArr = hrefPath.substr(hrefPath.indexOf(servername), hrefPath.length).split('/');

                //form the logging url for the resource
                for (i = 1; i < tempArr.length; i++)
                    logPath = logPath + "/" + tempArr[i];

                //finally log the entry
                pageTracker._trackPageview(logPath);
            }
            else {
                //if it is a link to CMS resource 
                var len = hrefPath.split('/').length;

                //Log the entry with the URL's relative path with the file name on the end
                pageTracker._trackPageview(logPath + relativeFilePath + "/" + hrefPath.split('/')[len - 1].toLowerCase());
            }
        }
    }
    else if (hrefPath.substr(0, 1) == "/") {
        if (hrefPath.toLowerCase().indexOf("/nr/rdonlyres/") == 0) {
            //if it is a link to CMS resource
            var len = hrefPath.split('/').length;

            //Log the entry with the URL's relative path with the file name on the end
            pageTracker._trackPageview(logPath + relativeFilePath + "/" + hrefPath.split('/')[len - 1].toLowerCase());
        }
        else {
            //direct link to an existing file on the file system
            pageTracker._trackPageview(hrefPath);
        }
    }
}
/**************************************************************
END: Function to track a file download only for the files hosted in our server
***************************************************************/

/**************************************************************
START: Function to check for support for silverlight on client's machine
***************************************************************/
function checkSilverlight() {
    var hasSilverlight = Boolean(window.Silverlight);
    var hasSilverlight1 = hasSilverlight && Silverlight.isInstalled('1.0');
    var hasSilverlight2 = hasSilverlight && Silverlight.isInstalled('2.0');
    if (hasSilverlight2) {
        //Silverlight 2.0 is installed
        pageTracker._trackPageview("/browserfeatures/silverlight/v2.0");
    }
    else if (hasSilverlight1) {
        //Silverlight 1.0 is installed
        pageTracker._trackPageview("/browserfeatures/silverlight/v1.0");
    }
    else if (hasSilverlight) {
        //No Silverlight installed
        pageTracker._trackPageview("/browserfeatures/silverlight/nosupport");
    }
}
/**************************************************************
END: Function to check for support for silverlight on client's machine
***************************************************************/
