// ---------------------------------------------------------------------------
// CustomerForm
// ---------------------------------------------------------------------------

function ClearCustomer() {
    with (document.CustomerForm) {
	product.value = '';
	id.value = '';
        date.value = '';
        expiry.value = '';
	numTokens.value = '';
	renewed.value = '';
        name.value = '';
        company.value = '';
        address.value = '';
        country.selectedIndex = 0;
        phone.value = '';
        fax.value = '';
        emailList.value = '';
        name2.value = '';
        company2.value = '';
        address2.value = '';
        country2.selectedIndex = 0;
        phone2.value = '';
        fax2.value = '';
        vatNumber.value = '';
	notes.value = '';
    }
}

// ---------------------------------------------------------------------------
// OrderForm
// ---------------------------------------------------------------------------

var kVatRate = 19.6;
var gProductList = null;

function CopyCustomerInformation() {
    with (document.OrderForm) {
        name2.value = name.value;
        company2.value = company.value;
        address2.value = address.value;
	country2.selectedIndex = country.selectedIndex;
        phone2.value = phone.value;
        fax2.value = fax.value;
    }
}

function Product(reference, description, priceUSD, priceEUR, priceList) {
    this.reference = reference;
    this.description = description;
    this.priceUSD = priceUSD; // must be an integer.
    this.priceEUR = priceEUR; // must be an integer.
    this.priceList = priceList;
}

function PriceListEntry(quantity, priceUSD, priceEUR) {
    this.quantity = quantity;
    this.priceUSD = priceUSD;
    this.priceEUR = priceEUR;
}

function GetPriceList(priceListSpec) {
    var entries = priceListSpec.split(':');
    var count = entries.length;
    var priceList = new Array(count);

    for (var i=0 ; i<count ; ++i) {
	var fields = entries[i].split(';');
	var quantity = parseInt(fields[0]);
	var priceUSD = parseInt(fields[1]);
	var priceEUR = parseInt(fields[2]);
	priceList[i] = new PriceListEntry(quantity, priceUSD, priceEUR);
    }

    return priceList;
}

function GetProductList() {
    if (gProductList == null) {
        var csv = document.OrderForm.productList.value;

	var records = csv.split(',');
	var count = records.length;
	gProductList = new Array(count);

	for (var i = 0; i < count; ++i) {
            var record = records[i].split('|');

	    var priceList = null;
	    if (record[5] != "")
		priceList = GetPriceList(record[5]);

	    gProductList[i] = new Product(record[0], record[1], 
	                                  parseInt(record[2]), 
                                          parseInt(record[3]),
					  priceList);
        }
    }

    return gProductList;
}

function GetPriceUSD(product, quantity) {
    var price = product.priceUSD;

    if (product.priceList != null) {
	var count = product.priceList.length;
	for (var i=0 ; i<count ; ++i) {
	    if (quantity < product.priceList[i].quantity)
		break;
	    price = product.priceList[i].priceUSD;
	}
    }

    return price;
}

function GetPriceEUR(product, quantity) {
    var price = product.priceEUR;

    if (product.priceList != null) {
	var count = product.priceList.length;
	for (var i=0 ; i<count ; ++i) {
	    if (quantity < product.priceList[i].quantity)
		break;
	    price = product.priceList[i].priceEUR;
	}
    }

    return price;
}

function NormalizeOrderedProducts() {
    var products = new Array(5);
    var quantities = new Array(5);
    var numProducts = 0;

    with (document.OrderForm) {
	var i, j;

        for (i=1 ; i<=5 ; ++i) {
            var productSelect = eval("document.OrderForm.product" + i);
            var quantityText = eval("document.OrderForm.quantity" + i);

	    var selected = productSelect.selectedIndex;
	    if (selected == 0) continue;

	    var qty = parseInt(quantityText.value);
	    if (isNaN(qty) || qty <= 0) qty = 1;

	    for (j=0 ; j<numProducts ; ++j) {
		if (products[j] == selected) {
		    quantities[j] += qty;
		    selected = 0;
		    break;
		}
	    }

	    if (selected != 0) {
		products[numProducts] = selected;
		quantities[numProducts] = qty;
		numProducts += 1;
	    }
	}

	for (i=0, j=1 ; i<numProducts ; ++i, ++j) {
            var productSelect = eval("document.OrderForm.product" + j);
            var quantityText = eval("document.OrderForm.quantity" + j);
	    productSelect.selectedIndex = products[i];
	    quantityText.value = quantities[i];
	}

	for ( ; i<5 ; ++i, ++j) {
            var productSelect = eval("document.OrderForm.product" + j);
            var quantityText = eval("document.OrderForm.quantity" + j);
	    productSelect.selectedIndex = 0;
	    quantityText.value = 1;
	}
    }

    UpdateOrderedProducts();
}

function UpdateOrderedProducts() {
    var products = GetProductList();
    var computedTotalUSD = 0;
    var computedTotalEUR = 0;

    with (document.OrderForm) {
        for (var i = 1; i <= 5; ++i) {
            var quantityText = eval("document.OrderForm.quantity" + i);
            var referenceText = eval("document.OrderForm.reference" + i);
            var productSelect = eval("document.OrderForm.product" + i);
            var unitPriceUSDText = eval("document.OrderForm.unitPriceUSD" + i);
            var unitPriceEURText = eval("document.OrderForm.unitPriceEUR" + i);
            var priceUSDText = eval("document.OrderForm.priceUSD" + i);
            var priceEURText = eval("document.OrderForm.priceEUR" + i);

	    var selected = 
                productSelect.options[productSelect.selectedIndex].value;
	    if (selected == "-") {
                referenceText.value = "";
		unitPriceUSDText.value = "";
		unitPriceEURText.value = "";
                priceUSDText.value = "";
	        priceEURText.value = "";
                continue;
            }

	    var selectedProduct;
	    for (var j = 0; j < products.length; ++j) {
	        if (products[j].reference == selected) {
		    selectedProduct = products[j];
		    break;
                }
            }

	    var qty = parseInt(quantityText.value);
	    if (isNaN(qty) || qty <= 0) {
                qty = 1;
	        quantityText.value = 1;
            }

	    referenceText.value = selectedProduct.reference;

	    var unitPrice = GetPriceUSD(selectedProduct, qty);
	    var price = unitPrice * qty;
	    unitPriceUSDText.value = unitPrice;
	    priceUSDText.value = price;
	    computedTotalUSD += price;

	    unitPrice = GetPriceEUR(selectedProduct, qty);
	    price = unitPrice * qty;
	    unitPriceEURText.value = unitPrice;
	    priceEURText.value = price;
	    computedTotalEUR += price;
        }

        totalUSD.value = computedTotalUSD;
        totalEUR.value = computedTotalEUR;
    }
}
