﻿//  Search Ahead Text Boxes
//
// Extends the HTML text box to include limited database search-aheads.
// After a number characters have been entered into the text box, a list box
// appears directly below the edit box, providing a smal number of
// choices beginning with the text appearing in the list box.
//
// The process employs AJAX to fetch the data and populate a dynamic list
// that is embedded within a DIV control for display.
//
// The control ignores carriage returns entered by the user. A tab key
// will close the DIV display. Clicking on any list item will place it in
// the search box and update an hidden field specifying the item's row identifier.
//
//
// Interface

var SA_TRIGGER_CHARS = 3;
var SA_LIST_LENGTH   = 4;
var sSADataURL       = "/shared/searchahead.aspx?";
var oSearchAhead     = new CreateSearchAhead();
var iSALocus = 0;
var sSADebug = "";
var iPadMode = 0;

// SA Locus Identifiers
// 0 : Status
// 1 : Compare Films
// 2 : Home Page Search Films

var ReadSAContent = function(sContent,sParent) {
    oSearchAhead.oDiv.innerHTML = sContent;
    oSearchAhead.CreateResponse(sContent);
}

function CreateSearchAhead() {
    this.sPrefix  = "ctl00_ContentPlaceHolder1_";
    this.iMode    = 0;              // 0=New, 1 = 0..SA_TRIGGER_CHARS - 1, 2 = SA_TRIGGER_CHARS..., 3=List
    this.oDiv     = null;
    this.oAnchor  = null;
    this.oSearch  = null;
    this.oList    = null;
    this.oID      = null;
    this.oName    = null;
    this.sKey     = "";
    this.iAID     = 0;
    this.iResult  = 0;
    this.bShifted = false;
           
    this.sFormControlID   = "";    
    this.sFormControlName = "";
        
    this.CreateSurface = function (iWidth) {
        this.oDiv                  = AddElement("","div","SADIV");
        this.oDiv.className        = "SearchBoxes";
        this.oDiv.innerHTML        = "Searching..."
        this.oDiv.onclick          = onSAListChange;
        this.oDiv.onkeydown        = onSAListKeyDown;
        this.oDiv.style.x          = 0;
        this.oDiv.style.y          = 0;
        this.oDiv.style.width      = iWidth;
        this.oDiv.style.position   = 'absolute';
        this.oDiv.style.visibility = 'visible';
    }   
    
    this.GetURL = function () {
        var sURL = sSADataURL + "ID="
        sURL = sURL + GetSessionID() + "&AID=" + this.iAID;
        
        if (this.iAID == 2) {
            sURL = sURL + "&TYPEID=" + document.getElementById(ASP_PREFIX+"TypeID").value +
                          "&SORT=" + document.getElementById(ASP_PREFIX+"SortField").value;
        }
        
        return sURL                  
    }
    
    this.LoadSearchContent = function(iKey) {
        var sURL = this.GetURL() + "&TXT=" + this.sKey;
        
        if (this.iAID == 3) SetValue("SATERMS",this.sKey);
        
        pProcessContent = ReadSAContent;
        LoadContent(sURL,"SADIV",true);
    }
    
    this.LoadReviewDefaults = function () {
        var sURL = sSADataURL + "ID=" + GetSessionID() + "&AID=10&MID=" +
                   GetValue("MEDIASEARCHID") + "&RID=" + GetValue("RELEASESEARCHID");
        
        pProcessContent = SetDefaultReviewValues;
        LoadContent(sURL,"",true);
    }

    this.SaveFilmTitleToPackage = function() {
        var sURL = sSADataURL + "ID=" + GetSessionID() + "&AID=36&RID=" + GetValue("SAMAILID");
        iSALocus = 5;
        pProcessContent = ProcessDynamicSAReturn;
        LoadContent(sURL, "", true);
        SetFocus("SAMAIL");
    }

    this.SaveAddressToPackage = function() {
        var sURL = sSADataURL + "ID=" + GetSessionID() + "&AID=37&ADDR=" + GetValue("ADDADDRESS");
        iSALocus = 6;
        pProcessContent = ProcessDynamicSAReturn;
        LoadContent(sURL, "", true);
        SetFocus("ADDADDRESS");
    }

    this.ClearPackage = function() {
        var sURL = sSADataURL + "ID=" + GetSessionID() + "&AID=38";
        iSALocus = 7;
        pProcessContent = ProcessDynamicSAReturn;
        LoadContent(sURL, "", true);
    }

    this.SendPackage = function() {
        var sURL = sSADataURL + "ID=" + GetSessionID() + "&AID=39&MSG=" + GetValue("EditEMailFromMsg");
        iSALocus = 8;
        pProcessContent = ProcessDynamicSAReturn;
        LoadContent(sURL, "", true);
    }

    this.CreateResponse = function(sContent) {

        this.oDiv.innerHTML = "";
        this.oDiv.className = "SearchBoxResults";
        if (bIsFirefox) this.oDiv.style.width = this.oSearch.style.width;
        this.oList = AddElement("SADIV", "select", "SALIST");
        this.oList.className = "SearchAheadList";
        if (!DetectIpad()) this.oList.size = iSearchAheadResults;
        this.oList.style.width = this.oDiv.style.width;
        //if ((DetectMobile()) && (!DetectIpad())) this.oList.onchange = onSAListChange;
        if (DetectMobile()) this.oList.onchange = onSAListChange;
        if (DetectIpad()) this.oList.onchange = onSAListChange;
        iPadMode = 0;
        
        var aResult = DecodeDelimittedList(sContent, TBL_DELIM);
        var aRows;
        var aCols;
        var i;
        var oOption;
        var sParent = siif(bIsFirefox, this.oList.id, "");

        if (aResult.length == 2) {
            if (aResult[0] == "0") {
                aRows = DecodeDelimittedList(aResult[1], ROW_DELIM);

                if (aRows.length == 0) {
                    oOption = AddElement(sParent, "option", "SAOPTION" + i);
                    oOption.value = "";
                    oOption.text = "";
                    
                    if (bIsFirefox) this.oList.add(oOption, null);
                    else this.oList.add(oOption);
                } else {
                    for (i = 0; i < aRows.length; i++) {
                        aCols = DecodeDelimittedList(aRows[i], COL_DELIM);

                        if (aCols.length == 2) {
                            oOption = AddElement(sParent, "option", "SAOPTION" + i);
                            oOption.value = aCols[0];
                            oOption.text = aCols[1];

                            if (bIsFirefox) this.oList.add(oOption, null);
                            else this.oList.add(oOption);
                        }
                    }
                }
            }
        }

        oSearchAhead.iMode = 3;
    }
    
    this.RemoveResponse = function() {
        if (this.oList != null) {
            if (this.oList.parent != null) {
                this.oList.parent.removeChild(this.oList);
            }
        }
        
        this.oList = null;
        
        if (this.oDiv != null) {
            this.oDiv.className = "SearchBoxes";
            this.oDiv.innerHTML = "Searching...";
        }
    }

    this.SetFormControls = function() {
        var oControl = document.getElementById(this.sFormControlID);

        if (oControl != null) {
            if (this.oList != null) {
                if (this.oList.selectedIndex > -1) {
                    oControl.value = this.oList.options[oSearchAhead.oList.selectedIndex].value;
                    oControl = document.getElementById(this.sFormControlName);

                    if (oControl != null) {
                        if (oSearchAhead.iResult == 0) oControl.innerHTML = this.oList.options[oSearchAhead.oList.selectedIndex].text;
                        else if (oSearchAhead.iResult == 1) oControl.value = this.oList.options[oSearchAhead.oList.selectedIndex].text;
                        else if (oSearchAhead.iResult == 2) oControl.text = this.oList.options[oSearchAhead.oList.selectedIndex].text;
                    }
                }
            }
        }
    }
}

function SearchAhead(oEvent, oControl, sAnchor, sObjectID, sObjectName, iAID) {
    var iKey    = 0;
    var sValue  = oSearchAhead.sKey;
    var sDebug  = "";
    var i;
    
    oSearchAhead.oAnchor          = document.getElementById(sAnchor);
    oSearchAhead.oSearch          = oControl;
    oSearchAhead.sFormControlID   = sObjectID;
    oSearchAhead.sFormControlName = sObjectName;
    oSearchAhead.iAID             = iAID;
    oSearchAhead.oName            = document.getElementById(oSearchAhead.sFormControlName);
    oSearchAhead.oID              = document.getElementById(oSearchAhead.sFormControlID);
        
    if (window.event) iKey = oEvent.keyCode;
    else if (oEvent.which) iKey = oEvent.which;

    if ((iKey == 109) && (bIsFirefox)) iKey = 45;

    sSADebug = sSADebug + iKey + " - ";

    if (iKey == 16) {
        oSearchAhead.bShifted = true;
    } else if (((iKey >= 186) && (iKey <= 222)) || ((iKey >= 94) && (iKey <= 105))) {
        if (oSearchAhead.bShifted) {
            oSearchAhead.bShifted = false;

                 if (iKey == 186) iKey = 58;
            else if (iKey == 187) iKey = 43;
            else if (iKey == 188) iKey = 60;
            else if (iKey == 189) iKey = 95;
            else if (iKey == 190) iKey = 62;
            else if (iKey == 191) iKey = 63;
            else if (iKey == 192) iKey = 126;
            else if (iKey == 219) iKey = 123;
            else if (iKey == 220) iKey = 124;
            else if (iKey == 221) iKey = 125;
            else if (iKey == 222) iKey = 34;

        } else {
                 if (iKey == 186) iKey = 59;
            else if (iKey == 187) iKey = 61;
            else if (iKey == 188) iKey = 44;
            else if (iKey == 189) iKey = 45;
            else if (iKey == 190) iKey = 46;
            else if (iKey == 191) iKey = 47;
            else if (iKey == 192) iKey = 96;
            else if (iKey == 219) iKey = 91;
            else if (iKey == 220) iKey = 92;
            else if (iKey == 221) iKey = 93;
            else if (iKey == 222) iKey = 39;
            else if (iKey == 96) iKey = 48;
            else if (iKey == 97) iKey = 49;
            else if (iKey == 98) iKey = 50;
            else if (iKey == 99) iKey = 51;
            else if (iKey == 100) iKey = 52;
            else if (iKey == 101) iKey = 53;
            else if (iKey == 102) iKey = 54;
            else if (iKey == 103) iKey = 55;
            else if (iKey == 104) iKey = 56;
            else if (iKey == 105) iKey = 57;
        }

        if ((isLetter(iKey)) || (iKey == 58) || (iKey == 47)) sValue = sValue + String.fromCharCode(iKey);
    } else if ((iKey > 31) && (iKey < 128)) {
        if (oSearchAhead.bShifted) {
            oSearchAhead.bShifted = false;
          
            if (iKey == 49) iKey = 33;
            else if (iKey == 50) iKey = 64;
            else if (iKey == 51) iKey = 35;
            else if (iKey == 52) iKey = 36;
            else if (iKey == 53) iKey = 37;
            else if (iKey == 54) iKey = 94;
            else if (iKey == 55) iKey = 38;
            else if (iKey == 56) iKey = 42;
            else if (iKey == 57) iKey = 40;
            else if (iKey == 48) iKey = 41;
            else if (iKey == 59) iKey = 58;
        }

        if ((isLetter(iKey)) || (iKey == 58)) sValue = sValue + String.fromCharCode(iKey);
    } else if (iKey == 8) {
        if (sValue != "") sValue = sValue.substring(0,sValue.length-1);
    } else if ((iKey == 9) || (iKey == 27)) {
        sValue = "";
    }
    
    oSearchAhead.sKey = sValue;

//    sDebug = sValue.length + ":";
    
//    for (i=0;i<sValue.length;i++) sDebug = sDebug = sDebug + sValue.charCodeAt(i) + ".";
//    for (i=0;i<sValue.length;i++) sDebug = sDebug + sValue.substr(i,1) + ".";
    
//    SetSpanString("DEBUG",sDebug);
    
    if ((iKey != 13) && (iKey != 9) && (iKey != 27)) {                  // Ignore CR, Tab and ESC
        if ((iKey == 40) && (oSearchAhead.iMode == 3)) {                // Process Down Arrow
            oSearchAhead.oList.focus();

            if ((bIsChrome) || (bIsSafari)) {
                if (oSearchAhead.oList.options.length > 0) oSearchAhead.oList.selectedIndex = 0;
            }
            
            return true;
        } else if ((iKey == 8) && (oSearchAhead.iMode > 1) && (sValue.length < iSearchAheadLength)) {
            CleanupSA();
            return true;                                                // Process Backspace
        } else if (sValue.length >= iSearchAheadLength) {
            if (oSearchAhead.iMode == 3) {
                oSearchAhead.RemoveResponse();                          // Close response if open
                oSearchAhead.iMode = 2;     
            } else if (oSearchAhead.iMode == 1) {                       // Create blank response
                oSearchAhead.iMode = 2;
                oSearchAhead.CreateSurface(parseInt(oControl.style.width) - 6);
            }

            SetValue(oSearchAhead.sFormControlID, "");           
            oSearchAhead.oAnchor.appendChild(oSearchAhead.oDiv);
            oSearchAhead.LoadSearchContent(iKey);                       // Fetch the search results
            return true;
        } else {
             CleanupSA();                                               // String too short too trigger
             return true;
        }
    } else if ((iKey == 9) || (iKey == 27)) {                           // Process Tab and ESC
        CleanupSA();
        oControl.value = "";
        return true;
    } else {                                                            // Ignore CR
        return false;
    }
}

function CleanupSA() {
    oSearchAhead.RemoveResponse();                                  // Reset the response div
    RemoveElementFromParent(oSearchAhead.oDiv);
    oSearchAhead.iMode = 1;
    oSearchAhead.iResult = 0;
}

function onCountPage(sContent) {

}

function ProcessSAReturn() {                                        // Process post-backs requests
    var oControl = null;
    var sPage = new String(document.location);
    sPage = sPage.toLowerCase();

    if ((GetValue("ctl00_UserID") == "") && (GetValue("GENVALUEID") != "6")) {
        pProcessContent = onCountPage;
        LoadContent("mri.aspx?MID=16&NID=" + GetValue("SEARCHBOXGENID"), "", true);
    }

    if (iSALocus > 0) {

        if ((sPage.search("adminpost") > -1) && (oSearchAhead.oSearch.id == "SAMAIL")) {
            oSearchAhead.SaveFilmTitleToPackage();
        }

        ProcessDynamicSAReturn();
    } else if (sPage.search("adminscript") > -1) {
        if (oSearchAhead.oSearch.id == "SATEXT") document.getElementById("SUBMITMODE").value = "1";
        else if (oSearchAhead.oSearch.id == "SASCRIPT") document.getElementById("SUBMITMODE").value = "2";
        else document.getElementById("SUBMITMODE").value = "3";
        document.forms['aspnetForm'].submit();
    } else if (sPage.search("adminchart") > -1) {
        document.getElementById("SEARCHMODE").value = "1";
        document.forms['aspnetForm'].submit();
    } else if (sPage.search("adminresource") > -1) {
        if (oSearchAhead.oSearch.id == "SATEXT") {
            document.getElementById("SEARCHMODE").value = "2";
            document.forms['aspnetForm'].submit();
        } // else  document.getElementById("SEARCHMODE").value = "1";
    } else if ((oSearchAhead.iAID == 1) && (sPage.search("adminpostedit") > -1)) {
        document.getElementById("IMAGESAVEMODE").value = "1";
        if (oSearchAhead.oSearch.id != "SAVAR") document.forms['aspnetForm'].submit();
    } else if (sPage.search("adminpostedit") > -1) {
        if (oSearchAhead.oSearch.id == "SARELEASE") {
            SetValue("RELEASEMODE", "1")
            document.forms['aspnetForm'].submit();
        } else if (oSearchAhead.oSearch.id != "SAVAR") SetValue("SEARCHMODE", "2");
    } else if (sPage.search("adminpost") > -1) {
        if (oSearchAhead.oSearch.id == "SATEXT") {
            oSearchAhead.oSearch.value = "";
            SetValue("SEARCHMODE", "1");
            document.forms['aspnetForm'].submit();
        } else SetValue("SEARCHMODE", "2");
    } else if (sPage.search("adminconfig") > -1) {
        if (oSearchAhead.oSearch.id == "SATEXT") SetValue("SEARCHMODE", "1");
        else if (oSearchAhead.oSearch.id == "SACHARTAUDIT") SetValue("SEARCHMODE", "2");
        else if (oSearchAhead.oSearch.id == "SANPRDEFAULT") SetValue("SEARCHMODE", "3");
        else if (oSearchAhead.oSearch.id == "SANPR1") SetValue("SEARCHMODE", "4");
        else if (oSearchAhead.oSearch.id == "SANPR2") SetValue("SEARCHMODE", "5");
        else if (oSearchAhead.oSearch.id == "SAMEDIAID1") SetValue("SEARCHMODE", "6");
        else if (oSearchAhead.oSearch.id == "SAMEDIAID2") SetValue("SEARCHMODE", "7");
        else if (oSearchAhead.oSearch.id == "SAMEDIAID3") SetValue("SEARCHMODE", "8");
        else if (oSearchAhead.oSearch.id == "SAMEDIAID4") SetValue("SEARCHMODE", "9");
        document.forms['aspnetForm'].submit();
    } else if (oSearchAhead.iAID == 2) {
        window.location = "/adminusers.aspx?ID=" + GetSessionID() + "&SEARCHID=" + document.getElementById("SEARCHID").value;
        oSearchAhead.oSearch.value = "";
    } else if (oSearchAhead.iAID == 3) {
        if (sPage.search("adminrelease") > -1) {
            document.getElementById("SEARCHDATEMODE").value = "2";
            oSearchAhead.oSearch.value = "";
            document.forms['aspnetForm'].submit();
        } else if ((sPage.search("adminreview") > -1) && (oSearchAhead.oSearch.id == "SATEXT")) {
            document.getElementById("SEARCHDATEMODE").value = "2";
            document.forms['aspnetForm'].submit();
        }
    } else if (oSearchAhead.iAID == 4) {
        if (sPage.search("admincritic") > -1) {
            oSearchAhead.oSearch.value = "";
            window.location = "admincritic.aspx?ID=" + GetSessionID() + "&CID=" + document.getElementById("SEARCHID").value;
        } else if ((sPage.search("adminreview") > -1) && (oSearchAhead.oSearch.id == "SATEXT")) {
            document.getElementById("SEARCHDATEMODE").value = "2";
            document.forms['aspnetForm'].submit();
        }
    } else if ((oSearchAhead.iAID == 5) && (sPage.search("adminreview") > -1) && (oSearchAhead.oSearch.id == "SATEXT")) {
        document.getElementById("SEARCHDATEMODE").value = "2";
        document.forms['aspnetForm'].submit();
    } else if ((oSearchAhead.iAID == 5) && (sPage.search("adminreview") > -1) && (oSearchAhead.oSearch.id == "SAMEDIA")) {
        oSearchAhead.LoadReviewDefaults();
    } else if ((oSearchAhead.iAID == 6) && (sPage.search("adminreview") > -1) && (oSearchAhead.oSearch.id == "SATEXT")) {
        document.getElementById("SEARCHDATEMODE").value = "2";
        document.forms['aspnetForm'].submit();
    } else if ((oSearchAhead.iAID == 8) && (sPage.search("adminsurvey") > -1)) {
        document.getElementById("SEARCHDATEMODE").value = "2";
        document.forms['aspnetForm'].submit();
    } else if ((oSearchAhead.iAID == 11) && (sPage.search("adminreview") > -1)) {
        if (oSearchAhead.oSearch.id == "SATEXT") {
            document.getElementById("SEARCHDATEMODE").value = "2";
            document.forms['aspnetForm'].submit();
        }
    } else if (oSearchAhead.iAID == 25) {
        if (sPage.search("adminrelease") > -1) {
            document.getElementById("SEARCHDATEMODE").value = "2";
            oSearchAhead.oSearch.value = "";
            document.forms['aspnetForm'].submit();
        } else if ((sPage.search("adminreview") > -1) && (oSearchAhead.oSearch.id == "SATEXT")) {
            document.getElementById("SEARCHDATEMODE").value = "2";
            document.forms['aspnetForm'].submit();
        } else if ((sPage.search("adminreview") > -1) && (oSearchAhead.oSearch.id == "SARELEASE")) {
            oSearchAhead.LoadReviewDefaults();
        } 
    }
            
    oSearchAhead.sKey = "";
}

function SetDefaultReviewValues(sContent,sParent) {
    var a = DecodeDelimittedList(sContent,COL_DELIM);
    var oControl = null;
    var sNotes = "";
    var sCode 
    
    if (a.length == 10) {
        pCalcGrade();

        SetValue("MEDIASEARCHID", a[9]);
        SetChecked(oSearchAhead.sPrefix+"CheckBroadcast",a[0]);
        SetValue(oSearchAhead.sPrefix+"EditPictureSize",a[1]);
        SetValue(oSearchAhead.sPrefix+"EditHeadlineSize",a[3]);
        SetSpanString(oSearchAhead.sPrefix+"LabelFrequency",a[7]);
        SetSpanString(oSearchAhead.sPrefix+"LabelNewsstandDay",a[8]);
        SetValue("BROADCAST",a[0]);
        
        sNotes = a[5];
        sNotes = sNotes.replace(String.fromCharCode(13,10),"<br><br>");
                
        SetSpanString(sControlPrefix + "LabelNotes",sNotes);
        
        document.getElementById(oSearchAhead.sPrefix+"RadioColor").checked = false;
        document.getElementById(oSearchAhead.sPrefix+"RadioBW").checked = false;
        
        if (a[4] == "True") {
            document.getElementById(oSearchAhead.sPrefix+"RadioColor").checked = true;
            document.getElementById(oSearchAhead.sPrefix+"RadioBW").checked = false;
        } else {
            document.getElementById(oSearchAhead.sPrefix+"RadioBW").checked = true;
            document.getElementById(oSearchAhead.sPrefix+"RadioColor").checked = false;
        }
         
        if (a[2] == "True") {
            document.getElementById(oSearchAhead.sPrefix+"RadioPremiumYes").checked = true;
            document.getElementById(oSearchAhead.sPrefix+"RadioPremiumNo").checked = false;
        } else {
            document.getElementById(oSearchAhead.sPrefix+"RadioPremiumNo").checked = true;
            document.getElementById(oSearchAhead.sPrefix+"RadioPremiumYes").checked = false;
        } 
                
        if (a[0] == "True") {
            document.getElementById("EDITTIME").style.display = "block";
            document.getElementById("EDITWORD1").style.display = "none";
            document.getElementById("EDITWORD2").style.display = "none";
            document.getElementById("EDITWORD3").style.display = "none";
        } else {
            document.getElementById("EDITTIME").style.display = "none";
            document.getElementById("EDITWORD1").style.display = "block";
            document.getElementById("EDITWORD2").style.display = "block";
            document.getElementById("EDITWORD3").style.display = "block";
        }
        
        if (((GetValue("SAMEDIAID") == GetValue("NPR1DEFAULTID"))) ||
           ((GetValue("SAMEDIAID") == GetValue("NPR1ID"))) ||
           ((GetValue("SAMEDIAID") == GetValue("NPR2ID")))) {
           document.getElementById("EDITWORD1").style.display = "block";
       }

       SetSpanString("FIVESTARSPAN", "");
       SetSpanString("FOURSTARSPAN", "");
       SetSpanString("LETTERSPAN", "");
       SetSpanString("NONESPAN", "");

       if (a[6] == "0") {
           oControl = document.getElementById(oSearchAhead.sPrefix + "Radio5Star");
           oControl.checked = true;
           SetSpanString("FIVESTARSPAN", "(Default)");
       } else if (a[6] == "1") {
           oControl = document.getElementById(oSearchAhead.sPrefix + "Radio4Star");
           oControl.checked = true;
           SetSpanString("FOURSTARSPAN", "(Default)");
       } else if (a[6] == "2") {
           oControl = document.getElementById(oSearchAhead.sPrefix + "RadioLetters");
           oControl.checked = true;
           SetSpanString("LETTERSPAN", "(Default)");
       } else if (a[6] == "3") {
           oControl = document.getElementById(oSearchAhead.sPrefix + "RadioGradeNone");
           oControl.checked = true;
           SetSpanString("NONESPAN", "(Default)");
       } else if (a[6] == "4") {
           oControl = document.getElementById(oSearchAhead.sPrefix + "RadioTen");
           oControl.checked = true;
           SetSpanString("TENSPAN", "(Default)");
       }
        
       pCalcGrade();
    }     
}

function onSABodyClick() {                                          // Check for clicks anywhere else            
    var oControl = document.activeElement;
    
    if ((oControl != null) && (oSearchAhead.iMode == 2)) {  
        if ((oControl.id != "SADIV") && (oSearchAhead.oDiv.parentNode == oSearchAhead.oAnchor)) {
            oSearchAhead.oSearch.value = "";
            CleanupSA();    
        }
    }
}

function onSAListChange() {                         // Handle clicks to response list = Save & Reset
    if ((DetectIpad()) && (iPadMode == 0)) {
        oSearchAhead.oList.onchange = onSAListChange;
        iPadMode = 1;
    } else {
        iPadMode = 0;
        oSearchAhead.SetFormControls();
        ProcessSAReturn();
        CleanupSA();
    }
}

function onSAListKeyDown(oEvent) {                  // Handle keystrokes to the response div
    if (window.event) iKey = event.keyCode;
    else if (oEvent.which) iKey = oEvent.which;

    if (iKey == 13) {                               // Process CR = Save & Reset
        oSearchAhead.SetFormControls();    
        CleanupSA();    
        ProcessSAReturn();
    } else if (iKey == 27) {                        // Process TAB = Reset
        oSearchAhead.oSearch.value = "";
        CleanupSA();    
    }
}

function SearchAheadPerCritic(oEvent, oControl, sAnchor, sObjectID, sObjectName, iAID) {
    iSALocus = 3;
    oSearchAhead.iResult = 1;
    return SearchAhead(oEvent, oControl, sAnchor, sObjectID, sObjectName, iAID);
}

function SearchAheadPerPublication(oEvent, oControl, sAnchor, sObjectID, sObjectName, iAID) {
    iSALocus = 4;
    oSearchAhead.iResult = 1;
    return SearchAhead(oEvent, oControl, sAnchor, sObjectID, sObjectName, iAID);
}

function CreateSAInputBand(sParent, sName, sLabel, sWidth, iAID) {
    var oParent = document.getElementById(sParent);
    var oInputBand;
    var oSABand;
    var sAnchor = "'" + sName + "ANCHOR'";
    var sID = "'" + sName + "ID'";
    var sQName = "'" + sName + "'";
    var oCell;
    
    if (oParent != null) {
       oInputBand = oParent.insertRow(oParent.rows.length);

       oCell = oInputBand.insertCell(oInputBand.cells.length);
       oCell.className = 'dialogtextbrown';
       oCell.style.textAlign = 'right';
       oCell.innerHTML = sLabel;
       oCell.id = "SALABEL"

       oCell = oInputBand.insertCell(oInputBand.cells.length);
       oCell.style.textAlign = 'left';
       oCell.innerHTML = '<input id="' + sName + '" name="' + sName + '" autocpmplete="off" ' +
                         'type="text" class="LargeTextBoxesBrown" style="width:' + sWidth + '" ' + 
                         'onkeydown="return SearchAheadCompareFilms(event,this,' + 
                         sAnchor + ',' + sID + ',' + sQName + ',' + iAID + ')">';

       oSABand = oParent.insertRow(oParent.rows.length);

       oCell = oSABand.insertCell(oSABand.cells.length);
       oCell.innerHTML = '<input type="hidden" name="' + sName + 'ID" id="' + sName + 'ID">';

       oCell = oSABand.insertCell(oSABand.cells.length);
       oCell.id = sName + "ANCHOR";
       oCell.style.textAlign = "left";
    }
}

function ProcessDynamicSAReturn(sContent) {
    if (iSALocus == 1) ReturnFromSACompareFilms(oSearchAhead.oSearch,oSearchAhead.oID,oSearchAhead.oName);
    
    if (iSALocus == 2) {
        oSearchAhead.oSearch.value = "Searching...";
        onCurrentMarketClick(null, GetValue("PERFILMGENID"), 6, GetValue("SAMAINRELEASEID"));
    }

    if (iSALocus == 3) {
        oSearchAhead.oSearch.value = "Searching...";
        // window.location = "/publish.aspx?BID=" + GetValue("GENID") +
        // "&CID=" + GetValue("CRITICSEARCHID");

        window.location = GetValue("CRITICSEARCHID") + "/";
    }

    if (iSALocus == 4) {
        oSearchAhead.oSearch.value = "Searching...";
        // window.location = "/publish.aspx?BID=" + GetValue("GENID") +
        // "&CID=" + GetValue("CRITICSEARCHID");

        window.location = GetValue("MEDIASEARCHID") + "/";
    }
    
    if (iSALocus == 5) {
        SetSpanString("MAILFILMLIST", sContent);
        SetValue("SAMAIL", "");
    }
    
    if (iSALocus == 6) {
        SetSpanString("MAILADDRESSES", sContent);
        SetValue("ADDADDRESS", "");
    }

    if (iSALocus == 8) {
        SetSpanString("SENDING", "Message sent.");
        ShowElement("ButtonCloseMail");
    }

}
