function setPaypalFields() {
    var HidElem;
    var ItemName = 'Hair Fix Order: ';
    var currentQty = 0;
    
    currentQty = getQty('white');
    if (currentQty > 0) {
        ItemName += currentQty + '-White; ';
    }
    
    currentQty = getQty('black');
     if (currentQty > 0) {
        ItemName += currentQty + '-Black; ';
    }    
    currentQty = getQty('brown');
     if (currentQty > 0) {
        ItemName += currentQty + '-Brown; ';
    }    
//    currentQty = getQty('red');
//     if (currentQty > 0) {
//        ItemName += currentQty + '-Red; ';
//    }
    currentQty = getQty('brush');
     if (currentQty > 0) {
        ItemName += currentQty + '-Brush; ';
    }
    currentQty = getQty('brown_brush');
     if (currentQty > 0) {
        ItemName += currentQty + '-Brown Brush Combo; ';
    }
    currentQty = getQty('black_brush');
     if (currentQty > 0) {
        ItemName += currentQty + '-Black Brush Combo; ';
    }
    currentQty = getQty('white_brush');
     if (currentQty > 0) {
        ItemName += currentQty + '-White Brush Combo; ';
    }
    
    HidElem = document.getElementById('hidItemName');
    HidElem.value = ItemName;
    
    HidElem = document.getElementById('hidShipping');
    HidElem.value = computeTotalShipping().toFixed(2);
    
    HidElem = document.getElementById('hidAmount');
    HidElem.value = computeTotalProductPrice().toFixed(2);
}   

function displayTotal() {
    var totalCell;
    totalCell = document.getElementById('total');
    totalCell.innerHTML = '$' + computeTotalPrice();
    setPaypalFields();
}

function computeTotalPrice() {
    var totalPrice = 0;
    totalPrice += parseFloat(computeTotalShipping());
    totalPrice += parseFloat(computeTotalProductPrice());
    return totalPrice.toFixed(2);
}

function computeTotalShipping() {
    var chkAddShipping = document.getElementById('chkAddShipping');
 
    if (chkAddShipping.checked) {
        var shippingPrice = 0;
        var totalQty = getTotalQty();
        
        if (totalQty == 0) {
            shippingPrice = 0;
        }
        
        if (totalQty == 1) {
            shippingPrice = 3;
        }
         
        
        if (totalQty > 1) {
            shippingPrice += 3;
            shippingPrice += totalQty - 1;
        }
        
    }
    else {
     shippingPrice = 1;
    }
    
    return parseFloat(shippingPrice);
}

function computeTotalProductPrice() {
    var total;
    var qty;
    var price;
    
    price = getPrice('white');
    qty = getQty('white');
    total = applySale(parseFloat(price) * parseInt(qty),price,qty,false);
    
    price = getPrice('black');
    qty = getQty('black');
    total += applySale(parseFloat(price) * parseInt(qty),price,qty,false);
    
    price = getPrice('brown');
    qty = getQty('brown');
    total += applySale(parseFloat(price) * parseInt(qty),price,qty,false);
    
//    price = getPrice('red');
//    qty = getQty('red');
//    total += applySale(parseFloat(price) * parseInt(qty),price,qty,false);
    
    price = getPrice('brush');
    qty = getQty('brush');
    total += applySale(parseFloat(price) * parseInt(qty),price,qty,false);
    
    price = getPrice('brown_brush');
    qty = getQty('brown_brush');
    total += applySale(parseFloat(price) * parseInt(qty),price,qty,false);
    
    price = getPrice('black_brush');
    qty = getQty('black_brush');
    total += applySale(parseFloat(price) * parseInt(qty),price,qty,false);
    
    price = getPrice('white_brush');
    qty = getQty('white_brush');
    total += applySale(parseFloat(price) * parseInt(qty),price,qty,false);
    
	var couponCode = getCouponCode();
	total = applyCoupon(parseFloat(total), couponCode);
	
    return parseFloat(total);
}

function getCouponCode() {
	var couponCode;
	var txtCoupon;
	txtCoupon = document.getElementById("txtCoupon");
	couponCode = txtCoupon.value;
	return couponCode.toUpperCase();
}

function applySale(fullPrice, pricePer, qty, apply) {

    var salePrice = 0;
    var workingQty = qty;
    
    if (qty < 1) {
        return 0;
    }
    
    if (qty == 1) {
        return pricePer;
    }
    
	if (apply == true) {
	    while (workingQty > 1) {
	        salePrice += 30;
	        workingQty -= 2;
	    }
	    
	    if (workingQty == 1) {
	        salePrice += pricePer;
	    }
	    
	}
	else {
	    return fullPrice;
	}
	return salePrice;
}

function applyCoupon(subtotal, couponCode) {
	
	var discountPercent = 0;

	switch (couponCode)  {
//	    case 'HF25SALE':
//	        var matchDate = new Date();
//	        matchDate.setFullYear(2009, 10, 27);
//	        if (isDate(matchDate)) {
//	            discountPercent = .25;
//	        }
//	        break;
	    case 'HFDS20':
	        discountPercent = .20;
	        break;
	}	
	if (discountPercent > 0) {
		var newTotal = 0;
		var discountAmount = 0;
		discountAmount = subtotal * discountPercent;
		newTotal = subtotal - discountAmount;
		return newTotal;
	}
	else {
		return subtotal;
	}

}
function isDate(matchDate) {
    var d = new Date;
    d.setFullYear(d.getFullYear(), d.getMonth(), d.getDate());
    if (matchDate.toDateString() == d.toDateString()) {
        return true;
    }
    else return false;
}
function getPrice(color) {
    var Price;
    
    switch (color) {
	    case 'white':
	        Price = 19.95;
	        break;
	    case 'black':
	        Price = 19.95;
	        break;
	    case 'brown':
	        Price = 19.95;
	        break;
//	    case 'red':
//	        Price = 9.95;
//	        break;
	    case 'brush':
	        Price = 15.95;
	        break;
	    case 'brown_brush':
	        Price = 29.95;
	        break;
	    case 'black_brush':
	        Price = 29.95;
	        break;
	    case 'white_brush':
	        Price = 29.95;
	        break;
	    
	}

   return parseFloat(Price);
}

function getTotalQty() {

    var totalQty = 0;
    
    //totalQty += parseInt(getQty('red'));
    totalQty += parseInt(getQty('white'));
    totalQty += parseInt(getQty('brown'));
    totalQty += parseInt(getQty('black'));
    totalQty += parseInt(getQty('brush'));
    totalQty += parseInt(getQty('brown_brush'));
    totalQty += parseInt(getQty('black_brush'));
    totalQty += parseInt(getQty('white_brush'));
    
    return parseInt(totalQty);
}

function getQty(color) {
    
    var Elem;
	var Qty;
	
	switch (color) {
	    case 'white':
	        Elem = document.getElementById('txtQuantityWhite');
	        break;
	    case 'black':
	        Elem = document.getElementById('txtQuantityBlack');
	        break;
	    case 'brown':
	        Elem = document.getElementById('txtQuantityBrown');
	        break;
//	    case 'red':
//	        Elem = document.getElementById('txtQuantityRed');
//	        break;
	    case 'brush':
	        Elem = document.getElementById('txtQuantityBrush');
	        break;
	    case 'brown_brush':
	        Elem = document.getElementById('txtQuantityBrownBrush');
	        break;
	    case 'black_brush':
	        Elem = document.getElementById('txtQuantityBlackBrush');
	        break;
	    case 'white_brush':
	        Elem = document.getElementById('txtQuantityWhiteBrush');
	        break;
	}
	
    if (Elem.value == '') {
        Qty = 0;
    }
    else {
        Qty = Elem.value;
    }
    
	return parseInt(Qty);

}

function submitOrder() {
    if (getTotalQty() > 0) document.forms[0].submit();
}

