function Img(sImgName, sImgBaseUrl, sImgSrc)
{
  sImgBaseUrl = (sImgBaseUrl) ? sImgBaseUrl + '/images'
                              : '/images'
  window[sImgName] = new Image();
  window[sImgName].src = sImgBaseUrl + '/' + (sImgSrc ? sImgSrc : sImgName) + '.gif';
  window[sImgName + "on"] = new Image();
  window[sImgName + "on"].src = sImgBaseUrl + '/' + (sImgSrc ? sImgSrc : sImgName) + 'on.gif';
}

function SwitchImg(sImgID, sNewImg, objNSDocument)
{
	if (document.images && !objNSDocument)
	{
		if (document.images[sImgID] && window[sNewImg])
		{
			document.images[sImgID].src = window[sNewImg].src;
		}
	    return;
	}

  if (objNSDocument)
    if (objNSDocument.images)
      if (objNSDocument.images[sImgID])
        objNSDocument.images[sImgID].src = window[sNewImg].src;
}

//toolbar=no,status=yes,scrollbars=yes,resizable=yes,width=700,height=700

function DisplayAttachedPerson()
{
	var theForm = document.forms["EditForm"];
	if (!document.getElementById)
	{
		return;
	}
	var personNameUI = document.getElementById("person_name_ui");
	if (!personNameUI)
	{
		return;
	}
	personNameUI.innerHTML = theForm["person_name"].value;
}

function SelectPersonPopUp(callbackFunction)
{
	var callback = (callbackFunction) ? "&function=" + callbackFunction : "";
	PopUpWindow("SelectPersonWindow",
		"/admin/selectprofile.php?form=EditForm&field=person_ID&field2=person_name" + callback, 
		"puWindow", 
		"toolbar=no,status=yes,scrollbars=yes,resizable=yes,width=700,height=700", 
		false);
}

function PopUpWindow(varName, url, winName, features, winReplace)
{
	if (window[varName] && window[varName].close)
		window[varName].close();

	var properties = GetPopUpWindowFeatures(features);
	var isGoodSize = true;
	
	if (properties["width"] && 
		!isNaN(parseInt(properties["width"])) &&
		properties["height"] && 
		!isNaN(parseInt(properties["height"])))
	{
		isGoodSize = IsGoodPopUpSize(parseInt(properties["height"]), parseInt(properties["width"]));
		if (!isGoodSize)
		{
			properties["height"] = screen.availHeight - 50;
			properties["width"] = screen.availWidth - 50;
		}
	}
	
	window[varName] = window.open(url, winName, PopUpPropertiesToString(properties), winReplace);
	window[varName].focus();
}

function IsGoodPopUpSize(height, width)
{
	return (screen.availHeight > height && screen.availWidth > width) ? true : false;
}

function TestPopUp()
{
	var features = "toolbar=no,status=yes,scrollbars=yes,resizable=yes,width=700,height=700";
	var properties = GetPopUpWindowFeatures(features);
	if (properties["width"] && 
		!isNaN(parseInt(properties["width"])) &&
		properties["height"] && 
		!isNaN(parseInt(properties["height"])))
	{
		isGoodSize = IsGoodPopUpSize(parseInt(properties["height"]), parseInt(properties["width"]));
		if (!isGoodSize)
		{
			properties["height"] = screen.availHeight - 50;
			properties["width"] = screen.availWidth - 50;
		}
	}
	alert(PopUpPropertiesToString(properties));
}

function GetPopUpWindowFeatures(features)
{
	var popUpFeatures = {};
	var properties = features.split(",");
	var property = [];
	
	for (var i = 0, len = properties.length; i < len; i++)
	{
		property = properties[i].split("=");
		popUpFeatures[property[0]] = property[1];
	}
	return popUpFeatures;
}

function PopUpPropertiesToString(properties)
{
	var features = "";
	for (var item in properties)
	{
		features += item + "=" + properties[item] + ",";
	}
	return (features.length > 0) 
		? features.substr(0, features.length - 1)
		: features;
}

function URLEncode(s)
{
	return (s!=null) ? escape(s).replace(/\+/, "%2B") : '';
}