/* 
 * For Chutters - if price of product is 9.29, display an option to use pounds and half pounds
 *
 */

/* CONSTANTS
    Used on other pages and includes:
    product, basket
*/
var YARDS = "Yards";
var QTY = "Quantity";

function formatQty(checker,quant)
{
    if(isInYards(checker))
    {
        return yardsQty(quant) + " lbs";
    }
    
    return quant;
    
}

function formatDiscount(checker,aPrice)
{
    
    return aPrice;
    
}

function formatPrice(checker,aPrice)
{
    if(isInYards(checker))
    {
        return aPrice*2;
    }
    
    return aPrice;
    
}

function isInYards(checker)
{
    if(checker.toUpperCase() == "LBS")
    {
        return true;
    }
    return false;
}

function yardsQty(quant)
{
    //1 quant = 1/4 yard
    var qty = parseInt(quant/2);
    if(quant%2 == 1) 
    {
        if (qty != 0)
            qty += " 1/2";
        else
            qty = "1/2";
    }
   
    return qty;
}

function getYardOptions(inputName,selectedQty,isprodpage)
{
    if(selectedQty == null) selectedQty = 2; //1/2 yard
    
    var wholeQty = parseInt(selectedQty/2);
    var fractQty = selectedQty%2;
	
    var optionsOutput = "";
	if(isprodpage){
		optionsOutput += '<input type="text" id="'+inputName+'_0" name="'+inputName+'_0" value="0" size="1" onchange="updateSelectedQty(\''+inputName+'\');" />';
	}else{
		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+'\');">';
   
   if(isprodpage){
		optionsOutput += '<option value="1" ' + (fractQty == 1 ? "selected":"") + '>1/2</option>';
		optionsOutput += '<option value="0">-</option>';
	}else{
		//console.log(fractQty);
		optionsOutput += '<option value="0">-</option>';
		optionsOutput += '<option value="1" ' + (fractQty == 1 ? "selected":"") + '>1/2</option>';
   }

	optionsOutput += '</select>';
	
	optionsOutput += '<input type="hidden" id="'+inputName+'" name="'+inputName+'" value="'+selectedQty+'"/>';
  
    return optionsOutput;
}

function updateSelectedQty(inputName)
{
    document.getElementById(inputName).value = parseInt(document.getElementById(inputName+'_0').value*2)+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);
}
function formatItemTotal(checker, quant, price, total, discount){
	if(isInYards(checker))
    {
		if(discount != "null" || discount != "0"){
			//console.log(discount == 0);
			return ((quant * price) - discount);
			
		}else{
			return (quant * price);
		}
		
        //return (parseFloat(quant/.5) * price);
    }else{
		
		return total;
	}
}

