var MS_ONE_SEC = 1000;
var MS_ONE_MIN = MS_ONE_SEC * 60;
var MS_ONE_HOUR = MS_ONE_MIN * 60;
var MS_ONE_DAY = MS_ONE_HOUR * 24;

function showTableRow (tableID, rowID) {
	updateTableRowDisplay(tableID, rowID, "");
}

function hideTableRow (tableID, rowID) {
	updateTableRowDisplay(tableID, rowID, "none");
}

function updateTableRowDisplay (tableID, rowID, display) {
	var tableObject = document.getElementById(tableID);
	for (i=0; i < tableObject.rows.length; i++) {
		if (tableObject.rows[i].id == rowID) {
			tableObject.rows[i].style.display=display;	
			break;
		}
	}
}

function popup (loc) {
  var winl = (screen.width-750)/2;
  var wint = (screen.height-640)/2;
  var winName = "myWin_Popup";
  var win = window.open(loc, winName, "status=1,top="+wint+",left="+winl+",height=500,width=700,resizable=yes,scrollbars=1");
  win.focus();
}

function popup1 (loc, height, width) {
  var winl = (screen.width-750)/2;
  var wint = (screen.height-640)/2;
  var winName = "myWin_Popup1";
  var win = window.open(loc, winName, "status=1,top="+wint+",left="+winl+",height="+height+",width="+width+",resizable=yes,scrollbars=1");	
  win.focus();
}

function popup400x600 () {
  var winl = (screen.width-750)/2;
  var wint = (screen.height-640)/2;
  var winName = "myWin_400x600";
  var win = window.open(loc, winName, "status=1,top="+wint+",left="+winl+",height=400,width=600,resizable=yes,scrollbars=1");
  win.focus();
}

function miniPopup () {
  var winl = (screen.width-750)/2;
  var wint = (screen.height-640)/2;
  var winName = "miniPopup";
  var win = window.open(loc, winName, "status=1,top="+wint+",left="+winl+",height=180,width=400,resizable=yes,scrollbars=1");
  win.focus();
}

function popup600x700 (loc) {
  var winl = (screen.width-750)/2;
  var wint = (screen.height-640)/2;
  var winName = "myWin_600x700";
  var win = window.open(loc, winName, "status=1,top="+wint+",left="+winl+",height=600,width=700,resizable=yes,scrollbars=1");
  win.focus();
}

function helpPopup (loc) {
  var winl = (screen.width-750)/2;
  var wint = (screen.height-640)/2;
  var winName = "myWin_HelpPopup";
  var win = window.open(loc, winName, "status=1,top="+wint+",left="+winl+",height=600,width=800,resizable=yes,scrollbars=1");
  win.focus();
}

function uncheckAll (checkBoxes) {
	for (i=0; i < checkBoxes.length; i++) {
		checkBoxes[i].checked = false;
	}
}

function collapseAction (divID, imageID) {
	var div = document.getElementById(divID);
	var image = document.getElementById(imageID);
	if (div.style.display=="none") {
		div.style.display="inline";
		image.src="/images/minus.gif";
	} else {
		div.style.display="none";
		image.src="/images/plus.gif";
	}
}

function refreshCurrentPage () {
	refreshPage(document.location+"");
}

function refreshPage (loc) {
	loc = loc.replace(/&jsrefresh=0\.([0-9]+)$/gi, "");
	document.location = loc + "&jsrefresh=" + Math.random();
}

function getRefreshUrl (loc) {
    	loc = loc.replace(/&jsrefresh=0\.([0-9]+)$/gi, "");
	return (loc + "&jsrefresh=" + Math.random());
}

function showDiv (divID) {
	document.getElementById(divID).style.display="";
}

function hideDiv (divID) {
	document.getElementById(divID).style.display="none";
}

function changeImageSrc (src, imageID) {
	img = document.getElementById(imageID);
	img.src = src;
}

function countDownTimer (divID, totalMillis, evalStr) {
        countDownTimer1(divID, totalMillis, 0, evalStr);
}

function countDownTimer1 (divID, totalMillis, currentSecs, evalStr) {
        var divObj = document.getElementById(divID);
        if (currentSecs >= (totalMillis/1000)) {
                //onbeforeunload ... do nothing..
                window.onbeforeunload = function () {};
                divObj.innerHTML = "00:00:00";
                eval(evalStr);
                return;
        } else {
		var ms = totalMillis - (currentSecs * MS_ONE_SEC);
		var hours = Math.floor( ms / MS_ONE_HOUR);
		var mins = Math.floor(ms / MS_ONE_MIN) % 60;
		var secs = Math.floor(ms / MS_ONE_SEC) % 60;
                divObj.innerHTML = (hours < 10 ? "0" + hours : hours) + ":" + 
	                           (mins < 10 ? "0" + mins : mins) + ":" + 
	                           (secs < 10 ? "0" + secs : secs);
                setTimeout("countDownTimer1(\"" + divID +"\", " + totalMillis + ", " + (currentSecs +1) + ", \"" + evalStr.replace(/\"/gi,"\\\"") + "\");",1000);
        }

}


function render1 (firstP, secondP) {
	document.write(firstP + "@" + secondP);	
}

function confirmExitPage (msg) {
	return msg;	
}

function ajaxLink(url, elementID) {
    ajaxLink1(url, document.getElementById(elementID));
}

function ajaxLink1(url, element) {
    var r = getAjaxRequest(url, element);
    r.onreadystatechange = function () {
        if (r.readyState != 4) {
            return;
        }
        element.innerHTML = r.responseText;
    }
    element.innerHTML = '<img src="/images/waiting.gif">';
}

function getAjaxRequestFinal(url, element, encURL) {
    var req = (window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"));
    if (!element) {
        alert("Element: " + element.id + " not found");
        return;
    }
    if (!req) {
        element.innerHTML = "Could not execute AJAX Request";
        return;
    }

    sendAjaxRequest(url, req, encURL);
    return req;
}

function getAjaxRequest(url, element) {
    return getAjaxRequestFinal(url, element, true);
}

function sendAjaxRequest(url, req, encURL) {
    url = (url.indexOf("?") > 0 ? url.split("?")[0] + "?ajax=true&" + url.split("?")[1] : url + "?ajax=true");
    url = (encURL ? encodeURL(url) : url);
    var baseurl = url.split("?")[0];
    req.open("POST", baseurl ,true);
    req.setRequestHeader('Content-type','application/x-www-form-urlencoded;charset=UTF-8');
    req.setRequestHeader('Connection', 'close');
    var params = url.split("?")[1];
    req.setRequestHeader('Content-length', params.length);
    req.send(params);
}

function encodeURL(url) {
    if (url.split("?").length == 0) {
        return;
    }
    url = url.split("#")[0];
    var ary = url.split("?")[1].split("&");
    var params = "";
    for (i=0; i < ary.length; i++) {
        params += ary[i].split("=")[0] + "=" + encodeURIComponent(ary[i].split("=")[1]) + "&";
    }
    return url.split("?")[0] + "?" + params.replace(/\&$/gi,"");
}

function convertFormToURL(formObj) {
    var str = "";
    for (i=0; i < formObj.elements.length;i++) {
        if (formObj[i].name && formObj[i].name != '' &&
            formObj[i].type &&
            formObj[i].type.toLowerCase() != 'button' &&
            formObj[i].type.toLowerCase() != 'submit') {
            if (formObj[i].type.toLowerCase() == 'checkbox' || formObj[i].type.toLowerCase() == 'radio') {
                str += (formObj[i].checked ?
                        formObj[i].name + "=" + encodeURIComponent(formObj[i].value) + "&" : "");
            } else {
                if (formObj[i].type.toLowerCase().indexOf('select') >= 0 &&
                    formObj[i].multiple) {
                    for (multiInd=0; multiInd < formObj[i].options.length; multiInd++) {
                        if (formObj[i].options[multiInd].selected) {
                            str += formObj[i].name + "=" + encodeURIComponent(formObj[i].options[multiInd].value) + "&";
                        }
                    }
                } else {
                    str += formObj[i].name + "=" + encodeURIComponent(formObj[i].value) + "&";
                }
            }
        }
    }
    str =  formObj.action + "?" + str.replace(/\&$/gi,"");
    return str;
}

function ajaxFormSubmit(formObj, elementID) {
    var url = convertFormToURL(formObj);
    var element = document.getElementById(elementID);
    var r = getAjaxRequestFinal(url, element, false);
    r.onreadystatechange = function () {
        if (r.readyState != 4) {
            return;
        }
	element.innerHTML = r.responseText;
    }
    element.innerHTML = '<img src="/images/waiting.gif">';
    return false;
}

function stringTrim(s) {
	s = s.replace(/^([ \r\n]+)/gi, "");
	s = s.replace(/([ \r\n]+)$/gi, "");
	return s;
}

function setElementText(divID, txt) {
    document.getElementById(divID).innerHTML = txt;
}

function clearText(origTxt, field) {
    if (origTxt == field.value) {
	field.value = '';
    }
    field.onblur = function () {
	if (field.value == '') {
	    field.value = origTxt;
	}
    };
}

function setStyle(element, styleText) {
    element.setAttribute("style", styleText);
    element.style.cssText = styleText;
}

function setStyleClass(element , className) {
    element.className=className;
}

function addDiv(divID) {
    var dialog = document.createElement('div');
    dialog.innerHTML = '<div id="' + divID + '"></div>';
    document.body.appendChild(dialog);
    return document.getElementById(divID);
}


/*** nicEdit WYSIWYG ***/

function addWysiwyg(elementID) {
    window.onload = function() {
	new nicEditor({fullPanel : true}).panelInstance(elementID);
    };
}

function dropWysiwyg(editor, elementID) {
    editor.removeInstance(elementID);
    editor = null;
}

function putWysiwygContent(wysiwyg, elementID) {
    if (wysiwyg) {
	document.getElementById(elementID).value = nicEditors.findEditor(wysiwyg).getContent();
    }
}

function setWysiwygContent(wysiwyg, text) {
    nicEditors.findEditor(wysiwyg).setContent(text);
}

/*** nicEdit END ***/
