var CpcSupportScriptUrl = '_lib/js/contact-cpc.js.php';
var CpcSupportFormId = 'CpcForm' ;
var CpcSupportModelId = 'Model' ;
var CpcSupportMakeId = 'Make' ;

function UpdateModels( items, index )
{
    var index = ( index ) ? index : 0 ;
    var menu = document.getElementById( CpcSupportModelId ) ;  // Create reference to the sub menu
    menu.options.length = 0;
    menu.disabled = ( 2 >= items.length ) ? true : false ;

    // Fill the menu with the array elements
    for (i=0; i<items.length; i++)
    {
        // Skip 'null' placed in array when it was generated by PHP
        if ( null == items[i] ) { break; }

        // Assign array element to the next top menu item
        menu.options[i] = new Option(items[i].label, items[i].value);
    }

    menu.selectedIndex = index;
    document.getElementById( 'ModelIndex' ).value = index;
    return true;
}

function Run( task, param )
{
    switch ( task )
    {
        case 'UpdateModels' :

            modelMenu = document.getElementById( CpcSupportModelId );
            if ( param )
            {
                modelIndex = param;
            }
            else
            {
                //if ( 0 > modelMenu.selectedIndex )
                //{
                    modelIndex = 0;
                //}
                //else
                //{
                //    modelIndex = modelMenu.selectedIndex;
                //}
            }
            makeMenu = document.getElementById( CpcSupportMakeId );
            makeValue = (0 >= makeMenu.selectedIndex) ? '' : makeMenu.options[ makeMenu.selectedIndex ].value;
            params = '&makename=' + makeValue + '&modelindex=' + modelIndex;

        break;
    }


    // Define and execute the script

    script = document.createElement( 'script' );
    script.src = CpcSupportScriptUrl + '?run=' + task + params ;
    document.getElementsByTagName( 'head' )[0].appendChild( script );

    return true;

}

function Init()
{
    Run( 'UpdateModels', document.getElementById( 'ModelIndex' ).value );
}