﻿//-------------- BEGIN CallBackObject --------------------
function ReceiveServerData(arg, context) {
    if (arg != "Error") {
        AutoCompleteTextBox.prototype.Cbo_Complete(arg);
    }
}
function ProcessCallBackError(arg, context) {
    alert('An error has occurred.');
}
//----------------End CallBackObject---------------------

//----------------BEGIN AutoCompleteTextBox ------------------------

function AutoCompleteTextBox(TextBoxId, DivId, DivClass) {
    // initialize member variables
    var oThis = this;
    var oText = document.getElementById(TextBoxId);
    var oDiv = document.getElementById(DivId);
    this.TextBox = oText;
    this.Div = oDiv;

    // CallBackObject + Event Handlers
//    this.Cbo = new CallBackObject();
//    this.Cbo.OnComplete = function(responseText, responseXML) { oThis.Cbo_Complete(responseText, responseXML); };
//    this.Cbo.OnError = function(status, statusText, responseText) { oThis.Cbo_Error(status, statusText, responseText); };

    // attach handlers to the TextBox
    oText.AutoCompleteTextBox = this;
    oText.onkeyup = AutoCompleteTextBox.prototype.OnKeyUp;
    oText.onkeydown = AutoCompleteTextBox.prototype.OnKeyDown;
    //oText.onblur = AutoCompleteTextBox.prototype.OnBlur;
    oDiv.onblur = AutoCompleteTextBox.prototype.OnBlur;

    // align the drop down div    
    var c = GetCoords(oText);
    var n = oText.style.pixelHeight;
    if (!n) {
        n = 25;
    }
    else {
        n += 2;
    }
    oDiv.style.left = c.x;
    oDiv.style.top = c.y + n;
    oDiv.style.display = 'none';
    oDiv.style.position = 'absolute';

    // Set some default styles
    if (DivClass)
        oDiv.className = DivClass;
    else {
        oDiv.style.border = '1';
        oDiv.style.borderColor = 'black';
        oDiv.style.borderStyle = 'solid';
        oDiv.style.backgroundColor = 'White';
        oDiv.style.color = 'Green';
        oDiv.style.padding = '2';
    }
}

AutoCompleteTextBox.prototype.DoAutoSuggest = false;

AutoCompleteTextBox.prototype.ListItemClass = '';

AutoCompleteTextBox.prototype.ListItemHoverClass = '';

// TextBox OnBlur
AutoCompleteTextBox.prototype.OnBlur = function() {
    AutoCompleteTextBox.prototype.TextBox_Blur();
}

AutoCompleteTextBox.prototype.TextBox_Blur = function()
{
	Div.style.display='none';
}

AutoCompleteTextBox.prototype.OnKeyDown = function(oEvent) {
    //check for the proper location of the event object
    if (!oEvent) {
        oEvent = window.event;
    }
    AutoCompleteTextBox.prototype.TextBox_KeyDown(oEvent);
}

AutoCompleteTextBox.prototype.TextBox_KeyDown = function(oEvent) {
    var iKeyCode = oEvent.keyCode;
    if (iKeyCode == 13) {
        GoToCandidate();
    }
}

// TextBox OnKeyUp
AutoCompleteTextBox.prototype.OnKeyUp = function(oEvent) {
    //check for the proper location of the event object
    if (!oEvent) {
        oEvent = window.event;
    }
    AutoCompleteTextBox.prototype.TextBox_KeyUp(oEvent);
}

AutoCompleteTextBox.prototype.TextBox_KeyUp = function(oEvent) {
    var iKeyCode = oEvent.keyCode;
    
    if (iKeyCode == 8) {
        Div.innerHTML = '';
        Div.style.display = 'none';
        return;
    }
    else if (iKeyCode == 16 || iKeyCode == 20) {
        this.DoAutoSuggest = true;
    }
    else if (iKeyCode == 40) {
        if (Div.childNodes.length > 0) {
            for (var i = 0; i < Div.childNodes.length; i++) {
                if (Div.childNodes[i].style.backgroundColor == 'black') {
                    unselectDiv(Div.childNodes[i])
                    if (i + 1 < Div.childNodes.length) {
                        selectDiv(Div.childNodes[i + 1]);
                        Div.scrollTop = Div.childNodes[i + 1].clientHeight * (i + 1);
                    }
                    else {
                        selectDiv(Div.childNodes[0]);
                        Div.scrollTop = 0;
                    }
                    return;
                }
            }
            selectDiv(Div.childNodes[0]);
        }
        return;
    }
    else if (iKeyCode == 38) {
        if (Div.childNodes.length > 0) {
            for (var i = Div.childNodes.length - 1; i >= 0; i--) {
                if (Div.childNodes[i].style.backgroundColor == 'black') {
                    unselectDiv(Div.childNodes[i])
                    if (i - 1 >= 0) {
                        selectDiv(Div.childNodes[i - 1]);
                        Div.scrollTop = Div.childNodes[i - 1].clientHeight * (i - 1);
                    }
                    else {
                        selectDiv(Div.childNodes[Div.childNodes.length - 1]);
                        Div.scrollTop = Div.childNodes[Div.childNodes.length - 1].clientHeight * (Div.childNodes.length - 1);
                    }
                    return;
                }
            }
            selectDiv(Div.childNodes[Div.childNodes.length - 1]);
        }
        return;
    }
    else if (iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode <= 46) || (iKeyCode >= 112 && iKeyCode <= 123)) {
        return;
    }
    else {
        this.DoAutoSuggest = false;
    }

    var txt = TextBox.value;
    if (txt.length > 0) {
        CallTheServer(txt, TextBox.name);
    }
    else {
        Div.innerHTML = '';
        Div.style.display = 'none';
        //this.Cbo.AbortCallBack();
    }
}

AutoCompleteTextBox.prototype.Cbo_Complete = function(responseText) {
    while (Div.hasChildNodes())
        Div.removeChild(Div.firstChild);

    // get all the matching strings from the server response
    var newObj = new Object();
    newObj.aStr = new Array();
    newObj.CandidateId = new Array();
    newObj.PartyId = new Array();
    Div.onmousedown = "";
    if (responseText != "Error") {
        var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
        xmldoc.loadXML(responseText);
        newObj.aStr = new Array(xmldoc.childNodes[0].childNodes.length);
        newObj.CandidateId = new Array(xmldoc.childNodes[0].childNodes.length);
        newObj.PartyId = new Array(xmldoc.childNodes[0].childNodes.length);
        for (var i = 0; i < xmldoc.childNodes[0].childNodes.length; i++) {
            var Node = xmldoc.childNodes[0].childNodes[i];
            var oDiv = document.createElement('div');
            Div.appendChild(oDiv);
            for (var j = 0; j < Node.attributes.length; j++) {
                try {
                    if (Node.attributes[j].name == "innerHTML") {
                        newObj.aStr[i] = Node.attributes[j].value;
                        oDiv.innerHTML = Node.attributes[j].value;
                    }
                    else {
                        oDiv.setAttribute(Node.attributes[j].name, Node.attributes[j].value);
                        if (Node.attributes[j].name == "CandidateId")
                            newObj.CandidateId[i] = Node.attributes[j].value;
                        else
                            newObj.PartyId[i] = Node.attributes[j].value;
                    }
                }
                catch (e) {
                    this.Cbo_Error('405', 'Error', 'Text returned from Call Back was invalid');
                    return;
                }
            }
            oDiv.noWrap = true;
            oDiv.style.width = '115px'; //'100%';
            oDiv.style.cursor = "hand";
            oDiv.className = this.ListItemClass;
            oDiv.onmousedown = AutoCompleteTextBox.prototype.Div_MouseDown;
            oDiv.onmouseover = AutoCompleteTextBox.prototype.Div_MouseOver;
            oDiv.onmouseout = AutoCompleteTextBox.prototype.Div_MouseOut;
            oDiv.AutoCompleteTextBox = this;
        }
        
        var c = GetCoords(TextBox);
        var n = TextBox.style.pixelHeight;
        if (!n) {
            n = 25;
        }
        else {
            n += 1;
        }
        Div.style.left = c.x;
        Div.style.top = c.y + n;
        
        if (i > 0) {
            Div.style.display = 'block';
        }
        else {
            Div.style.display = 'none';
        }

        if (this.DoAutoSuggest == true)
            this.AutoSuggest(newObj);
    }
    else {
        Div.innerHTML = '';
        Div.style.display = 'none';
    }
}

AutoCompleteTextBox.prototype.Cbo_Error = function(status, statusText, responseText)
{
  alert('CallBackObject Error: status=' + status + '\nstatusText=' + statusText + '\n' + responseText);
}

function selectDiv(divObj) {
    if (divObj.AutoCompleteTextBox.ListItemHoverClass.length > 0)
        divObj.className = divObj.AutoCompleteTextBox.ListItemHoverClass;
    else {
        divObj.style.backgroundColor = 'black';
        divObj.style.color = 'white';
    }
    TextBox.value = divObj.innerHTML;
    TextBox.CandidateId = divObj.CandidateId;
    TextBox.PartyId = divObj.PartyId;
}

function unselectDiv(divObj) {
    if (divObj.AutoCompleteTextBox.ListItemClass.length > 0)
        divObj.className = divObj.AutoCompleteTextBox.ListItemClass;
    else {
        divObj.style.backgroundColor = 'White';
        divObj.style.color = 'Green';
    }
}

AutoCompleteTextBox.prototype.Div_MouseDown = function() {
    TextBox.value = this.innerHTML;
    TextBox.CandidateId = this.CandidateId;
    TextBox.PartyId = this.PartyId;
    Div.style.display = 'none';
}

AutoCompleteTextBox.prototype.Div_MouseOver = function() {
    if (this.AutoCompleteTextBox.ListItemHoverClass.length > 0)
        this.className = this.AutoCompleteTextBox.ListItemHoverClass;
    else {
        this.style.backgroundColor = 'black';
        this.style.color = 'white';
    }
}

AutoCompleteTextBox.prototype.Div_MouseOut = function() {
    if (this.AutoCompleteTextBox.ListItemClass.length > 0)
        this.className = this.AutoCompleteTextBox.ListItemClass;
    else {
        this.style.backgroundColor = 'White';
        this.style.color = 'Green';
    }
}

AutoCompleteTextBox.prototype.AutoSuggest = function(aSuggestions /*:array's Object*/) {
    if (aSuggestions.aStr.length > 0) {
        this.TypeAhead(aSuggestions.aStr[0], aSuggestions.CandidateId[0], aSuggestions.PartyId[0]);

    }
}

AutoCompleteTextBox.prototype.TypeAhead = function(sSuggestion /*:string*/, sCandidate, sParty) {
    if (TextBox.createTextRange || TextBox.setSelectionRange) {
        var iLen = TextBox.value.length;
        TextBox.value = sSuggestion;
        TextBox.CandidateId = sCandidate;
        TextBox.PartyId = sParty;
        this.SelectRange(iLen, sSuggestion.length);
    }
}

AutoCompleteTextBox.prototype.SelectRange = function (iStart /*:int*/, iLength /*:int*/) 
{
  //use text ranges for Internet Explorer
  if (TextBox.createTextRange) 
  {
    var oRange = TextBox.createTextRange(); 
    oRange.moveStart("character", iStart); 
    oRange.moveEnd("character", iLength - TextBox.value.length);      
    oRange.select();
   
  //use setSelectionRange() for Mozilla
  } 
  else if (TextBox.setSelectionRange) 
  {
      TextBox.setSelectionRange(iStart, iLength);
  }     

  //set focus back to the textbox
  TextBox.focus();      
}

//----------------END AutoCompleteTextBox ------------------------
             
function GetCoords(obj /*:object*/) 
{   
  var newObj = new Object();
  newObj.x = obj.offsetLeft;
  newObj.y = obj.offsetTop;
  theParent = obj.offsetParent;
  while(theParent != null)
  {
    newObj.y += theParent.offsetTop;
    newObj.x += theParent.offsetLeft;
    theParent = theParent.offsetParent;
  }
  return newObj;
}

function GoToCandidate() {
    var SearchObj = document.getElementById("TextBoxSearch");
    if (SearchObj.CandidateId > 0 && SearchObj.PartyId > 0)
        location.replace("Donations.aspx?CandidateId=" + SearchObj.CandidateId + "&PartyId=" + SearchObj.PartyId);
    else
        alert("לא נמצא מועמד");

}