//
//	Author: Matt Boucher
//	Date:- 05/08/01
// 
//	common.js
//

// the following code is from http://www.webreference.com/dhtml/diner/resize/resize4.html
// it is used to fix netscape when you resize the screen and you are using DHTML
function reDo()
{
	if (innerWidth != origWidth || innerHeight != origHeight) 
		location.reload();
}

// we check document.all because NS is so dumb it doesn't have a document yet!!! aaargghh!
if (!document.all)
{
	origWidth = innerWidth;
    origHeight = innerHeight;
	onresize = reDo;
}

function lazySubmit(form)
{
	if (document.all)
		setTimeout("doLazySubmit(" + form.name + ")", 200);
	else
	{
		form.submit();
	}
}

function doLazySubmit(form)
{
	form.submit();
}

function hideObj(name)
{
	var obj = getObj(name);
	if (document.layers) 
		obj.visibility = "hide";
	else 
		obj.style.visibility = "hidden";
}

function showObj(name)
{
	var obj = getObj(name);
	if (document.layers) 
		obj.visibility = "show";
	else 
		obj.style.visibility = "visible";
}

function getObj(name)
{
	var obj;
	
	if (document.layers) 
		obj = netscape_getObj(document, name);
	else 
		obj = document.all[name];
	
	if (obj == null)
		obj = getImg(name);

	return obj;		
}

function netscape_getObj(doc, nameStr)
{
	var obj = null;
    if (obj = doc.layers[nameStr])
		return obj;

	if (obj = doc.forms[nameStr])
		return obj;

	for (var i = 0; i < doc.forms.length; i++)
	{
		if (obj = doc.forms[i].elements[nameStr])
			return obj;
	}

    for (var i = 0; i < doc.layers.length; i++)
    {
		if (obj = netscape_getObj(doc.layers[i].document, nameStr))
			return obj;
	}
	return obj;
}

function getImg(name)
{
	return document.images[name];
}

function toggleObj(name)
{
	var obj = getObj(name);

	if (obj == null)
		return;
		
	if (obj.myenabled == null)
		obj.myenabled = true;

	enableObj(name, !obj.myenabled);
}

function enableObj(name, enable)
{
	var obj = getObj(name);
	
	if (obj == null)
		return;

	if ((obj.tagName == "IMG") || 
		(document.layers && (typeof obj == 'object') && (obj.constructor == Image)))
	{
		enableImg(obj, enable);
	}
	else if (obj.tagName == "SPAN" || obj.tagName == "DIV" || obj.tagName == "A")
	{
		enableClass(obj, enable);
	}
	else if (obj.length > 0 && obj.type != "button" && obj[0].type == "radio")
	{
		enableRadioButtonGroup(obj, enable);
	}
	else
	{
		obj.disabled = !enable;
	}
}

function enableRadioButtonGroup(radio, enable)
{
	for (var i = 0; i < radio.length; i++)
	{
		radio[i].disabled = !enable;
	}
}

function enableImg(obj, enable)
{
	if (obj.inited != true)
		initImg(obj);

	// only update if different
	if (obj.myenabled != enable)
	{
		if (enable)
			obj.src = obj.enabledsrc;
		else
			obj.src = obj.disabledsrc;
	
		obj.myenabled = enable;
	}
}

function initImg(obj)
{
	obj.inited = true;

	obj.enabledsrc = obj.src;

	var re = /.gif/;
	obj.disabledsrc = obj.src.replace(re, "_d.gif");
}	

function enableClass(obj, enable)
{
	if (obj.inited != true)
		initClass(obj);

	// only update if different
	if (obj.myenabled != enable)
	{
		if (enable)
			obj.className = obj.enabledClassName;
		else
			obj.className = obj.disabledClassName;
	
		obj.myenabled = enable;
	}
}

function initClass(obj)
{
	obj.inited = true;

	obj.enabledClassName = obj.className;
	obj.disabledClassName = obj.className + "disabled";
}	

var popupWindow;

function popup(link, name)
{
	if (popupWindow && !popupWindow.closed && popupWindow.name == name)
	{
		popupWindow.location.href = link;
		popupWindow.focus();
	}
	else
		popupWindow = window.open(link, name, "toolbar=no,scrollbars=yes,menubars=no,toolbars=no,resizable=yes");
}

function centerPopup(link, name, width, height, sb)
{
    if (sb == null)
        sb = "no";

	if (document.all)
	{
		xTag = "left=";
		yTag = "top=";
	}
	else
	{
		xTag = "ScreenX=";
		yTag = "ScreenY=";
	}
	x = xTag + (screen.width - width) / 2;
	y = yTag + (screen.height - height) / 2;

	if (popupWindow && !popupWindow.closed && popupWindow.name == name)
	{
		popupWindow.location.href = link;
		popupWindow.focus();

		if (document.all)
			popupWindow.resizeTo(width + 12, height + 31);
	}
	else
		popupWindow = window.open(link, name, "width="+width+",height="+height+ "toolbar=no,scrollbars=" + sb + ",menubars=no,toolbars=no,resizable=yes,"+ x + "," + y);

	if (document.layers)
		popupWindow.resizeTo(width + 20, height + 20);
}

function sortList(obj)
{
	var opt = obj.options;
	for(var x = 0; x < opt.length - 1; x++) 
	{
		for(var y = (x + 1); y < opt.length; y++)
		{
			if (opt[x].text > opt[y].text)
			{
				var tmp = new Option(opt[x].text, opt[x].value);
				opt[x] = new Option(opt[y].text, opt[y].value);
				opt[y] = tmp;
			}
		}
	}
}

function getRadioButtonValue(radio)
{
	for (var i = 0; i < radio.length; i++)
	{
		if (radio[i].checked)
			break;
	}
	return radio[i].value;
}

function addOption(obj, text, value)
{
	var optionName = new Option(text, value)
	obj.options[obj.length] = optionName;
}

function deleteOption(obj, index)
{
	obj.options[index] = null;
}

function deleteAllOptions(obj)
{
	for (var i = obj.options.length - 1; i >= 0; i--)
	{
		deleteOption(obj, i);
	}
}

function getComboText(obj)
{
	if (obj.selectedIndex != -1)
		return obj.options[obj.selectedIndex].text;
	else
		return "";
}

function getComboValue(obj)
{
	if (obj.selectedIndex != -1)
		return obj.options[obj.selectedIndex].value;
	else
		return "";
}

function selectOptionByName(obj, name)
{
	for (var i = 0; i < obj.options.length; i++)
	{
		if (obj.options[i].text == name)
		{
			obj.selectedIndex = i;
			return;
		}
	}

}

function selectOptionByValue(obj, value)
{
	for (var i = 0; i < obj.options.length; i++)
	{
		if (obj.options[i].value == value)
		{
			obj.selectedIndex = i;
			return;
		}
	}
}

function trim(str)
{ 
	return str.replace(/^\s*([^\s]*)\s*$/, "$1");
} 

function formatCurrency(num)
{
	num = num.toString().replace(/\$|\,/g,'');
	if (isNaN(num))
		num = "0";
	
	sign = (num == (num = Math.abs(num)));
	
	num = Math.floor(num * 100 + 0.50000000001);
	cents = num % 100;
	
	num = Math.floor(num / 100).toString();
	if (cents < 10)
		cents = "0" + cents;
		
	for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) 
		num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3)); 
	
	return (((sign) ? '' : '-') + '$' + num + '.' + cents);
}

function onlyDigits(e, bDecimal, fieldValue)
{
	if (document.layers)
		keyCode = e.which;
	else
		keyCode = e.keyCode;

	if (keyCode == 46 && bDecimal) {
		if (fieldValue == null || fieldValue.indexOf ('.') == -1)
			return true;
	}

	if ((keyCode < 48 || keyCode > 57)) {
		return false;
	}
	
	return true;
}

function checkPasswords(pass, cpass)
{
	if (pass.value == "" && cpass.value == "")
		return true;
	else
	{
		if (pass.value.indexOf(" ") != -1)
		{
	        alert("Spaces are not allowed in your password.");
	        pass.value = "";
	        pass.focus();
	        return false;
		}
        if (pass.value != cpass.value)
        {
			alert("Passwords do not match.");
			cpass.value = "";
			cpass.focus();
			return false;
		}
	}
	return true;
}

// example of how to use the parseQueryString function
//<SCRIPT>
//var args = parseQueryString ();
//for (var arg in args) {
//  document.write(arg + ': ' + args[arg] + '<BR>');
//}
//</SCRIPT>

function parseQueryString (str) 
{
	str = str ? str : location.search;
	var query = str.charAt(0) == '?' ? str.substring(1) : str;
	var args = new Object();
	if (query)
	{
		var fields = query.split('&');
		for (var f = 0; f < fields.length; f++)
		{
			var field = fields[f].split('=');
			args[unescape(field[0].replace(/\+/g, ' '))] = unescape(field[1].replace(/\+/g, ' '));
		}
  	}
	return args;
}



