var http_request = null;

function launchSearch()
{
    var oForm = document.getElementById( 'dealersiteform' );
    if ( !oForm )
    {
        alert( 'Unable to find form object' );
        return;
    }

    pageOverlay( true );
    oForm.action.value = 'refinesearch';
    var poststr = packFormData( oForm );

    try
    {
        http_request = ajaxRequestCreate();
        if ( http_request.overrideMimeType )
        {
            http_request.overrideMimeType( 'text/html' )
        }

        var url = 'main.php?search';
        http_request.onreadystatechange = searchResponse;
        http_request.open( 'POST', url, true );
        http_request.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
        http_request.setRequestHeader( "Content-length", poststr.length );
        http_request.setRequestHeader( "Connection", "close" );
        http_request.send( poststr );
    }
    catch ( e )
    {
         http_request = null;
         pageOverlay( false );
         var msg = "XmlHttp error occured.<br>Error name: " + e.name + "<br>Error message: " + e.message;
         alert( msg );
    }

    return;
}

function productFinder( page, page_size )
{
    var oForm = document.getElementById( 'dealersiteform' );
    if ( !oForm )
    {
        alert( 'Unable to find form object' );
        return;
    }

    pageOverlay( true );
    oForm.action.value = 'searchresults';

    var poststr = packFormData( oForm );

    try
    {
        http_request = ajaxRequestCreate();
        if ( http_request.overrideMimeType )
        {
            http_request.overrideMimeType( 'text/html' )
        }

        var url = 'request.php';
        var qs = '';
        if ( page )
        {
            qs += 'gpage=' + page;
        }

        if ( page_size )
        {
            if ( qs != '' )
            {
                qs += '&';
            }

            qs += 'gpagesz=' + page_size;
        }

        if ( qs != '' )
        {
            url += '?' + qs;
        }

        http_request.onreadystatechange = searchResponse;
        http_request.open( 'POST', url, true );
        http_request.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
        http_request.setRequestHeader( "Content-length", poststr.length );
        http_request.setRequestHeader( "Connection", "close" );
        http_request.send( poststr );
    }
    catch ( e )
    {
         http_request = null;
         pageOverlay( false );
         var msg = "XmlHttp error occured.<br>Error name: " + e.name + "<br>Error message: " + e.message;
         alert( msg );
    }

    return;
}

function searchResponse()
{
    var container = document.getElementById( 'search_results' );

    if ( http_request.readyState == 4 )
    {
        if ( http_request.status == 200 )
        {
            var oForm = document.getElementById( 'dealersiteform' );
            if ( !oForm )
            {
                alert( 'Unable to find form object' );
                return;
            }

            oForm.productcomparison.value = '';

            if ( !container )
            {
                alert( 'Could not access search_results container' );
            }
            else
            {
                container.innerHTML = http_request.responseText;
            }
        }
        else
        {
            var msg = "XmlHttp error (return status " +
                                        http_request.status + ")";
            if ( container )
            {
                container.innerHTML = msg;
            }
            else
            {
                alert( msg );
            }
        }
    }

    pageOverlay( false );
    return;
}

function toggleContainer( h3obj, divId )
{
    var o = document.getElementById( divId );
    if ( !o )
    {
        alert( 'Unable to access element' );
        return;
    }

    if ( o.className == 'hidecontrol' )
    {
        h3obj.className = 'h3-opened';
        o.className = 'showcontrol';
    }
    else
    {
        h3obj.className = 'h3-closed';
        o.className = 'hidecontrol';
    }

    return;
}

function setPageSize( selectObj )
{
    var index = selectObj.selectedIndex;
    if ( index >= 0 )
    {
        page_size = selectObj.options[ index ].value;

        productFinder( 1, page_size );
    }

    return;
}

function callItemChildrenPage( url, gotoanchor )
{
    if ( gotoanchor )
    {
        window.location.hash = '#childanchor';
    }

    pageOverlay( true );
    try
    {
        http_request = ajaxRequestCreate();
        if ( http_request.overrideMimeType )
        {
            http_request.overrideMimeType( 'text/html' )
        }

        var poststr = 'action=' + escape( encodeURI( 'showpage' ) );

        http_request.onreadystatechange = itemChildrenPageResponse;
        http_request.open( 'POST', url, true );
        http_request.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
        http_request.setRequestHeader( "Content-length", poststr.length );
        http_request.setRequestHeader( "Connection", "close" );
        http_request.send( poststr );
    }
    catch ( e )
    {
         http_request = null;
         pageOverlay( false );
         var msg = "XmlHttp error occured.<br>Error name: " + e.name + "<br>Error message: " + e.message;
         alert( msg );
    }

    return;
}

function itemChildrenPageResponse()
{
    var container = document.getElementById( 'child-view' );

    if ( http_request.readyState == 4 )
    {
        if ( http_request.status == 200 )
        {
            if ( !container )
            {
                alert( 'Could not access search_results container' );
            }
            else
            {
                container.innerHTML = http_request.responseText;
            }
        }
        else
        {
            var msg = "XmlHttp error (return status " +
                                        http_request.status + ")";
            if ( container )
            {
                container.innerHTML = msg;
            }
            else
            {
                alert( msg );
            }
        }
    }

    pageOverlay( false );
    return;
}

function collectProduct( itemid )
{
    var oForm = document.getElementById( 'dealersiteform' );
    if ( !oForm )
    {
        alert( 'Unable to find form object' );
        return;
    }

    if ( !oForm.productcomparison )
    {
        alert( 'Unable to find form input object' );
        return;
    }

    var oCheckBox = document.getElementById( 'cmp_item_' + itemid );
    if ( !oCheckBox )
    {
        alert( 'Invalid checkbox object' );
        return;
    }

    if ( oCheckBox.checked )
    {
        if ( !oForm.productcomparison.value )
        {
            oForm.productcomparison.value = itemid;
        }
        else
        {
            var arr = oForm.productcomparison.value.split( ',' );

            if ( arr.length == 3 )
            {
                alert( 'You can not select more than three items to compare' );
                oCheckBox.checked = false;
            }
            else
            {
                oForm.productcomparison.value += ',' + itemid;
            }
        }
    }
    else
    {
        if ( oForm.productcomparison.value )
        {
            var newvalue = '';
            var arr = oForm.productcomparison.value.split( ',' );
            for ( i = 0; i < arr.length; i++ )
            {
                checkitem = parseInt( arr[ i ] );
                if ( checkitem != itemid )
                {
                    if ( !newvalue )
                    {
                        newvalue += checkitem;
                    }
                    else
                    {
                        newvalue += ',' + checkitem;
                    }
                }
            }

            oForm.productcomparison.value = newvalue;
        }
    }

    return;
}

function productsCompare( page, page_size, message )
{
    var oForm = document.getElementById( 'dealersiteform' );

    if ( !oForm )
    {
        alert( 'Unable to find form object' );
        return;
    }

    if ( !oForm.productcomparison.value )
    {
        alert( message );
        return;
    }

    var arr = oForm.productcomparison.value.split( ',' );
    if ( arr.length < 2 )
    {
        alert( message );
        return;
    }

    document.location.href = 'main.php?itemid=16042&compare=' + oForm.productcomparison.value;
    return;
}

function pfTextSearch( e )
{
    var key = 0;
    if ( !e )
    {
        e = window.event;
    }

    if ( window.event )
    {
        key = e.keyCode;
    }
    else
    {
        key = e.which;
    }

    if ( key == 13 )
    {
        productFinder( 0, 0 );
        return false;
    }

    return true;
}

