commonUtil = new Object();

commonUtil.URL_LIMIT = 2000;
commonUtil.validationPassed = true;
/**
 * stops the event from bubbling up, this works for both
 * IE and Firefox
**/
commonUtil.cancelBubble = function(e) {
    if (!e) {
        e = window.event;
    }
    e.cancelBubble = true;
    if (e.stopPropagation) {
        e.stopPropagation();
    }
}

/**
 * return the absolute X location of an element in the page
**/
commonUtil.calcPageX = function(o) {
    var x = 0;
    while (o) {
        x += parseInt(o.offsetLeft);
        o = o.offsetParent || null;
    }

    return x;
}

/**
 * return the absolute Y location of an element in the page
**/
commonUtil.calcPageY = function(o) {
    var x = 0;
    while (o) {
        x += parseInt(o.offsetTop);
        o = o.offsetParent || null;
    }

    return x;
}

/**
 * return true is the browser is Internet Explorer
**/
commonUtil.browserIsIE = function() {
    var browser = navigator.appName;
    if (browser.indexOf("Microsoft") >=0) {
        return true;
    } else {
        return false;
    }
}

/**
 * return true is the browser is Mozilla based
**/
commonUtil.browserIsMozilla = function() {
    var userAgent = navigator.userAgent;

    if (userAgent.indexOf("Mozilla/5.0") >= 0) {
        return true;
    } else {
        return false;
    }
}

/**
 * return true is the event happened inside the object
**/
commonUtil.isEventInObj = function(obj, evt) {
    if (!evt) var evt = window.event;
    var tg = (window.event) ? evt.srcElement : evt.target;
    while (tg && tg.nodeName != 'BODY') {
        if (tg == obj) {
            return true;
        } else {
            tg = tg.parentNode;
        }
    }

    return false;
}

/**
 * return true is the object is the target of the event
**/
commonUtil.isEventForObj = function(obj, evt) {
	if (!evt) var evt = window.event;
	var tg = (window.event) ? evt.srcElement : evt.target;
    return (obj == tg);
}

commonUtil.getPostDataFromForm = function(frm) {
	var data = new Array();
	for (var i = 0; i < frm.elements.length; i++) {
        if(frm.elements[i] && frm.elements[i].name) {
            if (frm.elements[i].type == 'checkbox') {
                // checkbox
                if (frm.elements[i].checked) {
                    data[frm.elements[i].name] = frm.elements[i].value;
                } else {
                	data[frm.elements[i].name] = '';
                }
            } else {
                data[frm.elements[i].name] = frm.elements[i].value;
            }
        }
    }
    return data;	
}

commonUtil.adjustIframeSizeToFit = function(iframeName, dialogName, adjustWidth) {
	var iframe;
	if(!adjustWidth) adjustWidth = false; // default this to false
	if (dialogName) {	
		var dialogIF = document.getElementById(dialogName + 'IF');
        if(dialogIF == null) {
            return;
        }
		iframe = dialogIF.contentWindow.document.getElementById(iframeName);
		if (iframe) {
			if (commonUtil.browserIsIE()) {
				var command = 'var dialogIF = document.getElementById("' + dialogName + 'IF"); ';
				command += 'var iframe = dialogIF.contentWindow.document.getElementById("' + iframeName + '"); ';
				command += 'iframe.style.height = iframe.contentWindow.document.body.scrollHeight + "px";';
				setTimeout(command, 500);				
			} else {
				iframe.style.height = iframe.contentWindow.document.documentElement.clientHeight + "px";
			}
		}	
	} else {
		iframe = document.getElementById(iframeName);
		if (iframe) {
			if (commonUtil.browserIsIE()) {
				var cmd = "document.getElementById('" + iframeName + "').style.height = window.frames['" + iframeName + "'].document.body.scrollHeight + 'px'; ";
				if(adjustWidth) {
				    cmd += "document.getElementById('" + iframeName + "').style.width = window.frames['" + iframeName + "'].document.body.scrollWidth + 'px';";
				}
				setTimeout(cmd, 500);					
			} else {
				iframe.style.height = "auto";
				iframe.style.height = window.frames[iframeName].document.documentElement.clientHeight + "px";
				if(adjustWidth) {
					iframe.style.width = window.frames[iframeName].document.documentElement.clientWidth + "px";
				}
			}
		}	
	}
}

commonUtil.validationAction = function() {    
    commonUtil.validationPassed = true;
	// noop
}

commonUtil.validationFailAction = function() {    
    commonUtil.validationPassed = false;
	// noop
}

/**
 * This method closes the dialog and refresh the parent window
 */
commonUtil.closeDialogAndReloadParent = function(dialogName) {
	dialogUtil.hideDialog(dialogName);
	// refresh the current page
	window.location.reload(true);
}


/**
 * This returns a javascript string with properly escaped quotes. (', ")
 * This is useful for use in innerHtml operations.
**/
commonUtil.escapeQuotes = function(str) {
	str = str.replace('\\', '\\\\');
	str = str.replace("'", "\\'");
	return str;
}

// This function returns a string that contains a "stack trace."
commonUtil.stackTrace = function() {
    var s = "";  // This is the string we'll return.
    // Loop through the stack of functions, using the caller property of
    // one arguments object to refer to the next arguments object on the
    // stack.
    for(var a = arguments.caller; a != null; a = a.caller) {
        // Add the name of the current function to the return value.
        s += commonUtil.funcName(a.callee) + "\n";

        // Because of a bug in Navigator 4.0, we need this line to break.
        // a.caller will equal a rather than null when we reach the end 
        // of the stack. The following line works around this.
        if (a.caller == a) break;
    }
    return s;
}

// This function returns the name of a given function. It does this by
// converting the function to a string, then using a regular expression
// to extract the function name from the resulting code.
commonUtil.funcName = function(f) {
    var s = f.toString().match(/function (\w*)/);
    if ((s == null) || (s.length == 0) || (s[1] == null) || (s[1].length == 0)) return "anonymous";
    return s[1];
}

commonUtil.numbersConsecutive = function(numbers) {
	var sorted = numbers.sort(commonUtil.compareNumbers);
	for (var i=0; i<sorted.length-1; i++) {
		if (sorted[i+1] - sorted[i] != 1) {
			return false;
		}
	}
	
	return true;
}

commonUtil.compareNumbers = function(a, b) {
   return a - b;
}


commonUtil.showWaitCursor = function(bodyElement) {
	if(bodyElement) {
		bodyElement.style.cursor = "wait";
	} else {
		document.body.style.cursor = "wait";
	}
}

commonUtil.setInnerTxt = function(element, value, isIE){
    if(isIE){
        element.innerText = value;
    }else{
        element.textContent = value;
    }
}

commonUtil.disableEnable = function(item1, item2) {
   obj1=document.getElementById(item1);
   obj2=document.getElementById(item2);
   obj1.disabled = true;
   obj2.disabled = false;
}

commonUtil.openPopup = function(url, width, height, encodeParams){
    if (encodeParams) {
        url = url + encodeURIComponent(appendStr);
    }
    
    var name = "NBPOPUP";
    
    var winProperties =
    "toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no";
    var winWidth  = (width != null)  ? width  : 900;
    var winHeight = (height != null) ? height : 675;
    winProperties += ",width=" + winWidth + ", height=" + winHeight;
    
    win = window.open(url,
                name,
                winProperties);
                
    var topWidth = 1024;
    var topHeight = 768;
    if(top.document.body.parentElement && top.document.body.parentElement.clientWidth) {
        topWidth = top.document.body.parentElement.clientWidth;
        topHeight = top.document.body.parentElement.clientHeight;
    } else if (top.document.body && top.document.body.clientWidth) {
        topWidth = top.document.body.clientWidth;
        topHeight = top.document.body.clientHeight;
    }
    var newLeft = (Math.round((topWidth - winWidth) / 2) + document.body.scrollLeft);
    var newTop = (Math.round((topHeight - winHeight) / 2) + document.body.scrollTop);
    win.moveTo(newLeft, newTop);
    return win;
}

commonUtil.escapeForHTML = function(value) {
    var undef;
    if(value == undef || value == "") {
        return "";
    }

    // It's important that the ampersand is substituted first!
    value = value.replace(/&/gi, "&amp;");
    value = value.replace(/</gi, "&lt;");
    value = value.replace(/>/gi, "&gt;");
    value = value.replace(/\"/gi, "&quot;");
    value = value.replace(/\'/gi, "&#39;");

    return value;
}

/**
 * ENG: Return the px distance from left border of the element to left border of the window 
 **/
commonUtil.getElementLeft = function(p_elm) {
	var x = 0;
	var elm;
	if(typeof(p_elm) == "object"){
		elm = p_elm;
	} else {
		elm = document.getElementById(p_elm);
	}
	while (elm != null) {
		x+= elm.offsetLeft;
		elm = elm.offsetParent;
	}
	return parseInt(x);
}

/**
 * ENG: Return the px width of the element 
 **/
commonUtil.getElementWidth = function(p_elm){
	var elm;
	if(typeof(p_elm) == "object"){
		elm = p_elm;
	} else {
		elm = document.getElementById(p_elm);
	}
	return parseInt(elm.offsetWidth);
}

/**
 * ENG: Return the px distances from right border of the element to left border of window
 **/
commonUtil.getElementRight = function(p_elm){
	return getElementLeft(p_elm) + getElementWidth(p_elm);
}

/**
 * ENG: Return the px distances from top border of the element to top border of the window
 **/
commonUtil.getElementTop = function(p_elm) {
	var y = 0;
	var elm;
	if(typeof(p_elm) == "object"){
		elm = p_elm;
	} else {
		elm = document.getElementById(p_elm);
	}
	while (elm != null) {
		y+= elm.offsetTop;
		elm = elm.offsetParent;
	}
	return parseInt(y);
}

/**
 * ENG: Return the px heght of the element
 **/
commonUtil.getElementHeight = function(p_elm){
	var elm;
	if(typeof(p_elm) == "object"){
		elm = p_elm;
	} else {
		elm = document.getElementById(p_elm);
	}
	return parseInt(elm.offsetHeight);
}

/**
 * ENG: Return the px distances from bottom border of the element to border top of the window
 **/
commonUtil.getElementBottom = function(p_elm){
	return getElementTop(p_elm) + getElementHeight(p_elm);
}

commonUtil.trimString = function(myStr){
    var undefined;
    if (myStr == undefined)
       return undefined;
    var start = 0;
    var count = myStr.length;
    var end = count;
    while((start < count) && (myStr.charAt(start) <= ' ')) {
        start++;
    }
    while((start < end) && (myStr.charAt(end-1) <= ' ')) {
        end--;
    }
    return ((start > 0) || (end < count)) ? myStr.substring(start, end) : myStr;
}

function toggleDiv(img, divId) {
        var div = document.getElementById(divId);
        if (img.src.indexOf("minus") != -1) {
                img.src='images/plus.gif';
                div.style.display = "none";
        } else if (img.src.indexOf("plus") != -1) {
                img.src='images/minus.gif';
                div.style.display = "inline";
        }
}      