// Common functionality for case law search. Supplements caselawsearchsimple.js and caselawsearchadvanced.js
function getSortOrder() {
	// Determine how they want to order their results.
	val = 'FilingDate';
	if (document.form1.OrderBy.length) {
		for (i=0; i < document.form1.OrderBy.length; i++) {
			if (document.form1.OrderBy[i].checked == true) {
				val = document.form1.OrderBy[i].value;
				break;
			}
		}
	}
	return val;
}

//Function to round numbers, changing the rlength variable indicates the number of decimal places to round to
function roundNumber(theNumber) {
	var rnum = theNumber;
	var rlength = 0;
	if (rnum > 8191 && rnum < 10485) {
		rnum = rnum-5000;
	}
	var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);

	return newnumber;
}

//Function that converts words in a string to the proper case
function PCase(STRING) {
	var strReturn_Value = "";
	var iTemp = STRING.length;
	if (iTemp == 0) {
		return "";
	}
	var UcaseNext = false;
	strReturn_Value += STRING.charAt(0).toUpperCase();
	for (var iCounter = 1; iCounter < iTemp; iCounter++) {
		if (UcaseNext == true) {
			strReturn_Value += STRING.charAt(iCounter).toUpperCase();
		}
		else {
			strReturn_Value += STRING.charAt(iCounter).toLowerCase();
		}
		var iChar = STRING.charCodeAt(iCounter);
		if (iChar == 32 || iChar == 45 || iChar == 46) {
			UcaseNext = true;
		}
		else {
			UcaseNext = false
		}
		if (iChar == 99 || iChar == 67) {
			if (STRING.charCodeAt(iCounter - 1) == 77 || STRING.charCodeAt(iCounter - 1) == 109) {
				UcaseNext = true;
			}
		}
	}
	return strReturn_Value;
}

//Function that returns an object having .x and .y properties which are the coordinates of the named anchor, relative to the page.
function getAnchorPosition(anchorname) {
	var useWindow = false;
	var coordinates = new Object();
	var x = 0, y = 0;
	var use_gebi = false, use_css = false, use_layers = false;
	if (document.getElementById) { use_gebi = true; }
	else if (document.all) { use_css = true; }
	else if (document.layers) { use_layers = true; }
	if (use_gebi && document.all) {
		x = AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y = AnchorPosition_getPageOffsetTop(document.all[anchorname]);
	}
	else if (use_gebi) {
		var o = document.getElementById(anchorname);
		x = AnchorPosition_getPageOffsetLeft(o);
		y = AnchorPosition_getPageOffsetTop(o);
	}
	else if (use_css) {
		x = AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y = AnchorPosition_getPageOffsetTop(document.all[anchorname]);
	}
	else if (use_layers) {
		var found = 0;
		for (var i = 0; i < document.anchors.length; i++) {
			if (document.anchors[i].name == anchorname) { found = 1; break; }
		}
		if (found == 0) {
			coordinates.x = 0; coordinates.y = 0; return coordinates;
		}
		x = document.anchors[i].x;
		y = document.anchors[i].y;
	}
	else {
		coordinates.x = 0; coordinates.y = 0; return coordinates;
	}
	coordinates.x = x;
	coordinates.y = y;
	return coordinates;
}

// Function getting the page offset left position
function AnchorPosition_getPageOffsetLeft(el) {
	var ol = el.offsetLeft;
	while ((el = el.offsetParent) != null) { ol += el.offsetLeft; }
	return ol;
}

// Function getting the page offset top position
function AnchorPosition_getPageOffsetTop(el) {
	var ot = el.offsetTop;
	while ((el = el.offsetParent) != null) { ot += el.offsetTop; }
	return ot;
}

//Function that bookmarks a web page
function BookmarkPage(title, url) {
	if (document.all)
		window.external.AddFavorite(url, title);
	else if (window.sidebar)
		window.sidebar.addPanel(title, url, "")
}

// Returns the name of an agency, based upon our internal id.
function getAgencyName(agencyId) {
	var agencyName = "";
	switch (agencyId) {
		case 1:
			agencyName = "WI Supreme Court";
			break;
		case 2:
			agencyName = "WI Court of Appeals"
			break;
		case 4:
			agencyName = "Tax Appeals"
			break;
		case 5:
			agencyName = "WERC Grievance Awards"
			break;
		case 6:
			agencyName = "WERC Decisions"
			break;
		default:
			agencyName = "";
	}
	return agencyName;
}
