var lkPrice = 1995.0;

function updateField()
{
	if(isNaN(parseInt(document.form1.lkQuantity.value)) || document.form1.lkQuantity.value < 0)
	{
		alert("Please enter a valid quantity.");
		document.form1.lkQuantity.value = 0;
		document.form1.lkQuantity.focus();
		return false;
	}
	
	var totalAmount = ((document.form1.lkQuantity.value * lkPrice)/100).toFixed(2);
	document.getElementById("calculatedTotal").innerHTML = "$" + totalAmount;
	document.form1.total.value = totalAmount;
}


function incrementLK()
{
	var qty = document.form1.lkQuantity.value;
	qty++;
	if (qty < 0) qty = 0;
	document.form1.lkQuantity.value = qty;
	
	updateField();
}

function decrementLK()
{
	var qty = document.form1.lkQuantity.value;
	qty--;
	if (qty < 0) qty = 0;
	document.form1.lkQuantity.value = qty;
	
	updateField();
}


function checkFields()
{
	if(isNaN(parseInt(document.form1.lkQuantity.value)) || document.form1.lkQuantity.value < 0)
	{
		alert("Please enter a valid quantity.");
		document.form1.lkQuantity.value = 0;
		document.form1.lkQuantity.focus();
		return false;
	}

	return true;
}







function codeFound(checkResults, waitIMG)
{
            
    document.getElementById(waitIMG).innerHTML = "";
    var discount = 0;
    if(checkResults == "Already Used") {
    
        alert("It appears that the code you have entered has already been used.");
        
    } else if (checkResults == "Expired") {
    	
    	// Expired Code
        alert("It appears that the code you have entered has expired.");
        
    } else if (checkResults == "Not Found") {
        
        // Bad Code
        discount = 0;
        
    } else {
    
    	discount = checkResults;
    }


	if (discount > 0) {
		var discountAmount;
		discountAmount = (19.95 * discount).toFixed(2);
	   	document.getElementById("discountAmount").innerHTML = '- $' + discountAmount;
    	document.getElementById("calculatedTotal").innerHTML = (19.95 - discountAmount).toFixed(2);
	}

}




function lookupCoupon(couponCode, waitIMG)
{
    var xmlhttp=false;
    var checkResults = "Not Found";
    try
    {
        xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of activeX object
    }
    catch(e)
    {
        try
        {
            xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of activeX object
        }
        catch (E)
        {
            xmlhttp = false;
        }
    }
    if (!xmlhttp && typeof XMLHttpRequest!='undefined')
    {
        xmlhttp = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
    }
    
    var file = 'couponLookup.php?code=';
    xmlhttp.open('GET', file + couponCode, true);

    var spinnerImg = waitIMG;
    xmlhttp.onreadystatechange = function()
    {
        if (xmlhttp.readyState == 4)
        {
            var content = xmlhttp.responseText;
            if( content )
            {
                checkResults = content;
                codeFound(checkResults, spinnerImg);
            }
        }
    }
    xmlhttp.send(null) //Nullify the XMLHttpRequest

    return checkResults;
}




function checkCoupon(waitImg)
{
   	var code = document.form1.couponCode.value;
   	if (code == "") {
	   	document.getElementById("discountAmount").innerHTML = "";
    	document.getElementById("calculatedTotal").innerHTML = "19.95";
   		return;
   	}

    document.getElementById(waitImg).innerHTML = "<img src='/store/loading.gif' style='height:32px;' alt='Checking Coupon...'>";
   	
	lookupCoupon(code, waitImg);
}

