function checkSearch(){
	setCheckedValue(document.forms['SearchForm'].elements['stype'],getCookie('anuSearch'));
}

function checkSkipnav(){
	var is_webkit = navigator.userAgent.toLowerCase().indexOf('webkit') > -1;
	var is_opera = navigator.userAgent.toLowerCase().indexOf('opera') > -1;
	if(is_webkit || is_opera) 
	{
	 target = document.getElementById("skipnav");
	 target.href="#content";
	 target.setAttribute("tabindex" , "0");
	 document.getElementById("skipnav").setAttribute("onclick" , "document.getElementById('content').focus();");
	}
}

function clearInput( theinput ) {
	if(theinput.value == theinput.defaultValue){
		theinput.value = '';
	}
}

function defaultInput( theinput ) {
	if(theinput.value == ''){
		theinput.value = theinput.defaultValue;
	}
}
	
function checkInput ( theinput, themessage) {
	elem = document.getElementById( theinput );
	oldelem = elem.value;
	elem.value = elem.value.replace(/^\s+|\s+$/g, '');
	if(elem.value == "" || elem.value == elem.defaultValue){
		clearInput(elem);
		alert(themessage);
		elem.focus()
		return false;
	}
	elem.value = oldelem;
	setCookie('anuSearch', getCheckedValue(document.forms['SearchForm'].elements['stype']),expdate,'/',null);
}

function getCheckedValue(radioObj) {
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function setCheckedValue(radioObj, newValue) {
	if(newValue){
		var radioLength = radioObj.length;
		if(radioLength == undefined) {
			radioObj.checked = (radioObj.value == newValue.toString());
			return;
		}
		for(var i = 0; i < radioLength; i++) {
			radioObj[i].checked = false;
			if(radioObj[i].value == newValue.toString()) {
				radioObj[i].checked = true;
			}
		}
	}
}

function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1){
    endstr = document.cookie.length;
  }
  return unescape(document.cookie.substring(offset, endstr));
}

function fixCookieDate (date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0) {
    date.setTime (date.getTime() - skew);
  }
}

function setCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=.uregina.ca" : "") +
    ((secure) ? "; secure" : "");
}

function getCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg){
      return getCookieVal (j);
    }
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) {
    	break; 
    }
  }
	return null;
}

function deleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=.uregina.ca" : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

var expdate = new Date();
fixCookieDate(expdate);
expdate.setTime (expdate.getTime() + (1000 * 60 * 60 * 24 )); 

if (window.onload) {
	var func = window.onload;
	if(typeof func == 'function') {
		window.onload = function() { func(); checkSkipnav(); checkSearch(); }
	} else {
		window.onload = function() { checkSkipnav(); checkSearch(); }
	}
} else {
	window.onload = function() { checkSkipnav(); checkSearch(); }
}