﻿//
//	Auto Completion - Textbox typing
//	--------------------------------
//	
//	Required:
//		1. Attach this javascript file to the page being used.
//		2. Attach two methods to the textbox: OnKeyUp="getKeyWord();" and OnClick="getKeyWord();"
//					dim sTMClick, sTMFocus, sTMBlur
//						   sTMClick = "getKeyWord('txtQuery', 'xxprompts', 'txtQuery_hidden', '/scripts/vbs/ajaxprodcodesearch.asp?tb=txtQuery&dv=acprompts&rb=txtQuery_hidden&keyword=');"
//						   sTMFocus = "clearSearch(1, 'txtQuery', 'xxprompts', 'txtQuery_hidden', 'Product Code');"
//						   sTMBlur = "clearSearch(2, 'txtQuery', 'xxprompts', 'txtQuery_hidden', 'Product Code');"
//         	<input type="text" autocomplete="off" id="txtQuery" name="txtQuery" class="Keyword" value="Search for Product Code here..." onfocus="<%=sTMFocus%>" onblur="<%=sTMBlur%>" onkeyup="<%=sTMClick%>" onclick="<%=sTMClick%>" /><div id="xxprompts" class="acprompts"></div>
//
//		3. <div class="acprompts"></div>
//		4. Write a web service, using the doInput 
//

        var xmlhttp;
        try {
            xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
        } catch (e) {
            try {
                xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
            } catch (e) {
                try {
                    xmlhttp = new XMLHttpRequest ();
                } catch (e) {}
            }
        }


        function clearSearch(nType, sTextBox, sDivBox, sResultBox, sText) {
            //clear the box where they type in the person to search for
            // the type indicates which event has caused the clear
            var objI = document.getElementById(sTextBox);
            var objRB = document.getElementById(sResultBox);
            var objDiv = document.getElementById(sDivBox);

            var sTextBoxText = 'Search for ' + sText + ' here...';

            if (nType == 1) {
                //received focus
                if (objI.value == '') {
                    //if there is nothing in the input box, set text to start text in grey
                    objI.value = sTextBoxText;
                    objI.style.color = '#A3A3A3';
                    objRB.value = '';
                }
                if (objI.value == sTextBoxText) {
                    //if the start text is in the input box, set text to blank in black
                    objI.value = '';
                    objI.style.color = '#000000';
                    objRB.value = '';
                }
            }
            //There are different possibilities for the blur
            if (nType == 2) {
                // received blur, so reset to start position and close div
                objI.value = sTextBoxText;
                objI.style.color = '#A3A3A3';
                objRB.value = '';

                objDiv.innerHTML = ""; // empty prompt level
                objDiv.style.display = "none";

            }
            if (nType == 3) {
                // received blur, so close div, but leave text box as is
                objDiv.innerHTML = ""; // empty prompt level
                objDiv.style.display = "none";
                objI.style.color = '#000000';
            }
            if (nType == 4) {
                // received blur, so reset to start position 
                //dont close the div, since we need to use data eg with doInputGo 
                objI.value = sTextBoxText;
                objI.style.color = '#A3A3A3';
                objRB.value = '';
            }
            if (nType == 8) {
                // received blur, so reset to start position 
                //dont close the div, since we need to use data eg with doInputGo
                //alert("clearing due to blur");
            }

        }


        function getKeyWord(sTextBox, sDivBox, sResultBox, sPageURL) {
	        var objTB = document.getElementById(sTextBox); // for text field object
	        var objDiv = document.getElementById(sDivBox); // for Div

	        if (objTB.value == '') {
	            objDiv.innerHTML = ""; // empty prompt level
    	        objDiv.style.display = "none";
                return;
            }
            xmlhttp.open('get', sPageURL + objTB.value, true);
            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4) {
                    if (xmlhttp.status == 200) {
                        if (xmlhttp.responseText != '') {
                            objDiv.innerHTML = unescape(xmlhttp.responseText); // return the data to fill the background layer to the tips
                            objDiv.style.display = "block"; // Set visible layers Tips
                        } else {
                            clearSearch(3, sTextBox, sDivBox, sResultBox, '');
                        }
                    }
                    else {

                    }
                }
            }
            xmlhttp.setRequestHeader('If-Modified-Since', '0');
            xmlhttp.send (null);
        }

        function doInputGo(sTextBox, sDivBox, sResultBox, sText) {
            // used to jump to the URL requested

            var objTB = document.getElementById(sTextBox); // for text field object
            var objRB = document.getElementById(sResultBox); // for result text field object
            var objDiv = document.getElementById(sDivBox); // for Div

            objRB.value = sText; // Set the hidden control value to the new data.


            if (sText == '') {
                clearSearch(2, sTextBox, sDivBox, sResultBox, '');

            } else {
                objDiv.innerHTML = ""; // empty layer Tips
                objDiv.style.display = "none"; // set the prompt level is not visible

                objTB.value = "processing...";
                window.location = sText;
            }          

        }

        function doInputTwo(sTextBox, sDivBox, sResultBox1, sResult1, sResultBoxDD2, sResult2, sTextResult) {
            // used to set up a text box and a second, drop-down list box with results
            var objTB = document.getElementById(sTextBox); // for text field object
            var objRB1 = document.getElementById(sResultBox1); // for result text field object
            var objDiv = document.getElementById(sDivBox); // for Div

            objRB1.value = sResult1;
            ChangeSelectByValue(sResultBoxDD2, sResult2);
            clearSearch(3, sTextBox, sDivBox, sResultBox1, '');
        }

        function ChangeSelectByValue(ddlID, value) {
            var ddl = document.getElementById(ddlID);
            for (var i = 0; i < ddl.options.length; i++) {
                if (ddl.options[i].value == value) {
                    if (ddl.selectedIndex != i) {
                        ddl.selectedIndex = i;
                    }
                    break;
                }
            }
        }

