
// Functions for orderform.htm
function orderFormInit() {
    
    // Initialize the content of the order form at start
    videoTapeList();
    basicServiceList();
    enhancedServiceList();
    calculate();
}

function calculate() {

    saleTaxPrice    = 0.00;
    shippingPrice   = 0.00;
    videoTapeNumber = document.orderform.VideoTapeNumber.value;

    // Calculate basic & enhanced service price
    basicPrice    = basicServicePrice();    
    enhancedPrice = enhancedServicePrice();
    
    // Calculate total cost
    servicePrice  = (basicPrice + enhancedPrice) * 1.0;    
    saleTaxPrice  = (document.orderform.State.value == "MA") ? servicePrice * 0.05 : 0.00;
    shippingPrice = ( servicePrice > 0.00 ) ? 5.00 + ((videoTapeNumber-1)*1.0) : 0.00;
    orderPrice    = servicePrice + saleTaxPrice + shippingPrice;
    
    document.getElementById("ServicePrice").innerHTML  = formatAsMoney( servicePrice );
    document.getElementById("SaleTaxPrice").innerHTML  = formatAsMoney( saleTaxPrice );
    document.getElementById("ShippingPrice").innerHTML = formatAsMoney( shippingPrice );
    document.getElementById("OrderPrice").innerHTML    = formatAsMoney( orderPrice );
}


function videoTapeList() {

    // Initialize variables
    videoTapeNumber = document.orderform.VideoTapeNumber.value * 1.0;
    out = "";
    row = "<table width=430 class=tableContent cellspacing=0 cellpadding=0>" +
              "<tr>" +
                  "<td width=40>Name</td>" +
                  "<td width=200><input type=text size=22 class=controlContent></td>" +
                  "<td width=50>Format</td>" +
                  "<td width=140>" +
                      "<select class=controlContent>" +
                          "<option>VHS</option>" +
                          "<option>8mm</option>" + 
                          "<option>Hi8</option>" +
                          "<option>D8 (Digital 8)</option>" +
                          "<option>miniDV</option>" + 
                      "</select>" +
                  "</td>" + 
              "</tr>" + 
          "</table>";
          
    // Compose rows based on the number of tapes
    for( i=0; i<videoTapeNumber; i++ )
       out = out + row;
              
    // Output rows to page
    document.getElementById("TapeList").innerHTML = out;
}

function basicServiceList() {

    // Initialize variables
    basicServiceNumber = document.orderform.BasicServiceNumber.value * 1.0;
    out = "";
    heading = "";
    
    // Output heading if there is more than one title selected
    if( basicServiceNumber > 0 ) {
        heading = 
            "<table width=100% class=tableContent cellspacing=0 cellpadding=0>" +
                "<tr align=center>" +
                    "<td width=140>Title Name</td>" + 
                    "<td width=115>Video Length</td>" + 
                    "<td width=115>Standard Menus</td>" + 
                    "<td width=110>Extra Markers</td>" + 
                    "<td width=110>Extra Copies</td>" + 
                    "<td width=70 align=right>Price</td>" + 
                "</tr>" +            
            "</table>";    
    }    

    for( i=1; i<=basicServiceNumber; i++ ) {
        out = out +
            "<table width=100% class=tableContent cellspacing=0 cellpadding=0>" +
                "<tr align=center>" +
                "<td width=140><input type=text size=14 id=BasicServiceTitle" + i + " class=controlContent onchange=updateTitle(\"BS\","+i+")></td>" + 
                "<td width=115>" + 
                    "<select name=BasicServiceLength" + i + " class=controlContent onchange=calculate();>" +
                        "<option value=1 selected>1-30 min</option>" + 
                        "<option value=2>31-60 min</option>" + 
                        "<option value=3>61-90 min</option>" +
                        "<option value=4>91-120 min</option>" +
                    "</select>" +
                "</td>" +
                "<td width=115>" +
                    "<input type=checkbox name=BasicServiceMenu" + i + " class=controlContent onclick=calculate();>" +
                "</td>" + 
                "<td width=110>" +
                    "<select name=BasicServiceMarker" + i + " class=controlContent onchange=updateMarker(\"BS\"," + i + ");calculate();>" +
                        "<option value=0 selected>0</option>" +
                        "<option value=1>1</option>" +
                        "<option value=2>2</option>" +
                        "<option value=3>3</option>" +
                        "<option value=4>4</option>" +
                        "<option value=5>5</option>" +
                        "<option value=6>6</option>" +
                        "<option value=7>7</option>" +
                        "<option value=8>8</option>" +
                        "<option value=9>9</option>" +
                        "<option value=10>10</option>" +
                "</td>" +
                "<td width=110>" +
                    "<select name=BasicServiceCopy" + i + " class=controlContent onchange=calculate();>" +
                        "<option value=0 selected>0</option>" +
                        "<option value=1>1</option>" +
                        "<option value=2>2</option>" +
                        "<option value=3>3</option>" +
                        "<option value=4>4</option>" +
                        "<option value=5>5</option>" +
                        "<option value=6>6</option>" +
                        "<option value=7>7</option>" +
                        "<option value=8>8</option>" +
                        "<option value=9>9</option>" +
                        "<option value=10>10</option>" +
                    "</select>" +
                "</td>" +        
                "<td width=70 align=right><span id=BSPrice" + i + ">&nbsp</span></td>" +
            "</tr>" +                 
            "</table>";
    }                

    document.getElementById("BasicServiceHeading").innerHTML = heading;
    document.getElementById("BasicServiceContent").innerHTML = out;            
    
    ServiceInstruction( "BS" );
}

function enhancedServiceList() {

    // Initialize variables
    enhancedServiceNumber = document.orderform.EnhancedServiceNumber.value * 1.0;
    out = "";
    heading = "";

    if( enhancedServiceNumber > 0 ) {
        heading = 
            "<table width=100% class=tableContent cellspacing=0 cellpadding=0>" +
                "<tr align=center>" +
                    "<td width=110>Title Name</td>" + 
                    "<td width=90>Extra Video Time</td>" + 
                    "<td width=105>Extra Video Segments</td>" + 
                    "<td width=85>Standard Menus</td>" + 
                    "<td width=100>Extra Markers</td>" + 
                    "<td width=100>Extra Copies</td>" + 
                    "<td width=70 align=right>Price</td>" + 
                "</tr>"+
            "</table>";    
    }    

    for( i=1; i<=enhancedServiceNumber; i++ ) {
        out = out + 
            "<table width=100% class=tableContent cellspacing=0 cellpadding=0>" +
            "<tr align=center>" +
                "<td width=110><input type=text size=12 name=EnhancedServiceTitle" + i + " class=controlContent onchange=updateTitle(\"ES\","+i+")></td>" +
                "<td width=90><input type=text size=3 name=EnhancedServiceTime" + i + " class=controlContent maxlength=3 onchange=calculate();></td>" +             
                "<td width=105>" +
                    "<select name=EnhancedServiceSegment" + i + " class=controlContent onchange=updateSegment(\"ES\","+i+");calculate();>" +
                        "<option value=0 selected>0</option>" +
                        "<option value=1>1</option>" + 
                        "<option value=2>2</option>" + 
                        "<option value=3>3</option>" + 
                        "<option value=4>4</option>" + 
                        "<option value=5>5</option>" + 
                        "<option value=6>6</option>" + 
                        "<option value=7>7</option>" + 
                        "<option value=8>8</option>" + 
                        "<option value=9>9</option>" + 
                        "<option value=10>10</option>" + 
                    "</select>" + 
                "</td>" + 
                "<td width=85><input type=checkbox name=EnhancedServiceMenu" + i + " class=controlContent onclick=calculate();></td>" +
                "<td width=100>" +
                    "<select name=EnhancedServiceMarker" + i + " class=controlContent onchange=updateMarker(\"ES\"," + i + ");calculate();>" +
                        "<option value=0 selected>0</option>" + 
                        "<option value=1>1</option>" + 
                        "<option value=2>2</option>" + 
                        "<option value=3>3</option>" + 
                        "<option value=4>4</option>" + 
                        "<option value=5>5</option>" + 
                        "<option value=6>6</option>" + 
                        "<option value=7>7</option>" + 
                        "<option value=8>8</option>" + 
                        "<option value=9>9</option>" + 
                        "<option value=10>10</option>" + 
                    "</select>" +          
                "</td>" + 
                "<td width=100>" + 
                    "<select name=EnhancedServiceCopy" + i + " class=controlContent onchange=calculate();>" +
                        "<option value=0 selected>0</option>" +
                        "<option value=1>1</option>" + 
                        "<option value=2>2</option>" + 
                        "<option value=3>3</option>" + 
                        "<option value=4>4</option>" + 
                        "<option value=5>5</option>" +
                        "<option value=6>6</option>" + 
                        "<option value=7>7</option>" + 
                        "<option value=8>8</option>" + 
                        "<option value=9>9</option>" + 
                        "<option value=10>10</option>" + 
                    "</select>" +                                         
                "</td>" + 
                "<td width=70 align=right><span id=ESPrice" + i + ">&nbsp</span></td>" + 
               "</tr>";
    }

    document.getElementById("EnhancedServiceHeading").innerHTML = heading;
    document.getElementById("EnhancedServiceContent").innerHTML = out; 

    ServiceInstruction( "ES" );
}

function basicServicePrice() {
    
    // Initialize constant variables
    videoBase    = 20.00;
    standardMenu = 10.00;
    extraMarker  = 1.00;
    extraCopy    = 15.00;
    
    // Initialize variables
    titleNumber = document.orderform.BasicServiceNumber.value;
    basicServiceTotal = 0.00;
    
    for( i=1; i<=titleNumber; i++ ) {
        titleTotal = 0;
        videoIndex  = document.getElementById("BasicServiceLength"+i).value;
        menuIndex   = document.getElementById("BasicServiceMenu"+i).checked.toString();
        markerIndex = document.getElementById("BasicServiceMarker"+i).value;
        copyIndex   = document.getElementById("BasicServiceCopy"+i).value;
        titleTotal = (videoIndex*videoBase) + (markerIndex*extraMarker) + (copyIndex*extraCopy);
        if( menuIndex == "true" )
            titleTotal = titleTotal + standardMenu;
            
        document.getElementById("BSPrice"+i).innerHTML = formatAsMoney(titleTotal);
        basicServiceTotal = basicServiceTotal + titleTotal;
    }
    
    document.getElementById("BSTPrice").innerHTML = formatAsMoney(basicServiceTotal);
    return basicServiceTotal;
}

function enhancedServicePrice() {

    // Initialize constant variables
    baseCharge   = 20.00;
    standardMenu = 10.00;
    extraTime    = 1.00;
    extraSegment = 5.00;
    extraMarker  = 1.00;
    extraCopy    = 15.00;
    
    // Initialize variables
    titleNumber = document.orderform.EnhancedServiceNumber.value;
    enhancedServiceTotal = 0.00;

    for( i=1; i<=titleNumber; i++ ) {       
        timeIndex    = document.getElementById("EnhancedServiceTime"+i).value * 1.00;
        segmentIndex = document.getElementById("EnhancedServiceSegment"+i).value * 1.00;
        menuIndex    = document.getElementById("EnhancedServiceMenu"+i).checked.toString();
        markerIndex  = document.getElementById("EnhancedServiceMarker"+i).value * 1.00;
        copyIndex    = document.getElementById("EnhancedServiceCopy"+i).value * 1.00;
        
        titleTotal = baseCharge + (extraTime*timeIndex) + (extraSegment*segmentIndex) + 
                     (extraMarker*markerIndex) + (extraCopy*copyIndex);
        if( menuIndex == "true" )
            titleTotal = titleTotal + standardMenu;
        
        document.getElementById("ESPrice"+i).innerHTML = formatAsMoney(titleTotal);
        enhancedServiceTotal = enhancedServiceTotal + titleTotal;
    }

    document.getElementById("ESTPrice").innerHTML = formatAsMoney(enhancedServiceTotal);
    return enhancedServiceTotal;

}

function ServiceInstruction( service ) {
    
    // Initialize variables
    out = "";            
    serviceName = "BasicService";
    serviceName1 = "Basic Service";        
    titleNumber = document.orderform.BasicServiceNumber.value;
    if( service == "ES" ) {
        serviceName = "EnhancedService";
        serviceName1 = "Enhanced Service";
        titleNumber = document.orderform.EnhancedServiceNumber.value;
    }
    
    if( titleNumber > 0 ) {
        for( i=1; i<=titleNumber; i++ ) {
            out = out +
                "<br>" + 
                "<table width=640 cellpadding=0 cellspacing=0 class=instructionBox>" +
                    "<tr>" +
                        "<td>" +
                            "<b>" + serviceName1 + " Title: <span id=" + serviceName + "InstructionTitle" + i + "></span></b>" +
                        "</td>" +
                    "</tr>" +
                    "<tr align=center>" + 
                        "<td>" +
                            "<span id=" + serviceName + "InstructionSegment" + i + "></span>" +                            
                        "</td>" +
                    "</tr>" +                    
                    "<tr align=center>" + 
                        "<td>" +
                            "<span id=" + serviceName + "InstructionMarker" + i + "></span>" +
                        "</td>" +
                    "</tr>" +
                "</table>";
        }
    }
    
    document.getElementById( serviceName + "Instruction" ).innerHTML = out;                

    // Update instruction sections    
    for( t=1; t<=titleNumber; t++ ) {
        updateSegment( service, t );        
        updateMarker( service, t );        
    }        
}

function updateTitle( service, index ) {
    
    serviceName = "BasicService";
    if( service == "ES" )
        serviceName = "EnhancedService";

    document.getElementById( serviceName + "InstructionTitle" + index ).innerHTML
        = document.getElementById( serviceName + "Title" + index ).value;
}

function updateMarker( service, index ) {

    out = "";
    extraMarker = 0.00;        
    serviceName = "BasicService";
    
    if( service == "ES" ) {
        serviceName = "EnhancedService";
        extraMarker = 5.00 + document.getElementById( serviceName + "Marker" + index ).value * 1.00;
;
    }


    if( extraMarker > 0 ) {                                                      
        out = out +
            "<table width=100% cellspacing=0 cellpadding=0 class=tableContent>" +
                "<tr align=center>" +
                    "<td colspan=5>Extra Chapter Markers</td>" +
                "</tr>" ;
            for( j=0; j<extraMarker; j+=5 ) {
                out = out +
                "<tr align=center>" +
                    "<td width=20%><input type=text size=15 class=controlContent></td>" +
                    "<td width=20%><input type=text size=15 class=controlContent></td>" +
                    "<td width=20%><input type=text size=15 class=controlContent></td>" +
                    "<td width=20%><input type=text size=15 class=controlContent></td>" +
                    "<td width=20%><input type=text size=15 class=controlContent></td>" +
                "</tr>";
            }
        out = out + "</table>";                                                                                                                                                     
    }
    
    document.getElementById( serviceName + "InstructionMarker" + index ).innerHTML = out;
}

function updateSegment( service, index ) {

    out = "";        
    segmentNumber = 1;
    serviceName = "BasicService";
    if( service == "ES" ) {
        serviceName = "EnhancedService";
        segmentNumber = 2.00 + document.getElementById( serviceName + "Segment" + index ).value * 1.00;
    }        

    if( segmentNumber > 0 ) {                                                      
        out = out +
            "<table width=100% cellspacing=0 cellpadding=0 class=tableContent>" +
                "<tr align=center>" +
                    "<td colspan=4>Video Segments</td>" +
                "</tr>" +                        
                "<tr align=center>" +
                    "<td width=25%>Tape ID</td>" +
                    "<td width=25%>Start Time</td>" +
                    "<td width=25%>Stop Time</td>" +
                    "<td width=25%>Video Length</td>" +
                "</tr>";
                for( i=1; i<=segmentNumber; i++ ) {
                out = out + 
                "<tr align=center>" +
                    "<td><input type=text size=18 class=controlContent></td>" +
                    "<td><input type=text size=18 class=controlContent></td>" +
                    "<td><input type=text size=18 class=controlContent></td>" +
                    "<td><input type=text size=18 class=controlContent></td>" +
                "</tr>";
                }          
            out = out + "</table>";                    
    }
    
    document.getElementById( serviceName + "InstructionSegment" + index ).innerHTML = out;
}

function formatAsMoney(mnt) {
    mnt -= 0;
    mnt = (Math.round(mnt*100))/100;
    return (mnt == Math.floor(mnt)) ? mnt + '.00' 
            : ( (mnt*10 == Math.floor(mnt*10)) ? mnt + '0' : mnt);
}

function printOrderForm() {
    window.print();
}

function findPlayers() {
    window.location = "dvdplayers.asp?brand=" + document.dvdplayersform.brand.value;
}  

function brandFocus() {
    document.dvdplayersform.brand.focus();
}        

