﻿/*** Utilities **/
/*
	START
	Written by Jonathan Snook, http://www.snook.ca/jonathan
	Add-ons by Robert Nyman, http://www.robertnyman.com
*/

var sBasePath = "http://www.umt.edu/";

function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all) ? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for (var i = 0; i < arrElements.length; i++) {
		oElement = arrElements[i];
		if (oRegExp.test(oElement.className)) {
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}
/*
	END
	Written by Jonathan Snook, http://www.snook.ca/jonathan
	Add-ons by Robert Nyman, http://www.robertnyman.com
*/

// grabbed from elated.com and then modified by NS expireLength is in days
function set_cookie( name, value, expireLength)
{
	var cookie_string = name + "=" + escape ( value );
	var lengthInSeconds = expireLength * 60 * 60 * 24 * 1000;
	if(expireLength) {
		var expires = new Date ();
		expires.setTime(expires.getTime()+lengthInSeconds);
		cookie_string += "; expires=" + expires.toGMTString();
		cookie_string += "; domain=" + escape ('.umt.edu');
	}
	document.cookie = cookie_string;
}

function get_cookie(cookie_name)
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if (results) {
  	return (unescape(results[2]));
  }
  else {
  	return null;
  }
}
// end grab from elated.com

/*** Email  ***/
function formatEmail(user, domain, content)
{
    if(content == "") {
        content = user + "@" + domain;
    }
    document.write("<a href='mailto:" + user + "@" + domain + "'>" + content + "</a>");
}

/*** Search ***/
function resetText(elem){
    if(elem.value == "") {elem.value = "SEARCH UM"; }
}
function clearBox(elem){
    
    if(elem.value == "SEARCH UM") {elem.value = ""; }
}


function oldSearch()
{
	var s = "http://www.google.com/u/umtest?q=";
		s += document.getElementById('txtSearch').value.replace(" ", "+");
		s += "&amp;sa=Go&amp;domains=umt.edu&amp;sitesearch=umt.edu";
		// redirect to search results page
		//window.location.href = s;
		centerContent.innerHTML = s;
}
function search(searchBoxId)
{
	// redirect to search results page
	var sQuery = document.getElementById(searchBoxId).value.replace(" ", "+");
	var searchURL = sBasePath + "home/search/?srchingtext="+sQuery;
	document.location.href = searchURL;
} 

/*
*Ajax Functions
*/
function feedbackAjax(style)
{
    var word = document.getElementById('feedbackBox').value;
    document.getElementById('feedbackBox').value='';
    if(style == 'Ribbon'){
        document.getElementById('feedbackBox').style.height='13px';
    }
    
    qstr = 'w=' + escape(word);  // NOTE: no '?' before querystring
    qstr += '&loc=' + location.href;
    qstr += '&action=feedback';
    
    xmlhttpPost(qstr,'result');
}
function suggestAjax()
{
    document.getElementById('suggestSubmit').disabled = true;
    document.getElementById('result').innerHTML = "<img style='width: 205px;' src='"+sBasePath+"_common/resources/imx/ajax-loader.gif' alt='waiting to submit'>";
    qstr = 'txtLinkName=' + escape(document.getElementById('txtLinkName').value);  // NOTE: no '?' before querystring
    qstr += '&txtLinkURL=' + escape(document.getElementById('txtLinkURL').value);
    qstr += '&txtComments=' + escape(document.getElementById('txtComments').value);
    qstr += '&txtContactName=' + escape(document.getElementById('txtContactName').value);
    qstr += '&txtContactEmail=' + escape(document.getElementById('txtContactEmail').value);
    qstr += '&action=suggest';
    
    xmlhttpPost(qstr,'result');
}

function suggestStoryAjax()
{
    document.getElementById('suggestSubmit').disabled = true;
    document.getElementById('result').innerHTML = "<img style='width: 205px;' src='"+sBasePath+"_common/resources/imx/ajax-loader.gif' alt='waiting to submit'>";
    qstr = 'txtStoryName=' + escape(document.getElementById('txtStoryName').value);  // NOTE: no '?' before querystring
    qstr += '&txtStoryURL=' + escape(document.getElementById('txtStoryURL').value);
    qstr += '&txtComments=' + escape(document.getElementById('txtComments').value);
    qstr += '&txtContactName=' + escape(document.getElementById('txtContactName').value);
    qstr += '&txtContactEmail=' + escape(document.getElementById('txtContactEmail').value);
    qstr += '&action=suggestStory';
    
    xmlhttpPost(qstr,'result');
}

function xmlhttpPost(sQueryStr,resultDiv) {

    if(resultDiv == "") {resultDiv = "result";}
    var strURL = sBasePath+'action.aspx';
	var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(self.xmlHttpReq.responseText,resultDiv);
        }
    }
    self.xmlHttpReq.send(sQueryStr);
    return true;
}

function getquerystring(sQueryInfo) {
    var word = document.getElementById('feedbackBox').value;
    qstr = 'w=' + escape(word);  // NOTE: no '?' before querystring
    qstr += '&loc=' + location.href;
    qstr += sQueryInfo;
    document.getElementById('feedbackBox').value='';
    return qstr;
}

function updatepage(str,resultDiv){
    if(resultDiv != 'none'){
        document.getElementById(resultDiv).innerHTML = str;
    }
}