function CancelWindow() {
	self.close();
}

function PopUpWindow (url, hWind, nWidth, nHeight, nScroll, nResize) {
	var cToolBar = 'toolbar=0,location=0,directories=0,status=' + nResize + ',menubar=0,scrollbars=' + nScroll + ',resizable=' + nResize + ',width=' + nWidth + ',height=' + nHeight;
	var popupwin = window.open(url, hWind, cToolBar);
	return popupwin;
}

function openWindow(url){
	newWindow=window.open(url,'newWin','tollbar=yes,location=yes,scrollbars=yes,width=300,hight=200')
}

function openWindow2(url){
	newWindow=window.open(url,'newWin','tollbar=yes,menubar=yes,location=yes,scrollbars=yes,directories=yes')
}

function closeWindow()
	{
	if(newWindow && !newWindow.closed)
		{
			newWindow.close()
		}	
	}
	
function PopUpWindowFull (url, hWind, nWidth, nHeight, nResize)
	{
	var cToolBar = "toolbar=yes,location=yes,directories=yes,resizable=1,status=" + nResize + ",menubar=yes,scrollbars=yes,width=" + nWidth + ",height=" + nHeight
	var popupwin = window.open(url, hWind, cToolBar);
	return popupwin;
	}

function QuickLinkSelect(baseFolderID, formName,  titleFormElem, useQLinkCheck, SetBrowserState) {
	// There are specific requirements for this function. See "QuickLinkSelect.asp" for those.
	//
	var sendQString = '?folderid=' + baseFolderID + '&formName=' + formName + '&titleFormElem=' + 
				titleFormElem + '&useQLinkCheck=' +  useQLinkCheck + '&SetBrowserState=' + SetBrowserState;
	var cToolBar = 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=450,height=300';
				
	window.open('QuickLinkSelect.asp' + sendQString, 'QlinkSelectWin', cToolBar) ;
	
	return true ;
}

function Trim (string) {
	if (string.length > 0) {
		string = RemoveLeadingSpaces (string);
	}
	if (string.length > 0) {
		string = RemoveTrailingSpaces(string);
	}
	return string;
}
	
function RemoveLeadingSpaces(string) {
	while(string.substring(0, 1) == " ") {
		string = string.substring(1, string.length);
	}
	return string;
}

function RemoveTrailingSpaces(string) {
	while(string.substring((string.length - 1), string.length) == " ") {
		string = string.substring(0, (string.length - 1));
	}
	return string;
}
	
function EnableElement(id, bEnable)
{
	var objElem = document.getElementById(id);
	if (objElem)
	{
		// Mozilla FireFox 1.0 does not support .disabled for <label>
		if (typeof objElem.disabled != "undefined")
		{
			objElem.disabled = !bEnable;
		}
		// if a label, follow the 'for' attribute as well
		if (typeof objElem.htmlFor != "undefined")
		{
			// don't use recursion to avoid possibility of infinite recursion
			objElem = document.getElementById(objElem.htmlFor);
			if (objElem && typeof objElem.disabled != "undefined")
			{
				objElem.disabled = !bEnable;
			}
		}
	}
}
	
function ShowElement(id, bShow)
{
	var objElem = document.getElementById(id);
	if (objElem)
	{
		objElem.style.display = (bShow ? "" : "none");
		ShowSelectElements(objElem, bShow);	
	}
}

function ShowSelectElements(objElem, bShow)
// due to a bug in IE, descendent select lists may still be displayed
// so target each individually
{
	if (!objElem) return;
	if ("undefined" == typeof objElem.children) return;
	if ("SELECT" == objElem.tagName)
	{
		objElem.style.display = (bShow ? "" : "none");
	}
	else
	{
		for (var i = 0; i < objElem.children.length; i++)
		{
			ShowSelectElements(objElem.children[i], bShow);
		}
	}
}

function GetElementValue(oElem)
{
	if (!oElem) return;
	if (typeof oElem.value != "undefined")
	{
		if ("INPUT" == oElem.tagName && ("checkbox" == oElem.type || "radio" == oElem.type))
		{
			var strValue = oElem.value + "";
			if (strValue.length > 0 && strValue != "true")
			{
				if (oElem.checked)
				{
					return strValue;
				}
				else
				{
					return "";
				}
			}
			else // boolean
			{
				if (oElem.checked)
				{
					return true;
				}
				else
				{
					return false;
				}
			}
		}
		else
		{
			return oElem.value;
		}
	}
	else if (typeof oElem.innerHTML != "undefined")
	{
		return oElem.innerHTML;
	}
	else if (typeof oElem.innerText != "undefined")
	{
		return oElem.innerText;
	}
	else
	{
		return; // no value
	}
}

function SetElementValue(oElem, value)
{
	if (!oElem) return;
	if (typeof oElem.value != "undefined")
	{
		if ("INPUT" == oElem.tagName && ("checkbox" == oElem.type))
		{
			// boolean
			if ("true" == value || true == value)
			{
				oElem.checked = true;
			}
			else if ("false" == value || false == value)
			{
				oElem.checked = false;
			}
			else
			{
				oElem.value = value;
			}
		}
		else
		{
			oElem.value = value;
		}
	}
	else if (typeof oElem.innerHTML != "undefined")
	{
		oElem.innerHTML = value;
	}
	else if (typeof oElem.innerText != "undefined")
	{
		oElem.innerText = value;
	}
}

function numberValidate(field)
{
	if (field.value == "")
	{
		return true;
	}
	if ((! /^\d+$/.test(field.value)) && field.value != "") 
	{
		alert("You must use only numeric characters");
		field.value = "";
		field.focus();
		return false;		
	} 
	return true;
}
	
function textareaValidate(field)
{
	var strText = GetElementValue(field) + "";
	var len = strText.length;
	if(len > 500)
	{
		alert("Over limit of characters in text box.");
		field.focus();
		return false;	
	}
	return true;
}

function ValidateMeta(formName)
// CAUTION: do not name this function 'V a l i d a t e M e t a d a t a', 
// suspect the word 'M e t a d a t a' causes ASP to omit the JavaScript
{

	var reqfields;
	var validationfields;
	var validpair;
	var objform = document.forms[formName];
	if(objform.req_fields)
	{
		reqfields = objform.req_fields.value.split(",");
		for(var i = 0; i < reqfields.length; i++)
		{
			if(objform.elements[reqfields[i]])
			{
				if(objform.elements[reqfields[i]].value == "")
				{
					alert('<%= (CmsMsg("js: alert cannot submit meta incomplete"))%>');
					//objform.elements[reqfields[i]].focus();
					return false;
				}
			}
		}
	}
	if(objform.needed_validation)
	{
		validationfields = objform.needed_validation.value.split(":");
		for(var j = 0; j < validationfields.length; j++)
		{
			validpair = validationfields[j].split(",");
			if(objform.elements[validpair[0]])
			{
				if(validpair[1] == "number")
				{
					if(!numberValidate(objform.elements[validpair[0]]))
					{
						return false;
					}
				}
				else if(validpair[1] == "text")
				{
					if(!textareaValidate(objform.elements[validpair[0]]))
					{
						return false;
					}
				}
				else if(validpair[1] == "date")
				{
					var originalDate;
					var arrayForNumber = validpair[0].split("_");
					if(arrayForNumber.length > 0)
					{
						if(arrayForNumber[0] = "frm_text")
						{
							originalDate = "frm_text_sel" + arrayForNumber[arrayForNumber.length-1];
							objform.elements[originalDate].value = Trim(objform.elements[originalDate].value);
						}
					}
					else
					{
						originalDate = validpair[0];
					}
					if(!CheckDate(objform.elements[originalDate].value))
					{
						return false;
					}
				}
			}	
		}
	}
	return true;
}