﻿

// Utility functions

function doHourglass() { document.body.style.cursor = "wait"; }

function ltrim(s) {return s.replace( /^\s*/, "" );}
function rtrim(s) {return s.replace( /\s*$/, "" );}
function trim(s) {return rtrim(ltrim(s));}

function blinkElement(elementName) { var obj = document.getElementById("element"); if (obj.style.visibility=="hidden") obj.style.visibility="visible"; else obj.style.visibility="hidden"; setTimeout('blinkElement("' + elementName + '")',500); }

function attachEventHandler (eventName, handler)
{
    if (window.addEventListener) //DOM method for binding an event
        window.addEventListener(eventName, handler, false);
    else 
    if (window.attachEvent) //IE exclusive method for binding an event
        window.attachEvent("on" + eventName, handler);
}


// Browser Window Size and Position

function getLocation (element) { var offsetX = 0; var offsetY = 0; var parent; for (parent = element; parent; parent = parent.offsetParent) { if (parent.offsetLeft) offsetX += parent.offsetLeft; if (parent.offsetTop) offsetY += parent.offsetTop; } return { x: offsetX, y: offsetY }; }
function getBounds (element) { var offset = getLocation(element);  var width = element.offsetWidth; var height = element.offsetHeight; return { x: offset.x, y: offset.y, width: width, height: height }; }

function pageWidth() {return window.innerWidth != null? window.innerWidth: document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth:document.body != null? document.body.clientWidth:null;}
function pageHeight() {return window.innerHeight != null? window.innerHeight: document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight:document.body != null? document.body.clientHeight:null;}
function posLeft() {return typeof window.pageXOffset != "undefined" ? window.pageXOffset:document.documentElement && document.documentElement.scrollLeft? document.documentElement.scrollLeft:document.body.scrollLeft? document.body.scrollLeft:0;}
function posTop() {return typeof window.pageYOffset != "undefined" ? window.pageYOffset:document.documentElement && document.documentElement.scrollTop? document.documentElement.scrollTop: document.body.scrollTop?document.body.scrollTop:0;}
function posRight() {return posLeft()+pageWidth();}
function posBottom() {return posTop()+pageHeight();}


function addElement(tag, id, zIndex, left, top, width, height) 
{
  var newElement = document.createElement(tag);
  newElement.setAttribute("id", id);
  newElement.style.visibility = "hidden";
  newElement.style.zIndex = zIndex;
  newElement.style.display = "block";
  newElement.style.position = "absolute";
  newElement.style.left = left + "px";
  newElement.style.top = top + "px";
  newElement.style.width = width + "px";
  newElement.style.height = height + "px";
  document.body.appendChild(newElement);
  return newElement;
}

function removeElement(id) 
{
  var d = document.getElementById(id);
  if (d != null) document.body.removeChild(d);
}

/////////////

function generateLoadingHtml()
{
    return '<div align=center style="width: 100%; height: 100%;"><img src="images/loading.gif" border="0"/><br/>Loading...</div>';
}

// ButtonComboBox

var comboImageOn;
var comboImageOff;
var comboImagePressed;

if (document.images) 
{
    var comboImageOn = new Image();
    var comboImageOff = new Image();
    var comboImagePressed = new Image();

    comboImageOn.src = "images/combo_on.bmp"; 
    comboImageOff.src = "images/combo_off.bmp";
    comboImagePressed.src = "images/combo_pressed.bmp";
}


function handleComboOver(obj) { if (document.images) obj.src = comboImageOn.src; }

function handleComboOut(obj) { if (document.images) obj.src = comboImageOff.src; }

function handleComboPressed(obj) { if (document.images) obj.src = comboImagePressed.src; }


var ignoreFirstMouseDownEvent = false;
var popupDiv;

// Standard ComboBox

function handleComboClick1(containerName, objName) 
{
    var containerObj = document.getElementById(containerName);
    
    if (popupDiv != null)
    {
        var isOurPopup = popupDiv == containerObj.popupDiv;
        closePopup();
        if (isOurPopup) return;
    }
    
    popupDiv = containerObj.popupDiv;
    if (popupDiv == null)
    {
        var bounds = getBounds(containerObj);
        popupDiv = addElement("div", containerName + "$Popup", 1000, bounds.x, bounds.y + bounds.height, bounds.width, 200);
        popupDiv.mode = 1;
        popupDiv.style.height = "0px";
        popupDiv.style.borderWidth = "1px";
        popupDiv.style.borderStyle = "solid";
        popupDiv.style.borderColor = containerObj.style.borderColor;
        popupDiv.style.backgroundColor = "window";
        
        containerObj.popupDiv = popupDiv;
        popupDiv.textbox = objName;
        popupDiv.innerHTML =  buildDropDownTable1(document.getElementById(objName).Items);
        popupDiv.style.visibility = "visible";
        attachEventHandler("mousedown", onDocumentMouseDown);
    }
    else
    {
        popupDiv.style.visibility = "visible";
        ignoreFirstMouseDownEvent = true;
        attachEventHandler("mousedown", onDocumentMouseDown);
    }
}

// AJAX Combo

function handleComboClick2(containerName, objName, webServiceName, displayFields, valueField, textField) 
{
    var containerObj = document.getElementById(containerName);
    
    if (popupDiv != null)
    {
        var isOurPopup = popupDiv == containerObj.popupDiv;
        closePopup();
        if (isOurPopup) return;
    }
    
    popupDiv = containerObj.popupDiv;
    if (popupDiv == null)
    {
        var bounds = getBounds(containerObj);
        popupDiv = addElement("div", containerName + "$Popup", 1000, bounds.x, bounds.y + bounds.height, bounds.width, 200);
        popupDiv.mode = 2;
        popupDiv.style.borderWidth = "1px";
        popupDiv.style.borderStyle = "solid";
        popupDiv.style.borderColor = containerObj.style.borderColor;
        popupDiv.style.backgroundColor = "window";
        
        popupDiv.innerHTML = generateLoadingHtml();
        popupDiv.style.visibility = "visible";
        
        containerObj.popupDiv = popupDiv;
        popupDiv.textbox = objName;
        popupDiv.displayfields = displayFields;
        popupDiv.valuefield = valueField;
        popupDiv.textfield = textField;
    
        //Call script proxy passing the input element data
        eval(webServiceName + "({onMethodComplete:OnComboDataComplete,onMethodTimeout:OnComboDataTimeOut,onMethodError:OnComboDataError,onMethodAborted:OnComboDataAborted,userContext:popupDiv});");
    }
    else
    {
        popupDiv.style.visibility = "visible";
        ignoreFirstMouseDownEvent = true;
        attachEventHandler("mousedown", onDocumentMouseDown);
    }
}

function dropDownTableRowClicked (row)
{
    document.getElementById(popupDiv.textbox).value = row.text;
    document.getElementById(popupDiv.textbox + "$HiddenValue").value = row.value;
    closePopup();
}

function buildDropDownTable1(items)
{
    var html = '<table class="dropdowntable" style="width: 100%; height: 100%">';

    // Data
    for (r = 0; r < items.length; r++)
    {   var css = r % 2 ? '\'dropdowntable_row_even\'' : '\'dropdowntable_row_odd\'';
        var css_selected = '\'dropdowntable_row_selected\'';
        
        var row = items[r];
        html += '<tr text=\'' + row[1] + '\' value=' + row[0] + ' class=' + css + ' onmousedown="dropDownTableRowClicked(this);" onmouseover="this.className=' + css_selected + '" onmouseout="this.className=' + css + '">';
        html += "<td>" + row[1] + "</td>";
        html += "</tr>";
    }
    html += "</table>";
    return html;
}

function buildDropDownTable2(dt, displayFields, valueField, textField)
{
    var fields = displayFields.split(";");
    var fieldNos = new Array();
    
    for (i = 0; i < fields.length; i++)
    {
        for (j = 0; j < dt.get_columns().length; j++)
        {
            var column = dt.get_columns()[j];
            if (column.get_columnName() == fields[i]) { fieldNos[i] = j; break; }
        }
    }

    var html = '<table class="dropdowntable" style="width: 100%; height: 100%"><tr class="dropdowntable_caption">';

    // Header
    for (i = 0; i < fieldNos.length; i++)
    {
        var column = dt.get_columns()[fieldNos[i]];
        html += "<td>" + column.get_columnName() + "</td>";
    }
    html += "</tr>";

    // Data
    for (r = 0; r < dt.get_length(); r++)
    {   var css = r % 2 ? '\'dropdowntable_row_even\'' : '\'dropdowntable_row_odd\'';
        var css_selected = '\'dropdowntable_row_selected\'';
        var row = dt.getRow(r);
        html += '<tr text=\'' + row.getProperty(textField) + '\' value=' + row.getProperty(valueField) + ' class=' + css + ' onmousedown="dropDownTableRowClicked(this);" onmouseover="this.className=' + css_selected + '" onmouseout="this.className=' + css + '">';
        
        for (i = 0; i < fieldNos.length; i++)
        {
            var column = dt.get_columns()[fieldNos[i]];
            html += "<td>" + row.getProperty(column.get_columnName()) + "</td>";
            //debug.dump(row, "XXXX", true);
        }
        html += "</tr>";
    }
    html += "</table>";
    return html;
}

function OnComboDataComplete(result, response, userContext) 
{
    //debug.dump(userContext,"result",true); 
    popupDiv.innerHTML = buildDropDownTable(result, popupDiv.displayfields, popupDiv.valuefield, popupDiv.textfield);
    attachEventHandler("mousedown", onDocumentMouseDown);
}

 function OnComboDataTimeOut(request, userContext) 
{
    popupDiv.innerHTML = "Time out!"; 
}
   
function OnComboDataError(result, response, userContext) 
{
    var errorString = "Error: ";
    if (result == null)
        errorString += "Status code=" + response.get_statusCode();
    else 
        errorString += result.get_message(); // + "'\r\nstackTrace = " + result.get_stackTrace();
    popupDiv.innerHTML = errorString;
}  
   
function OnComboDataAborted(request, userContext) 
{
    popupDiv.innerHTML = "Request is aborted!";
}    


function closePopup()
{ 
    if (popupDiv != null)
    {   
        //document.body.removeChild(popupDiv);
        popupDiv.style.visibility = "hidden";
        popupDiv = null;
        document.detachEvent("onmousedown", onDocumentMouseDown);
        
        return true;
    }
    return false;
}

function isInside(container, obj)
{
    if (container == null || obj == null) return false;
    if (container == obj) return true;
    if (container.children != null)
    {
        var i;
        for (i = 0; i < container.children.length; i++)
        {   
            if (container.children(i) == obj) return true;
            if (isInside(container.children(i), obj)) return true;
        }
    } 
    return false;
}

function onDocumentMouseDown()
{
    if (ignoreFirstMouseDownEvent)
    {
        ignoreFirstMouseDownEvent = false;
        return true;
    }
    
    if (popupDiv == null) return true;
    
    if (isInside(popupDiv, event.srcElement)) return true;

    closePopup();
    return true;
}
 
 
function showSelects()
{
    var elements = document.getElementsByTagName("select");
    for (i = 0; i< elements.length; i++) elements[i].style.visibility = "visible";
}

function hideSelects()
{
    var elements = document.getElementsByTagName("select");
    for (i = 0; i< elements.length; i++) elements[i].style.visibility = "hidden";
}
 
// Message box

function generateMessageBoxHTML(caption, message)
{
    var s =
        '<div style="padding: 10px">' +
				'<embed src="http://www.eventica.co.uk/flash/russia.swf" quality="high" bgcolor="#a2a1a1" width="570" height="167"  name="ver1" align="middle" value="high" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />'

 + '</div>';
    return s;
}        

function repositionDimerDiv()
{
    var el = document.getElementById("dimmer");
    var pw = document.body.clientWidth;
    if (pw < pageWidth()) pw = pageWidth();
    el.style.width = pw + "px";
    
    var ph = document.body.clientHeight;
    if (ph < pageHeight()) ph = pageHeight();
    el.style.height = ph + "px";

    el = document.getElementById("dimmer_messagebox");
    el.style.left = ((pageWidth() - el.clientWidth) / 2) + "px";
    el.style.top = ((pageHeight() - el.clientHeight) / 2) + "px";
}

var oldOnResizeHandler;

function onDimmerDivResize()
{
    repositionDimerDiv();
    
    if (oldOnResizeHandler)
        oldOnResizeHandler();
}

function closeMessageBox()
{
    window.onresize = oldOnResizeHandler;
    document.getElementById("dimmer_messagebox").style.visibility = "hidden";
    document.getElementById("dimmer").style.visibility = "hidden";
    
    removeElement("dimmer_messagebox");
    removeElement("dimmer");
    
    showSelects();
}

function showDimmedElement(innerHtml, width, height) 
{
    removeElement("dimmer");
    removeElement("dimmer_messagebox");

    hideSelects();
    
    var newdiv = addElement("div", "dimmer", 20000, 0, 0, 0, 0);
    
    newdiv.style.filter = "alpha(opacity=40)";
    newdiv.style.backgroundColor = "#181818";
    newdiv.style.visibility = "visible";
    newdiv.style.positon = "fixed";
    
    newdiv = addElement("div", "dimmer_messagebox", 20001, 0, 0, width, height);
  //  newdiv.style.backgroundColor = "ButtonFace";
//    newdiv.style.borderColor = "ActiveCaption";
  //  newdiv.style.borderWidth = "2px";
   // newdiv.style.borderStyle = "solid";
    newdiv.style.visibility = "visible";    
    newdiv.innerHTML = innerHtml;
    
    repositionDimerDiv();    

    oldOnResizeHandler = window.onresize;
    window.onresize = onDimmerDivResize;
}

function messageBox(caption, message) 
{
    showDimmedElement(generateMessageBoxHTML(caption, message), 700, 126);
}

