﻿//    Author        : Henk-Johan Ketelaar
//    Date          : 08-01-2010
//    Description   : Send the text to the qweery server

var xmlhttp = null;
var dodont = false;

    // get content and fill the search div
    function showHint(evt, str) {
        q = trim(str);

        if (str != "") {
            // check if enter if pressed
            evt = (evt) ? evt : event;
            var charCode = (evt.which) ? evt.which : evt.keyCode;
            if (charCode == 13) {
                return;
            }
            
            var a = setTimeout("checkTyping()", 50);
            if (dodont) {
                // trim the input
                var qweery_input = trim(str);

                // place an aterisk behind the string
                qweery_input += "*"

                if (qweery_input.length == 0) {
                    document.getElementById("divtyperesult").innerHTML = "";
                    return;
                }
                if (window.XMLHttpRequest) {
                    // code for IE7+, Firefox, Chrome, Opera, Safari
                    xmlhttp = new XMLHttpRequest();
                }
                else {
                    // code for IE6, IE5
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                //        var url = "http://box.qweery.nl/create2fit/?fe=MHwxfDIx&q=" + qweery_input;
                var url = "/qweery_search.aspx?template_id=MHwxfDIx&searchterm=" + qweery_input + "&startpage=0&pagenum=0";

                url = url + "&sid=" + Math.random();
                xmlhttp.open("GET", url, false);
                xmlhttp.send(null);
                document.getElementById("divtyperesult").innerHTML = xmlhttp.responseText;

                // show the div
                document.getElementById('divtyperesult').style.visibility = "visible";

                dodont = false;
            }
        }
    }

    // get content and fill the center div "content"
    function showBigHint(str, pagestart, pagenum) {
        
        // fill if string is empty
        if (!str) {
            str = document.getElementById("search").value;
        }

        // hide the search div
        document.getElementById('divtyperesult').style.visibility = "hidden";

        // trim the input
        var qweery_input = trim(str);

        // place an aterisk behind the string
        qweery_input += "*"

        // to clean the extra asterix if there is one
        qweery_input = qweery_input.replace("**","*");

        if (qweery_input.length == 0) {
            document.getElementById("divtyperesult").innerHTML = "";
            return;
        }
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        }
        else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        //        var url = "http://box.qweery.nl/create2fit/?fe=MHw2fDIx&q=" + qweery_input;
        var url = "qweery_search.aspx?template_id=MHw2fDIx&searchterm=" + qweery_input + "&startpage=" + pagestart + "&pagenum=" + pagenum;

        url = url + "&sid=" + Math.random();
        xmlhttp.open("GET", url, false);
        xmlhttp.send(null);
        document.getElementById("content").innerHTML = xmlhttp.responseText;
    }

    // trim the input to get no trailing or leading spaces
    function trim(str, charlist) {
        var whitespace;

        if (!charlist) {
            whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
        } else {
            whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
        }

        for (var i = 0; i < str.length; i++) {
            if (whitespace.indexOf(str.charAt(i)) === -1) {
                str = str.substring(i);
                break;
            }
        }
        for (i = str.length - 1; i >= 0; i--) {
            if (whitespace.indexOf(str.charAt(i)) === -1) {
                str = str.substring(0, i + 1);
                break;
            }
        }
        return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
    }

    function closeTypeResult() {

        document.getElementById('divtyperesult').style.visibility = "hidden";
    }

    // catch the navigation click
    // and reload the content div with the right content
    function OtherHintPage(pageurl) {

        // in argument and value couples
        var aryUrl = pageurl.split("&");

        // split the couples
        var aryUrl2 = aryUrl[1].split("=");
        var aryUrl3 = aryUrl[2].split("=");
        var aryUrl4 = aryUrl[3].split("=");

        showBigHint(aryUrl2[1],aryUrl3[1],aryUrl4[1])
    }
    

    // check if enter is pressed
    function captureEvt(evt,str) {
        evt = (evt) ? evt : event;
        var charCode = (evt.which) ? evt.which : evt.keyCode;
        if (charCode == 13) {
            // Henk-Johan Ketelaar: 14-01-2010
            // turned off, turn on to load the result in the center div
//            showBigHint(str,"0","0");
        }
    }

    // clean the input box
    function clean_search() {

        if (document.getElementById("search").value == "zoeken")
        {
            document.getElementById("search").value = "";
        }
    }

    function reset_search() {

        if (document.getElementById("search").value == "")
        {
            document.getElementById("search").value = "zoeken";
        }
    }

////    var q;
////    function checkTyping() {
////        var q2 = trim(document.getElementById("search").value);
////        
////        if (q2 == q) {
////            return true;
////        }
////        else {
////            return false;
////        }
////    }

    function onSearchType() {
        var q = trim(document.getElementById("search").value);
        document.getElementById("divtyperesult").innerHTML = q;
        document.getElementById("divtyperesult").style.visibility = "visible";
        setTimeout("getTypeResult('" + q + "')", 50);        
    }

    function getTypeResult(q2) {
        var q = trim(document.getElementById("search").value);
        if (q != "") {
            if (q == q2) {
                q = q + "*";
                var url = "/qweery_search.aspx?template_id=MHwxfDIx&searchterm=" + q + "&startpage=0&pagenum=0";

                new Ajax.Request(url,
			{ method: 'GET',
			    parameters: {},
			    onSuccess: function(transport) {
			        var response = transport.responseText;
			        //alert(response);
			        if (response == "") {
			            document.getElementById("divtyperesult").style.visibility = "hidden";
			            document.getElementById("divtyperesult").innerHTML = "";
			        }
			        else {
			            document.getElementById("divtyperesult").innerHTML = response;
			            document.getElementById("divtyperesult").style.visibility = "visible";
			        }
			    }
			});
            } else {
                return;
            }
        }
        else {
            document.getElementById("divtyperesult").style.visibility = "hidden";
            document.getElementById("divtyperesult").innerHTML = "";
        }
    }
    
    