/********************************************************
 *
 * FileName: utils.js
 * Author: Robert Tsai (rtsai@tjhinc.com)
 * Description: Useful javascript utilities
 * Date: 8/14/2002
 *
 * Copyright 2002 Theodore J. Hogan & Associates, Inc.
 * All Rights Reserved.
 *
 ********************************************************/

var BROWSER_IE_4 = 0;
var BROWSER_IE_5_PLUS = 1;
var BROWSER_NETSCAPE_4 = 2;
var BROWSER_NETSCAPE_6_PLUS = 3;

function trim(str) {
	var st = 0;
	var len = str.length;

	for (i=0;i<len;i++) {
		if (str.charAt(i) == ' ')
			st++;
		else
			break;
	}
	if (st >= len) return "";
	for (i=len-1;i>-1;i--) {
		if (str.charAt(i) == ' ')
			len--;
		else
			break;
	}
	return str.substring(st, len);
}

function checkMaxLength(txt, maxlen) {
	if(txt.length > maxlen) {
		return false;
	} else {
		return true;
	}
}

function selectComboItem(whichCombo, id) {
	for (i=0;i<whichCombo.length;i++) {
		if (whichCombo.options[i].value == id) {
			whichCombo.selectedIndex = i;
			break;
		}
	}
}

function selectComboItemMultiples(whichCombo, id) {
	for (i=0;i<whichCombo.length;i++) {
		if (whichCombo.options[i].value == id) {
			whichCombo.options[i].selected = true;
			break;
		}
	}
}

function setTextValue(whichControl, txt) {
	whichControl.value = txt;
}

function setCheckValue(whichControl, txt) {
	if (trim(txt) == "checked") {
		whichControl.checked = true;
	}
}

function getCounterImage(isAssoc) {
	sw = window.screen.width; // screen width
	sh = window.screen.height; // screen height

	// browser/window width and height
	if (document.all) {
		bw = document.body.clientWidth;
		bh = document.body.clientHeight;
	} else {
		bw = window.innerWidth;
		bh = window.innerHeight;
	}

	if (navigator.javaEnabled() < 1) je = 0;
	if (navigator.javaEnabled() == 1) je =1;

	pg = new String(window.location);
	if (pg.indexOf("http://prototrack.com") == 0 || pg.indexOf("prototrack.com") == 0) {
		window.location = pg.replace("prototrack", "www.prototrack");
		return "";
	}

	if (pg.lastIndexOf("/") != -1)
		pg = pg.substring(pg.lastIndexOf("/"));

	if (pg.length <=1 || pg == "/") {
		pg = "index.htm";
	} else {
		pg = pg.substring(1);
	}

	// check if from an association
	if (isAssoc != null && isAssoc != "") {
		upDir = "";

		// this is b/c of stupid pma mirror site
		if (isAssoc.indexOf("..") > -1) {
			upDir = "../";
			isAssoc = isAssoc.substring(2);
		}

		isAssoc = (isAssoc == "demo") ? "" : "&isA=" + isAssoc;

		return "<img src=\"" + upDir + "../servlet/HitStatistics?pg=" + pg + "&sw=" + sw + "&sh=" + sh +
			   "&bw=" + bw + "&bh=" + bh + "&je=" + je + isAssoc +
			   "\" border=0 height=1 width=1></img>";
	} else {
		return "<img src=\"servlet/HitStatistics?pg=" + pg + "&sw=" + sw + "&sh=" + sh +
			   "&bw=" + bw + "&bh=" + bh + "&je=" + je +
			   "\" border=0 height=1 width=1></img>";
	}
}

function BrowserSelector() {
	var agent = navigator.userAgent.toLowerCase();
	this.major = parseInt(navigator.appVersion);
	this.minor = parseFloat(navigator.appVersion);
	this.ns = ((agent.indexOf('mozilla') != -1) &&
			   (agent.indexOf('spoofer') == -1) &&
			   (agent.indexOf('compatible') == -1) &&
			   (agent.indexOf('opera') == -1) &&
			   (agent.indexOf('webtv') == -1));
	this.ns2 = (this.ns && (this.major == 2));
	this.ns3 = (this.ns && (this.major == 3));
	this.ns4 = (this.ns && (this.major == 4));
	this.ns6 = (this.ns && (this.major >= 5));
	this.ie = (agent.indexOf("msie") != -1);
	this.ie3 = (this.ie && (this.major < 4));
	this.ie4 = (this.ie && (this.major == 4) &&
			   (agent.indexOf("msie 5.0")   == -1));
	this.ie5 = (this.ie && (this.major == 4) &&
	 		   (agent.indexOf("msie 5.0") != -1));
	this.ieX = (this.ie && !this.ie3 && !this.ie4);
}

var is = new BrowserSelector();

function myGetElementById(id) {
	if (is.ie5||is.ns6){
		return document.getElementById(id);
	} else if(is.ie4) {
		return document.all[id];
	} else if(is.ns4) {
		return document.layers[id];
    }
}

function findSize() {
	if(is.ns4 ||is.ns6) {
		available_width=innerWidth;
		available_height=innerHeight;
	} else if (is.ie4 || is.ie5 || is.ieX) {
		available_width=document.body.clientWidth;
		available_height=document.body.clientHeight;
	}
}

function checkAllCheckBoxes(chk) {
	var frm = chk.form;
	for(i=0;i<frm.length;i++) {
		e=frm.elements[i];
		if (e.type == 'checkbox') {
			e.checked = chk.checked;
		}
	}
}

function addOptionToList (myList, strText, intVal) {
	nextOpen = myList.length;
	myList.options[nextOpen] = new Option (strText, intVal);
}

function removeOptionsFromList(myList) {
	if (myList.selectedIndex == -1) { alert ("Please select an item to remove."); return; }

	for (var i=0;i<myList.options.length;i++) {
    	if (myList.options[i].selected)
        	myList.options[i--] = null;
	}
/*

		for (i=ix;i<myList.length-1;i++) {
			tmpOpt = myList.options[i+1];
			newOpt = new Option(tmpOpt.text, tmpOpt.value);
			newOpt.style.background = tmpOpt.style.background;
			myList.options[i] = newOpt;
		}
		myList.length -= 1;
	}
*/
}

function openWindow(pg, w, h) {
	newWindow = window.open(pg,
				"newWindow", "width=" + w + ", height=" + h + ", resizable, scrollbars, status");
	newWindow.focus();
	newWindow.moveTo((screen.width - w) / 2, (screen.height - h) / 2);
}

function openWindow2(pg, w, h, params) {
	newWindow = window.open(pg,
				"newWindow", "width=" + w + ", height=" + h + ", " + params);
	newWindow.focus();
	newWindow.moveTo((screen.width - w) / 2, (screen.height - h) / 2);	
}

function strReplace(strEntry, strToReplace, strReplaceWith) {
	var strReturn = "";
	var ix=0;

	if (strEntry != null && strToReplace != null && strReplaceWith != null) {
		while (strEntry != "") {
			ix = strEntry.indexOf(strToReplace);
			if (ix != -1) {
				strReturn = strReturn + strEntry.substring(0, ix) + strReplaceWith;
				strEntry = strEntry.substring(ix + strToReplace.length, strEntry.length);
			} else if (ix == -1) {
				strReturn = strReturn + strEntry;
				strEntry = "";
			}
		}
	}
	return strReturn;
}
