
//100 CDs
var current_qty;
//AAC 128kbps Native iTunes / iPod Format
var current_format;
//keeps track of total data size in GB
var current_total_GB;

var custom_btn_already_checked;

function init( qty, format, total_GB, is_custom ) 
{
    current_qty = qty;
    current_format = format;
    current_total_GB = total_GB;

    var format_title = getFormatTitle("AAC_128");
    
    document.getElementById("alerts").style.display = 'none';
    document.getElementById("format_type").innerHTML = format_title;
    document.getElementById("total_GB").value = current_total_GB;
    
    if( is_custom ) {
        custom_btn_already_checked = true;   
    }
}


function getCustomQuote()
{
    var input_custom_qty = document.getElementById("custom_qty");
    var custom_qty = input_custom_qty.value;
    var span_quote_text = document.getElementById("quote_text");
    var quote = 'XXX';

    //if what was entered is not numeric   
    if( custom_qty != parseInt(custom_qty) ) {
        alert('This value is not numeric.\n\nPlease try again.');
    }    
    //else if what was entered is not a multiple of 25
    else if( (custom_qty % 25) > 0 ) {
        alert('This number is not a multiple of 25.\n\nPlease try again.');
    }
    else if( custom_qty <= 0 ) {
        alert('This value cannot be chosen.\n\nPlease try again.');   
    }  
    else {
    	//PRICING 
        if( custom_qty <= 50 ) {
            quote = "37.50"; 
        }
        else if( custom_qty > 50 && custom_qty <= 1000 ) {
            quote = (custom_qty * .75 );
            quote = getMoneyFormat(quote);
        } else {
            quote = "XXX";   
        }
        
        calcSize('custom_qty', 'NULL');
        
        //show the continue button and realtime update section once a valid number has been entered
        document.getElementById("btn-submit").style.display = 'block';
        document.getElementById("alerts").style.display = 'block';
        
        if( custom_qty > 1000 ) {
            document.getElementById("storage_recommendation").innerHTML = '<span class="colorBlack">The size of the selected options require that you <strong><a href="sales.php?mode=custom">place a custom order</a></strong>.</span> ';
            document.getElementById("custom-order-link").innerHTML = '<span class="colorBlack">The size of the selected options require that you <strong><a href="sales.php?mode=custom">place a custom order</a></strong>.</span> ';
            document.getElementById("btn-submit").style.display = 'none';
            quote = "XXX";
        }
        else if( current_qty > 1000 ) {
            document.getElementById("storage_recommendation").innerHTML = '<span class="colorBlack">The size of the selected options require that you <strong><a href="sales.php?mode=custom">place a custom order</a></strong>.</span> ';
            document.getElementById("custom-order-link").innerHTML = '<span class="colorBlack">The size of the selected options require that you <strong><a href="sales.php?mode=custom">place a custom order</a></strong>.</span> ';
            document.getElementById("btn-submit").style.display = 'none';
            quote = "XXX";
        }
    }
    quote = "$" + quote;
    
    span_quote_text.innerHTML = quote;
    
    
}



/***************************************
Function that refreshes the cookies 
that hold price and the text that tells
the user which storage option they 
should choose. 
***************************************/
function calcSize( mode, value ) 
{
    var orig_current_qty //only used when mode is 'custom_input_click'
    
    if( mode == 'qty' ) {
        current_qty = value;
        

        //if a non-custom qty's button has been clicked
        //then show the continue button and the realtime update section
        document.getElementById("btn-submit").style.display = 'none';
        //document.getElementById("alerts").style.display = 'none'; 
        
        var arrSpan = document.getElementsByTagName("span");
        
        for(i = 0; i < arrSpan.length; i++) {
            
            if( arrSpan[i].id.substring(0, 5) == "price" ) {
                arrSpan[i].style.display = "none";   
            }   
        }
        
        switch( value ) {
            case 50: document.getElementById("price50").style.display = "block"; break;
            case 75: document.getElementById("price75").style.display = "block"; break;
            case 100: document.getElementById("price100").style.display = "block"; break;
            case 125: document.getElementById("price125").style.display = "block"; break;
            case 150: document.getElementById("price150").style.display = "block"; break;
            case 175: document.getElementById("price175").style.display = "block"; break;
            case 200: document.getElementById("price200").style.display = "block"; break;
            case 225: document.getElementById("price225").style.display = "block"; break;
            case 250: document.getElementById("price250").style.display = "block"; break;
            case 275: document.getElementById("price275").style.display = "block"; break;
            case 300: document.getElementById("price300").style.display = "block"; break;
            case 325: document.getElementById("price325").style.display = "block"; break;
            case 350: document.getElementById("price350").style.display = "block"; break;
            case 375: document.getElementById("price375").style.display = "block"; break;
            case 400: document.getElementById("price400").style.display = "block"; break;
            case 425: document.getElementById("price425").style.display = "block"; break;
            case 450: document.getElementById("price450").style.display = "block"; break;
            case 475: document.getElementById("price475").style.display = "block"; break;
            case 500: document.getElementById("price500").style.display = "block"; break;
            case 525: document.getElementById("price525").style.display = "block"; break;
            case 550: document.getElementById("price550").style.display = "block"; break;  
            case 575: document.getElementById("price575").style.display = "block"; break;
            case 600: document.getElementById("price600").style.display = "block"; break;
            case 625: document.getElementById("price625").style.display = "block"; break;
            case 650: document.getElementById("price650").style.display = "block"; break;
            case 675: document.getElementById("price675").style.display = "block"; break;
            case 700: document.getElementById("price700").style.display = "block"; break;
            case 725: document.getElementById("price725").style.display = "block"; break;
            case 750: document.getElementById("price750").style.display = "block"; break;
            case 775: document.getElementById("price775").style.display = "block"; break;
            case 800: document.getElementById("price800").style.display = "block"; break;
        }
	
	var arrInputFormats = document.getElementsByTagName("input");

    	//for (i = 0; i < arrInputFormats.length; i++) {
        //    if( arrInputFormats[i].name == "format" ) {
    //		    arrInputFormats[i].checked = ""  ;
    //		}
    //	}

    } else if( mode == 'format' ) {
        current_format = value;   
        //if a non-custom qty's button has been clicked
        //then show the continue button and the realtime update section
        document.getElementById("btn-submit").style.display = 'block';
        document.getElementById("alerts").style.display = 'block'; 
    } 
    
    
    //array of all input fields
    var arrInputs = document.getElementsByTagName("input");  
      
    //loop through each input and filter out the radio buttons associated with quantity and format
    for (i = 0; i < arrInputs.length; i++) {
        if( mode == 'qty' ) {
            if( arrInputs[i].name == "format" ) {
    		    if( arrInputs[i].checked == "checked" ) {
    		        current_format = arrInputs[i].value;   
    			var format_title = getFormatTitle(current_format);
    			document.getElementById("format_type").innerHTML = format_title;
    		    }   
    		}
    	}
        else if( mode == 'format' ) {
    		if( arrInputs[i].name == "cd_qty" ) {
    		    if( arrInputs[i].checked == true ) {
    		        //if the custom option has been checked, then retrieve the custom quantity that has been entered
    		        if( arrInputs[i].value == "CUSTOM" ) {
    		            var custom_qty = document.getElementById('custom_qty').value;
    		            current_qty = custom_qty;
    		        } else {
    		            current_qty = arrInputs[i].value;
    		        }
    		    }   
    		}
        }
        else if( mode == "custom_qty" ) {
            var custom_qty = document.getElementById("custom_qty").value;
            current_qty = custom_qty;   
        }
	}
	
	var size_per_100 = getSizePer100(current_format); 
    var total_MB = (current_qty / 100) * size_per_100;
    var total_GB = total_MB / 1024;
    
    //alert( "QTY: " + current_qty + "\nSizePer100: " + size_per_100 + "\nTotalMB: " + total_MB + "\nTotalGB: " + total_GB ); 
    
    //if the total size exceeds 40GB, then hide the DVD storage option
    if( total_GB >= 40 ) {
        document.getElementById("storage-DVD").style.display = "none";
        document.getElementById("radio-harddrives").checked = true;
        showHideHarddrives( 'show' )
    } else {
        document.getElementById("storage-DVD").style.display = "block";   
    }  
    
    //get the proper title for the format and then update the text which 
    //tells the customer what size hard drive to get    
    var format_title = getFormatTitle(current_format);
    
    document.getElementById("cd_count").innerHTML = current_qty;
    document.getElementById("format_type").innerHTML = format_title;
    
    total_GB = Math.round(total_GB);
    current_total_GB = total_GB;
    
    filterHarddrives( current_total_GB );
    
    //update the total size of the order
    document.getElementById("total_GB").value = total_GB;
    
    document.getElementById("min_storage_size").innerHTML = total_GB + "GB";
    
    if( total_GB >= 40 ) {
        if( total_GB >= 40 && total_GB < 72 ) {
            document.getElementById("storage_recommendation").innerHTML = "<strong>80GB Harddrive</strong>";
            document.getElementById("btn-submit").style.display = 'block';
        }
        else if( total_GB >= 72 && total_GB < 146 ) {
            document.getElementById("storage_recommendation").innerHTML = "<strong>160GB Harddrive</strong>";
            document.getElementById("btn-submit").style.display = 'block';
        }
        else if( total_GB >= 146 && total_GB < 225 ) {
            document.getElementById("storage_recommendation").innerHTML = "<strong>250GB Harddrive</strong>";
            document.getElementById("btn-submit").style.display = 'block';
        } 
        else if( total_GB >= 225 && total_GB < 270 ) {
            document.getElementById("storage_recommendation").innerHTML = "<strong>300GB Harddrive</strong>";
            document.getElementById("btn-submit").style.display = 'block';
        } 
        else if( total_GB >= 270 && total_GB < 450 ) {
            document.getElementById("storage_recommendation").innerHTML = "<strong>500GB Harddrive</strong>";
            document.getElementById("btn-submit").style.display = 'block';
        } 
        else if( total_GB > 450 ) {
            document.getElementById("storage_recommendation").innerHTML = '<span class="colorBlack">The size of the selected options require that you <strong><a href="sales.php?mode=custom">place a custom order</a></strong>.</span> ';
            document.getElementById("btn-submit").style.display = 'none';
        } 
        else if( current_qty > 1000 ) {
        	document.getElementById("storage_recommendation").innerHTML = '<span class="colorBlack">The size of the selected options require that you <strong><a href="sales.php?mode=custom">place a custom order</a></strong>.</span> ';
            document.getElementById("btn-submit").style.display = 'none';
        }
    } 
    else if( current_qty > 1000 ) {
        	document.getElementById("storage_recommendation").innerHTML = '<span class="colorBlack">The size of the selected options require that you <strong><a href="sales.php?mode=custom">place a custom order</a></strong>.</span> ';
            document.getElementById("btn-submit").style.display = 'none';
        }
    else {
        document.getElementById("storage_recommendation").innerHTML = "<strong>DVDs</strong>"
        document.getElementById("btn-submit").style.display = 'block';   
    }
     
    //if the custom qty's button has been clicked
    //then hide the continue button and the realtime update section 
    //until the "get custom quote" button has been clicked
    if( mode == 'custom_input_click' ) {
        
        if( document.getElementById("radio_custom_qty").checked == true ) { 
            //don't hide if the button was already selected before it was clicked
            if( !custom_btn_already_checked ) {
                document.getElementById("btn-submit").style.display = 'none';
                document.getElementById("alerts").style.display = 'none';
            }
        }
    }
}


/************************************
Function that shows the custom
quantity input field and "Get Quote"
button when the custom input button
is clicked.
************************************/
function refreshCustomQuote(disabled_option, display_option) 
{
    var input_qty = document.getElementById("custom_qty");
    var span_btn = document.getElementById("btn_get_quote");
    
    input_qty.disabled = disabled_option;
    //input_qty.style.disabled = "false";
    span_btn.style.display = display_option;
    
}

//gets the average size for 100 CDs of the given format
function getSizePer100( current_format )
{
    var size_per_100;
    
    switch( current_format ) {
        case "MP3_192": size_per_100 = 8192; break;
        case "MP3_224": size_per_100 = 9216; break;
        case "MP3_320": size_per_100 = 12800; break;
        case "AAC_128": size_per_100 = 5120; break;
        case "AAC_192": size_per_100 = 8192; break;
        case "APPLE_LOSS": size_per_100 = 33792; break; 
        case "WAV": size_per_100 = 51200; break; 
    }
   
    return size_per_100;
}

function getFormatTitle( current_format )
{
    var title;
    
    switch( current_format ) {
        case "MP3_192": title = "MP3 192kbps"; break;
        case "MP3_224": title = "MP3 224kbps"; break;
        case "MP3_320": title = "MP3 320kbps"; break;
        case "AAC_128": title = "AAC 128kbps"; break;
        case "AAC_192": title = "AAC 192kbps"; break;
        case "APPLE_LOSS": title = "Apple Lossless"; break; 
        case "WAV": title = "WAV"; break; 
    }
   
    return title;
}

function showHideHarddrives( mode ) {
    
    if( mode == "show" ) {
        document.getElementById("harddrives").style.display = "block";
        
        if( current_total_GB <= 72 ) {
            document.getElementById("hd-80").style.display = "block";
        } 
        if( current_total_GB <= 146 ) {
            document.getElementById("hd-160").style.display = "block";
        } 
        if( current_total_GB <= 225 ) {
            document.getElementById("hd-250").style.display = "block";
        } 
        if( current_total_GB <= 270 ) {
            document.getElementById("hd-300").style.display = "block";
        } 
        if( current_total_GB <= 450 ) {
            document.getElementById("hd-500").style.display = "block";
        } 
        if( current_total_GB > 450 ) {
            document.getElementById("custom_order_required").style.display = 'block';
        } 
    } else {
        document.getElementById("harddrives").style.display = "none";   
    }   
}

function filterHarddrives( total_GB ) {
    
    //this order requires a harddrive, only show harddrives that
    //are sufficiently large to hold the total order
    if( total_GB >= 40 ) {
        
        if( total_GB < 72 ) {
            document.getElementById("hd-80").style.display = "block";
        } else if( total_GB >= 72 ) { document.getElementById("hd-80").style.display = "none"; }
        
        if( total_GB <= 146 ) {
            document.getElementById("hd-160").style.display = "block";
        } else if( total_GB > 146 ) { document.getElementById("hd-160").style.display = "none"; }
        
        if( total_GB <= 225 ) {
            document.getElementById("hd-250").style.display = "block";
        } else if( total_GB > 225 ) { document.getElementById("hd-250").style.display = "none"; }
        
        if( current_total_GB <= 270 ) {
            document.getElementById("hd-300").style.display = "block";
        } else if( total_GB > 270 ) { document.getElementById("hd-300").style.display = "none"; }
        
        if( current_total_GB <= 450 ) {
            document.getElementById("hd-500").style.display = "block";
        } else if( total_GB > 450 ) { document.getElementById("hd-500").style.display = "none"; }
        
        if( current_total_GB > 450 ) {
            document.getElementById("custom_order_required").style.display = 'block';
        } else { document.getElementById("custom_order_required").style.display = 'none'; }
    } 
    //this order doesn't require a harddrive
    else {
        if( document.getElementById("radio-harddrives").checked == true ) {
            document.getElementById("hd-80").style.display = "block"; 
            document.getElementById("hd-160").style.display = "block"; 
            document.getElementById("hd-250").style.display = "block"; 
            document.getElementById("hd-300").style.display = "block";
            document.getElementById("hd-500").style.display = "block";
            document.getElementById("custom_order_required").style.display = 'none';
        }
        else {
            document.getElementById("harddrives").style.display = "none";   
        }
    }
}
