
document.qsArrDates    = new Array();
document.qsArrDisabled = new Array();

/**
 * load the dates for searchbyword
 * when the month is changed.
 *
 * @param   object  The object which holds the months
 */
function qsLoadDates(objMonth) {

    var strDates = document.wordsearch.dayvalue.value;
    var arrDates = strDates.split(';');
    var objDay = document.wordsearch.df;

    while(objDay.options.length > 0) objDay.options[0] = null;

    for(var i=0;i<(arrDates.length-1);i++) {

        var arrMonth = arrDates[i].split(':');

        if(arrMonth[0] == objMonth.options[objMonth.selectedIndex].value) {
            var arrDays = arrMonth[1].split(',');

            for(j=0;j<arrDays.length;j++) {
                var strDatePos = arrDays[j].indexOf("(");
                var strDateValue = arrDays[j].substring(0,strDatePos);
                arrDays[j]=arrDays[j].replace(/^\s+/,"");
                objDay.options[objDay.options.length] = new Option(arrDays[j],strDateValue);
            }
        }
    }
}

/**
 * Updates the available days in the dropdown
 * when the month is changed.
 * @param   object  The object which holds the months
 * @param   object  The object which holds the days
 * @param   array   An array with available days per month
 */
function qsUpdateDays(objMonth, objDay, arrDays){

    while(objDay.options.length > 0) objDay.options[0] = null;
    if (arrDays[objMonth.options[objMonth.selectedIndex].value]){
        arrValues = arrDays[objMonth.options[objMonth.selectedIndex].value].split(",");

        for(i=0;i<arrValues.length;i++){
            arrValue = arrValues[i].split(' ');
            objDay.options[objDay.options.length] = new Option(arrValues[i], arrValue[0]);
        }
    }

}

/**
 * Sets the month, updates the days dropdown and sets the day selected in the popup calendar
 * @param   integer  The month selected
 * @param   integer  The day selected
 */
function qsUpdateDateSimple(intMonth, intDay){
    document.forms[0].mf.value = intMonth;
    qsChangeMonth(document.forms[0].name, document.forms[0].mf);

    // After filling the day dropdown the chosen day is set
    document.forms[0].df.value = intDay;

    return true;
}

/**
 * Updates the hidden form elements in the dateselect form
 * whenever the date is changed in the quicksearch form.
 * @param   object  The object that holds the month
 * @param   object  The object that holds the day
 */
function qsUpdateDate(objMonth, objDay){
    var intDay   = '';
    var intMonth = '';
    if (objMonth.selectedIndex > 0) intMonth = objMonth.options[objMonth.selectedIndex].value;
    if (objMonth.selectedIndex > 0)   intDay = objDay.options[objDay.selectedIndex].value;
    document.forms['dateselect'].elements['mf'].value = intMonth;
    document.forms['dateselect'].elements['df'].value = intDay;
}


function qsChangeMonth(strForm, objMonth){
    var objForm = document.forms[strForm];
    if (!objForm) return;
    var objField = objForm.elements['df'];
    if (!objField) return;
    var intMonth = objMonth.options[objMonth.selectedIndex].value;
    qsUpdateDays(objMonth, objField, document.qsArrDates);
}

/**
 * Callback function for Ajax calls
 * @param   string  The form in which the field resided
 * @param   string  The field which should be updated
 * @param   array   The data to update with
 */

function qsUpdateField(strForm, strField, arrData)
{
    objForm = document.forms[strForm];
    if (!objForm) return;

    objField = objForm.elements[strField];
    if (!objField) return;

    intSet   = 0;
    intValue = objField.options[objField.selectedIndex].value;
    if (!intValue) {
        intValue = 0;
    }

    objField.selectedIndex = 0;
    while(objField.options.length > 0) objField.options[0] = null;
    var blnEnableDate = false;
    for(i=0;i<arrData.length-1;i+=2){
        if (strField == 'df' && arrData[i] > '') blnEnableDate = true;
        objField.options[objField.options.length] = new Option(arrData[i+1], arrData[i]);
        if (arrData[i] == intValue) {
            intSet = objField.options.length-1;
        }
    }

    objField.selectedIndex = intSet;

    arrFields = objForm.elements;
    for(i=0;i<arrFields.length;i++) arrFields[i].disabled = false;
    document.body.style.cursor = 'auto';
}

/**
 * Callback function for Ajax call to enable/disable the submit button
 * @param   boolean True enables the button, false disables it
 */
function qsToggleSubmit(strForm, blnEnable) {
    var objButton = document.getElementById('SearchButton');
    if (!objButton) return;
    if (blnEnable) {
            objButton.style.display = '';
            objButton.className = 'button buttondisabled';
            objButton.innerHTML = '<a href="#"><span>Zoeken</span></a>';
    } else {
        objButton.style.display = 'none';
    }

    /*arrFields = objForm.elements;
    for(i=0;i<arrFields.length;i++){
        if (arrFields[i].type == "submit"){
            if (blnEnable == true) arrFields[i].style.display = '';
            else arrFields[i].style.display = 'none';
        }
    }*/
}

/**
 * Updates the hidden value of the facility field, based
 * on selected facilities
 */
function qsSetFacilities(objCheckbox){
    var objForm   = objCheckbox.form;
    var objField  = objForm.elements['facility'];
    var arrName   = objCheckbox.name.split("_");
    var strId     = arrName[1];
    if (objField.value > '') arrValues = objField.value.split(",");
    else arrValues = new Array();
    var blnFound   = false;
    var intRemove  = -1;

    for(var i=0;i<arrValues.length;i++){
        if (arrValues[i] == strId){
            blnFound = true;
            if (objCheckbox.checked == false) intRemove = i;
        }
    }
    if (blnFound == false && objCheckbox.checked == true) arrValues[arrValues.length] = strId;
    else if (blnFound == true && objCheckbox.checked == false){
        arrNewValues = new Array();
        for(i=0;i<arrValues.length;i++){
            if (i!=intRemove) arrNewValues[arrNewValues.length] = arrValues[i];
        }
        arrValues = arrNewValues;
    }
    objField.value = arrValues.join(',');
    if (objField.value.substr(0,1) == ',') objField.value = objField.substr(1);
    objForm.submit();
}


/**
 * Updates the quicksearch fields whenever the users changes something.
 */
function qsUpdateData(strForm, strReqFields, strUpdate){
    strUrl    = '';
    objForm   = document.forms[strForm];

    arrFields = objForm.elements;
    strFields = '';
    for(i=0;i<arrFields.length;i++){
        strVal = '';
        switch(arrFields[i].type){
            case 'select':
            case 'select-one':
                strVal = arrFields[i].options[arrFields[i].selectedIndex].value;
            break;
            case 'hidden':
                strVal = arrFields[i].value;
            break;
        }
        arrFields[i].disabled = true;
        if (strVal > '') strUrl += (strUrl > '' ? "&" : "ajaxsrch.php?") + arrFields[i].name + '=' + strVal;
    }
    strUrl += '&qsflds=' + strReqFields + '&qsfrm=' + strForm + '&qsupd=' + strUpdate + '&qssrv=1';

    document.body.style.cursor = 'wait';
    ajaxLoadIFrame(strUrl);
}

function qsInitForm(strUrl){
    window.onload = function() {
        alaxLoadIFrame(strUrl);
    }
}

function qsSubmitForm(objForm){
    
    var arrFields     = objForm.elements;
    var strUrl        = "";
    var intAdded      = 0;
    var blnDateSet    = false;
    var blnPeriodSet  = false;
    for(i=0;i<arrFields.length;i++) {
        strVal   = '';
        strVar   = '';
        blnStore = false;
        switch(arrFields[i].type){
            case 'select':
            case 'select-one':
                if (arrFields[i].disabled == false){
                    if (arrFields[i].options.length > 0){
                        strVal   = arrFields[i].options[arrFields[i].selectedIndex].value;
                        strVar   = arrFields[i].name;
                        blnStore = (strVal > '' && (strVar == 'mf' || strVar == 'df' || strVar == 'period'));

                        if (strVar == 'df' && strVal != '') blnDateSet = true;
                        else if (strVar == 'period' && strVal != '') blnPeriodSet = true;
                    }
                }
                else blnStore = false;
            break;
            case 'hidden':
                strVal   = arrFields[i].value;
                strVar   = arrFields[i].name;
                blnStore = (strVar != 'rewrite' && strVal > '' && strVar != 'ext' && strVar != 'showsubmit');
            break;
            case 'checkbox':
                strVar = "facility[]";
                strVal = arrFields[i].name.substr(3);
                blnStore = arrFields[i].checked;
            break;
        }
        if (blnStore == true){
            strUrl += (intAdded == 0 ? '?' : '&') + strVar + '=' + strVal;
            intAdded++;
        }
    }

    if (blnDateSet && !blnPeriodSet){
        // alert('Selecteer eerst een verblijfsduur');
        // return false;
    }
    strUrl = '/hotels/' + arrFields['rewrite'].value + '/' + strUrl;
	
	var strRegexp = new RegExp("/\/*/", "i");
	
	if(strRegexp.exec(strUrl)) strUrl = strUrl.replace(strRegexp.exec(strUrl), '/');

	if(objForm.iswidget && objForm.iswidget.value) {
		//window.parent.document.location.href = strUrl;
		window.open(strUrl);
	} else {
		document.location.href=strUrl;
		$("#SearchButton").addClass('button buttondisabled');
	}

	setCookie('form_posted', "1");
    return false;
}

function qsUpdateExplanation(strExplanation){
    objExplanation = document.getElementById('qsExplanation');
    if (objExplanation)
        objExplanation.innerHTML = strExplanation;
}

function qsDisableSearch() {
	qsFieldperiod.style.visibility = "hidden";
}

function qsEnableSearch() {
    for(var i=0;i<document.qsArrDisabled.length;i++){
        if (document.qsArrDisabled[i]) document.qsArrDisabled[i].disabled = false;
    }
    document.qsArrDisabled = new Array();
	// qsFieldperiod.style.visibility = "visible";
}

function qsUpdateForm(strForm, strFields){
    var objForm   = document.forms[strForm];
    var arrFields = objForm.elements;
    var strUrl    = "/ajaxsrch.php";

    var searching = false;
    var widget    = false;

    if (document.qsArrDisabled.length > 0) return false;
    document.qsArrDisabled = new Array();
    for(i=0;i<arrFields.length;i++){
        strVal   = '';
        strVar   = '';
        blnStore = false;

        if (arrFields[i].name == 'iswidget') {
            var widget = true;
        }

        switch(arrFields[i].type){
            case 'select':
            case 'select-one':
                if (arrFields[i].disabled) blnStore = false;
                else {
                    document.qsArrDisabled[document.qsArrDisabled.length] = arrFields[i];
                    if (arrFields[i].options.length > 0){
                        strVal = arrFields[i].options[arrFields[i].selectedIndex].value;
                        blnStore = true;
                        if (!searching) {
                            searching = (strVal.toString().length > 0);
                        }
                        strVar = arrFields[i].name;
                    }
                }
            break;
            case 'hidden':
                document.qsArrDisabled[document.qsArrDisabled.length] = arrFields[i];
                strVal = arrFields[i].value;
                strVar = arrFields[i].name;
                blnStore = true;
            break;
            case 'checkbox':
                strVar = "facility[]";
                strVal = arrFields[i].name.substr(3);
                blnStore = arrFields[i].checked;
                if (!searching) {
                    searching = arrFields[i].checked;
                }
                document.qsArrDisabled[document.qsArrDisabled.length] = arrFields[i];
            break;
        }
        if (blnStore == true){
            strUrl += (i==0 ? '?' : '&') + strVar + '=' + strVal;
        }
    }

    if (searching) {
        strUrl += '&showsubmit=1';
    }

    if (widget) {
        strUrl += '&iframe=widget';
    }

    strUrl += '&flds=' + escape(strFields);
    for(var i=0;i<document.qsArrDisabled.length;i++){
        document.qsArrDisabled[i].disabled = true;
    }
    ajaxLoadIFrame(strUrl);
    qsToggleSubmit(strForm, searching);
}

function qsTouricoPage(strUrl,strValue) {
    var myExp = /\s/g;

    if(strValue.charAt(0) == "@"){
        url=strUrl+"world/search/";
        window.location.href=url;
    }
}

function qsOnloadFunction(strUrl){
    var objForm   = document.forms['quicksearch'];
    var arrFields = objForm.elements;

    for(i=0;i<arrFields.length;i++){
        strVal = '';
        strKey='';
        var intIndex = 0;
        
        if(arrFields[i].selectedIndex < 1) {
            intIndex = 0;
        } else {
            intIndex = arrFields[i].selectedIndex;
        }

        switch(arrFields[i].name) {
            case 'country':
            case 'region':
            case 'city':
            case 'hotel':
                if(arrFields[i].options[intIndex]) {
                    strVal = arrFields[i].options[intIndex].value;
                } else {
                    strVal = '';
                }
                break;
            case 'facility':
            case 'feature':
            case 'stars':
            case 'price':
                if(arrFields[i].options[intIndex]) {
                    strVal = arrFields[i].options[intIndex].value;
                } else {
                    strVal = '';
                }
            break;
        }

        if(strVal!="") {
            qsUpdateForm('quicksearch', arrAttrib[arrFields[i].name] ,strUrl);
        }
     
    }
}

//autocomplete search function starts
var suggestions=new Array();
var arrResult="";
var outp;
var oldins;
var posi = -1;
var words = new Array();
var input;
var key;
var xPos;
var yPos;
var lock=false;
var searchUrl;
/* Function that disbales the zoeken button when submitted
 * @param:none
 *
**/
function qsdisableSearchWord(){
    //qsactualDisableSearch();
    setTimeout("qsactualDisableSearch()",60);
}

function qsEnableSearchWord(strUrl){
    document.forms['wordsearch'].elements['word'].disabled = false;
    document.forms['wordsearch'].elements['mf'].disabled = false;
    document.forms['wordsearch'].elements['df'].disabled = false;

    var objButton = document.getElementById('SearchWordButton');
    if (!objButton) return;
    objButton.className = 'button';
    objButton.innerHTML = "<a  href='#' onClick='qsSubmitSearch(\""+strUrl+"\");return false;'><span>Zoek</span></a>";
    lock = false;
}
/* Function that disbales the zoeken button when submitted
 * @param:none
 *
**/
function qsactualDisableSearch(){
    var objButton = document.getElementById('SearchWordButton');
    if (!objButton) return;
    objButton.className = 'button buttondisabled';
    objButton.innerHTML = '<a href="#"><span>Zoek</span></a>';
    document.forms['wordsearch'].elements['word'].disabled = true;
    document.forms['wordsearch'].elements['mf'].disabled = true;
    document.forms['wordsearch'].elements['df'].disabled = true;
}

function getSearchWordUrl(strFound,strWord,mf,df) {
    if(mf != "" && df != ""){
        url = "zoek/?found="+strFound+"&word="+strWord+"&mf="+mf+"&df="+df;
    } else {
        url = "zoek/?found="+strFound+"&word="+strWord;
    }
    return url
}

function qsSubmitSearch(strUrl,strText) {
    if(!lock){
        lock = true;
        var strWord     = escape(document.forms['wordsearch'].elements['word'].value);
        var strFound    = document.forms['wordsearch'].elements['found'].value;
        var mf          = document.forms['wordsearch'].elements['mf'].value;
        var df          = document.forms['wordsearch'].elements['df'].value;

        if(strWord == "" || strWord == null || strWord == strText) {
            alert('Geen resultaten gevonden voor uw zoekopdracht');
            document.forms['wordsearch'].elements['word'].focus();
            lock    =false;
            return false;
        } else {
            qsactualDisableSearch();
			setCookie('form_posted', "1");
            url = strUrl+ getSearchWordUrl(strFound,strWord,mf,df);
            document.location.href=url;
            setTimeout("qsEnableSearchWord('"+strUrl+"')",10000);
            return false;
       }
    }
}

function qsCheckValue(e,strUrl) {
    var strWord=escape(document.forms['wordsearch'].elements['word'].value);
    var strFound=document.forms['wordsearch'].elements['found'].value;
    var mf=document.forms['wordsearch'].elements['mf'].value;
    var df=document.forms['wordsearch'].elements['df'].value;

    if(window.event) { 
        keynum = e.keyCode;
    }else if(e.which){
        keynum = e.which;
    }

    if(keynum == 13){
        qsdisableSearchWord();
        url = strUrl + getSearchWordUrl(strFound,strWord,mf,df);
        window.status=url;
        document.location.href = url;
        return false;
    }
}

function qsAutoCompleteList(url,strSearchUrl) {
    var strWord         = document.forms['wordsearch'].elements['word'].value.toLowerCase();
    var strWordLength   = strWord.length;
    var strUrl          = url;
    strUrl +='&word='+strWord;

    if(arrResult.length ==0 &&  strWord.length>0 && strWord.length<=4) {
        ajaxLoadRequest(strUrl,qsAjaxdata);
    }else if (arrResult.length>0 && strWord.length>0) {

        suggestions = new Array();
        var tmpSplitArray = arrResult.split(",");

        if(tmpSplitArray[0]=="file")
            document.wordsearch.found.value=1;
        else
            document.wordsearch.found.value=0;

        var j = 0;
        for(var i=1;i<tmpSplitArray.length;i++){
            if(tmpSplitArray[i].substring(0,strWordLength)==strWord && j<10){
                suggestions[j]=tmpSplitArray[i];
                j++;
            }
        }

        searchUrl=strSearchUrl;
        if(suggestions.length >0)   {
            outp = document.getElementById("output");
            qsLookAt();
            qsSetVisible("visible");
            document.onkeydown = qsKeyHandler;
            document.onkeyup = qsKeyHandler;
        }else if(suggestions.length==0 && (strWord.length==4 || strWord.length==5)){
            strUrl +='&word='+strWord+"&found=1";
            ajaxLoadRequest(strUrl,qsAjaxdata);	
        }else{
            qsSetVisible("hidden");
        }
    } else {
        arrResult = "";
        qsClearOutput();
        qsSetVisible("hidden");
    }
}

function qsAjaxdata(arrData) { 
    arrResult   = arrData.toLowerCase();
    var strWord = document.wordsearch.word.value.toLowerCase();
    var strWordLength   = strWord.length;

    if(arrData.length>0) {
        suggestions = new Array();
        var tmpSplitArray = arrResult.split(",");

        if(tmpSplitArray[0] == "file")
            document.wordsearch.found.value=1;
        else
            document.wordsearch.found.value=0;

        var j=0;
        for(var i=1;i<tmpSplitArray.length;i++) {
            if(tmpSplitArray[i].substring(0,strWordLength) == strWord && j<15) {
                suggestions[j]=tmpSplitArray[i];
                j++;
            }
        }

        if(suggestions.length >0) {
            outp = document.getElementById("output");
            qsLookAt();
            qsSetVisible("visible");
            document.onkeydown = qsKeyHandler;
            document.onkeyup = qsKeyHandler;
        }
     }
}

function qsSetVisible(visi) {

    var x = document.getElementById("shadow");
    var t = document.getElementsByName("word")[0];
    x.style.position = 'absolute';
    x.style.top =  (qsFindPosY(t))+"px";
    var intMinuxWidth = 0;
    if (screen.width>= 1600)
                    intMinuxWidth = 390;
    else if (screen.width>= 1280 && screen.height>=720)
                    intMinuxWidth = 220;
    else if (screen.width>= 1152 && screen.height>=864)
                    intMinuxWidth = 160;
    else if (screen.width>= 1024 && screen.height>=768)
                    intMinuxWidth = 90;
    else if (screen.width>= 800 && screen.height>=600)
                    intMinuxWidth = -20;
    x.style.left = (qsFindPosX(t) - intMinuxWidth)+"px";
    x.style.visibility = visi;
}


function qsFindPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (1) {
            curleft+=obj.offsetLeft;
            if (!obj.offsetParent) {
                break;
            }
            obj=obj.offsetParent;
        }
    } else if (obj.x) {
        curleft+=obj.x;
    }
    return curleft;
}

function oldqsFindPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curleft += obj.offsetLeft;
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function qsFindPosY(obj){
    var curtop = 0;
    if (obj.offsetParent){
        curtop += obj.offsetHeight;
        while (obj.offsetParent){
            curtop += obj.offsetTop;
            obj = obj.offsetParent;
        }
    }
    else if (obj.y) {
        curtop += obj.y;
        curtop += obj.height;
    }
    return curtop;
}

function qsLookAt(){
    var ins = document.getElementsByName("word")[0].value.toLowerCase();
    if (oldins == ins) return;
    else if (posi > -1);
    else if (ins.length > 0){
        words = qsGetWord(ins);
        if (words.length > 0){
            qsClearOutput();
            for (var i=0;i<words.length; ++i) {
                qsAddWord (initCaptial(words[i]));
            }
            qsSetVisible("visible");
            input = document.getElementsByName("word")[0].value;
        }
        else{
            qsSetVisible("hidden");
            posi = -1;
        }
    }
    else{
        qsSetVisible("hidden");
        posi = -1;
    }
    oldins = ins;
}
	
function qsAddWord(word) {
    var Text1=document.createElement("span");
    Text1.innerHTML = qsHighlight(word, document.wordsearch.word.value.toLowerCase(), "", "");

    //var Text2=document.createTextNode("");
    var sp = document.createElement("div");
    var tbl = document.createElement("span");
    sp.appendChild(Text1);
    tbl.className = "look2";
    sp.appendChild(tbl);
    //tbl.appendChild(Text2);
    sp.onmouseover = mouseHandler;
    sp.onmouseout = mouseHandlerOut;
    sp.onclick = mouseClick;
    outp.appendChild(sp);
}

function qsClearOutput() {
  if(outp) {
    while (outp.hasChildNodes()) {
        noten=outp.firstChild;
        outp.removeChild(noten);
    }
    posi = -1;
    suggestions = new Array();
    oldins=""  
  }
}

function qsGetWord(beginning){
   beginning = beginning.toLowerCase();
   var words = new Array();
   var ins = document.getElementsByName("word")[0].value;
    for (var i=0;i<suggestions.length; ++i){
        var j = -1;
        var correct = 1;
        while (correct == 1 && ++j < beginning.length){
            if (suggestions[i].charAt(j) != beginning.charAt(j) ) correct = 0;
        }
        if (correct == 1) words[words.length] = suggestions[i];
    }
    return words;
}

function qsSetColor(_posi, _color, _forg){

    if(outp && _posi>=0){
        outp.childNodes[_posi].style.background = _color;
        outp.childNodes[_posi].style.color = _forg;
        outp.childNodes[_posi].firstChild.firstChild.style.background = _color;
        outp.childNodes[_posi].firstChild.firstChild.style.color = _forg;
    }
    if(_posi>0){
       qsResetColor(_posi - 1);
    }
    if((_posi+1) < words.length){
       qsResetColor(_posi + 1);
    }
}

function qsResetColor(_posi) {
    if(outp){
        outp.childNodes[_posi].style.background = "white";
        outp.childNodes[_posi].style.color = "black";
        outp.childNodes[_posi].firstChild.firstChild.style.background = "white";
        outp.childNodes[_posi].firstChild.firstChild.style.color = "black";
    }
}

function qsKeygetter(event){
    if (!event && window.event) event = window.event;
    if (event) key = event.keyCode;
    else key = event.which;
}

var WhetherKesupforDownKey = 0;
var WhetherKesupforUpkey = 0;

function KeepDown(){ 
    var textfield = document.getElementsByName("word")[0].value;
    if(WhetherKesupforDownkey != 1){
        if (words.length > 0 && posi < words.length-1){
            if(posi < words.length-1){
                posi++;	
                if (posi >=0) qsSetColor(posi, "#ffffff", "black");
                else input = initCaptial(textfield.value);
                qsSetColor(posi, "#0a2d64", "white");
                if(document.all){
                    document.getElementsByName("word")[0].value =outp.childNodes[posi].innerText;
                } else{
                    document.getElementsByName("word")[0].value =outp.childNodes[posi].textContent;
                }
            }
        }
    }
    return;
}

function KeepUp(){ 
    var textfield = document.getElementsByName("word")[0].value;
    if(WhetherKesupforUpkey != 1){
        if (words.length > 0 && posi > 0){
            if(posi >0){
                posi--;
                if (posi < words.length-1) qsSetColor(posi+1, "#ffffff", "black");
                else input = initCaptial(textfield.value);
                qsSetColor(posi, "#0a2d63", "white");

                if(document.all){
                    document.getElementsByName("word")[0].value =outp.childNodes[posi].innerText;
                } else{
                    document.getElementsByName("word")[0].value =outp.childNodes[posi].textContent;
                }
            }
        }
    }
    return;
}

//Function for capitalize the first letter of every word
function initCaptial(strSearchWord){
    return (strSearchWord.substring(0,1).toUpperCase() + strSearchWord.substring(1,strSearchWord.length));
}

var keyname;
function qsKeyHandler(event){
    var textfield   = document.getElementsByName("word")[0].value;
    var mf          = document.forms['wordsearch'].elements['mf'].value;
    var df          = document.forms['wordsearch'].elements['df'].value;
    var strFound    = document.forms['wordsearch'].elements['found'].value;

    if (!event && window.event) event = window.event;
    if (event) key = event.keyCode;
    else key = event.which;
    if (event) keyname = event.type;
    if (document.getElementById("shadow").style.visibility == "visible") {
        if (key == 27){ // Esc
            textfield.value =input;
            qsSetVisible("hidden");
            posi = -1;
            oldins = input;
            return false;
        }else if(key == 13){ //enter

            if (words.length > 0 && posi < words.length){

                if (posi >=0) qsSetColor(posi, "#ffffff", "black");
                else input = textfield.value;

                qsSetColor(posi, "#0a2d63", "white");
                qsSetVisible("hidden");
                qsdisableSearchWord();
                var strWord=escape(textfield);
                url = searchUrl + getSearchWordUrl(strFound,strWord,mf,df);
                window.status=url;
                document.location.href=url;
                return false;
            }
        }else if (key == 40) { //Key down
            if(keyname == "keydown") {
                WhetherKesupforDownkey = 0;
                KeepDown();
            } else {
                if(document.all){
                    document.getElementsByName("word")[0].value =outp.childNodes[posi].innerText;
                } else{
                    document.getElementsByName("word")[0].value =outp.childNodes[posi].textContent;
                }
                WhetherKesupforDownkey = 1;
            }
        }else if (key == 38){ //Key up
            if(keyname == "keydown"){
                WhetherKesupforUpkey = 0;
                KeepUp();
            }else{
                if(document.all) {
                    document.getElementsByName("word")[0].value =outp.childNodes[posi].innerText;
                } else {
                    document.getElementsByName("word")[0].value =outp.childNodes[posi].textContent;
                }
                WhetherKesupforUpkey = 1;
            }
        }else if (key == 8){ // Backspace
            posi = -1;
            oldins="";
        }
    }
}

var mouseHandler=function(){
    for (var i=0;i<words.length;++i)
        qsSetColor (i, "#ffffff", "black");

    if(document.all){
        document.getElementsByName("word")[0].value =this.innerText;
    } else{
        document.getElementsByName("word")[0].value =this.textContent;
    }

    this.firstChild.firstChild.style.color  = "#ffffff";
    this.firstChild.firstChild.style.background  = "#0a2d63";
    this.style.background = "#0a2d63";
    this.style.color= "#ffffff";
}

var mouseHandlerOut=function(){

    this.style.background = "#ffffff";
    this.style.color= "black";
    this.firstChild.firstChild.style.color  = "black";
}

var mouseClick=function(){
    if(document.all){
        document.getElementsByName("word")[0].value =this.innerText;
    } else {
        document.getElementsByName("word")[0].value =this.textContent;
    }
    qsSetVisible("hidden");
    posi = -1;
    oldins = this.innerText;
}

function qsHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag) {

    // the highlightStartTag and highlightEndTag parameters are optional
    if ((!highlightStartTag) || (!highlightEndTag)) {
        highlightStartTag = "<b>";
        highlightEndTag = "</b>";
    }
  
    // find all occurences of the search term in the given text,
    // and add some "highlight" tags to them (we're not using a
    // regular expression search, because we want to filter out
    // matches that occur within HTML tags and script blocks, so
    // we have to do a little extra validation)
    var newText = "";
    var i = -1;
    var lcSearchTerm = searchTerm.toLowerCase();
    var lcBodyText = bodyText.toLowerCase();
    
    while (bodyText.length > 0) {
        i = lcBodyText.indexOf(lcSearchTerm, i+1);
        if (i < 0) {
            newText += bodyText;
            bodyText = "";
        } else {
          // skip anything inside an HTML tag
          if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {

                // skip anything inside a <script> block
                if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
                   if(i==0) {
                        return newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag + bodyText.substr(i + searchTerm.length);
                   }
                      bodyText = bodyText.substr(i + searchTerm.length);
                      lcBodyText = bodyText.toLowerCase();
                      i = -1;
                }
            }
       }
  }
  
  return newText;
}

function expandSearch(fliplink){
  var elem, vis;
  if( document.getElementById )
    elem = document.getElementById( fliplink );
  else if( document.all ) 
      elem = document.all[fliplink];
  else if( document.layers )
    elem = document.layers[fliplink];
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here
  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}