

if(typeof(XMLHttpRequest)!='undefined')
{
    var getXMLHttpObj = function(){ return new XMLHttpRequest(); }
}
else 
{
    var getXMLHttpObj = function()
    {
        var activeXObjects = ['Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0',
        'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP'];

        for(var i=0; i<activeXObjects.length; i++){
            try{
                return new ActiveXObject(activeXObjects[i]);
            }catch(err){}
        }
    }
}


var oXml = getXMLHttpObj();
var one_listing;
var all_parts_listing = new Array();
var parts_listing_str;


if( oXml )
{
        oXml.open('GET', '/get_parts_selection.php', true);

        oXml.onreadystatechange = function()
        {
                if( oXml.readyState == 4 )
                {
                        if( oXml.status == 200 ) 
                        {				
                                //var all_parts_listing = oXml.responseText;
                                //all_parts_listing = oXml.responseText;
                                parts_listing_str = oXml.responseText;
                                //parts_listing_str.replace(/'/g," ");
                                all_parts_listing = parts_listing_str.split("|");

/*		

                                for( one_listing in all_parts_listing )
                                {       
                                        alert( one_listing );
                                }
*/
                                //alert( "All parts listing: " + oXml.responseText );
                        }
                        else
                        {
                                alert( "Sorry, we are experiencing technical difficulties.\n" +
                                        "Please call us at 1-800-807-2967\n"
                                        + "Request Status:  " + oXml.statusText );
                        }
                }

                // we process the results here.  More on that later!
        }

        oXml.send(null);
        
        //alert( "Sent AJAX parts listing request!" );
}
else
{
        alert( "Sorry, we are experiencing technical difficulties.\n" +
                        "Please call us at 1-800-807-2967\n" );
}

function add_parts_listing( num_parts, insert_row_pos, default_num_rows )
{
    var tbl = document.getElementById('search_table');

    var lastRow = tbl.rows.length;
    var iteration = lastRow;

    //alert( "table has " + lastRow + " Rows, and you want " + (num_parts - 1) + " more inputs." );

    //
    // the part_id is used by the php code to insert the 'name' and 'id'
    // values for each of the parts listing drop down select boxes.
    // 
    var part_id = 2;

    //
    // the insert_row_pos is the starting row to insert a new row after.
    // this is hard coded, just determined the value from the existing 
    // offshore team code - would be nice to make this dynamic, 
    // but for now, I know it is 11.
    //
    // the position of the first ADDITIONAL "Select A Part" list.  
    //

    //
    // this is the number of rows in the current table - 
    // with NO additions.
    // so if they add a row and revert, we need to just 
    // delete the new rows...
    //

    while( iteration >= default_num_rows )
    {
        //alert( "deleting row " + (iteration-3) );
        tbl.deleteRow( iteration-3 );
        iteration--;
    }

    if( 1 < num_parts )
    { 

        var x;
        var num_new_rows = 0;

        for( num_new_rows = 0; num_new_rows < (num_parts-1); num_new_rows++ )
        {
            //
            // this is the index for the <option> tag.
            // each option for each part and part id will be added
            // via the new Option(..) construct,
            // and the options will be added to the select options array, 
            // just like so:  sel.options[option_index] = new Option();
            //
            var option_index = 0;

            var row = tbl.insertRow( insert_row_pos );
            var cellRightSel = row.insertCell(0);
            cellRightSel.colSpan = "3";
            cellRightSel.height = "23px";
            cellRightSel.align = "center";


            var sel = document.createElement('select');

            sel.name = 'parts' +  part_id;
            sel.id = 'parts' +  part_id;
            sel.classname = 'select_box';
            //sel.class = 'select_box';

            sel.options[option_index] = new Option('-Select Another Part (Optional)-', 'INVALID');
            option_index++;

            //alert( "Array Length: " + all_parts_listing.length );

            for( x = 0; x < all_parts_listing.length; )
            {

                sel.options[option_index] = new Option( all_parts_listing[x], all_parts_listing[x+1] );
                x += 2;
                option_index++;
            }

            cellRightSel.appendChild(sel);

            part_id++;
            insert_row_pos++;
        }
    }

    {
        var row = tbl.insertRow( insert_row_pos );
        var cellRightSel = row.insertCell(0);
        cellRightSel.colSpan = "3";
        cellRightSel.height = "23px";
        cellRightSel.align = "center";
        var txt = document.createTextNode("\u00a0"); // this is a non breaking space
        cellRightSel.appendChild(txt);
    }
}



function keyPressTest(e, obj)
{
  var validateChkb = document.getElementById('chkValidateOnKeyPress');
  if (validateChkb.checked) {
    var displayObj = document.getElementById('spanOutput');
    var key;
    if(window.event) {
      key = window.event.keyCode; 
    }
    else if(e.which) {
      key = e.which;
    }
    var objId;
    if (obj != null) {
      objId = obj.id;
    } else {
      objId = this.id;
    }
    displayObj.innerHTML = objId + ' : ' + String.fromCharCode(key);
  }
}



function removeRowFromTable()
{
  var tbl = document.getElementById('tblSample');
  var lastRow = tbl.rows.length;
  if (lastRow > 2) tbl.deleteRow(lastRow - 1);
}

