
// *************************************
// KNOWLEDGE BASE FUNCTIONS
// USED IN DIFFERENT PLACES
// *************************************
var lastsent="";
var request=0;
var searchstring = "";

Ext.BLANK_IMAGE_URL = 'javascript/ext/images/gray/s.gif';

function setKBRequest(t)
{
    if (document.getElementById('ajaxLoader')) {
        document.getElementById('ajaxLoader').style.display = 'none';
    }

    if (document.getElementById('kb_wrap_searchingarticles')) {
        document.getElementById('kb_wrap_searchingarticles').style.display = 'none';
    }

    var newcontent = "";

	if(t.responseText!=""){
            var responseArr = t.responseText.split('|');
            if (typeof responseArr[1] == 'undefined' || responseArr[0] != 'OK') {
                    alert(t.responseText.stripTags());
                    return;
            }
            if (responseArr[1] == "") {
                newcontent = "Sorry we didn't find any articles matching your search phrase: "+ searchstring;
                //newcontent = "";
            } else {
                newcontent = responseArr[1];
            }
            if (document.getElementById('kb_wrap_buttons')) {
                document.getElementById('kb_wrap_buttons').style.display = '';
            }

	}

    document.getElementById('kb_wrap').innerHTML = newcontent;
    if (document.getElementById('kb_wrap_wrap')) {
        document.getElementById('kb_wrap_wrap').style.display = "block";
    }
}

function sendKBRequest(str, ispublic, path)
{

    searchstring = str;

	if(str=="") {
        return;
    }
	lastsent=str;
	request++;

    var timeStamp = new Date();

    if (document.getElementById('ajaxLoader')) {
        document.getElementById('ajaxLoader').style.display = 'block';
    }

    if (document.getElementById('latestArticles')) {
        document.getElementById('latestArticles').style.display = 'none';
    }

    Ext.Ajax.request({
        url: path + 'index.php?fuse=knowledgebase&view=KB_AutoSuggestion',
        params: {subject:str, request:request, path:path},
        success: setKBRequest
    });    
}

function loadKBArticles(e, inputField, ispublic, path)
{
    //kb_wrap_searchingarticles
    if (document.getElementById('kb_wrap_searchingarticles')) {
        document.getElementById('kb_wrap_searchingarticles').style.display = '';
        document.getElementById('kb_wrap').innerHTML = "";
    }

    var str = ltrim(inputField.value);
    if(str == lastsent || str == "") {
        if (document.getElementById('kb_wrap_searchingarticles')) {
            document.getElementById('kb_wrap_searchingarticles').style.display = 'none';
        }

        if (document.getElementById('kb_wrap')) {
            document.getElementById('kb_wrap').innerHTML = "";
        }

        if (document.getElementById('kb_wrap_buttons')) {
            document.getElementById('kb_wrap_buttons').style.display = 'none';
        }
        return;
    }

    var lastChar = str.substr(str.length -1);
    if (lastChar != " ") {
        setTimeout("checkIfTypingStopped('"+inputField.id+"','"+str+"',"+ ispublic+",'"+path+"')", 500);
    } else {
        sendKBRequest(str, ispublic, path);
    }
}

function checkIfTypingStopped(fieldName, str, ispublic, path)
{

    if (document.getElementById(fieldName).value != str) {
        return;
    }

    sendKBRequest(str, ispublic, path);
}


/*
 * JS for GreyBox used for ToS / TC's
 */
var GB_DONE = false;
var GB_HEIGHT = 400;
var GB_WIDTH = 400;
var GB_ANIMATION = true;

function GB_show(caption, div, height, width) {
  GB_HEIGHT = height;
  GB_WIDTH = width;
  if(!GB_DONE) {
    $(document.body)
      .append("<div id='GB_overlay'></div><div id='GB_window'></div>");
    
    $(window).resize(GB_position);
    GB_DONE = true;
  }

  $("#GB_frame").remove();
  $("#GB_window").html("<div id='GB_caption'></div>"
        + "<img src='templates/default/images/greybox/close.gif' alt='Close window'/>"+document.getElementById(div).innerHTML);
  $("#GB_window img").click(GB_hide);
    $("#GB_overlay").click(GB_hide);
  $("#GB_caption").html(caption);
  $("#GB_overlay").show();
  GB_position();

  if(GB_ANIMATION)
    $("#GB_window").slideDown("slow");
  else
    $("#GB_window").show();
}

function GB_hide() {
  $("#GB_window,#GB_overlay").hide();
}

function GB_position() {
  var de = document.documentElement;
  var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
  $("#GB_window").css({width:GB_WIDTH+"px",height:GB_HEIGHT+"px",
    left: ((w - GB_WIDTH)/2)+"px" });
  $("#GB_frame").css("height",GB_HEIGHT - 32 +"px");
}
