var requestInProgress = false;

// Removes leading whitespaces
function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

// Removes trailing whitespaces
function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}

// Removes whitespaces from both ends
function trim( value ) {
	return LTrim(RTrim(value));
}

// Utility function to add an event listener
function addEvent(o,e,f){
	if (o.addEventListener){ o.addEventListener(e,f,true); return true; }
	else if (o.attachEvent){ return o.attachEvent("on"+e,f); }
	else { return false; }
}

// Validates if a request can be processed - checks if some other request is already in progress
function validateSubmit() {
	if (requestInProgress) {
		alert("Please wait while we process your request.");
		return false;
	}
	else {
		requestInProgress = true;
		return true;
	}
}

// submit default form - by default, don't disable Subsequent Requests
function submitForm() {
	return submitFormCheck(true);
}

// submit default form - provides an option to disable Subsequent Requests
function submitFormCheck(disableSubsequentRequests) {
	return submitFormNameCheck("", disableSubsequentRequests);
}

// submit named form - by default, don't disable Subsequent Requests
function submitFormName(formName) {
	return submitFormNameCheck(formName, true);
}

// submit named form - provides an option to disable Subsequent Requests
function submitFormNameCheck(formName, disableSubsequentRequests) {
	if (disableSubsequentRequests) {
		if (!validateSubmit()) {
			return false;
		}
	}

	var form = getForm(formName);
	if (form == null) {
		form = getDefaultForm();
	}

	form.submit();
	return true;
}

// returns a reference to a named form
function getForm(formName) {
	if (formName == null || formName == '') {
		return null;
	} 
	else {
		return document.forms[formName];
	}
}

// returns a reference to the default form (first one in the document)
function getDefaultForm() {
	return document.forms[0];
}

// navigate to the specified url - provides an option to disable Subsequent Requests
function processLink(url, disableSubsequentRequests) {
	// navigate to the specified link only if it is valid to process a request
	if (disableSubsequentRequests) {
		if (validateSubmit()) {
			location.href = url;
		}
	}
}

// navigate to the specified url - by default, don't disable Subsequent Requests
function processLink(url) {
	processLink(url, false);
}

function openNewTab(url) {
	return window.open(url);
}


function closeTab(url) {
	return window.close(url);
}


function openPopupWindow(url) {
	var newWin = window.open(url, '_blank');
	newWin.focus();
	return newWin;
}

function popupWindow(url, winName) {
	var winPopup = window.open(url, winName,
		'height=400,width=500,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no');
	winPopup.focus();
}

function getHtmlControl(ctlName) {
	var obj;
	if (document.all) {
		obj = document.all[ctlName];
	} 
	if (!obj && document.getElementById) {
		obj = document.getElementById(ctlName);
	}
	if (!obj) {
		obj = document.getElementsByName(ctlName);
		if (obj.length) {
			return obj[0];
		} 
	}
	if (!obj) {
		obj = document.forms[0][ctlName];
	}
	return obj;
}

function disableHtmlControl(ctlName) {
	var obj = getHtmlControl(ctlName);
	if (obj) {
		if (obj.type == "text") {
			obj.readOnly = true;
		} else {
			obj.disabled = true;
		}
		obj.className = "readonly";
	}
}

function showElement(ctlName, show) {
	var obj = getHtmlControl(ctlName);
	obj.style.visibility = (show ? "visible" : "hidden");
}

function setControlValue(ctlName, value) {
	var obj = getHtmlControl(ctlName);
	if (obj) {
		obj.value = value;
	}
}

function getControlValue(ctlName) {
	var obj = getHtmlControl(ctlName);
	return (obj) ? obj.value : "";
}

function isIE() {
	var agent = navigator.userAgent.toLowerCase();
	return ((agent.indexOf("msie") > 0) && (agent.indexOf("opera") < 0));
}

function isIE7() {
	var agent = navigator.userAgent.toLowerCase();
	return isIE() && (agent.indexOf("msie 7") > 0 || agent.indexOf("msie 8") > 0);
}

function isUndefined(value) {
	return (undefined == value || null == value);
}

