﻿
/* SEARCH URL CLASS */

var SearchUrl = new Object;
SearchUrl.url = 'busqueda.aspx';

SearchUrl.addQuery = function(key, value)
{
    if (SearchUrl.url.indexOf('?') >= 0)
        SearchUrl.url += '&';
    else
        SearchUrl.url += '?';
        
    SearchUrl.url += key + '=' + value;
}

SearchUrl.toString = function()
{
    return SearchUrl.url;
}


/* SEARCH BOX CLASS */

var SearchBox = new Object;
SearchBox.WaterMarks = new Object();

SearchBox.setWatermarkText = function(textBoxId, watermarkText) {

    SearchBox.WaterMarks[textBoxId] = watermarkText;
//    alert(SearchBox.WaterMarks[textBoxId]);
}

SearchBox.blur = function(textBoxId)
{
    if (!textBoxId)
        textBoxId = 'q';
    var t = $get(textBoxId);
    var inputDefText = SearchBox.WaterMarks[textBoxId];
//    var inputDefText = t.defText;
//    if(!t.defText && t.getAttribute('defText'))
//        inputDefText = t.getAttribute('defText');
        
    if(t.value == '' || t.value == inputDefText)
    {
        t.style.color = '#aaa';
        t.value = inputDefText;
    }
}

SearchBox.focus = function(textBoxId)
{
    if (!textBoxId)
        textBoxId = 'q';
    var t = $get(textBoxId);
    var inputDefText = SearchBox.WaterMarks[textBoxId];
//    var inputDefText = t.defText;
//    if(!t.defText && t.getAttribute('defText'))
//        inputDefText = t.getAttribute('defText');
    
    if(t.value == inputDefText)
        t.value = '';
        
    t.style.color = 'black';
}

SearchBox.value = function(textBoxId)
{
    if (!textBoxId)
        textBoxId = 'q';
    var t = $get(textBoxId);
    return t.value;
}

SearchBox.pressEnterToGo = function(event, buttonId)
{
    var keyCode = null;

    if (event.which)
        keyCode = event.which;
    else if (event.keyCode)
        keyCode = event.keyCode;

    if (keyCode == 13)
    {
        $get(buttonId).click();
        return false;
    }
    
    return true;
}

SearchBox.go = function(textBoxId, comarcaId, output, requestUrl)
{
    if (!textBoxId)
        textBoxId = 'q';
    if (!comarcaId)
        comarcaId = 0;
    if (requestUrl)
        SearchUrl.url = requestUrl;
    else
        SearchUrl.url = 'busqueda.aspx';
        
    SearchBox.focus(textBoxId);
    
    var queryEncode = encodeURIComponent(SearchBox.value(textBoxId));

    if (queryEncode.length > 0)
    SearchUrl.addQuery('q', queryEncode);
	
	if (comarcaId > 0)
    SearchUrl.addQuery('comarca', comarcaId);
    
    if (output)
        SearchUrl.addQuery('output', output);
    
    document.location.href = SearchUrl;
    
    SearchBox.blur(textBoxId);
}


/* SEARCH LIST CLASS */

var SearchList = new Object;

SearchList.blur = function(listBoxId)
{
    var t = $get(listBoxId);
    if(t.value == '0')
    {
        t.style.color = '#aaa';
    }
}

SearchList.focus = function(listBoxId)
{
    var t = $get(listBoxId);
    t.style.color = 'black';
}

SearchList.value = function(listBoxId)
{
    var t = $get(listBoxId);
    return t.value;
}


/* SEARCH RADIO CLASS */

var SearchRadio = new Object;

SearchRadio.value = function(radioNameGroup)
{
    var value = null;
    var items = document.getElementsByName(radioNameGroup);

    for (var i=0; i<items.length; i++)
        if (items[i].checked)
            value = items[i].value;

    return value;
}


/* UTIL */
function $get(itemId) 
{
    return document.getElementById(itemId);
}
