var nCurrentBeer = null;

/*************************************************************
* if it's IE change the display type for div stuff
*************************************************************/
var isIE = (!window.XMLHttpRequest) ? true : false;
var sBrowserInline   = (isIE) ? 'block' : 'inline';
var sBrowserTableCell = (isIE) ? 'block' : 'table-cell';
var sUpArrow   = (isIE) ? '&uarr;' : '&uArr;'
var sDownArrow = (isIE) ? '&darr;' : '&dArr;'

/**
 * Trim leading and trailing spaces.
 * @param      string      
 * @return     string
 */
function trimString(sString)
{
    var sTrimmed = sString.replace(/^\s+|\s+$/g, '');
    return sTrimmed;
}

/**
 * Format number by removing all but 0-9
 * @param      string      
 * @return     int
 */
function formatNumber(sString)
{
    var sTrimmed = sString.replace(/[^0-9]/g, '');
    return sTrimmed;
}

/**
 * Format phonenumber
 * @param      string      
 * @return     int
 */
function formatPhoneNumber(sString)
{
    var sNumber = sString.substr(0,3) + '-' + sString.substr(3,3) + '-' + sString.substr(6,4);
    return sNumber;
}

/**
 * Standard error checking on XML response
 */
function checkXML(response)
{
    bReturn = true;
    if (response.nodeName != 'beerotopia')
    {
        alert('Error fetching data');
        bReturn = false;
    }
    return bReturn;
}

/**
 * Add event listener
 * @param      object     target of listener, usually 'document'
 * @param      type       'mousemove', 'mouseover' etc...
 * @param      func       function to call when listener is triggered
 * @param      boolean    should events bubble up ? (don't think this works properly)
 */
function addEventListener (target, type, func, bubbles)
{
    // mozilla
    if (document.addEventListener)
    {
        target.addEventListener(type, func, bubbles);
    }
    // IE
    else if (document.attachEvent)
    {
        target.attachEvent('on' + type, func, bubbles);
    }
    // others
    else
    {
        target['on' + type] = func;
    }
}

/**
 * Remove event listener
 * @param      object     target of listener, usually 'document'
 * @param      type       'mousemove', 'mouseover' etc...
 * @param      func       function to call when listener is triggered
 * @param      boolean    should events bubble up ? (don't think this works properly)     
 */
function removeEventListener (target, type, func, bubbles)
{
    // mozilla
    if (document.removeEventListener)
    {
        target.removeEventListener(type, func, bubbles);
    }
    // IE
    else if (document.detachEvent)
    {
        target.detachEvent('on' + type, func, bubbles);
    }
    // others
    else
    {
        target['on' + type] = null;
    }
}

function load_beer (nId)
{
    if (!nId) 
    {
        var sInput = document.getElementById('search').value;
        if (!sInput) return;
        document.location.href = 'report.php?action=search&search=' + sInput;
        return;
    }
    var req = ajaxQuery('api/get_beer.php?id=' + nId, function (n, response) { processAjaxReturn(n, response) }, 'GET');
}

function load_reviews (nId)
{
    var req = ajaxQuery('api/get_reviews.php?id=' + nId, function (n, response) { processAjaxReturn(n, response) }, 'GET');
}

function get_random_beer ()
{
    var req = ajaxQuery('api/get_beer.php', function (n, response) { processAjaxRandom(n, response) }, 'GET');
}

function get_last_beer (nId)
{
    var req = ajaxQuery('api/get_last_beer.php?id=' + nId, function (n, response) { processAjaxReturn(n, response) }, 'GET');
}   

function processAjaxRandom (n, response)
{
    if (!checkXML)
    {
        alert('XML response error');
        return;
    }
    var nBeerId   = response.getElementsByTagName('beer_id')[0].firstChild.data;
    var ajaxXHTML = response.getElementsByTagName('ajax_xhtml')[0];
    var nId       = ajaxXHTML.getAttribute('id');
    var sXHTML    = ajaxXHTML.firstChild.data;
    document.getElementById(nId).innerHTML = sXHTML;    
    load_reviews(nBeerId);
}

function processAjaxReturn (n, response)
{
    if (!checkXML)
    {
        alert('XML response error');
        return;
    }
    
    var ajaxXHTML = response.getElementsByTagName('ajax_xhtml')[0];
    var nId       = ajaxXHTML.getAttribute('id');
    var sXHTML    = ajaxXHTML.firstChild.data;
    
    document.getElementById(nId).innerHTML = sXHTML;
}

function processAjaxReview ()
{
    if (!checkXML)
    {
        alert('XML response error');
        return;
    }
    var beerId = document.getElementById('last').value;
    load_reviews(beerId);
}

function processAjaxScore()
{
    if (!checkXML)
    {
        alert('XML response error');
        return;
    }
    var nBeerId   = response.getElementsByTagName('beer_id')[0].firstChild.data;   
    get_last_beer(nBeerId);
    get_random_beer();   
}

function write_debug (sDebug)
{
    document.getElementById('debug').innerHTML += sDebug;
}

function escape_html (sHTML)
{
    sHTML = sHTML.replace(/</g, '&lt;');
    sHTML = sHTML.replace(/>/g, '&gt;');
    sHTML = sHTML.replace(/&/g, '&amp;');
    return sHTML;
}