
function getElementByID(id)
{
    if (document.getElementById) return document.getElementById(id);
    if (document.all) return document.all[id];
    if (document.layers) return document.layers[id];
    return $find(id);
}

function getRef(id)
{
    return getElementByID(id);
}

/******************************
    ERROR MESSAGE
******************************/

var errMsgArr = new Array();

// Add error message to the collection
function addErrMessage(msg)
{
    errMsgArr.push(msg);
}

// Show all error messages
function showErrMessage()
{
    var message = '';
    for (var i=0; i<errMsgArr.length;i++)
    {
        message += errMsgArr[i] + '\n';
    }
    errMsgArr = new Array();
    alert(message);
}

/******************************
    COOKIES
******************************/
function setCookie(name, value, days) {
    try {
	    var expires;
	    
	    if (days == null)
	        days = 30;
    	
	    var date = new Date();
	    date.setTime(date.getTime() + (days*24*60*60*1000));
	    expires = "; expires=" + date.toGMTString();

	    document.cookie = name + "=" + value + expires + "; path=/";
    }
    catch (err) {}
}

function getCookie(name) {
    try {
	    name += "=";
	    var ca = document.cookie.split(';');
	    for(var i = 0; i < ca.length; i++) {
		    var c = ca[i];
		    while (c.charAt(0)==' ') c = c.substring(1,c.length);
		    if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
	    }
	    return null;
    }
    catch (err) {}
}

/******************************
    JS DEBUG
******************************/
var logCount = 0;

function writeLog(content) {
    c = new Date();
    
    if (logCount > 30) {
        logCount = 0;
        getRef('JsDebug').innerHTML = '';
    }
    
    getRef('JsDebug').innerHTML += '[' + formatHMS(c.getHours()) + ':' + formatHMS(c.getMinutes()) + 
        ':' + formatHMS(c.getSeconds()) + '.' + formatMS(c.getMilliseconds()) + '] ' + 
        content + '<br>';
    
    logCount++;
}

function logInfo(content) {
    writeLog('[INFO] ' + content);
}

function logErr(content) {
    writeLog('[ERRR] ' + content);
}

function logWarning(content) {
    writeLog('[WARN] ' + content);
}

function formatHMS(hms) {
    if (hms < 10) return '0' + hms;
    return hms;
}

function formatMS(ms) {
    if (ms < 10) return '00' + ms;
    if (ms < 100) return '0' + ms;
    return ms;
}
/*###########################*/

var newWindow;

//Vantd: OpenWin and auto move center screen 
function NewWindow(url,w,h){
    // generate random window name
    var randomnumber=Math.floor(Math.random()*11);
    var windowName = "NewWindows" + randomnumber.toString();;
    LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
    TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
    settings ='height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars=yes,resizable';
    newWindow = window.open(url,windowName,settings)
}

// Open a popup
function OpenPopup(url, popupName, attribute)
{
    newWindow = window.open(url, popupName, attribute);
}

// Open a dialog (modal popup)
function OpenDialog(url, popupName, attribute)
{
    newWindow = window.open(url, popupName, attribute);
    
    // TODO:
    //if (window.showModalDialog) {
    //    window.showModalDialog(url, popupName, attribute);
    //} else {
    //    window.open(url,popupName, attribute + ',modal=yes');
    //}
}

/*########### for listbox ################*/
function addOption(selectObject,optionText,optionValue) {
        var optionObject = new Option(optionText,optionValue)
        var optionRank = selectObject.options.length
        selectObject.options[optionRank]=optionObject
    }

function deleteOption(selectObject,optionRank) {
    if (selectObject.options.length!=0) { selectObject.options[optionRank]=null }
}