/**
 * Copyright 2005 Zervaas Enterprises (www.zervaas.com.au)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

function ajaxac_createXMLHttp()
{
    var ret = null;
    try {
        ret = new ActiveXObject('Msxml2.XMLHTTP');
    }
    catch (e) {
        try {
            ret = new ActiveXObject('Microsoft.XMLHTTP');
        }
        catch (ee) {
            ret = null;
        }
    }
    if (!ret && typeof XMLHttpRequest != 'undefined')
        ret = new XMLHttpRequest();

    return ret;
}

function ajaxac_attachWidget(hook, id)
{
    if (hook.length > 0 && id.length > 0) {
        evalStr = hook + " = document.getElementById('" + id + "');";
        eval(evalStr);
    }
}

function ajaxac_receivejsarray(code)
{
    eval('var ret = ' + code);
    return ret;
}

function ajaxac_countdowntimer(cmd, ms)
{
    this.cmd = cmd;
    this.ms = ms;
    this.tp = 0;
}

ajaxac_countdowntimer.prototype.start = function()
{
    if (this.tp > 0)
        this.reset();
    this.tp = window.setTimeout(this.cmd, this.ms);
}

ajaxac_countdowntimer.prototype.reset = function()
{
    if (this.tp > 0)
        window.clearTimeout(this.tp);
    this.tp = 0;
}

function trim(str)
{
    return str.replace(/^(\s+)?(\S*)(\s+)?$/, '$2');
}

function ltrim(str)
{
    return str.replace(/^\s*/, '');
}

function rtrim(str)
{
    return str.replace(/\s*$/, '');
}

function delay(milliseconds)
{
    var then, now;
    then = new Date().getTime();
    now = then;
    while ((now - then) < milliseconds) {
        now = new Date().getTime();
    }
}

function ajaxac_getkeycode(e)
{
    if (document.layers)
        return e.which;
    else if (document.all)
        return event.keyCode;
    else if (document.getElementById)
        return e.keyCode;
    return 0;
}


//======================================================================

var gsc_basicmatch = /[a-z0-9]/i;

function gsc_getquery(elt, q)
{
    q = ltrim(q);
    q = q.replace('\s+', ' ');
    if (q.length == 0 || !gsc_basicmatch.test(q)) {
        gsc_emptyresults(elt);
        return '';
    }

    if (elt.currentQuery && (elt.currentQuery == q || elt.tempQuery == q))
        return '';

    elt.currentQuery = q;
    return q;
}

function gsc_hide(elt)
{
    elt.numResults = 0;
	elt.mouseOver = false;
    if (elt) elt.style.display = 'none';
}

function gsc_ishidden(elt)
{
	return elt.style.display == 'none';
}

function gsc_show(elt, n)
{
    if (elt) elt.style.display = 'block';
	if (n > 10) {
		if (elt) elt.style.height = 192;
	} else {
		if (elt) elt.style.height = "auto";
	}
}

function gsc_checkpos(elt, qElt) {
	if (!elt.style.position) {
		elt.style.position = 'absolute';
		var qEltPos = new positionInfo(qElt);
		var x = qEltPos.getElementLeft();
		var y = qEltPos.getElementBottom();
		var w = qEltPos.getElementWidth();
		elt.style.left = x;
		elt.style.top = y;
		elt.style.width = w;
	}
}

function gsc_emptyresults(elt)
{
    if (!elt) return;

    elt.innerHTML = '';
    elt.numResults = 0;
    elt.selectedIndex = -1;
    elt.results = [];
    gsc_hide(elt);
}

function gsc_addresult(elt, qElt, q, c, sel)
{
    if (!elt) return;

    if (sel) elt.selectedIndex = elt.numResults;

    idx = elt.numResults;
    elt.results[elt.numResults++] = q;

    var _res = '';
    _res += '<div class="' + (sel ? 'srs' : 'sr') + '"'
         +  ' onmouseover="gsc_mouseover(\'' + elt.id + '\', \'' + qElt.id + '\', ' + idx + ')"'
         +  ' onmouseout="gsc_mouseout(\'' + elt.id + '\', ' + idx + ')"'
         +  ' onclick="gsc_mouseclick(\'' + elt.id + '\', \'' + qElt.id + '\', ' + idx + ')">';
    _res += '<span class="srt">' + q + '</span>';
    //if (c.length > 0)
        //_res += '<span class="src">' + c + '</span>';
    _res += '</div>';

    elt.innerHTML += _res;
}

function gsc_mouseover(id, qId, idx)
{
    elt = document.getElementById(id);
    elt.selectedIndex = idx;
	elt.mouseOver = true;
    qElt = document.getElementById(qId);
    qElt.focus();

    gsc_highlightsel(elt);
}

function gsc_mouseout(id, idx)
{
    elt = document.getElementById(id);
    elt.selectedIndex = -1;
	elt.mouseOver = false;

    gsc_highlightsel(elt);
}

function gsc_mouseclick(id, qId, idx)
{
	elt = document.getElementById(id);
    qElt = document.getElementById(qId);

    qElt.value = elt.results[idx];
    //qElt.form.submit();
	gsc_hide(elt);
}

function gsc_enterkey(elt, qElt)
{
    qElt.value = elt.results[elt.selectedIndex];
	gsc_hide(elt);
}

function gsc_handleup(elt, qElt)
{
    if (elt.numResults > 0 && gsc_ishidden(elt)) {
        gsc_show(elt);
        return;
    }

    if (elt.selectedIndex == 0)
        return;
    else if (elt.selectedIndex < 0)
        elt.selectedIndex = elt.numResults - 1;
    else
        elt.selectedIndex--;
    gsc_highlightsel(elt, qElt);
}

function gsc_handledown(elt, qElt)
{
    if (elt.numResults > 0 && gsc_ishidden(elt)) {
        gsc_show(elt);
        return;
    }

    if (elt.selectedIndex == elt.numResults - 1)
        return;
    else if (elt.selectedIndex < 0)
        elt.selectedIndex = 0;
    else
        elt.selectedIndex++;
    gsc_highlightsel(elt, qElt);
}

function gsc_highlightsel(elt, qElt)
{
    divs = elt.getElementsByTagName('div');

    for (i = 0; i < divs.length; i++) {
        if (i == elt.selectedIndex) {
            divs[i].className = 'srs';
            elt.tempQuery = elt.results[i];

            if (qElt) {
                qElt.value = elt.results[i];
                if (qElt.createTextRange) {
                    r = qElt.createTextRange();
                    r.moveStart('character', elt.currentQuery.length);
                    r.moveEnd('character', qElt.value.length);
                    r.select();
                }
            }
        }
        else
            divs[i].className = 'sr';
    }
}