/* 
    *********************************************************
     PAYPAL JAVASCRIPT LOGIC
     ********************************************************
*/

var counter = 0;

function updatePrice(formName, counter) {
    form = getForm(formName);
    
    size = parseInt(form.size.value);
    quantity = parseInt(form.quantity.value);
    getDiv(counter).innerHTML = "$" + getPricePerUnit(size, quantity, form);
}

function getDiv(counter) {
    divId = "total_price_" + counter;
    return document.getElementById(divId);
}

function getForm(formName) {
    return document.forms[formName];
}

function resetForm(formName, counter) {
    form = getForm(formName);
    form.size[0].selected = true;
    form.quantity[0].selected = true;
    getDiv(counter).innerHTML = "$" + getPricePerUnit(4, 12, form);
}


function buildCartInfo(item_number, colors, price_difference) {
    var formName = "cart_item_" + counter;

    if( !price_difference ) {
        price_difference = 0;
    }

    var html = "<form name='" + formName + "'>\n";
    html += "<input type='hidden' name='item_number' value='" + item_number + "'>\n";
    html += "<input type='hidden' name='price_difference' value='" + price_difference + "'>\n";


    html += "<table cellspacing='1' cellpadding='1' bgcolor='#000000' width='100%' ><tr><td>\n";
    html += "<table cellspacing='1' cellpadding='3' bgcolor='#E0E0E0' width='100%'><tr><td>\n";
    html += "<center>\n";
    html += "<table cellspacing='1' cellpadding='1'>\n";
    html += "<tr><td>Size: </td><td>\n";
    html += "<select name='size' onchange=\"updatePrice('" + formName + "', " + counter + ")\">\n";
    html += "<option value='4' selected>4 GB</option>\n";
    html += "<option value='8'>8 GB</option>\n";
    html += "<option value='16'>16 GB</option>\n";
    html += "</select></td></tr>\n";
    html += "<tr><td>Color:</td><td><select name='color'>\n";
    
    for(var i = 0; i < colors.length; i++) {
        html += "<option value='" + colors[i] + "' " + (i == 0 ? 'selected' : '') + ">"  + 
                 colors[i] + "</option>\n";
    }
 
    html += "</select> </td></tr>\n";
    html += "<tr><td>Quantity:</td><td>\n";
    html += "<select name='quantity' onchange=\"updatePrice('" + formName + "', " + counter + ")\">\n";

    for(var i = 24; i <= 100; i++) {
        html += "<option value='" + i + "' " + (i == 12 ? 'selected' : '') + ">" + i + "</option>\n";
    }

    html += "</select></td></tr></table>\n";
    html += "</form>\n";
    html += "<hr border='1' height='1' width='85%'>\n";
    html += "<center>Price Per Unit: <span id='total_price_" + counter + "'></span><p>\n";
    html += "<a href=\"javascript:void()\" onclick=\"addToCart('" + formName + "')\">";
    html += "<img src=\"http://www.paypal.com/en_US/i/btn/btn_cart_SM.gif\" border=\"0\" alt=\"Add To Cart\">";
    html += "</a><br/><br/>\n";
    html += "</td></tr></table>\n";
    html += "</td></tr></table>\n";

    document.getElementById(item_number).innerHTML = html;

    // Resets the form so browsers such as Firefox
    // return to the default selected values
    resetForm(formName, counter);

    counter++;
}

function getPricePerUnit(size, quantity, form) {
    price = 0;

    switch(size) {
		
        // 4 gigs
        case 4:
            // 1 -24 units: $19.95
            if (quantity < 25) {
                price = 19.95;
            }
			
            // 25 - 50 units: $18.95
            else if (quantity < 51) {
                price = 18.95;
            }
				
	    // 51 - 100 units: $17.95
            else {
                price =  17.95;
            }

            break;

        // 8 gigs
        case 8:
            // 1 -24 units: $31.50
            if (quantity < 25) {
                price = 29.50;
            }
			
            // 13 - 50 units: $29.50
            else if (quantity < 51) {
                price = 28.50;
            }
				
	    // 51 - 100 units: $28.50
            else {
                price = 27.25;
            }
        
            break;
    
        // 16 gigs
        case 16:
            // 1 -24 units: $47.75
            if (quantity < 25) {
                price = 39.95;
            }
			
            // 13 - 50 units: $46.75
            else if (quantity < 51) {
                price = 38.50;
            }
				
	    // 51 - 100 units: $45.75
            else {
                price = 37.25;
            }

           break;
    }

    price_difference = parseFloat(form.price_difference.value);

    price = new String(price + price_difference);
     
    if (price.length == 2) {
        return price + ".00";
    }

    if (price.length == 4) {
        return price + "0";
    }

    return price;
}
  
function addToCart(formName) {
        itemForm    = getForm(formName);
        size        = parseInt(itemForm.size.value);
        quantity    = parseInt(itemForm.quantity.value);
        color       = itemForm.color.value;
        item_number = itemForm.item_number.value;

 	paypal = getForm('paypal_form');
	paypal.item_name.value = size + "GB " + color + " USB Drive";
	paypal.item_number.value = item_number;

        price = getPricePerUnit(size, quantity, itemForm);

	paypal.os0.value = "$" + price;
	paypal.amount.value = price;
	paypal.quantity.value = quantity;

	paypal.submit();
}


function viewCart() {
 	getForm('paypal_view').submit();
}


function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function loadDocument() {
}

//window.onload = function() {loadDocument()};
