//
//  Functions to copy selected web data to the Windows clipboard.
//  Functions to save selected web data as text or xml or csv files.
//

function    Copy2Clipboard( b2PerPage )
{
    // Loop through my items looking for clipcopy checked.
    // Then save the relevant data from each to strClipboard.
    // Finally copy strClipboard to clipboard.
    var CR = "\u000D";
    var strClipboard = "";
    var j = 0;
    var formNames = new Array();
    for ( var i=0; i<document.forms.length; i++)
        formNames[i] = document.forms[i].name;
    if ( b2PerPage )        // true = 2 Control Clipboard Regions on page.
    {
        strClipboard += "Here is the Acura Spa Systems product information that you asked about." + CR + CR;
        strClipboard += "  Item # |         Part Number             |       Part Description           |  Part Price  |  Qty  |    Cost    " + CR;
        strClipboard += "---------|---------------------------------|----------------------------------|--------------|-------|------------" + CR;
        for ( var i = 0; i < document.forms.length; i++ )
        {
            if ( (formNames[i].search(/megatrol1/) > -1) ||
                 (formNames[i].search(/minitrol1/) > -1) ||
                 (formNames[i].search(/pmaster1/) > -1) ||
                 (formNames[i].search(/hotlineCHL1/) > -1) ||
                 (formNames[i].search(/quickset1/) > -1) )
            {
                if ( document.forms[i].clipcopy1.checked )
                {
                    strClipboard += "  " + PadString( Number( ++j ), 6 ) + " | ";
                    strClipboard += " " + PadString( document.forms[i].ID_NUM.value, 30 ) + " | ";
                    strClipboard += " " + PadString( document.forms[i].NAME.value, 30 ) + "  | ";
                    if ( document.forms[i].QUANTITY == null )
                    {
                        strClipboard += "             | ";
                        strClipboard += "      | ";
                        strClipboard += "          " + CR;
                    } else {
                        strClipboard += " $" + PadString( document.forms[i].PRICE.value.replace("$", ""), 10 ) + " | ";
                        strClipboard += " " + PadString( document.forms[i].QUANTITY.value, 4 ) + " | ";
                        strClipboard += " $" + PadString( Number( Number( document.forms[i].PRICE.value.replace("$", "") ) *
                                                                  Number( document.forms[i].QUANTITY.value.replace("$", "") ) ).toFixed(2), 10 ) + CR;
                    }
                    if ( document.forms[i].NAME.value.indexOf( "***" )  > 0 )
                    {
                        alert(document.forms[i].NAME.value);
                        var indx = document.forms[i].NAME.value.indexOf( "***" );
                        strClipboard += "  " + PadString( " ", 6 )  + " |  " + PadString( " ", 30 ) + " | ";
                        strClipboard += " " + PadString( document.forms[i].NAME.value.substr( indx ), 30 ) + "  | ";
                        strClipboard += "  " + PadString( " ", 10 ) + " | " + PadString( " ", 4 ) + "  | " + PadString( " ", 10 ) + CR;
                    }
                }
            }
            else
            if ( (formNames[i].search(/megatrol2/) > -1) ||
                 (formNames[i].search(/minitrol2/) > -1) ||
                 (formNames[i].search(/pmaster2/) > -1) ||
                 (formNames[i].search(/hotlineCHL2/) > -1) ||
                 (formNames[i].search(/quickset2/) > -1) )
            {
                if ( document.forms[i].clipcopy2.checked )
                {
                    strClipboard += "  " + PadString( Number( ++j ), 6 ) + " | ";
                    strClipboard += " " + PadString( document.forms[i].ID_NUM.value, 30 ) + " | ";
                    strClipboard += " " + PadString( document.forms[i].NAME.value, 30 ) + "  | ";
                    if ( document.forms[i].QUANTITY == null )
                    {
                        strClipboard += "             | ";
                        strClipboard += "      | ";
                        strClipboard += "          " + CR;
                    } else {
                        strClipboard += " $" + PadString( document.forms[i].PRICE.value.replace("$", ""), 10 ) + " | ";
                        strClipboard += " " + PadString( document.forms[i].QUANTITY.value, 4 ) + " | ";
                        strClipboard += " $" + PadString( Number( Number( document.forms[i].PRICE.value.replace("$", "") ) *
                                                                  Number( document.forms[i].QUANTITY.value.replace("$", "") ) ).toFixed(2), 10 ) + CR;
                    }
                    if ( document.forms[i].NAME.value.indexOf( "***" )  > 0 )
                    {
                        alert(document.forms[i].NAME.value);
                        var indx = document.forms[i].NAME.value.indexOf( "***" );
                        strClipboard += "  " + PadString( " ", 6 )  + " |  " + PadString( " ", 30 ) + " | ";
                        strClipboard += " " + PadString( document.forms[i].NAME.value.substr( indx ), 30 ) + "  | ";
                        strClipboard += "  " + PadString( " ", 10 ) + " | " + PadString( " ", 4 ) + "  | " + PadString( " ", 10 ) + CR;
                    }
                }
            }
        }
        strClipboard += "---------|---------------------------------|----------------------------------|--------------|-------|------------" + CR;
        strClipboard += CR + CR + "To view the webpage for this information, copy and paste the following address into your browser:" + CR + CR;
        strClipboard += document.location.href + CR + CR;
        strClipboard += "Thank your for choosing our high quality, long lasting products to power your spa." + CR;
        strClipboard += "-----------------------" + CR;
        strClipboard += "Acura Spa Systems, Inc." + CR + CR;
    }
    else
    {
//        show_all_forms();
        strClipboard += "Here is the Acura Spa Systems product information that you asked about." + CR + CR;
        strClipboard += "  Item # |         Part Number             |       Part Description           |  Part Price  |  Qty  |    Cost    " + CR;
        strClipboard += "---------|---------------------------------|----------------------------------|--------------|-------|------------" + CR;
        for ( var i = 0; i < document.forms.length; i++ )
        {
            if ( (formNames[i].search(/product/) > -1) ||
//                 (formNames[i].search(/extras/) > -1) ||
                 (formNames[i].search(/part_list/) > -1) ||
                 (formNames[i].search(/aquaheat\d+/) > -1) || 
                 (formNames[i].search(/aquaheat_plus\d+/) > -1) || 
                 (formNames[i].search(/maverick_ss_115_\d+/) > -1) ||
                 (formNames[i].search(/maverick_115_\d+/) > -1) ||
                 (formNames[i].search(/maverick_50hz_230_\d+/) > -1) ||
                 (formNames[i].search(/maverick_230_\d+/) > -1) ||
                 (formNames[i].search(/magnaflow\d+/) > -1) ||
                 (formNames[i].search(/magnaflow_50hz\d+/) > -1) ||
                 (formNames[i].search(/magnaflow_48_60hz\d+/) > -1) ||
                 (formNames[i].search(/magnaflow_56_60hz\d+/) > -1) ||
                 (formNames[i].search(/megaflow\d+/) > -1) || 
                 (formNames[i].search(/megaflow_50hz\d+/) > -1) || 
                 (formNames[i].search(/megaflow_60hz\d+/) > -1) || 
                 (formNames[i].search(/supercharger\d+/) > -1) ||
                 (formNames[i].search(/supercharger_ii\d+/) > -1) )
            {
//                alert( formNames[i] );
                if ( document.forms[i].clipcopy.checked )
                {
                    strClipboard += "  " + PadString( Number( ++j ), 6 ) + " | ";
                    strClipboard += " " + PadString( document.forms[i].ID_NUM.value, 30 ) + " | ";
                    strClipboard += " " + PadString( document.forms[i].NAME.value, 30 ) + "  | ";
                    if ( document.forms[i].QUANTITY == null )
                    {
                        strClipboard += "             | ";
                        strClipboard += "      | ";
                        strClipboard += "          " + CR;
                    } else {
                        strClipboard += " $" + PadString( document.forms[i].PRICE.value.replace("$", ""), 10 ) + " | ";
                        strClipboard += " " + PadString( document.forms[i].QUANTITY.value, 4 ) + " | ";
                        strClipboard += " $" + PadString( Number( Number( document.forms[i].PRICE.value.replace("$", "") ) *
                                                                  Number( document.forms[i].QUANTITY.value.replace("$", "") ) ).toFixed(2), 10 ) + CR;
                    }
                    if ( document.forms[i].NAME.value.indexOf( "***" )  > 0 )
                    {
                        alert(document.forms[i].NAME.value);
                        var indx = document.forms[i].NAME.value.indexOf( "***" );
                        strClipboard += "  " + PadString( " ", 6 )  + " |  " + PadString( " ", 30 ) + " | ";
                        strClipboard += " " + PadString( document.forms[i].NAME.value.substr( indx ), 30 ) + "  | ";
                        strClipboard += "  " + PadString( " ", 10 ) + " | " + PadString( " ", 4 ) + "  | " + PadString( " ", 10 ) + CR;
                    }
                }
            }
        }
        strClipboard += "---------|---------------------------------|----------------------------------|--------------|-------|------------" + CR;
        strClipboard += CR + CR + "To view the webpage for this information, copy and paste the following address into your browser:" + CR + CR;
        strClipboard += document.location.href + CR + CR;
        strClipboard += "Thank your for choosing our high quality, long lasting products to power your spa." + CR;
        strClipboard += "-----------------------" + CR;
        strClipboard += "Acura Spa Systems, Inc." + CR + CR;
    }
    if ( j > 0 )
    {
        clip_copy.value = strClipboard;
        cpyData = clip_copy.createTextRange();
        cpyData.execCommand( "Copy" );
    }
}


function    CopyExp2Clipboard( bHTML )
{
    // Loop through my items looking for clipcopy checked.
    // Then save the relevant data from each to strClipboard.
    // Finally copy strClipboard to clipboard.
    var CR = "\u000D";
    var strClipboard = "";
    var j = 0;
    var formNames = new Array();
    for ( var i=0; i<document.forms.length; i++) {
        formNames[i] = document.forms[i].name;
    }
    if ( bHTML )        // true = format output as html document, false = format as text.
    {
        strClipboard += "<html><head><title>Acura Spa Systems Product Information Response</title></head>";
        strClipboard += "<body>";
        strClipboard += "<p>Here is the Acura Spa Systems product information that you asked about.<br /></p>";
        strClipboard += "<table border='1'>";
        strClipboard += "<thead><tr>";
        strClipboard += "<th>Item #</th>";
        strClipboard += "<th>Part Number</th>";
        strClipboard += "<th>Part Description</th>";
        strClipboard += "<th>Part Price</th>";
        strClipboard += "<th>Qty</th>";
        strClipboard += "<th>Cost</th>";
        strClipboard += "</tr></thead>";
        strClipboard += "<tbody>";
        for ( var i = 0; i < document.forms.length; i++ )
        {
            if ( formNames[i].search(/exploded/) > -1 )
            {
                if ( document.forms[i].clipcopy.checked )
                {
                    strClipboard += "<tr>";
                    strClipboard += "<td>" + Number( ++j ) + "</td>";
                    strClipboard += "<td>" + document.forms[i].ID_NUM.value + "</td>";
                    strClipboard += "<td>" + document.forms[i].NAME.value + "</td>";
                    strClipboard += "<td>$" + document.forms[i].PRICE.value + "</td>";
                    strClipboard += "<td>" + document.forms[i].QUANTITY.value + "</td>"; 
                    strClipboard += "<td>$" + Number( document.forms[i].PRICE.value ) * Number( document.forms[i].QUANTITY.value ) + "</td>";
                    strClipboard += "</tr>";
                }
            }
        }
        strClipboard += "</tbody></table>";
        strClipboard += "<p><br /><br />To view the webpage for this information, click on the address below or copy and paste the address into your browser:<br /><br />";
        strClipboard += document.location.href + "<br /><br />";
        strClipboard += "Thank your for choosing our high quality, long lasting products to power your spa.<br />";
        strClipboard += "-----------------------<br />";
        strClipboard += "Acura Spa Systems, Inc.<br /></p>";
        strClipboard += "</body></html>" + CR;
    }
    else
    {
        strClipboard += "Here is the Acura Spa Systems product information that you asked about." + CR + CR;
        strClipboard += "  Item # |  Part Number     |       Part Description           |  Part Price  |  Qty  |    Cost    " + CR;
        strClipboard += "---------|------------------|----------------------------------|--------------|-------|------------" + CR;
        for ( var i = 0; i < document.forms.length; i++ )
        {
            if ( formNames[i].search(/exploded/) > -1 )
            {
                if ( document.forms[i].clipcopy.checked )
                {
                    strClipboard += "  " + PadString( Number( ++j ), 6 ) + " | ";
                    strClipboard += " " + PadString( document.forms[i].ID_NUM.value, 15 ) + " | ";
                    strClipboard += " " + PadString( document.forms[i].NAME.value, 30 ) + "  | ";
                    strClipboard += " $" + PadString( document.forms[i].PRICE.value, 10 ) + " | ";
                    strClipboard += " " + PadString( document.forms[i].QUANTITY.value, 4 ) + " | "; 
                    strClipboard += " $" + PadString( (Number( document.forms[i].PRICE.value ) * Number( document.forms[i].QUANTITY.value )).toFixed(2), 10 ) + CR;
                }
            }
        }
        strClipboard += "---------|------------------|----------------------------------|--------------|-------|------------" + CR;
        strClipboard += CR + CR + "To view the webpage for this information, copy and paste the following address into your browser:" + CR + CR;
        strClipboard += document.location.href + CR + CR;
        strClipboard += "Thank your for choosing our high quality, long lasting products to power your spa." + CR;
        strClipboard += "-----------------------" + CR;
        strClipboard += "Acura Spa Systems, Inc." + CR + CR;
    }
    if ( j > 0 )
    {
        clip_copy_exp.value = strClipboard;
        cpyData = clip_copy_exp.createTextRange();
        cpyData.execCommand( "Copy" );
    }
}


function    PadString( strData, iSize )
{
    var strTemp = strData;
    var iLength = String(strTemp).length;
    if ( iLength > iSize ) {
        return strTemp.substr(0, iSize);
    }
    else {
        for ( var i=0; i<(iSize - iLength); i++ ) {
            strTemp += " ";
        }
        return strTemp;
    }
}


function show_all_forms() {
    var strDump = "";
    for ( var cntr=0; cntr<document.forms.length; cntr++ )
        strDump += document.forms[cntr].name + ", ";
    alert( strDump.slice( 0, strDump.length-2 ) );
}

