
function trim(value) {
	if(value != null) {
		var temp = value;
		var obj = /(^\s*)([\S].*[\S])*(\s*$)/;
		if (obj.test(temp)) {
			temp = temp.replace(obj, '$2');
		}
		return temp;
	}
	return "";
}

function getElement(elemId){
	var elem = (document.getElementById) ? document.getElementById(elemId) :
		((document.all) ? document.all(elemId) : null);
	return elem;
}

function getTextButton(onclickFunction, displayText) {
	var text = '<span onmouseover="this.style.cursor=\'pointer\';" ';
	text += 'onmouseout="this.style.cursor=\'default\';" onclick="' + onclickFunction + ';">' + displayText + '</span>';
	return text;
}

function getTableCell(row, align, width) {
	var col = row.insertCell(-1);
	col.align=align;
	col.width=width;
	col.vAlign="top";
	return col;
}

function getCheckbox(idKey, value, edit, textLabel) {
	var textString = '';
	if(edit) {
		textString += '<input type="checkbox" id="' + idKey + '" name="' + idKey + '" value="true"';
		if(value=='true') {
			textString += ' checked';
		}
		textString += '> ' + textLabel;
	} else {
		textString += textLabel + ': ' + value;
	}
	return textString;
}

function getTextField(idKey, value, edit, maxLength, size) {
	var textString = '';
	if(edit) {
		textString += '<input type="text" id="' + idKey + '" name="' + idKey + '" value="' + value + '"';
		if(maxLength > 0) {
			textString += ' maxlength="' + maxLength + '"';
		}
		if(size > 0) {
			textString += ' size="' + size + '"';
		}
		textString += '>';
	} else {
		textString += value;
	}
	return textString;
}

function textAreaMaxLengthCheck(textArea, maxLimit) {
	// if too long...trim it!
	if (textArea.value.length >= maxLimit) {
		textArea.value = textArea.value.substring(0, maxLimit);
		alert('Maximum number of characters (' + maxLimit + ') has been exceeded.');
	}
}

function getTextArea(idKey, value, edit, rows, columns, maxLength) {
	var textString = '';
	if(edit) {
		textString += '<textarea rows="' + rows + '" cols="' + columns + '" id="' + idKey + '" name="' + idKey + '"';
		if(maxLength > 0) {
			textString += ' onkeydown="textAreaMaxLengthCheck(this, ' + maxLength + ');" onkeypress="textAreaMaxLengthCheck(this, ' + maxLength + ');"';
		}
		textString += '>' + value + '</textarea>';
	} else {
		textString += value;
	}
	return textString;
}

function sendDataToServer(dataArrayToSend, url) {
	var postString = '';
	var firstData = true;
	for(key in dataArrayToSend) {
		if(firstData) {
			firstData = false;
		} else {
			postString += '&';
		}
		postString += key + "=" + escape(dataArrayToSend[key]);
	}
	var xmlRequest = newXMLHttpRequest();
	xmlRequest.overrideMimeType('text/xml');
	// true is asynchronous, false is synchronous
	xmlRequest.open("POST",url,false);
	xmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
	xmlRequest.send(postString);
	
	if (xmlRequest.status == 200) {
			var quoteHTML = '';
			var errorString = '';
			errorNodes = xmlRequest.responseXML.documentElement.getElementsByTagName("error");
			for(var i = 0; i < errorNodes.length; i++) {
				if(i > 0) {
					errorString += '<br>';
				}
				errorString += errorNodes[i].firstChild.nodeValue;
			}
			if(errorString != '') {
				throw errorString;
			}
			resultString  = xmlRequest.responseXML.documentElement.getElementsByTagName("result")[0].firstChild.nodeValue;

			if(resultString == '') {
				results = null;
			} else {
				eval('results = ' + resultString + ';');
			}

			return results;
		} else {
			return 'Error saving changes.  Please try again';
		}
}


/*
 * Returns a new XMLHttpRequest object, or false if this browser
 * doesn't support it. Script found at
 * http://www-128.ibm.com/developerworks/library/j-ajax1/
 */
function newXMLHttpRequest() {

  var xmlreq = false;
  if (window.XMLHttpRequest) {
    // Create XMLHttpRequest object in non-Microsoft browsers
    xmlreq = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    // Create XMLHttpRequest via MS ActiveX
    try {
      // Try to create XMLHttpRequest in later versions
      // of Internet Explorer
      xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e1) {
      // Failed to create required ActiveXObject
      try {
        // Try version supported by older versions
        // of Internet Explorer
        xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e2) {
        // Unable to create an XMLHttpRequest with ActiveX
      }
    }
  }
  return xmlreq;
}
/*
 * Returns a new XMLHttpRequest object, or false if this browser
 * doesn't support it. Script found at
 * http://www-128.ibm.com/developerworks/library/j-ajax1/
 */
function handleXMLResponse(req, responseXmlHandler, otherData) {
	// Return an anonymous function that listens to the 
	// XMLHttpRequest instance
	return function () {
		// If the request's status is "complete"
		if (req.readyState == 4) {
			// Check that a successful server response was received
			if (req.status == 200) {
				// Pass the XML payload of the response to the 
				// handler function
				// alert(req.getAllResponseHeaders());
				// alert(req.responseXML);
				responseXmlHandler(req.responseXML, otherData);
			} else {
				// An HTTP problem has occurred
				alert("HTTP error: "+req.status);
			}
		}
	}
}

