// shows element of the given id
function showElement (id) {
	var el = document.getElementById(id);
	if (el) {
		el.style.display = '';
	}
}

// hide element of the given id
function hideElement (id) {
	var el = document.getElementById(id);
	if (el) {
		el.style.display = 'none';
	}
}

// hides the elements with the id's in array
function hideElements(idsArray){
	if (idsArray !== null){		
		for (var i=0; i < idsArray.length; i++){			
			hideElement(idsArray[i]);
		}
	}
}

// shows the elements with the id's in array
function showElements(idsArray){
	if (idsArray !== null){
		for (var i=0; i < idsArray.length; i++){
			showElement(idsArray[i]);
		}
	}
}

function isEmpty(str){
	return (str === null || str.length === 0);
}

function isNotEmpty(str){
	return (str !== null && str.length > 0);
}

function isNull(el){
	return (el === null || el === undefined);
}

function isNotNull(el){
	return (el !== null && el !== undefined);
}

function $(elId){
	return document.getElementById(elId);
}

function addListener(element, type, expression, bubbling)
{
	bubbling = bubbling || false;
	if(window.addEventListener) { // Standard
		element.addEventListener(type, expression, bubbling);
		return true;
	} else if(window.attachEvent) { // IE
		element.attachEvent('on' + type, expression);
		return true;
	} else { 
		return false;
	}
}

/**
 * Validation of email address through regular expression.
 * @param email
 * @return
 */
function validateEmail(email) {
	return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email);
}

/**
 * Returns the value of the DOM element
 * 
 * @param elId
 * @return
 */

function getValue(elId){
	var el = $(elId);
	if (isNotNull(el)){
		return el.value;
	}
	return;
}
 
function createWindow (url, name, x, y, w, h, props) {
		
	if (!x) { x = 100; }

	if (!y) { y = 100; }
	
	if (!w) { w = 650; }

	if (!h) { h = 500; }

	var properties = "toolbar=no,directories=no,resize=yes,resizable=yes,menubar=yes,location=no,scrollbars=yes,status=yes,";
	if (props !== null) {
		properties = props;
	}
	properties += "screenX=" + x + ",screenY=" + y + ",";
	properties += "left=" + x + ",top=" + y + ",";
	properties += "width=" + w + ",height=" + h;
	
	// while writing 'name' there should be no spaces btn them,
	// otherwise pop up window will not be created in IE.
	myPopup = window.open(url, name, properties);
	if (myPopup && myPopup !== null && !myPopup.opener) {
         myPopup.opener = self; 
    }
	if (window.focus) { myPopup.focus (); }
}
/**
* For example if the current URL is "...?opendocument&id=testid" then calling 
* getURLParam("id") will return "testid".
* @param strParamName
* @return
*/
function getURLParam(strParamName){
	  var strReturn = "";
	  var strHref = window.location.href;
	  if ( strHref.indexOf("?") > -1 ){
	    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
	    var aQueryString = strQueryString.split("&");
	    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
	      if (aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
	        var aParam = aQueryString[iParam].split("=");
	        strReturn = aParam[1];
	        break;
	      }
	    }
	  }
	  return unescape(strReturn);
}

/**
 * validation for fields. str is the error message. args is the dynamic variable
 * name.
 */
function formatMessage(userMessage, args) {
	if (args && args.length > 0) {
		for ( var i = 0; i < args.length; i++) {
			// Regular Expression to match the pattern '{number}'
			var regStr = "\\{(\\s)*" + i + "(\\s)*\\}";
			var regExp = new RegExp(regStr, "g");
			userMessage = userMessage.replace(regExp, args[i]);
		}
	}

	if (userMessage && userMessage.length > 0) {
		return userMessage;
	}
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

//chop it to length chars.
function getTruncateString(inputStr, length) {
	
	if (inputStr !== null) {
		// Removes leading and ending whitespaces
		inputStr = inputStr.trim();
		var len = parseInt(inputStr.length, 10);
		if (len < length) {
			// the inputStr is less than length characters; do nothing.
			return inputStr;
		}
		// find the last space before the 'length'th character
		var lastSpaceIx = parseInt(inputStr.lastIndexOf("<br/>", length));
		if (lastSpaceIx == -1) {
			lastSpaceIx = parseInt(inputStr.lastIndexOf(" ", length), 10);
		}
		if (lastSpaceIx !== -1) {

			// replace the space with a <br/>
			var firstLine = inputStr.substring(0, lastSpaceIx);
			return firstLine;
			//return firstLine + "...";
		} else {
			return inputStr.substring(0, length);
			//return inputStr.substring(0, length) + "...";
		}
		return "";
	}
}

function removeHTMLTags(data) {
 	if(data !== null && data.length > 0) {
 		var strInputCode = data;
 		
 		// removes the style tags and content between the style tags.
 		strInputCode = strInputCode.replace(/<style.*?>(.*?)<\/style>/g, "");
 		
 		// removes the xml tags and content between the xml tags.
 		strInputCode = strInputCode.replace(/<xml>([^*]|[\\r\\n])*<\/xml>/g, "");
 		/* 
  			This line is optional, it replaces escaped brackets with real ones, 
  			i.e. < is replaced with < and > is replaced with >
 		*/	
 	 	strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1){
 		 	return (p1 == "lt")? "<" : ">";
 		});
 		var strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
 		//alert("Output text:\n" + strTagStrippedText);
 		return strTagStrippedText;
 	}	
}
function validateDate(date) {
	re = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
	if(date != '' && !date.match(re)) { 
		return false; 
	}
	return true;
}

/* This function is used to set cookies */
function setCookie(name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
}

/* This function is used to get cookies */
function getCookie(name) {
	var prefix = name + "=" 
	var start = document.cookie.indexOf(prefix) 

	if (start==-1) {
		return null;
	}
	
	var end = document.cookie.indexOf(";", start+prefix.length) 
	if (end==-1) {
		end=document.cookie.length;
	}

	var value=document.cookie.substring(start+prefix.length, end) 
	return unescape(value);
}

/* This function is used to delete cookies */
function deleteCookie(name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function confirmDelete(obj) {   
    var msg = "Click Ok to confirm deleting this " + obj + "?";
    ans = confirm(msg);
    if (ans) {
        return true;
    } else {
        return false;
    }
}
