// Browser detect
var tt_u = 'undefined',
	tt_n = navigator.userAgent.toLowerCase();

// Browser flags
var tt_op = (window.opera && document.getElementById)? true : false,
	tt_op6 = tt_op && !document.defaultView,
	tt_ie = tt_n.indexOf('msie') != -1 && document.all && !tt_op,
	tt_n4 = (document.layers && typeof document.classes != "undefined"),
	tt_kq = tt_n.indexOf('konq') >= 0 || tt_n.indexOf('safari') >= 0,
	tt_n6 = (!tt_op && !tt_kq && document.defaultView && typeof document.defaultView.getComputedStyle != tt_u);
	tt_n = '';

function GetID( t_id ) {
	return (
		tt_n4 ? (document.layers[t_id] || null)
		: tt_ie ? (document.all[t_id] || null)
		: (document.getElementById(t_id) || null)
	);
}
function GetIDW( obj ) {
	return (
		tt_n4 ? obj.clip.width
		: obj.style.pixelWidth ? obj.style.pixelWidth
		: obj.offsetWidth
	);
}
function GetIDH( obj ) {
	return (
		tt_n4 ? obj.clip.height
		: obj.style.pixelHeight ? obj.style.pixelHeight
		: obj.offsetHeight
	);
}
function SetIDW( obj, w ) {
	if (tt_n4) {
		obj.clip.width = w;
	} else {
		if (typeof obj.style.pixelWidth != tt_u) obj.style.pixelWidth = w;
		else obj.style.width = w + 'px';
	}
}
function SetIDH( obj, h ) {
	if (tt_n4) {
		obj.clip.height = h;
	} else {
		if (typeof obj.style.pixelHeight != tt_u) obj.style.pixelHeight = h;
		else obj.style.height = h + 'px';
	}
}

function getElementPosition(elemID, asString, strOffsetX, strOffsetY) {
    var offsetTrail = GetID(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
    if (!offsetTrail) offsetTrail = elemID;
    while (offsetTrail && offsetTrail.offsetParent) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 &&
        typeof document.body.leftMargin != tt_u) {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    if (asString) return (offsetLeft+(strOffsetX?strOffsetX:0))+","+(offsetTop+(strOffsetY?strOffsetY:0));
    return {left:offsetLeft, top:offsetTop};
}

// General Functions

function ucwords (str)
{
  newstr = '';
  str = this != window ? this : str;
  str = str.toLowerCase();
  for (u=1,i=0; i < str.length; i++) {
  	c = str.charAt(i);
    if (u) { newstr += c.toUpperCase(); u = false; }
    else { newstr += c; u = c == ' '; }
  }
  return newstr;
}

function fixComma( inputFld ) {
	inputFld.value = inputFld.value.replace( /,/ , '.' );
}

function blankIfZero (str)
{
	if (str === undefined) return '';
	return (parseFloat(str) == 0.0 ? '' : str);
}
function zeroIfBlank (str)
{
	if (str === undefined) return;
	return parseFloat(str);
}

function trimString (str)
{
  str = this != window ? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function showDiv(divName) {
	thisDiv = document.getElementById(divName);
	if (thisDiv) {
		thisDiv.style.display = "block";
	} else {
		errorString = "Error: Could not locate div with id: " + divName;
		alert(errorString);
	}
}

function hideDiv(divName) {
	thisDiv = document.getElementById(divName);
	if (thisDiv) {
		thisDiv.style.display = "none";
	} else {
		errorString = "Error: Could not locate div with id: " + divName;
		alert(errorString);
	}
}

function toggleDiv(divName) {
	thisDiv = document.getElementById(divName);
	if (thisDiv) {
		if (thisDiv.style.display == "none") {
			thisDiv.style.display = "block";
		}
		else {
			thisDiv.style.display = "none";
		}
	}
	else {
		errorString = "Error: Could not locate div with id: " + divName;
		alert(errorString);
	}
}

function toggleCheckbox(thisCheckbox) {
	if (thisCheckbox.checked == true) {
		thisCheckbox.checked = false;
	}
	else {
		thisCheckbox.checked = true;
	}
}

function setSelectedRadioButton(radioArray, radioValue) {
	for (i = 0; radioArray[i]; i++) {
		if (radioArray[i].value == radioValue) {
			radioArray[i].checked = true;
		}
	}
}

function setSelectToValue(thisSelect, thisValue) {
	for (i = 0; thisSelect[i]; i++) {
		if (thisSelect[i].value == thisValue) {
			thisSelect[i].selected = true;
		}
	}
}

function getSelectedValue(thisSelect) {
	return thisSelect.options[thisSelect.selectedIndex].value;
}

function getSelectedText(thisSelect) {
	return thisSelect.options[thisSelect.selectedIndex].text;
}

function getSelectOptionOfThisValue(thisSelect, thisValue) { // select the specified option
	success = 0;
	for (i = 0; thisSelect[i]; i++) {
		if (thisSelect[i].value == thisValue) {
			success = 1;
			return i;
		}
	}
	if (success == 0) {
		return false;
	}
}

function zero(thisString, thisLength) {
	thisString = thisString.toString();
	if (thisString.length < thisLength) {
		var padding = "";
		for (i = 0; i < (thisLength - thisString.length); i++) {
			padding += "0";
		}
		thisString = padding + thisString;
	}
	return thisString;
}

function increaseNotesHeight(thisTextarea, add) {
	if (thisTextarea) {
		if (thisTextarea.style.height == '' && parseInt(thisTextarea.rows) > 1)
			newHeight = thisTextarea.rows * 15 + add;
		else
			newHeight = parseInt(thisTextarea.style.height) + add;
		thisTextarea.style.height = newHeight + "px";
	}
	if (document.getElementById('notes_height')) {
		document.getElementById('notes_height').value = newHeight;
	}
}

function decreaseNotesHeight(thisTextarea, subtract) {
	if (thisTextarea) {
		if (thisTextarea.style.height == '' && thisTextarea.rows && thisTextarea.rows > 1)
			newHeight = thisTextarea.rows * 16;
		else
			newHeight = parseInt(thisTextarea.style.height);
		if ((newHeight - subtract) > 32) {
			newHeight -= subtract;
			thisTextarea.style.height = newHeight + "px";
		}
		else {
			newHeight = 32;
			thisTextarea.style.height = "32px";
		}
	}
	if (document.getElementById('notes_height')) {
		document.getElementById('notes_height').value = newHeight;
	}
}

function popIt( link, t, w, h, params )
{
	if (w === undefined) w = 640;
	if (h === undefined) h = 480;
	if (t === undefined) t = "popup";
	if (params === undefined) params = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes";
	popWin = window.open( link, t, "width="+w+",height="+h+","+params );
	return popWin;
}

function popItFocus( link, t, w, h, params )
{
	popWin = popIt( link, t, w, h, params );
	if (popWin) popWin.focus();
}

function PDFWindow( path )
{
	window.open( path, 'pdfwindow', 'scrollbars=auto,location=no,resizable=yes,width=780,height=580');
	return false;
}
