// input.js
// Assorted JS functions to drive the AJAX form on walmart/input.php
// Copyright (c) 2008 Nukote International.  All rights reserved.
// RH 08-12-2008


var jsIncludesList = "";

var xmlhttp = null;
if ( document.getElementById ) 
{
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    var ARR_ACTIVEX = ["Msxml2.XMLHTTP",
                       "Microsoft.XMLHTTP"];

    var bFound = false;
    for ( var i = 0; i < ARR_ACTIVEX.length && ! bFound; i++) 
    {
        eval( 'try { var xmlhttp = new ActiveXObject( ARR_ACTIVEX[ i ] ); bFound = true; } catch ( e ) { }' );
    }
    @end @*/
    if ( xmlhttp == null && ! document.all) 
    {
        eval( 'try { var xmlhttp = new XMLHttpRequest(); } catch( e ) { }' );
    }
}


function trim( str ) 
{
    str = str.replace ( /^\s+/g, "" );
    str = str.replace ( /\s+$/g, "" );
    return str;
}


function populateDropDownsFromResponse( responseText ) 
{
    var frm = document.nuksearchm3;
    var response = trim( responseText );
    var response_array = response.split( "\n" );
    for ( var loop = 0; loop < response_array.length; loop++ ) 
    {
        eval( 'var ' + response_array[ loop ] );
    }

    var optionName;
    var currentIndex;

    frm.type.options.length = 0;
    if ( hType != '' ) 
    {
        var hType_array = hType.split( "|" );
        if ( hType_array.length > 2 ) 
        {
            optionName = new Option( "Select a Type", "", true, true );
            currentIndex = frm.type.options.length;
            frm.type.options[ currentIndex ] = optionName;
            for ( var loop = 0; loop < hType_array.length; loop += 2 ) 
            {
                optionName = new Option( hType_array[ loop + 1 ], hType_array[ loop ], false, false );
                currentIndex = frm.type.options.length;
                frm.type.options[ currentIndex ] = optionName;
            }
        } else {
            optionName = new Option( "Select a Different Type", "", false, false );
            currentIndex = frm.type.options.length;
            frm.type.options[ currentIndex ] = optionName;
            optionName = new Option( hType_array[ 1 ], hType_array[ 0 ], true, true );
            currentIndex = frm.type.options.length;
            frm.type.options[ currentIndex ] = optionName;
        }
    }

    frm.manufacture.options.length = 0;
    if ( hManufacture != '' ) 
    {
        var hManufacture_array = hManufacture.split( "|" );
        if ( hManufacture_array.length > 2) 
        {
            optionName = new Option( "Select a Manufacturer", "", true, true );
            currentIndex = frm.manufacture.options.length;    
            frm.manufacture.options[ currentIndex ] = optionName;
            for ( var loop = 0; loop < hManufacture_array.length; loop += 2 ) 
            {
                optionName = new Option( hManufacture_array[ loop + 1 ], hManufacture_array[ loop ], false, false );
                currentIndex = frm.manufacture.options.length;
                frm.manufacture.options[ currentIndex ] = optionName;
            }
        } else {
            optionName = new Option( "Select a Different Manufacturer", "", false, false );
            currentIndex = frm.manufacture.options.length;
            frm.manufacture.options[currentIndex] = optionName;
            optionName = new Option(hManufacture_array[1], hManufacture_array[0], true, true);
            currentIndex = frm.manufacture.options.length;
            frm.manufacture.options[currentIndex] = optionName;
        }
    } else {
        optionName = new Option("Please select a Type first!", "", true, true);
        currentIndex = frm.manufacture.options.length;
        frm.manufacture.options[currentIndex] = optionName;
    }

    frm.model.options.length = 0;
    if ( hModel != '' ) 
    {
        var hModel_array = hModel.split( "|" );
        if ( hModel_array.length > 2 ) 
        {
            optionName = new Option( "Select a Model", "", true, true );
            currentIndex = frm.model.options.length;  
            frm.model.options[ currentIndex ] = optionName;
            for ( var loop=0; loop < hModel_array.length; loop += 2 ) 
            {
                optionName = new Option( hModel_array[ loop + 1 ], hModel_array[ loop ], false, false );
                currentIndex = frm.model.options.length;
                frm.model.options[ currentIndex ] = optionName;
            }
        } else {
            optionName = new Option( "Select a Different Model", "", false, false );
            currentIndex = frm.model.options.length;
            frm.model.options[ currentIndex ] = optionName;
            optionName = new Option( hModel_array[ 1 ], hModel_array[ 0 ], true, true );
            currentIndex = frm.model.options.length;
            frm.model.options[ currentIndex ] = optionName;
            frm.submit();
        }
    } else {
        if ( hManufacture != '' ) 
        {
            optionName = new Option( "Please select a Manufacturer first!", "", true, true );
            currentIndex = frm.model.options.length;
            frm.model.options[ currentIndex ] = optionName;
        } else {
            optionName = new Option( "Please select a Type first!", "", true, true );
            currentIndex = frm.model.options.length;
            frm.model.options[ currentIndex ] = optionName;
        }
    }
}


function xmlhttpSubmitDropDownForm( what ) 
{
    var frm = what.form;

    if ( typeof xmlhttp == 'object' && what.name != 'model' ) 
    {
        var url  = parent_dir + "/search.php";
        var body = '';
        if ( frm.elements[ 'type' ].selectedIndex >= 0 ) 
        {
            body += "type=" + frm.elements[ 'type' ].options[ frm.elements[ 'type' ].selectedIndex ].value;
        }
        if ( frm.elements[ 'manufacture' ].selectedIndex >= 0) 
        {
            if ( body != '' ) 
            {
                body += "&";
            }
            body += "manufacture=" + frm.elements[ 'manufacture' ].options[ frm.elements[ 'manufacture' ].selectedIndex ].value;
        }
        if ( frm.elements[ 'model' ].selectedIndex >= 0 ) 
        {
            if ( body != '' ) 
            {
                body += "&";
            }
            body += "model=" + frm.elements[ 'model' ].options[ frm.elements[ 'model' ].selectedIndex ].value;
        }
        if ( body != '' ) 
        {
            body += "&";
        }
        body += "form=off";

        xmlhttp.open( "POST", url, true );
        xmlhttp.onreadystatechange = function() 
        {
            if ( xmlhttp.readyState == 4 ) 
            {
                populateDropDownsFromResponse( xmlhttp.responseText );
            }
        }

        xmlhttp.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
        xmlhttp.send( body );
    } else {
        if ( what.options[ what.selectedIndex ].value != '' ) 
        {
            frm.submit();
        }
    }

    return false;
}


function submitCurrentForm( thisForm ) 
{
    if ( thisForm.currentform.value != '' ) 
    {
        document.forms[ thisForm.currentform.value ].submit();
    }
}


function handleOtherFormElements( thisElement ) 
{
    if ( thisElement.value != '' ) 
    {
        document.nuksearchgeneric.currentform.value = thisElement.form.name;
    } else {
        document.nuksearchgeneric.currentform.value = '';
    }

    for ( var i = 0; i < document.forms.length; i++ ) 
    {
        if ( document.forms[ i ].name.indexOf( 'nuksearch' ) > -1) 
        {
            for ( var j = 0; j < document.forms[ i ].elements.length; j++ ) 
            {
                document.forms[ i ].elements[ j ].disabled = false;
                if ( document.forms[ i ].name != thisElement.form.name ) 
                {
                    if ( thisElement.value != '') 
                    {
                        document.forms[ i ].elements[ j ].disabled = true;
                    }
                }
            }
        }
    }
}

