﻿
function addOption(sel, name, value)
{
    sel.options[sel.options.length] = new Option (name, value);
}

function addOptionSelected(sel, name, value, isSelected)
{
    var opt = new Option(name, value);
    opt.selected = isSelected;
    sel.options[sel.options.length] = opt;
}

function displayLookup(results, sel, unselText)
{
    sel.options.length = 0;
    for (i = 0; i < results.length; i++)
    {
        if (results[i] == "")
            addOption(sel, unselText, "");
        else
            addOption(sel, results[i], results[i]);
    }
}

function displayLookupSelected(results, sel, unselText, seleText)
{
    sel.options.length = 0;
    for (i = 0; i < results.length; i++)
    {
        if (results[i] == "")
            addOption(sel, unselText, "");
        else
        {
            var sele = (seleText == results[i]);
            addOptionSelected(sel, results[i], results[i], sele);
        }
    }
}

function selectLookupItem(sel, seleText)
{
    for (i=0; i<sel.options.length; i++)
    {
        if (sel.options[i].value == seleText)
            sel.options[i].selected = true;
    }
}

function showHide(theid)
{
    if (document.getElementById)
    {
        var switch_id = document.getElementById(theid);
        switch_id.style.display = (switch_id.style.display == "none") ? "block" : "none";
    }
}

function show(theid)
{
    theid.style.display = "block";
}

function hide(theid)
{
    theid.style.display = "none";
}

function onFailed(a, b, c)
{
    alert(a.get_message());
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function StringBuffer() { 
   this.buffer = []; 
}

StringBuffer.prototype.append = function append(string) { 
   this.buffer[this.buffer.length] = string; 
   return this;
};

StringBuffer.prototype.toString = function toString() { 
   return this.buffer.join(""); 
};

StringBuffer.prototype.length = function length() { 
   return this.buffer.length; 
};

function showWaiting(div)
{
    show(div);
    div.innerHTML = "<img src='/images/loading3.gif'><br />Please wait...";
}

function KeyEvent(event)
{
    // press key "Enter"
    if (event.keyCode == 13 || event.charCode == 13)
    {
        event.cancelBubble = true;
        if (event.stopPropagation)
            event.stopPropagation();
        if (event.preventDefault)
            event.preventDefault();
        
        doKeyPress(event);
            
        return false;
    } 
    else
        return true;
}  

function doKeyPress(event)
{
 // please leave empty!!
}   

function doPageLoad()
{
 // please leave empty!!
}

function showShowroomHint(e)
{
    var divShowroomHint = $get('divShowroomHint');
    moveToMouse(divShowroomHint, e);
	showHint(divShowroomHint);
}

function showRecentlyViewedHint(e)
{
    var divRecentlyViewedHint = $get('divRecentlyViewedHint');
    moveToMouse(divRecentlyViewedHint, e);
	showHint(divRecentlyViewedHint);
}

function hideShowroomHint(e)
{
    var divShowroomHint = $get('divShowroomHint');
    hideHint(divShowroomHint);
}

function hideRecentlyViewedHint(e)
{
    var divShowroomHint = $get('divRecentlyViewedHint');
    hideHint(divShowroomHint);
}

function moveToMouse(el, e) {
    e = e || window.event;
    
    var x, y;
    if (e.pageX || e.pageY) {
        x = e.pageX;
        y = e.pageY;
    } 
    else {
        var de = document.documentElement;
        var b = document.body;
        x = e.clientX - (de.clientLeft || 0);
        y = e.clientY - (de.clientTop || 0);
    }
	
	el.style.left = (x - 150) + "px";
	el.style.top = (y + 15) + "px";
}

function showHint(div)
{ 
    show(div);
    setTimeout("hide($get('" + div.id + "'))", 24000);
}

function hideHint(div)
{ 
    setTimeout("hide($get('" + div.id + "'))", 300);
}

function checknumber(x)
{
    var anum=/(^\d+$)|(^\d+\.\d+$)/
    if (anum.test(x))
        testresult=true;
    else
        testresult=false;
    return testresult;
}

var DateFormat = {
    getMonthName: function(i) {
    var m = ['','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
    var mnthName = '';
    if (isNaN(i))
        return m[0];
    else
        return m[i]; 
    } 
}
