﻿/* Format qty */

/* CONSTANTS
    Used on other pages and includes:
    product, basket
*/
var YARDS = "Yards";
var QTY = "Quantity";
var CARTQTYERROR = "Fabric sold by the yard has a minimum order quantity of 1/2 yard.  Quantity has been adjusted below.";
var CARTQTYERROR2 = "Fabric sold by the yard has a minimum order quantity of 1 yard.  Quantity has been adjusted below.";

var qtyErrorMsg = "";

function formatQty(checker,quant)
{
    if(isInYards(checker))
    {
        return yardsQty(quant) + " " + YARDS;
    }
    
    return quant;
    
}

function formatDiscount(checker,aPrice)
{

    if(isInYards(checker))
    {
        return aPrice*4;
    }
    
    return aPrice;
    
}

function formatPrice(checker,aPrice)
{
    if(isInYards(checker))
    {
        return aPrice*4;
    }
    
    return aPrice;
    
}

function isInYards(checker)
{
    if(checker.toUpperCase() == "YARDS")
    {
        return true;
    }
    return false;
}

function yardsQty(quant)
{
    //1 quant = 1/4 yard
    var qty = parseInt(quant/4);

if(quant%4 == 1) 
    {
        if (qty != 0)
            qty += " 1/4";
        else
            qty = "1/4";
    }
    else if(quant%4 == 2) 
    {
        if (qty != 0)
            qty += " 1/2";
        else
            qty = "1/2";
    }
    else if(quant%4 == 3) 
    {
        if (qty != 0)
            qty += " 3/4";
        else
            qty = "3/4";
    }
    return qty;
}

function getYardOptions(inputName,selectedQty,isOnSale)
{
	if(isOnSale == null){ isOnSale = false; }
	
	if(selectedQty == null){
		selectedQty = 4; //1 yard
    }
	// JAT 9/8/09 SUP37664 - changing minimum to 1 yard instead of 1/2 yard ONLY IF ON SALE
	if(isOnSale){
		if(selectedQty < 4) {
			selectedQty = 4; //1 yard
			//fill qty error msg
			qtyErrorMsg = CARTQTYERROR2;
		}
	}else{
		if(selectedQty == 1) {
			selectedQty = 2; //1/2 yard
			//fill qty error msg
			qtyErrorMsg = CARTQTYERROR;
		}
	}
    var wholeQty = parseInt(selectedQty/4);
    var fractQty = selectedQty%4;
    
    var optionsOutput = "";
    optionsOutput += '<input type="text" id="'+inputName+'_0" name="'+inputName+'_0" value="'+wholeQty+'" size="1" onchange="updateSelectedQty(\''+inputName+'\');" />';
    optionsOutput += ' and <select id="'+inputName+'_1" name="'+inputName+'_1" onchange="updateSelectedQty(\''+inputName+'\');">';
    optionsOutput += '<option value="0" '+ (fractQty== 0 ? "selected":"") +'>-</option>';
    optionsOutput += '<option value="1" '+ (fractQty== 1 ? "selected":"") +'>1/4</option>';
    optionsOutput += '<option value="2" '+ (fractQty== 2 ? "selected":"") +'>1/2</option>';
    optionsOutput += '<option value="3" '+ (fractQty== 3 ? "selected":"") +'>3/4</option></select>';
    optionsOutput += '<input type="hidden" id="'+inputName+'" name="'+inputName+'" value="'+selectedQty+'"/>';
    
    /*
    if(selectedQty == null) selectedQty = 1;
    
    optionsOutput += '<option value="1">1/4</option>';
    optionsOutput += '<option value="2">1/2</option>';
    optionsOutput += '<option value="3">3/4</option>';
    for(var x=4,y=1;y<=10;y++,x+=4){
        
        optionsOutput += '<option value="'+x+'" '+ (selectedQty==x ? "selected":"") +'>'+y+'</option>';
        optionsOutput += '<option value="'+(x+1)+'" '+ (selectedQty==x+1 ? "selected":"") +'>'+y+' 1/4</option>';
        optionsOutput += '<option value="'+(x+2)+'" '+ (selectedQty==x+2 ? "selected":"") +'>'+y+' 1/2</option>';
        optionsOutput += '<option value="'+(x+3)+'" '+ (selectedQty==x+3 ? "selected":"") +'>'+y+' 3/4</option>';
    }*/
	
	if (qtyErrorMsg!="") {
		//show quantity error messages
		if(document.getElementById("qtyErrorHolder")){ //there is a holder for the qty message...
			document.getElementById("qtyErrorHolder").innerHTML = qtyErrorMsg;
		}
	}
    return optionsOutput;


}

function updateSelectedQty(inputName)
{
    document.getElementById(inputName).value = parseInt(document.getElementById(inputName+'_0').value*4)+parseInt(document.getElementById(inputName+'_1').value);
}

function updateSelectedYards(inputName,selectedQty)
{
    document.getElementById(inputName+'_0').value = parseInt(selectedQty/4);
    document.getElementById(inputName+'_1').value = parseInt(selectedQty)%4;
    updateSelectedQty(inputName);
}