String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, '');
};

sharedScripts = new Object();
sharedScripts.contentSearch = function(searchString) {
    if (searchString.length) {
        window.location = '?page=search&searchString=' + searchString;
    };
};

// Write a script tag to the page while the page is loading 
// - Caution - potential for timing issues
// - Caution - if called after the page is loaded will overwrite the existing document content
sharedScripts.writeScript = function(src) {

	document.write('<script src="' + src + '" type="text/JavaScript"><\/script>');

};

// Add a script tag to the page after it has loaded
// - Caution - potential for timing issues
sharedScripts.addScript = function(src) {
   var head = document.getElementsByTagName("head")[0];
   script = document.createElement('script');
   script.type = 'text/javascript';
   script.src = src;
   head.appendChild(script);
};

// Syncronous XMLHttpRequest to load script content
// Caution - cross site limitations - an XMLHttpRequest is being made to request the .js file content
sharedScripts.loadScript = function(src) {
	
	var request = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
	request.open("GET", src, false);
	request.send(null);
	
	var head = document.getElementsByTagName("head")[0];
	script = document.createElement('script');
	script.type = 'text/javascript';
	script.text = request.responseText;
	head.appendChild(script);	
	
};

//TODOD - create ajax class and move this into it
function hostpath() {
	var href = window.location.href;
	var d = href.match('^(ftp|http|https|file)://([^/]+)(/.*)?(/.*)')[4];
	//alert(d);
	if (d!='/') {
		var href = href.replace(d, '') + '/';	
	};
	//alert(href);
		
	//href = window.location.href.replace(window.location.hash, '').replace(window.location.search, '').replace('default.aspx', '');
	//alert(href);
	
	return href;
};

var popupTitle
var popupHeadline
var popupContent
var popupWindow
	
function trackOutboundLink(a, _event) {
	pageTracker._trackPageview(_event); window.setTimeout(function(){ window.open(a.href); },200);
}		

/* begin - restore scroll position on postback */
window.onscroll = function () { 
	if (document.getElementsByName('scrolly')[0]) {
		var ScrollTop = document.body.scrollTop;
		if (ScrollTop == 0)
		{
			document.getElementsByName('scrollx')[0].value = (document.all) ? document.body.parentElement.scrollLeft : window.pageXOffset; 
			document.getElementsByName('scrolly')[0].value = (document.all) ? document.body.parentElement.scrollTop : window.pageYOffset; 
		}
		window.status = document.getElementsByName('scrollx')[0].value + ',' + document.getElementsByName('scrolly')[0].value;
	};
} 
window.onload = function() { 
	if (document.getElementsByName('scrolly')[0]) {
		window.scrollTo(document.getElementsByName('scrollx')[0].value, document.getElementsByName('scrolly')[0].value);

		window.status = document.getElementsByName('scrollx')[0].value + ',' + document.getElementsByName('scrolly')[0].value;

	};
}
/* end - restore scroll position on postback */

function popup(title,headline,page,querystring,content,height,width,action,bScrollbars) {
  // If the "page" is popupText.aspx, you may include several sentences as "content".
  // If the "page" is default.aspx, the page should use template pageBodyPopup.ascx 
  // to hide side menus.
	
	querystringParams = 'title=' + title + '&headline=' + headline
	if (querystring == '') {
		querystring = '?' + querystringParams
		} else {
		if (querystring.substring(0, 1) != '?') {
		  querystring = '?' + querystring;
		  };
		querystring = querystring + '&' + querystringParams
		}
	var URL = page + querystring
	var scrollbars = 'no';
	var action = '';
	if (bScrollbars) {scrollbars='yes'}
	if (action != '') {
		if (querystring == '') {
			URL += '?action=' + action
		}
		else {
			URL += '&action=' + action
		}
	}
	if ((window.navigator.userAgent.indexOf('Opera') < 0) && (popupWindow != null)) { popupWindow.close() }
	popupTitle = title;
	popupHeadline = headline;
	popupContent = content;
	popupWindow = window.open(URL,'popup','top=45,left=80,width=' + width + ',height=' + (height+30) + ',menubar=no,toolbar=no,scrollbars='+scrollbars,true);
	//popupWindow = null;
	
}

function emailContent(URL,Title,siteName) {
	if (!siteName) {
		siteName = 'Marvin.com';
	};
	if (!URL) {
		URL = escape(window.location);
	}
	if (!Title) {
		Title = escape(document.title);
	}
	var mailto = 'mailto:?subject=' + Title + '&body=Look%20what%20I%20found%20on%20' + siteName + ':%0A%0A' + Title + '%0A' + URL;
	window.location = mailto;
}

function printPage() {
	if (window.navigator.userAgent.indexOf('Netscape') > 0) {
			window.setTimeout(window.print,1);
	} 
	else {
		window.print();
	}
}


/* Querystring functionality used on search page (search.ascx) */

function Querystring()
{
// get the query string, ignore the ? at the front.
	var querystring=location.search.substring(1,location.search.length);

// parse out name/value pairs separated via &
	var args = querystring.split('&');

// split out each name = value pair
	for (var i=0;i<args.length;i++)
	{
		var pair = args[i].split('=');

		// Fix broken unescaping
		temp = unescape(pair[0]).split('+');
		name = temp.join(' ');

		temp = unescape(pair[1]).split('+');
		value = temp.join(' ');

		this[name]=value;
	}

	this.get=Querystring_get;
}

function Querystring_get(strKey,strDefault)
{
	var value=this[strKey];
	if (value==null)
	{
		value=strDefault;
	}

	return value;
}

function getQuerystringVar(varName) {
	var value = ""
	if (window.location.search.indexOf(varName + '=') != -1) {value = window.location.search.substring(window.location.search.indexOf(varName + '=') + varName.length + 1,(window.location.search+'&').indexOf('&',window.location.search.indexOf(varName + '=') + varName.length))}
	return value;
	}

/* Cookies */

function Cookies()
{

	// get the cookie string
	var cookies=document.cookie;
	
	// parse out name/value pairs
	var args = cookies.split(';');
	
	// split out each name = value pair
	for (var i=0;i<args.length;i++)
	{
		var pair = args[i].split('=');
		name = pair[0].trim();
		if (pair[1]==undefined) { //can happen if the key has no value
		    value = '';
		} else {
		    value = pair[1].trim();
		};
		this[name]=value;

	}
	this.get=Cookies_get;
    this.set=Cookies_set;	
}

function Cookies_get(name,strDefault)
{
	var value=this[name];
	if (value==null)
	{
		value=strDefault;
	}
	return value;
}
function Cookies_set(name, value)
{
    //alert(document.cookie);
}

function getCookiesVar(varName) {
	var value = ""
	if (window.location.search.indexOf(varName + '=') != -1) {value = window.location.search.substring(window.location.search.indexOf(varName + '=') + varName.length + 1,(window.location.search+'&').indexOf('&',window.location.search.indexOf(varName + '=') + varName.length))}
	return value;
	}


function ajax(url, vars, callbackFunction, callbackParams, returnXML) {
                    
    var request = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");

    //request.open("POST", url, true);
    //request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	request.open("GET", url, true);

    request.onreadystatechange = function(){
            if (request.readyState == 4 && request.status == 200) {
                if (request.responseText){
                    if (callbackParams) {
						if (returnXML) {
							callbackFunction(request.responseXML.documentElement, callbackParams);
						} else {
							callbackFunction(request.responseText, callbackParams);
						};						
                    } else {
						if (returnXML) {
							callbackFunction(request.responseXML.documentElement);
						} else {
							callbackFunction(request.responseText);;
						};		
                    };          
                } 
                else {
					callbackFunction('', callbackParams);
            }
        } 
        else if (request.readyState == 4) {
			callbackFunction('', callbackParams);
        }
    }

    request.send(vars);

}


