﻿// trims leading and trailing spaces from a string
function trim(s)
{
	while (s.substring(0, 1) == ' ')
	{
		s = s.substring(1, s.length);
	}
	
	while (s.substring(s.length-1, s.length) == ' ')
	{
		s = s.substring(0, s.length-1);
	}

	return s;
}

// retrieves a value of a query string key
function queryString(key)
{
	var args = new Object();
	var query = location.search.substring(1); 
	var pairs = query.split("&"); 
	
	for(var i = 0; i < pairs.length; i++)
	{
		var pos = pairs[i].indexOf('='); 

		if (pos == -1)
			continue;
		
		var argname = pairs[i].substring(0,pos); 	
		var value = pairs[i].substring(pos+1); 
	
		if (trim(argname) == trim(key))
			return trim(value);
	}

	return "";
}


// Client side postback functionality
if (window.ActiveXObject && !window.XMLHttpRequest)
{
    window.XMLHttpRequest = function()
    {
        return new ActiveXObject((navigator.userAgent.toLowerCase().indexOf('msie 5') != -1) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP');
    };
}

function getHttpResponse(sURL)
{
	try
	{
		var oXMLHTTP = new XMLHttpRequest();

		oXMLHTTP.open("GET", sURL, false);
		oXMLHTTP.setRequestHeader("content-type", "application/x-www-form-urlencoded");
		oXMLHTTP.setRequestHeader("content-length", 0);
		oXMLHTTP.send("");
	
		if (oXMLHTTP.readyState < 4 || (oXMLHTTP.status != 0 && oXMLHTTP.status != 200 && oXMLHTTP.status != 201))
		{
			alert("Client Side Postback Error: Postback to URL " + trim(sURL) + " has failed.");
			return;
		}
		else if (oXMLHTTP.responseText != null && trim(oXMLHTTP.responseText) != "")
		{
			return oXMLHTTP.responseText;
		}
	}
	catch(e)
	{
	}
}

// help window functions
function openMediaMatrixHelp()
{
    var helpWindow = window.open("http://plus.useplus.org/IS_more_info_msc.htm", "Help", "top=50,left=350,width=685,height=720,scrollbars=1,status=0");

	if (helpWindow && helpWindow.open)
	{
		helpWindow.focus();
	}
	else
	{
		alert("Your browser's pop-up blocker is preventing the term window from displaying on your screen. " +
	    	"Please set your pop-up blocker to allow this site to open the window.");
    }    
}

function openMediaMatrixInformation()
{
    var helpWindow = window.open("../Help/Definitions.html?aMediaMatrix", "Help", "top=50,left=350,width=685,height=720,scrollbars=1,status=0");

	if (helpWindow && helpWindow.open)
	{
		helpWindow.focus();
	}
	else
	{
		alert("Your browser's pop-up blocker is preventing the term window from displaying on your screen. " +
	    	"Please set your pop-up blocker to allow this site to open the window.");
    }    
}
