﻿/* begin code for custom drop-down boxes */
var DD_currentObj = null; //stores the current object
//initialize global variables
var DD_isVisible = 0; //stores whether the drop down is visible or hidden
var DD_isActive = 0; //stores whether the textbox is active (being moused over) - helps decide whether or not it should be closed when "HideCurrentDropDown" is called
var DD_isTyping = 0;
//call DropDownItemSelect(obj) when the page loads to pre-select the value

//find the value to go to when the down arrow is pressed

//page up & page down buttons?
//home & end buttons?
//left & right arrows?

//this is called when the mouse is clicked anywhere on the page
function HideCurrentDropDown() {
    if ((DD_isActive == 0) && (DD_isVisible == 1) && (DD_currentObj != null))
        HideDropDown(DD_currentObj); //hide the current object
}

//this is called when the dropdown panel textbox is clicked on
function HandleDropDownClick(obj) {
    //determine if the dd is new and a current one exists
    if ((obj != DD_currentObj) && (DD_currentObj != null)) //a different drop-down has been clicked on
        HideDropDown(DD_currentObj); //hide the current object

    //show/hide the new object
    if (DD_isVisible == 0)
        ShowDropDown(obj);
    else
        HideDropDown(obj);
}

//  8 - backspace
// 13 - Return/Enter
// 27 - Escape
// 33 - page up
// 34 - page down
// 35 - end
// 36 - home
// 37 - Left arrow
// 38 - Up arrow
// 39 - Right arrow
// 40 - Down arrow
// 45 - insert
// 46 - delete

// e.returnValue = false;
// e.cancelBubble = true;

//handle textbox keypress
function HandleDropDownKeyDown(e) {
    if (!e) e = event;
    //alert("key1: " + e.keyCode);

    var obj;
    if (e.target)
        obj=e.target;
    else if (e.srcElement)
        obj=e.srcElement;
    if (obj.nodeType==3) // defeat Safari bug
        obj = obj.parentNode;
    //alert("name: " + obj.tagName);
    
    var pnl = document.getElementById(obj.id + "Panel");
    //c stores the actual "first child" - the actual element is not the firstChild in FF (and others?)
    var c = 0;
    if (pnl.firstChild.tagName == null)
	    c = 1;

    //don't do anything for the tab button
        
    //escape key - hide the drop down panel and focus on the textbox    
    if (e.keyCode == 27) {
        obj.focus(); //focus on the textbox
        //StopEvent(e);
        HideDropDown(obj); //hide the current object
    }

    //down key pressed when textbox has focus
    if (e.keyCode == 40) {
        ShowDropDown(obj);
        var lnk = pnl.childNodes[c].childNodes[c];
        DropDownItemOver(lnk); //highlight the item
        StopEvent(e); //if this wasn't there the panel would scroll
    }

    //don't do anything for the up button
}

function HandleDropDownKeyUp(e) {

    if (!e) e = event;
    //alert("key1: " + e.keyCode);

    var obj;
    if (e.target)
        obj=e.target;
    else if (e.srcElement)
        obj=e.srcElement;
    if (obj.nodeType==3) // defeat Safari bug
        obj = obj.parentNode;
    //alert("name: " + obj.tagName);

    var pnl = document.getElementById(obj.id + "Panel");
    //c stores the actual "first child" - the actual element is not the firstChild in FF (and others?)
    var c = 0;
    if (pnl.firstChild.tagName == null)
	    c = 1;

    //a character, backspace or delete
    if ((e.keyCode >= 48) || (e.keyCode == 8) || (e.keyCode == 46)) {
        DD_isTyping = 1;
        //find the next value in the list that matches the value the user has typed
        //if a value doesn't match, delete the last character typed
        var valueLength = obj.value.length;
        for (i = c; i < pnl.childNodes[c].childNodes.length; i++) {
            if (obj.value.toLowerCase() == document.getElementById(pnl.childNodes[c].childNodes[i].id + "Text").innerHTML.substring(0, valueLength).toLowerCase()) {
                ShowDropDown(obj);
                //alert(pnl.childNodes[c].childNodes[i].id);
                pnl.scrollTop = GetRealTop(document.getElementById(pnl.childNodes[c].childNodes[i].id + "Text")) - GetRealTop(pnl);// + "px";
                obj.value = document.getElementById(pnl.childNodes[c].childNodes[i].id + "Text").innerHTML.substring(0, valueLength); //converts the text to the correct case
                DropDownItemOver(pnl.childNodes[c].childNodes[i]);
                break;
                //alert(GetRealTop(document.getElementById(pnl.childNodes[c].childNodes[i].id + "Text")) - GetRealTop(pnl));
            }
            //handle down/up clicks seamlessly - when arrow is clicked see if item is hovered over and give it focus
            //if the textbox doesn't match a list item, erase it - when tab/enter is clicked, compare text to list
        }
    } else
        DD_isTyping = 0;
}

//handle key presses from inside the drop down panel
function HandleDropDownPanelKeyDown(e) {
    //escape hides panel
    //enter selects item (this happens automatically when the link has focus)
    //up & down

    if (!e) e = event;
    //alert("key1: " + e.keyCode);

    var obj;
    if (e.target)
        obj=e.target;
    else if (e.srcElement)
        obj=e.srcElement;
    if (obj.nodeType==3) // defeat Safari bug
        obj = obj.parentNode;

    var pnl = document.getElementById(DD_currentObj.id + "Panel");
    //c stores the actual "first child" - the actual element is not the firstChild in FF (and others?)
    var c = 0;
    if (pnl.firstChild.tagName == null)
	    c = 1;
    
    //tab key or enter key - want to actually select the value (call click event)
    if ((e.keyCode == 9) || (e.keyCode == 13)) {
        obj.click(); //this fires the onclick event (which is handled automatically by the link)
        StopEvent(e); //this stops the "tab" or enter events from doing their thing
    }

    //escape key - hide the drop down pane and focus on the textbox    
    if (e.keyCode == 27) {
        DD_currentObj.focus(); //focus on the textbox - must be before HideCurrentDropDown
        HideDropDown(DD_currentObj);
    }

    //up key - moves the selection up 1 item
    if (e.keyCode == 38) {
        if (obj != pnl.childNodes[c].childNodes[c]) //compare current item to first node
            var lnk = obj.previousSibling; //there is a "text" node in between each link
            if (lnk != null) {
                DropDownItemOff(obj);
                DropDownItemOver(lnk); //highlight the new item
                //lnk.focus(); //set the focus on the new item
            }
    }

    //down key - moves selection down 1 item
    if (e.keyCode == 40) {
        var lnk = obj.nextSibling; //there is a "text" node in between each link
        if (lnk != null) {
            DropDownItemOff(obj);
            DropDownItemOver(lnk); //highlight the new item
            //lnk.focus(); //set the focus on the new item
        }
    }
    
    //document.getElementById(DD_currentObj).value = lnk.innerHTML; //set the text to the new item
}

function ShowDropDown(obj) {
    var pnl = document.getElementById(obj.id + "Panel");
    pnl.style.width = obj.offsetWidth + "px";
    pnl.style.top = GetRealTop(obj) + obj.offsetHeight + "px";
    pnl.style.left = GetRealLeft(obj) + "px";
    pnl.className = "DropDownPanel-visible";
    obj.className = "DropDownText-active"
    DD_isVisible = 1;
    DD_isActive = 1; 
    DD_currentObj = obj; //assign the object as the current object
}

function HideDropDown(obj) {
    var pnl = document.getElementById(obj.id + "Panel");
    pnl.className = "DropDownPanel-hidden";
    obj.className = "DropDownText-enabled"
    DD_isVisible = 0;
    DD_isActive = 0;
    DD_currentObj = obj; //assign the object as the current object
}

//this will only select, it won't unselect
function DropDownItemClick(obj) {
    var txt = DD_currentObj; //the current visible textbox input
    var val = document.getElementById(txt.id + "SelectedValue"); //the current hidden selected value label
    txt.value = document.getElementById(obj.id + "Text").innerHTML; //gets the value from the "Text" span
    val.value = document.getElementById(obj.id + "Value").value; //gets the value from a hidden input
    DropDownItemSelect(txt);
    DD_currentObj.focus(); //set focus on the current textbox
    DD_currentObj.select(); //selects all the text
    HideDropDown(DD_currentObj); //hide the panel;
}

function DropDownItemOver(obj) {
    if (DD_isTyping == 0)
        obj.focus();

    var txt = DD_currentObj; //the current visible textbox input
    //loop through all items - unhover all but current
    var pnl = document.getElementById(txt.id + "Panel");
    //c stores the actual "first child" - the actual element is not the firstChild in FF (and others?)
    var c = 0;
    if (pnl.firstChild.tagName == null)
	    c = 1;

    //we loop through all items in case the user uses both the mouse & keypad
    for (i = c; i < pnl.childNodes[c].childNodes.length; i++)
        if (obj == pnl.childNodes[c].childNodes[i]) //this is the current item - hover!
            if ((pnl.childNodes[c].childNodes[i].className == "DropDownItem-selected") || (pnl.childNodes[c].childNodes[i].className == "DropDownItem-selected-hover")) //if the item is selected, select-hover
                pnl.childNodes[c].childNodes[i].className = "DropDownItem-selected-hover";
            else
                pnl.childNodes[c].childNodes[i].className = "DropDownItem-normal-hover";
        else
            if ((pnl.childNodes[c].childNodes[i].className == "DropDownItem-selected") || (pnl.childNodes[c].childNodes[i].className == "DropDownItem-selected-hover")) //if the item is selected, keep it selected
                pnl.childNodes[c].childNodes[i].className = "DropDownItem-selected";
            else
                pnl.childNodes[c].childNodes[i].className = "DropDownItem-normal";
}

function DropDownItemOff(obj) {
    obj.blur();
    if ((obj.className == "DropDownItem-selected-hover") || (obj.className == "DropDownItem-selected"))
        obj.className = "DropDownItem-selected";
    else
        obj.className = "DropDownItem-normal";
        
}

//this gets called when an item is clicked or when the panel is shown
function DropDownItemSelect(obj) {
    var txt = obj;
    var val = document.getElementById(txt.id + "SelectedValue"); //the current hidden selected value label
    //loop through all items - deselect all but current
    var pnl = document.getElementById(txt.id + "Panel");
    //c stores the actual "first child" - the actual element is not the firstChild in FF (and others?)
    var c = 0;
    if (pnl.firstChild.tagName == null)
	    c = 1;
    if (val.value != "")
        for (i = c; i < pnl.childNodes[c].childNodes.length; i++)
            if (val.value == document.getElementById(pnl.childNodes[c].childNodes[i].id + "Value").value) { //this is the current item - select it
                pnl.childNodes[c].childNodes[i].className = "DropDownItem-selected";
                txt.value = document.getElementById(pnl.childNodes[c].childNodes[i].id + "Text").innerHTML;
            } else //unselect all other items
                pnl.childNodes[c].childNodes[i].className = "DropDownItem-normal";
}

function StopEvent(e) {
   if (!e)
     if (window.event)
	e = window.event;
     else
	return;
   if (e.cancelBubble != null)
      e.cancelBubble = true;
   if (e.stopPropagation)
      e.stopPropagation();
   if (e.preventDefault)
      e.preventDefault();
   if (window.event)
      e.returnValue = false;
   if (e.cancel != null)
      e.cancel = true;
} 

/* end code for custom drop-down boxes */

//textbox input masking
function mask(str,textbox,loc,delim){
    var locs = loc.split(',');

    for (var i = 0; i <= locs.length; i++){
	    for (var k = 0; k <= str.length; k++){
	     if (k == locs[i]){
	      if (str.substring(k, k+1) != delim){
	       if (event.keyCode != 8){ //backspace
	        str = str.substring(0,k) + delim + str.substring(k,str.length);
           }
	      }
	     }
	    }
     }
    textbox.value = str
}

function HideCalendar(sender, args) {
    sender.hide();
    //alert(sender.id);
    //alert(cb.id);
}