// Table Delete Row with Calendar
// mredkj.com
// based off tabledeleterow.js version 1.2 2006-02-21
// tabledeleterow-calendar.js version 1.2.1 2006-05-04
// tabledeleterow-calendar.js version 1.2.2 2006-10-10

// CONFIG notes. Below are some comments that point to where this script can be customized.
// Note: Make sure to include a <tbody></tbody> in your table's HTML
// Note: Cannot use this with any calendar script that references object ids
//         The way the rows are added and deleted, ids aren't reset.

var INPUT_NAME_PREFIX = 'inputName'; // this is being set via script
var TABLE_NAME = 'products_table'; // this should be named in the HTML
var ROW_BASE = 1; // first number (for display)
var hasLoaded = false;

window.onload=fillInRows;

function debug(msg)
{
    var debug = document.getElementById('txtdebug')
    var d = new Date();
    var tempTimestamp =
        (d.getHours() < 10 ? '0' + d.getHours() : d.getHours())
        + ':' +
        (d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes())
        + ':' +
        (d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds())
    debug.value = tempTimestamp + ':' + msg + '\n' + debug.value ;
}

function fillInRows()
{
    hasLoaded = true;
//    add_row_products();
//    add_row_documents();    
}

function chknumeric(obj) 
{     
    var val = "";
    var vals = obj.value; 
    var numChar = "0123456789";
    for (var i=0;i<vals.length;i++)          
    {
        if (numChar.indexOf(vals.charAt(i)) > -1) 
        {
            val += vals.charAt(i);
        }     
    }
    document.getElementById(obj.id).value = val;
}

// CONFIG:
// myRowObject is an object for storing information about the table rows
function myRowObject(one, two)
{
    this.one = one; // text object
    this.two = two; // input text object
}

function selectedItemChange(val, key)
{
    var product_part = document.getElementById('_products_part').value;
    var product_partList = product_part.split(";");
    for(i = 0; i < product_partList.length; i++)
    {
        var individualProducts = product_partList[i].split(":");
        if(individualProducts[0] == val)
        {
            document.getElementById('div'+key).innerHTML = individualProducts[1];
            return;
        }
    }
}

function setProductPart(val)
{
    var product_part = document.getElementById('_products_part').value;
    var product_partList = product_part.split(";");
    for(i = 0; i < product_partList.length; i++)
    {
        var individualProducts = product_partList[i].split(":");
        if(individualProducts[0] == val)
        {
            return individualProducts[1];
        }
    }
}

function showInputText(my, key)
{
    document.getElementById('divDisplay'+key).style.display = (my.value == "25" ? 'none' : 'block' );
    document.getElementById('RebateFormDescription_c'+key).style.display = (my.value == "25" ? 'block' : 'none' );
}

function add_row_manage_level(num)
{
    if (hasLoaded) {
        var tbl = document.getElementById('level_table');
        var nextRow = tbl.tBodies[0].rows.length;
        var iteration = nextRow + ROW_BASE;
        if (num == null) { 
            num = nextRow;
        } else {
            iteration = num + ROW_BASE;
        }
        
        // add the row
        var row = tbl.tBodies[0].insertRow(num);
        row.className = 'odd';
        
        var cell0 = row.insertCell(0);
        var txtLevelId = document.createElement('input');
        txtLevelId.type = 'hidden';
        txtLevelId.name = 'LevelID_n' + iteration;
        txtLevelId.value = '';
        cell0.appendChild(txtLevelId);
        
        var txtOrder = document.createElement('input');
        txtOrder.type = 'hidden';
        txtOrder.name = 'Order_n' + iteration;
        txtOrder.value = iteration - 1;
        cell0.appendChild(txtOrder);
        
        var txtLevelName = document.createElement('input');
        txtLevelName.type = 'text';
        txtLevelName.name = 'Level_c' + iteration;
        txtLevelName.size = 30;
        txtLevelName.value = '';
        cell0.appendChild(txtLevelName);
        
        var cell1 = row.insertCell(1);
        var pointer = document.createElement('a');
        pointer.className = 'pointer';
        var arrow_up = document.createElement('img');
        arrow_up.src = "images/arrow_up.png";
        arrow_up.onclick = function () {setOrder();};
        pointer.appendChild(arrow_up);
        /*cell1.appendChild(pointer);*/
        
        var cell2 = row.insertCell(2);
            
        var cell3 = row.insertCell(3);
        var linkDel = document.createElement('a');
        linkDel.className = 'pointer';                
        linkDel.appendChild(document.createTextNode('Remove'));
        linkDel.onclick = function(){deleteCurrentRow(this); return false;}
        cell3.appendChild(linkDel);
    }
}

/*
 * add_row_products
 * Inserts at row 'num', or appends to the end if no arguments are passed in. Don't pass in empty strings.
 */
function add_row_products(products,rebate_startdate,rebate_enddate,num,use_cell6)
{
    if (hasLoaded) {
        
        var tbl = document.getElementById('products_table');
        var nextRow = tbl.tBodies[0].rows.length;
        var iteration = nextRow + ROW_BASE;
        if (num == null) { 
            num = nextRow;
        } else {
            iteration = num + ROW_BASE;
        }
        
        if (use_cell6 == null) {
            use_cell6 = true;
        }
        
        // add the row
        var row = tbl.tBodies[0].insertRow(num);
        
        // CONFIG: requires classes named classy0 and classy1
        //if (iteration % 2 == 0) {
            row.className = 'odd';
        //}
        
        // CONFIG: This whole section can be configured
        
        /* Create select menu (from products list)
        -------------------------------------------------*/    
        var cell0 = row.insertCell(0);
        myselect = document.createElement("select");
        myselect.name = 'ProductID_n' + iteration;
        var productList = products.split(";");

        //create drop-down list        
        for(i = 0; i < productList.length; i++) {
            var individualProducts = productList[i].split(":");
        
            //create an option
            theOption = document.createElement("option");
            //make some text
            theText  = document.createTextNode(individualProducts[1]);
            theOption.title = document.createTextNode(individualProducts[1]);
            theOption.value = individualProducts[0];
            //add the text to the option
            theOption.appendChild(theText);
            //add the option to the select
            myselect.appendChild(theOption);
            
        }//for
        //end of options
        cell0.appendChild(myselect);

        var textNodeSp1 = document.createTextNode(' ');
        var textNodeSp2 = document.createTextNode(' ');
        
        // cell 1 - input text (rebate amount)
        var cell1 = row.insertCell(1);        
        var textNode = document.createTextNode('$ ');
        cell1.appendChild(textNode);        
        cell1.noWrap = true;        
        var txtAmount = document.createElement('input');
        txtAmount.type = 'text';
        txtAmount.name = 'RebateAmount_n' + iteration;
        // Don't refer to ids
        //txtInp.id = INPUT_NAME_PREFIX + iteration;
        txtAmount.size = 4;
        txtAmount.value = '';
        txtAmount.onkeyup = function(){IsNumeric(this)};
        cell1.appendChild(txtAmount);

        // cell 2 - input text (start date)
        var cell2 = row.insertCell(2);
        cell2.noWrap = true;
        var txtStartDate = document.createElement('input');
        txtStartDate.type = 'text';
        txtStartDate.name = 'StartDate_d' + iteration;
        // Don't refer to ids
        //txtInp.id = INPUT_NAME_PREFIX + iteration;
        txtStartDate.size = 7;
        txtStartDate.value = rebate_startdate;
        txtStartDate.setAttribute('readOnly', 'readonly');
        cell2.appendChild(txtStartDate);
        cell2.appendChild(textNodeSp1);
        

        // cell 2 - calendar button
        var btnCalStart = document.createElement('input');
        btnCalStart.className = 'date_picker';        
        btnCalStart.type = 'button';
        btnCalStart.value = 'calendar';
        btnCalStart.onclick = function () {toggleCalendar(txtStartDate);};
        cell2.appendChild(btnCalStart);    
        
        // cell 3 - input text (end date)
        var cell3 = row.insertCell(3);
        cell3.noWrap = true;        
        var txtEndDate = document.createElement('input');
        txtEndDate.type = 'text';
        txtEndDate.name = 'EndDate_d' + iteration;
        // Don't refer to ids
        //txtInp.id = INPUT_NAME_PREFIX + iteration;
        txtEndDate.size = 7;
        txtEndDate.value = rebate_enddate;
        txtEndDate.setAttribute('readOnly', 'readonly');        
        cell3.appendChild(txtEndDate);
        cell3.appendChild(textNodeSp2);
        
        // cell 3 - calendar button
        var btnCalEnd = document.createElement('input');
        btnCalEnd.className = 'date_picker';        
        btnCalEnd.type = 'button';
        btnCalEnd.value = 'calendar';
        btnCalEnd.onclick = function () {toggleCalendar(txtEndDate);};
        cell3.appendChild(btnCalEnd);    
        
        // cell 5 - delete link
        var cell4 = row.insertCell(4);        
        var linkDel = document.createElement('a');
        linkDel.className = 'pointer';                
        linkDel.appendChild(document.createTextNode('Remove'));
        linkDel.onclick = function(){deleteCurrentRow(this); return false;}
        cell4.appendChild(linkDel);
        
        //only used for certain calls, otherwise will break html
        if(use_cell6){
            // cell 6 - empty
            var cell5 = row.insertCell(5);    
            var textNode = document.createTextNode('');
            cell5.appendChild(textNode);
        }
        
            // Pass in the elements you want to reference later
            // Store the myRow object in each row
            row.myRow = new myRowObject(myselect, textNode, txtAmount, txtStartDate, btnCalStart, txtEndDate, btnCalEnd, linkDel);

    }
}

/*
 * add_row_product_point
 * Inserts at row 'num', or appends to the end if no arguments are passed in. Don't pass in empty strings.
 */
function add_row_product_points(products, num)
{
    if (hasLoaded) {
        var tbl = document.getElementById('products_point_table');
        var nextRow = tbl.tBodies[0].rows.length;
        var iteration = nextRow + ROW_BASE;
        var cell_count = 1;
        var rebate_type = document.getElementById('_products_points_form').value;
        
        if (num == null) { 
            num = nextRow;
        } else {
            iteration = num + ROW_BASE;
        }
        
        // add the row
        var row = tbl.tBodies[0].insertRow(num);
        
        // CONFIG: requires classes named classy0 and classy1
        //if (iteration % 2 == 0) {
            row.className = 'odd';
        //}
        
        // CONFIG: This whole section can be configured
        
        /* Create select menu (from products list)
        -------------------------------------------------*/    
        var cell0 = row.insertCell(0);
        myselect = document.createElement("select");
        myselect.name = 'ProductID_n' + iteration;
        //myselect.className = "formfield_width_100_percent";
        myselect.width = "150px;";
        var productList = products.split(";");

        //create drop-down list        
        for(i = 0; i < productList.length; i++) {
            var individualProducts = productList[i].split(":");
        
            //create an option
            theOption = document.createElement("option");
            //make some text
            theText  = document.createTextNode(individualProducts[1]);
            theOption.value = individualProducts[0];
            //add the text to the option
            theOption.appendChild(theText);
            //add the option to the select
            myselect.appendChild(theOption);
            
        }//for
        //end of options
        cell0.appendChild(myselect);

        // cell 1 - input text (rebate amount)
        var cell1 = row.insertCell(cell_count++);        
        cell1.noWrap = true;        
        var txtPoints = document.createElement('input');
        txtPoints.type = 'text';
        txtPoints.name = 'ResellerRepPoints_n' + iteration;
        txtPoints.className = "formfield_width_100_percent";
        // Don't refer to ids
        //txtInp.id = INPUT_NAME_PREFIX + iteration;
        txtPoints.value = '';
        txtPoints.onkeyup = function(){IsNumeric(this)};
        cell1.appendChild(txtPoints);
        
        if(rebate_type == '14'){
            // cell 1 - input text (rebate amount)
            var cell3 = row.insertCell(cell_count++);        
            cell3.noWrap = true;        
            var txtSalesManagerPoints = document.createElement('input');
            txtSalesManagerPoints.type = 'text';
            txtSalesManagerPoints.name = 'SalesManagerPoints_n' + iteration;
            txtSalesManagerPoints.className = "formfield_width_100_percent";
            // Don't refer to ids
            //txtInp.id = INPUT_NAME_PREFIX + iteration;
            txtSalesManagerPoints.size = 8;
            txtSalesManagerPoints.value = '';
            txtSalesManagerPoints.onkeyup = function(){IsNumeric(this)};
            cell3.appendChild(txtSalesManagerPoints);
        }
        
        // cell 2 - delete link
        var cell2 = row.insertCell(cell_count++);        
        var linkDel = document.createElement('a');
        linkDel.className = 'pointer';                
        linkDel.appendChild(document.createTextNode('Remove'));
        linkDel.onclick = function(){deleteCurrentRow(this); return false;}
        cell2.appendChild(linkDel);
        // Pass in the elements you want to reference later
        // Store the myRow object in each row
        row.myRow = new myRowObject(myselect, txtPoints, linkDel);
    }    
}

/*
 * add_row_product_point
 * Inserts at row 'num', or appends to the end if no arguments are passed in. Don't pass in empty strings.
 */
function add_row_point_level1(levels, num)
{
    if (hasLoaded) {
        var tbl = document.getElementById('point_level_table1');
        var nextRow = tbl.tBodies[0].rows.length;
        var iteration = nextRow + ROW_BASE;
        var cell_count = 1;
        
        
        if (num == null) { 
            num = nextRow;
        } else {
            iteration = num + ROW_BASE;
        }
        
        // add the row
        var row = tbl.tBodies[0].insertRow(num);
        
        // CONFIG: requires classes named classy0 and classy1
        //if (iteration % 2 == 0) {
            row.className = 'odd';
        //}
        
        // CONFIG: This whole section can be configured
        
        /* Create select menu (from products list)
        -------------------------------------------------*/    
        var cell0 = row.insertCell(0);
        myselect = document.createElement("select");
        myselect.name = 'LevelID_n' + iteration;
        var levelList = levels.split(";");

        //create drop-down list        
        for(i = 0; i < levelList.length; i++) {
            var individualLevels = levelList[i].split(":");
        
            //create an option
            theOption = document.createElement("option");
            //make some text
            theText  = document.createTextNode(individualLevels[1]);
            theOption.value = individualLevels[0];
            //add the text to the option
            theOption.appendChild(theText);
            //add the option to the select
            myselect.appendChild(theOption);
            
        }//for
        //end of options
        cell0.appendChild(myselect);
        
        // cell 1 - input text (rebate amount)
        var cell1 = row.insertCell(cell_count++);        
        var textNode = document.createTextNode('$ ');
        cell1.appendChild(textNode);        
        cell1.noWrap = true;        
        var txtAmountFrom = document.createElement('input');
        txtAmountFrom.type = 'text';
        txtAmountFrom.name = 'AmountFrom_n' + iteration;
        // Don't refer to ids
        //txtInp.id = INPUT_NAME_PREFIX + iteration;
        txtAmountFrom.size = 4;
        txtAmountFrom.value = '';
        txtAmountFrom.onkeyup = function(){IsNumeric(this)};
        cell1.appendChild(txtAmountFrom);
        
        
        var textNode = document.createTextNode(' - $ ');
        cell1.appendChild(textNode);        
        cell1.noWrap = true;        
        var txtAmountTo = document.createElement('input');
        txtAmountTo.type = 'text';
        txtAmountTo.name = 'AmountTo_n' + iteration;
        // Don't refer to ids
        //txtInp.id = INPUT_NAME_PREFIX + iteration;
        txtAmountTo.size = 4;
        txtAmountTo.value = '';
        txtAmountTo.onkeyup = function(){IsNumeric(this)};
        cell1.appendChild(txtAmountTo);

        // cell 1 - input text (rebate amount)
        var cell3 = row.insertCell(cell_count++);        
        var textNode = document.createTextNode('% ');
        cell3.appendChild(textNode);        
        cell3.noWrap = true;        
        var txtPercentage = document.createElement('input');
        txtPercentage.type = 'text';
        txtPercentage.name = 'RebatePercentage_n' + iteration;
        // Don't refer to ids
        //txtInp.id = INPUT_NAME_PREFIX + iteration;
        txtPercentage.value = '';
        txtPercentage.onkeyup = function(){IsNumeric(this)};
        
        cell3.appendChild(txtPercentage);
                
        // cell 2 - delete link
        var cell4 = row.insertCell(cell_count++);        
        var linkDel = document.createElement('a');
        linkDel.className = 'pointer';                
        linkDel.appendChild(document.createTextNode('Remove'));
        linkDel.onclick = function(){deleteCurrentRow(this); return false;}
        cell4.appendChild(linkDel);
        // Pass in the elements you want to reference later
        // Store the myRow object in each row
        row.myRow = new myRowObject(myselect, txtAmountFrom, txtAmountTo, txtPercentage, linkDel);
    }    
}

/*
 * add_row_product_point
 * Inserts at row 'num', or appends to the end if no arguments are passed in. Don't pass in empty strings.
 */
function add_row_point_level(levels, num)
{
    if (hasLoaded) {
        var tbl = document.getElementById('point_level_table');
        var nextRow = tbl.tBodies[0].rows.length;
        var iteration = nextRow + ROW_BASE;
        var cell_count = 1;
        try{
            var rebate_type = document.getElementById('_products_level_form').value;
        }catch(err){
            var rebate_type = document.getElementById('_products_level_form1').value;
        }
        if (num == null) { 
            num = nextRow;
        } else {
            iteration = num + ROW_BASE;
        }
        
        // add the row
        var row = tbl.tBodies[0].insertRow(num);
        
        // CONFIG: requires classes named classy0 and classy1
        //if (iteration % 2 == 0) {
            row.className = 'odd';
        //}
        
        // CONFIG: This whole section can be configured
        
        /* Create select menu (from products list)
        -------------------------------------------------*/    
        var cell0 = row.insertCell(0);
        myselect = document.createElement("select");
        myselect.name = 'LevelID_n' + iteration;
        myselect.className = "formfield_width_100_percent";
        var levelList = levels.split(";");

        //create drop-down list        
        for(i = 0; i < levelList.length; i++) {
            var individualLevels = levelList[i].split(":");
        
            //create an option
            theOption = document.createElement("option");
            //make some text
            theText  = document.createTextNode(individualLevels[1]);
            theOption.value = individualLevels[0];
            //add the text to the option
            theOption.appendChild(theText);
            //add the option to the select
            myselect.appendChild(theOption);
            
        }//for
        //end of options
        cell0.appendChild(myselect);
        
        if(rebate_type == 6)
        {
            // cell 1 - input text (rebate amount)
            var cell1 = row.insertCell(cell_count++);        
            cell1.noWrap = true;        
            var txtPoints = document.createElement('input');
            txtPoints.type = 'text';
            txtPoints.name = 'Points_n' + iteration;
            txtPoints.className = "formfield_width_100_percent";
            // Don't refer to ids
            //txtInp.id = INPUT_NAME_PREFIX + iteration;
            txtPoints.size = 4;
            txtPoints.value = '';
            txtPoints.onkeyup = function(){IsNumeric(this)};
            cell1.appendChild(txtPoints);
        }else if(rebate_type == 7){
            // cell 1 - input text (rebate amount)
            var cell1 = row.insertCell(cell_count++);        
            cell1.noWrap = true;        
            var txtRequirements = document.createElement('input');
            txtRequirements.type = 'text';
            txtRequirements.name = 'Requirement_c' + iteration;
            // Don't refer to ids
            //txtInp.id = INPUT_NAME_PREFIX + iteration;
            txtRequirements.size = 35;
            txtRequirements.value = '';
            cell1.appendChild(txtRequirements);
            
            // cell 1 - input text (rebate amount)
            var cell2 = row.insertCell(cell_count++);        
            var textNode = document.createTextNode('% ');
            cell2.appendChild(textNode);        
            cell2.noWrap = true;        
            var txtAmountFrom = document.createElement('input');
            txtAmountFrom.type = 'text';
            txtAmountFrom.name = 'AmountFrom_n' + iteration;
            // Don't refer to ids
            //txtInp.id = INPUT_NAME_PREFIX + iteration;
            txtAmountFrom.size = 15;
            txtAmountFrom.value = '';
            txtAmountFrom.onkeyup = function(){IsNumeric(this)};
            cell2.appendChild(txtAmountFrom);
        }
        
        
        // cell 2 - delete link
        var cell3 = row.insertCell(cell_count++);        
        var linkDel = document.createElement('a');
        linkDel.className = 'pointer';                
        linkDel.appendChild(document.createTextNode('Remove'));
        linkDel.onclick = function(){deleteCurrentRow(this); return false;}
        cell3.appendChild(linkDel);
        // Pass in the elements you want to reference later
        // Store the myRow object in each row
        row.myRow = new myRowObject(myselect, txtPoints, linkDel);
    }    
}

/*
 * add_row_product_point
 * Inserts at row 'num', or appends to the end if no arguments are passed in. Don't pass in empty strings.
 */
function add_row_product_with_level(products, levels, num)
{
    if (hasLoaded) {
        var tbl = document.getElementById('products_table');
        var nextRow = tbl.tBodies[0].rows.length;
        var iteration = nextRow + ROW_BASE;
        var cell_count = 1;
        
        if (num == null) { 
            num = nextRow;
        } else {
            iteration = num + ROW_BASE;
        }
        
        // add the row
        var row = tbl.tBodies[0].insertRow(num);
        
        // CONFIG: requires classes named classy0 and classy1
        //if (iteration % 2 == 0) {
            row.className = 'odd';
        //}
        
        // CONFIG: This whole section can be configured
        /* Create select menu (from products list)
        -------------------------------------------------*/    
        var cell0 = row.insertCell(0);
        myselect = document.createElement("select");
        myselect.name = 'ProductID_n' + iteration;
        myselect.className = "formfield_width_100_percent";
        var productList = products.split(";");

        //create drop-down list        
        for(i = 0; i < productList.length; i++) {
            var individualProducts = productList[i].split(":");
        
            //create an option
            theOption = document.createElement("option");
            //make some text
            theText  = document.createTextNode(individualProducts[1]);
            theOption.value = individualProducts[0];
            //add the text to the option
            theOption.appendChild(theText);
            //add the option to the select
            myselect.appendChild(theOption);
            
        }//for
        //end of options
        cell0.appendChild(myselect);
        
        /* Create select menu (from level list)
        -------------------------------------------------*/    
        var cell1 = row.insertCell(1);
        myselect = document.createElement("select");
        myselect.name = 'LevelID_n' + iteration;
        myselect.className = "formfield_width_100_percent";
        var levelList = levels.split(";");

        //create drop-down list        
        for(i = 0; i < levelList.length; i++) {
            var individualLevels = levelList[i].split(":");
        
            //create an option
            theOption = document.createElement("option");
            //make some text
            theText  = document.createTextNode(individualLevels[1]);
            theOption.value = individualLevels[0];
            //add the text to the option
            theOption.appendChild(theText);
            //add the option to the select
            myselect.appendChild(theOption);
            
        }//for
        //end of options
        cell1.appendChild(myselect);

        // cell 1 - input text (rebate amount)
        var cell2 = row.insertCell(2);        
        var textNode = document.createTextNode('$ ');
        cell2.appendChild(textNode);        
        cell2.noWrap = true;        
        var txtAmount = document.createElement('input');
        txtAmount.type = 'text';
        txtAmount.name = 'RebateAmount_n' + iteration;
        txtAmount.className = "formfield_width_90_percent";
        // Don't refer to ids
        //txtInp.id = INPUT_NAME_PREFIX + iteration;

        txtAmount.value = '';
        txtAmount.onkeyup = function(){IsNumeric(this)};
        cell2.appendChild(txtAmount);
        
        // cell 2 - delete link
        var cell3 = row.insertCell(3);        
        var linkDel = document.createElement('a');
        linkDel.className = 'pointer';                
        linkDel.appendChild(document.createTextNode('Remove'));
        linkDel.onclick = function(){deleteCurrentRow(this); return false;}
        cell3.appendChild(linkDel);
        // Pass in the elements you want to reference later
        // Store the myRow object in each row
        row.myRow = new myRowObject(myselect, txtPoints, linkDel);
    }    
}

/*
 * add_row_products_printer
 * Inserts at row 'num', or appends to the end if no arguments are passed in. Don't pass in empty strings.
 */
function add_row_products_printer(products,rebate_startdate,rebate_enddate,num,use_cell6)
{
    if (hasLoaded) {
        
        var tbl = document.getElementById('products_table');
        var nextRow = tbl.tBodies[0].rows.length;
        var iteration = nextRow + ROW_BASE;
        var rebate_type = document.getElementById('_products_form').value;
        var cell_count = 1;
        
        if (num == null) { 
            num = nextRow;
        } else {
            iteration = num + ROW_BASE;
        }
        
        if (use_cell6 == null) {
            use_cell6 = true;
        }
        
        // add the row
        var row = tbl.tBodies[0].insertRow(num);
        
        // CONFIG: requires classes named classy0 and classy1
        //if (iteration % 2 == 0) {
            row.className = 'odd';
        //}
        
        // CONFIG: This whole section can be configured
        
        /* Create select menu (from products list)
        -------------------------------------------------*/    
        var cell0 = row.insertCell(0);
        myselect = document.createElement("select");
        myselect.name = 'ProductID_n' + iteration;
        if (rebate_type == "2" || rebate_type == "12") {
            myselect.className = "formfield_width_160";
        } else {
            myselect.className = "formfield_width_100_percent";
        }
        myselect.onchange = function () {selectedItemChange(this.value, iteration);};
        var productList = products.split(";");
        var firstId = 0;
        //create drop-down list        
        for(i = 0; i < productList.length; i++) {
            var individualProducts = productList[i].split(":");
        
            //create an option
            theOption = document.createElement("option");
            //make some text
            theText  = document.createTextNode(individualProducts[1]);
            theOption.value = individualProducts[0];
            //add the text to the option
            theOption.appendChild(theText);
            //add the option to the select
            myselect.appendChild(theOption);
            if(i==0) firstId = individualProducts[0];
        }//for
        //end of options
        cell0.appendChild(myselect);
        
        if(rebate_type != "8" && rebate_type != "9")    
        {
            if(rebate_type == "12")
            {
                var cell1 = row.insertCell(cell_count++);        
                var divobj = document.createElement('div');
                divobj.name = 'div' + iteration;
                divobj.setAttribute('innerHTML', setProductPart(firstId));
                divobj.setAttribute('id','div' + iteration); 
                cell1.appendChild(divobj);        
                cell1.noWrap = true;
                
                // cell 2 - input text (Notes)
                var cell_notes = row.insertCell(cell_count++);
                cell_notes.noWrap = true;
                var txtAutoRollFeed = document.createElement('input');
                txtAutoRollFeed.type = 'text';
                txtAutoRollFeed.name = 'AutoRollFeedUnitNo_c' + iteration;
                txtAutoRollFeed.className = "formfield_width_75";
                // Don't refer to ids
                //txtInp.id = INPUT_NAME_PREFIX + iteration;
                txtAutoRollFeed.value = '';
                cell_notes.appendChild(txtAutoRollFeed);
            }
            
            if(rebate_type != "12")
            {
                // cell 1 - input text (rebate amount)
                var cell1 = row.insertCell(cell_count++);        
                var textNode = document.createTextNode('$ ');
                cell1.appendChild(textNode);        
                cell1.noWrap = true;        
                var txtAmount = document.createElement('input');
                txtAmount.type = 'text';
                txtAmount.name = 'RebateAmount_n' + iteration;
                txtAmount.id = 'RebateAmount_n' + iteration;
                txtAmount.className = "formfield_width_40";
                 txtAmount.setAttribute('tmt:filters','numbersonly');
                txtAmount.setAttribute('onkeyup','tmt_filterField(this)');
                txtAmount.setAttribute('onblur','tmt_filterField(this)');
                
                // Don't refer to ids
                //txtInp.id = INPUT_NAME_PREFIX + iteration;
                txtAmount.size = 4;
                txtAmount.value = '';
                txtAmount.onkeyup = function(){IsNumeric(this)};
                cell1.appendChild(txtAmount);
            }
        
            if(rebate_type == "2")
            {
                // cell 2 - input text (Notes)
                var cell_notes = row.insertCell(cell_count++);
                cell_notes.noWrap = true;
                var txtNotes = document.createElement('input');
                txtNotes.type = 'text';
                txtNotes.name = 'Notes_c' + iteration;
                txtNotes.className = "formfield_width_75";
                // Don't refer to ids
                //txtInp.id = INPUT_NAME_PREFIX + iteration;
                txtNotes.size = 7;
                txtNotes.value = '';
                cell_notes.appendChild(txtNotes);
            }
            
            // cell 2 - input text (start date)
            var cell2 = row.insertCell(cell_count++);
            cell2.noWrap = true;
            var txtStartDate = document.createElement('input');
            txtStartDate.type = 'text';
            txtStartDate.name = 'StartDate_d' + iteration;
            // Don't refer to ids
            //txtInp.id = INPUT_NAME_PREFIX + iteration;
            txtStartDate.size = 6;
            txtStartDate.value = rebate_startdate;
            txtStartDate.setAttribute('readOnly', 'readonly');
            cell2.appendChild(txtStartDate);
            
    
            // cell 2 - calendar button
            var btnCalStart = document.createElement('input');
            btnCalStart.className = 'date_picker';        
            btnCalStart.type = 'button';
            btnCalStart.value = 'calendar';
            btnCalStart.onclick = function () {toggleCalendar(txtStartDate);};
            cell2.appendChild(btnCalStart);    
            
            // cell 3 - input text (end date)
            var cell3 = row.insertCell(cell_count++);
            cell3.noWrap = true;        
            var txtEndDate = document.createElement('input');
            txtEndDate.type = 'text';
            txtEndDate.name = 'EndDate_d' + iteration;
            // Don't refer to ids
            //txtInp.id = INPUT_NAME_PREFIX + iteration;
            txtEndDate.size = 6;
            txtEndDate.value = rebate_enddate;
            txtEndDate.setAttribute('readOnly', 'readonly');        
            cell3.appendChild(txtEndDate);
            
            // cell 3 - calendar button
            var btnCalEnd = document.createElement('input');
            btnCalEnd.className = 'date_picker';        
            btnCalEnd.type = 'button';
            btnCalEnd.value = 'calendar';
            btnCalEnd.onclick = function () {toggleCalendar(txtEndDate);};
            cell3.appendChild(btnCalEnd);    
        }else{
            // cell 2 - input text (Notes)
            var cell_notes = row.insertCell(cell_count++);
            cell_notes.noWrap = true;
            var txtInksAndMedia = document.createElement('input');
            txtInksAndMedia.type = 'text';
            txtInksAndMedia.name = 'InksAndMedia_c' + iteration;
            // Don't refer to ids
            //txtInp.id = INPUT_NAME_PREFIX + iteration;
            txtInksAndMedia.size = 50;
            txtInksAndMedia.value = '';
            cell_notes.appendChild(txtInksAndMedia);
        }
        // cell 5 - delete link
        var cell4 = row.insertCell(cell_count++);        
        var linkDel = document.createElement('a');
        linkDel.className = 'pointer';                
        linkDel.appendChild(document.createTextNode('Remove'));
        linkDel.onclick = function(){deleteCurrentRow(this); return false;}
        cell4.appendChild(linkDel);
        
        //only used for certain calls, otherwise will break html
        if(use_cell6){
            // cell 6 - empty
            var cell5 = row.insertCell(cell_count++);    
            var textNode = document.createTextNode('');
            cell5.appendChild(textNode);
        }
        
        // Pass in the elements you want to reference later
        // Store the myRow object in each row
        row.myRow = new myRowObject(myselect, textNode, txtAmount, txtStartDate, btnCalStart, txtEndDate, btnCalEnd, linkDel);
    }
}

/*
 * add_row_documents
 * Inserts at row 'num', or appends to the end if no arguments are passed in. Don't pass in empty strings.
 */
function add_row_documents(doctypes,num)
{
    if (hasLoaded) {
        
        var tbl = document.getElementById('documents_table');
        var nextRow = tbl.tBodies[0].rows.length;
        var iteration = nextRow + ROW_BASE;
        if (num == null) { 
            num = nextRow;
        } else {
            iteration = num + ROW_BASE;
        }
        
        var cellCount = 1; 
        
        try
        {
            var rebate_type = document.getElementById('_documents_form').value;    
        }
        catch(err)
        {
            var rebate_type = 0;
        }
        
        
        // add the row
        var row = tbl.tBodies[0].insertRow(num);
        
        // CONFIG: requires classes named classy0 and classy1
        //if (iteration % 2 == 0) {
            row.className = 'odd';
        //}
        
        // CONFIG: This whole section can be configured
        
        /* Create select menu (from document types list)
        -------------------------------------------------*/    
        var cell0 = row.insertCell(0);
        myselect = document.createElement("select");
        myselect.name = 'RebateDocumentTypeID[]';
        myselect.className = '';
        if(rebate_type == 13)
        {
            myselect.onchange = function () {showInputText(this, iteration);};
        }
        var docTypeList = doctypes.split(";");
        
        for(i = 0; i < docTypeList.length; i++) {
            var individualDocumentType = docTypeList[i].split(":");
        
            //create an option
            theOption = document.createElement("option");
            //make some text
            theText  = document.createTextNode(individualDocumentType[1]);
            theOption.value = individualDocumentType[0];
            //add the text to the option
            theOption.appendChild(theText);
            //add the option to the select
            myselect.appendChild(theOption);
            
        }//for
        /*if(rebate_type == 13)
        {
            theOption = document.createElement("option");
            theText  = document.createTextNode('Rebate Program');
            theOption.value = 'Rebate Program';
            theOption.appendChild(theText);
            myselect.appendChild(theOption);
        }*/
        //end of options
        cell0.appendChild(myselect);
        
        if(rebate_type == 13)
        {
            var cellDisplayName = row.insertCell(cellCount++);
            var txtDisplayName = document.createElement('input');
            txtDisplayName.type = 'text';
            txtDisplayName.name = 'RebateFormDescription_c[]';
            txtDisplayName.id = 'RebateFormDescription_c' + iteration;
            txtDisplayName.size = 30;
            txtDisplayName.style.width='100px';
            txtDisplayName.style.display = 'none';
            txtDisplayName.value = '';
            cellDisplayName.appendChild(txtDisplayName);
            
            var divDisplayName = document.createElement('div');
            divDisplayName.id = 'divDisplay' + iteration;
            divDisplayName.innerHTML = "-";
            divDisplayName.style.width='100px';
            cellDisplayName.appendChild(divDisplayName);
        }
        
        // cell: upload file
        var cell1 = row.insertCell(cellCount++);
        var txtFile = document.createElement('input');
        txtFile.type = 'text';
        txtFile.name = '_FileName[]';
        txtFile.className = 'formfield_width_80';
        txtFile.id = 'file_' + iteration;
        txtFile.value = '';
        txtFile.readOnly = true;
        cell1.appendChild(txtFile);
        
        var ezButton = document.createElement('input');
        ezButton.type = 'button';
        ezButton.value = ' Browse... ';
        ezButton.onclick = function(){phpNiceBrowser(0,iteration);}
        cell1.appendChild(ezButton);
        
        //include HIDDEN rebateformID
        var dmzHidden = document.createElement('input');
        dmzHidden.type = 'hidden';
        dmzHidden.name = '_DmzFilePath[]';
        dmzHidden.id = 'dmz_' + iteration;
        // Don't refer to ids
        //txtInp.id = INPUT_NAME_PREFIX + iteration;
        dmzHidden.size = 30;
        dmzHidden.value = '';
        cell1.appendChild(dmzHidden);
        
        //include HIDDEN rebateformID
        var txtHidden = document.createElement('input');
        txtHidden.type = 'hidden';
        txtHidden.name = 'RebateFormID_n[]';
        // Don't refer to ids
        //txtInp.id = INPUT_NAME_PREFIX + iteration;
        txtHidden.size = 30;
        txtHidden.value = '';
        cell1.appendChild(txtHidden);

        // cell 2 - delete link
        var cell2 = row.insertCell(cellCount++);        
        var linkDel = document.createElement('a');
        linkDel.className = 'pointer';                        
        linkDel.appendChild(document.createTextNode('Remove'));
        linkDel.onclick = function(){deleteCurrentRow(this); return false;}
        cell2.appendChild(linkDel);
                        
        // Pass in the elements you want to reference later
        // Store the myRow object in each row
        row.myRow = new myRowObject(myselect, theText, txtFile, theOption, linkDel);
    }
}

// If there isn't an element with an onclick event in your row, then this function can't be used.
function deleteCurrentRow(obj)
{
    if (hasLoaded) {
        var delRow = obj.parentNode.parentNode;
        var tbl = delRow.parentNode.parentNode;
        var rIndex = delRow.sectionRowIndex;
        var rowArray = new Array(delRow);
        deleteRows(rowArray);
        reorderRows(tbl, rIndex);
    }
}

function reorderRows(tbl, startingIndex)
{
    if (hasLoaded) {
        if (tbl.tBodies[0].rows[startingIndex]) {
            var count = startingIndex + ROW_BASE;
            for (var i=startingIndex; i<tbl.tBodies[0].rows.length; i++) {
            
                // CONFIG: next line is affected by myRowObject settings
                tbl.tBodies[0].rows[i].myRow.one.data = count; // text
                
                // CONFIG: next line is affected by myRowObject settings
                tbl.tBodies[0].rows[i].myRow.two.name = INPUT_NAME_PREFIX + count; // input text
                
                // CONFIG: requires class named classy0 and classy1
                //if (count % 2 == 0) {
                    tbl.tBodies[0].rows[i].className = 'odd';
                //}
                
                count++;
            }
        }
    }
}

function deleteRows(rowObjArray)
{
    if (hasLoaded) {
        for (var i=0; i<rowObjArray.length; i++) {
            var rIndex = rowObjArray[i].sectionRowIndex;
            rowObjArray[i].parentNode.deleteRow(rIndex);
        }
    }
}

function openInNewWindow(frm)
{
    // open a blank window
    var aWindow = window.open('', 'TableAddRow2NewWindow',
    'scrollbars=yes,menubar=yes,resizable=yes,toolbar=no,width=400,height=400');
    
    // set the target to the blank window
    frm.target = 'TableAddRow2NewWindow';
    
    // submit
    frm.submit();
}

// Calendar Display
// www.mredkj.com
// June 08, 2002 - version 1.0.0
// November 14, 2002 - version 1.1.0
// March 07, 2006 - version 2.BETA.20060307
// May 08, 2006 - version 2.BETA.20060508
// July 25, 2006 - version 2.BETA.20060725
// October 10, 2006 - version 2.BETA.20061010
//
// to do: If click away from the calendar, then hide it. This will help avoid the orphan calendar when deleting text boxes.
//
function toggleCalendar(txtObj)
{    
    cObj = txtObj.myCalendar;
    if (!cObj) {
        cObj = new CalendarDisplay(txtObj);
        document.body.appendChild(cObj.cDiv);
        txtObj.myCalendar = cObj;
    }
    
    cObj.toggle();
}

CalendarDisplay = function(txtObj) {
    this.txtObj = txtObj;
    this.tBox = this.txtObj;
    this.cDiv = document.createElement('div');
    this.cDiv.style.position = 'absolute';
    this.cDiv.style.display = 'none';
}

CalendarDisplay.prototype.MONTHS_CALENDAR = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
CalendarDisplay.prototype.DAYS_1_CALENDAR = new Array("S", "M", "T", "W", "T", "F", "S");
CalendarDisplay.prototype.DAYS_2_CALENDAR = new Array("Su", "Mo", "Tu", "We", "Th", "Fr", "Sa");

CalendarDisplay.prototype.toggle = function() {
    if (this.cDiv.style.display == 'none') {
        this.adjustPosition();
        this.fillCalendar(this.grabDate());
        this.cDiv.style.display = 'block';
    } else {
        this.cDiv.style.display = 'none';
    }
}

CalendarDisplay.prototype.grabDate = function() {
    var tempDate = new Date(this.tBox.value);
    if (!tempDate.getYear()) {
        tempDate = new Date();
    }
    return tempDate;
}

CalendarDisplay.prototype.fillCalendar = function(theDate) {
    if (this.cDiv.firstChild) {
        this.cDiv.removeChild(this.cDiv.firstChild);
    }
    this.adjustPosition();
    this.cDiv.appendChild(this.getCalendar(theDate));
}

CalendarDisplay.prototype.adjustPosition = function() {
    // !! IE, FF, and Opera positions can be slightly different depending on the page layout.
    // !! I think it has to do with the page padding
    if (BrowserDetect.browser == "Explorer" && BrowserDetect.version.toString() == "6") 
    {
        var tmp = findPos(this.tBox);
        this.cDiv.style.left = tmp[0];
        this.cDiv.style.top = tmp[1]+18;
    } 
    else 
    {
        this.cDiv.style.top = this.tBox.offsetHeight + this.findPosY(this.tBox) + 'px';
        this.cDiv.style.left = this.findPosX(this.tBox) + 'px';
    }
    //this.cDiv.style.left = this.findPosX(this.tBox) + 'px';

}

CalendarDisplay.prototype.getCalendar = function(theDate) {
    var theYear = theDate.getFullYear();
    var theMonth = theDate.getMonth();
    var theDay = theDate.getDate();

    var theTable = document.createElement('table');
    theTable.id = 'calendartable';
    var theTHead = theTable.createTHead();
    var theTBody = document.createElement('tbody');
    theTable.appendChild(theTBody);
    
    var monthRow = theTHead.insertRow(0);
    var navLeftCell = monthRow.insertCell(0);
    var monthCell = monthRow.insertCell(1);
    var navRightCell = monthRow.insertCell(2);
    monthCell.colSpan = 5;
    monthCell.appendChild(document.createTextNode(this.MONTHS_CALENDAR[theMonth] + ', ' + theYear));
    var leftLink = document.createElement('a');
    leftLink.href = '#';
    this.setCalendarPrevious(leftLink, this.txtObj, theYear, theMonth, theDay);
    leftLink.appendChild(document.createTextNode('-'));
    navLeftCell.appendChild(leftLink);
    var rightLink = document.createElement('a');
    rightLink.href = '#';
    this.setCalendarNext(rightLink, this.txtObj, theYear, theMonth, theDay);
    rightLink.appendChild(document.createTextNode('+'));
    navRightCell.appendChild(rightLink);
    
    var weeksRow = theTHead.insertRow(1);
    for (var i=0; i<7; i++) {
        var tempWeeksCell = weeksRow.insertCell(i);
        tempWeeksCell.appendChild(document.createTextNode(this.DAYS_2_CALENDAR[i]));
    }
    
    var temporaryDate1 = new Date(theYear, theMonth, 1);
    var startDayOfWeek = temporaryDate1.getDay();
    var temporaryDate2 = new Date(theYear, theMonth + 1, 0);
    var lastDateOfMonth = temporaryDate2.getDate();
    var dayCount = 1;
        
    for (var r=0; r<6; r++) {
        var tempDaysRow = theTable.tBodies[0].insertRow(r);
        tempDaysRow.className = 'dayrow';
        for (var c=0; c<7; c++) {
            var tempDaysCell = tempDaysRow.insertCell(c);
            var mysteryNode;
            if ((r > 0 || c >= startDayOfWeek) && dayCount <= lastDateOfMonth) {
                tempDaysCell.className = 'yestext';
                var mysteryNode = document.createElement('a');
                mysteryNode.href = '#';
                this.setCalendarClick(mysteryNode, this.txtObj, theYear, theMonth, dayCount);
                mysteryNode.appendChild(document.createTextNode(dayCount));
                dayCount++;
            } else {
                tempDaysCell.className = 'notext';
                mysteryNode = document.createTextNode('');
            }
            tempDaysCell.appendChild(mysteryNode);
        }
    }
    
    return theTable;
}
CalendarDisplay.prototype.setCalendarClick = function (node, theObj, theYear, theMonth, theDay) {
    node.onclick = function() {fillInFields(theObj, theYear, (theMonth + 1), theDay); return false;}
}
CalendarDisplay.prototype.setCalendarPrevious = function (node, theObj, theYear, theMonth, theDay) {
    node.onclick = function() {showPrevious(theObj, theYear, theMonth, theDay); return false;}
}
CalendarDisplay.prototype.setCalendarNext = function (node, theObj, theYear, theMonth, theDay) {
    node.onclick = function() {showNext(theObj, theYear, theMonth, theDay); return false;}
}
    

// http://www.quirksmode.org/js/findpos.html
CalendarDisplay.prototype.findPosX = function(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curleft += obj.offsetLeft;
            obj = obj.offsetParent;
        }
    }
    else if (obj.x) {
        curleft += obj.x;
    }
    return curleft;
}

// http://www.quirksmode.org/js/findpos.html
CalendarDisplay.prototype.findPosY = function(obj) {
    var curtop = 0;
    if (obj.offsetParent)    {
        while (obj.offsetParent) {
            curtop += obj.offsetTop;
            obj = obj.offsetParent;
        }
    }
    else if (obj.y) {
        curtop += obj.y;
    }
    return curtop;
}

function fillInFields(obj, year, month, day)
{
    obj.value = (month < 10 ? '0'+month : month) + '/' + (day < 10 ? '0'+day : day) + '/' + year;
    cObj = obj.myCalendar;
    cObj.toggle();
}

function showPrevious(obj, year, month, day)
{
    cObj = obj.myCalendar;
    var lastMonth = new Date(year, month - 1, day)
    cObj.fillCalendar(lastMonth);
}
function showNext(obj, year, month, day)
{
    cObj = obj.myCalendar;
    var nextMonth = new Date(year, month + 1, day)
    cObj.fillCalendar(nextMonth);
}

function findPos(obj) 
{
    var curleft = curtop = 0;
    if (obj.offsetParent) 
    {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
        return [curleft,curtop];
    }
}