﻿function ChangeCoupon()
{
    COUPON_CODE = document.getElementById("codeCoupon").value;
    CountInstantQuote();
}

function ProcessPrices(costSubtotal, costPerUnit, costPerSqFoot, packageId)
{
    var fieldPackage = document.getElementById("packageId");
    var outputSubtotal = document.getElementById("outputSubtotal");
    var outputPerUnit = document.getElementById("outputPerUnit");
    var fieldPerUnit = document.getElementById("pricePerUnit");
    var fieldSubtotal = document.getElementById("priceSubtotal");
    var outputSqFoot = document.getElementById("outputSqFoot");
    
    OutputText(outputSubtotal, costSubtotal);
    OutputText(outputPerUnit, costPerUnit);
    OutputText(outputSqFoot, costPerSqFoot);
    
    if(fieldSubtotal != null)
    {
         fieldSubtotal.value = costSubtotal.replace(/,/, "");
    }
    
    if(fieldPerUnit != null)
    {
        fieldPerUnit.value = costPerUnit.replace(/,/, "");
    }    
    
    if(fieldPackage != null)
    {  
        fieldPackage.value = packageId;
    }
}

function ProcessCoupon(couponCode, costDiscount, costDiscountPercentage, costFinalPrice, couponError, isErrorCritical)
{
    var isCritical = ((isErrorCritical == "True") || (isErrorCritical == "true"));
    
    var rowSubtotal = document.getElementById("rowSubtotal");
    var rowSaving = document.getElementById("rowSaving");
    var rowYourPrice = document.getElementById("rowYourPrice");
    
     if(parseFloat(costDiscount) == 0)
    {
        rowSubtotal.style.color = "#000000";
        rowSaving.style.display = "none";
        rowYourPrice.style.display = "none";
    }
    else
    {            
        rowSubtotal.style.color = "#999999";
        rowSaving.style.display = "";
        rowYourPrice.style.display = "";
        
        var outputSavingPercentage = document.getElementById("outputSavingPercentage");
        var outputSaving = document.getElementById("outputSaving");
        var outputYourPrice = document.getElementById("outputYourPrice");
            
            
        if(parseFloat(costDiscountPercentage) > 0)
        {
            OutputText(outputSavingPercentage, "&nbsp;" + costDiscountPercentage + "%:");
        }
        else
        {
            OutputText(outputSavingPercentage, ":");
        }
        
        OutputText(outputSaving, costDiscount);           
        OutputText(outputYourPrice, costFinalPrice);           
    }
       
        
    var rowCouponError = document.getElementById("rowCouponError");
    var rowCouponRight = document.getElementById("rowCouponRight");
    var outputCouponError = document.getElementById("outputCouponError");
    var outputCouponCode = document.getElementById("outputCouponCode");
    var outputCouponDiscount = document.getElementById("outputCouponDiscount");
    if((couponError == null) || (couponError.length == 0))
    {
        rowCouponError.style.display = "none";
        if((COUPON_CODE != null) && (COUPON_CODE.length > 0))
        {
            rowCouponRight.style.display = "";
            OutputText(outputCouponCode, COUPON_CODE);
            
            var textDiscount = "";
            if(costDiscountPercentage > 0)
            {
                textDiscount = costDiscountPercentage + "%";
            }
            else
            {
                textDiscount = "$" + costDiscount;
            }
            
            OutputText(outputCouponDiscount, textDiscount);
        }
        else
        {
            rowCouponRight.style.display = "none";
        }
    }
    else
    {
        rowCouponError.style.display = "";
        rowCouponRight.style.display = "none";
        OutputText(outputCouponError, couponError);       
       
    }  
    
    if(isCritical)
    {
        COUPON_CODE = "";
    }     

}



function StopEvent(pE)
{
   if (!pE)
     if (window.event)
	pE = window.event;
     else
	return;
   if (pE.cancelBubble != null)
      pE.cancelBubble = true;
   if (pE.stopPropagation)
      pE.stopPropagation();
   if (pE.preventDefault)
      pE.preventDefault();
   if (window.event)
      pE.returnValue = false;
   if (pE.cancel != null)
      pE.cancel = true;
} 

function EnterCoupon(e)
{
	var pK = document.all? window.event.keyCode : e.which;
	if(pK == 13)
	{
		ChangeCoupon();
		StopEvent(e);
		return false;
	}
	return true;
}

function OutputText(obj, text)
{
    if(obj == null)
    {
        return;
    }
    
    if(obj.innerHTML)
    {
        obj.innerHTML = text;
    }
    else
    {
        obj.textContent = text;
    }
}
