/*
 *
 * File: basicQuery.js
 *
 * Javascript for Verity K2 Enterprise Web Sample Templates - Basic Query
 *
 * Depends upon:
 *      none
 */

var g_blnLastChecked = false;


// Helper function to trim whitespace
String.prototype.trim = function()
{
  var strThis = this;
  strThis = strThis.replace(/^\s*(.*)/, "$1");
  strThis = strThis.replace(/(.*?)\s*$/, "$1");
  return strThis;
}

// AUTO100
/*
Array.prototype.isset = function(key)
{
    for (i in this)
    {
        if (key == i) return true;
    }
    return false;
}
*/    
/*
 * =====================================================================
 *                 F O R M   S U B M I T   &   C L E A R
 * =====================================================================
 */
 
function generateQueryBasic(form, g_strTypo, g_strSoundex, g_strThesaurus)
{
    var strTextInput = getValue(form, "TextInput");
    strTextInput = strTextInput.trim();
        
    // backward compatibility: check if TextInput is available as field at
    // all, if not, try to read-out QueryText
    if (strTextInput == "")
    {
        strTextInput = getValue(form, "QueryText");
    }
    
    
    // encode any non-ASCII chars into utf-8 charcodes and send them
    // over the wire as {XXX}
    strTextInput = encodeUtf8Entities(strTextInput);

    setValue(form, "QueryText", strTextInput);
    setValue(form, "hiddenOriginalQueryText", strTextInput);
    setValue(form, "hiddenBasicQueryText", strTextInput);

    var strSourceQuery = "";
    
	// exclude pdf
	// -----------
    var strExcludePDF = getExcludePDF(form);
    if (strExcludePDF != "")
    {
        strSourceQuery = "(" + strExcludePDF + ")";
    }

    setValue(form, "SourceQueryText", strSourceQuery);

	// disable the submit button, so if the search takes a little longer the user does
	// not do click-click-click
	// the search field with the name "search" may not exist if the form is the header 
	// search form
	if (form.elements['search'])
	{
		form.elements['search'].disabled = true;
	}
	
    // finally: submit
    // -----------------------
    form.submit();
}


/*
 * =====================================================================
 *                 H E L P E R   F U N C T I O N S
 * =====================================================================
 */


function getValue(myForm, name) {
    if (myForm == null) return "";
	myElem = myForm.elements[name];
	if (myElem == null) return "";
	return myElem.value;
}

function setValue(myForm, name, value) {
	if (myForm == null) return false;
	myElem = myForm.elements[name];
	if (myElem == null) return false;
	myElem.value = value;
	return true;
}

// ACI: Abfrage des (Index-)Feldes "FILETYPE"
function getExcludePDF(form)
{
    var strResult = "";
    var elmExcludePDF = form.elements["ExcludePDF"];
    if (elmExcludePDF)
    {
        var strExcludePDF = elmExcludePDF.checked;
        if (strExcludePDF == true) {
            //strResult = "<NOT> MIME-Type <MATCHES> application/pdf";
            strResult = "NOT(.pdf:FILETYPE)";
        }
    }

    return strResult;
}

//
// inspired by from http://aktuell.de.selfhtml.org/artikel/javascript/utf8b64/utf8.htm
//
function encodeUtf8Entities(source)
{
     source = source.replace(/\r\n/g,"\n");
     var dest = "";
     
     for (var n=0; n<source.length; n++)
     {
         var c=source.charCodeAt(n);
         if (c<128)
         {
             dest += String.fromCharCode(c);
         }
         else if(c>127)
         {
             dest += "{"+c+"}";
         }
     }
     return dest;
}

//
// Calls generateQuery for the head search field.
//
function generateQueryHead() {
    
	var form = document.getElementById("formBasicQueryHead");
	if (!form || typeof form != "object")
	{
		alert("Cannot find form, please ensure that\nit has the unique ID \"formBasicQueryHead\".");
		return false;
	}
	
    generateQueryBasic(form, false, false, false);   
}