
function budgetize()
{
	var productsid = $('productsid').value;
	var budget = $('budget').value
	var url = '?fuseaction=budget.&productsid=' + productsid + '&tospend=' + budget;
	var pars = '';
	new Ajax.Updater('budget_results', url,	{budgetAjax: true, method: 'get', parameters: pars, onFailure: reportError});
	var mydrag = new Draggable('budget_container', {scroll: window});
	//$('budget_container').absolutize().center();
}
function reportError(request)
{
	alert('Sorry. We are unable to process your request at this time. Please try again later');
}

function showCalc(divid)
{

	//alert(document.body.clientHeight);
	//alert(document.body.clientWidth);
	if ($(divid).style.display != "block")
	{
		$(divid).style.display = "block";
		var IpopTop = (document.body.clientHeight - $(divid).offsetHeight) / 2;
		var IpopLeft = (document.body.clientWidth - $(divid).offsetWidth) / 2;

		$(divid).style.left=IpopLeft + document.body.scrollLeft;
		$(divid).style.top=IpopTop + document.body.scrollTop;
	}

}
function validateCoupon()
{
	var couponValue = $('coupon').value;
	var url = '?fuseaction=coupon.validate&coupon=' + couponValue;
	//var pars = '';
	var myAjax = new Ajax.Updater({success: 'couponVali'},url, {method: 'get', onFailure: reportError, onComplete: reportSuccess});
	//var mydrag = new Draggable('budget_container', {scroll: window});
}
function reportError(request)
{
	alert('Sorry. We are unable to process your request at this time. Please try again later');
}
function reportSuccess(request)
{
	var myResponse = request.responseText;
	var myParsList = myResponse.split(',');//split ajax response into an array
	//alert(myParsList[0] + myParsList[1] + myParsList[2] + myParsList[3] + myParsList[4]);//returns coupon details
	if (myParsList[0] == 1) {
	  //alert("valid");
		$('coupon').value = myParsList[1];
		if (myParsList[3] == "perc") {
			var newSubTotal = ($('subtotal').value - ( $('subtotal').value * (myParsList[2] * 0.01))).toFixed(2);
			$('validateLink').innerHTML = "<img src='images/coupon/coupon-checkgreen.jpg'>"; // change check link
			$('couponResult').innerHTML = "<font color='green'><b>&nbsp;&nbsp;" + myParsList[2] + "%&nbsp;Off!</b></font>"; // change check link
			$('couponValue').value = " " + myParsList[2] + "% Off"; // populate hidden coupon field
			$('div_subtotal').innerHTML = "<font color='red'>"+newSubTotal+"</font>"; // change visual displayed Total
			$('subtotal').value = newSubTotal; // change hidden input value for subtotal
			var newOrderTotal = (parseFloat($('shipping').value) + parseFloat($('tax').value) + parseFloat(newSubTotal)).toFixed(2);
			$('div_total').innerHTML = "<font color='red'>"+newOrderTotal+"</font>";
			$('order_total').value = newOrderTotal; // change hidden input value for order_total
			$('ustotal').value = newOrderTotal;

		}
		else if (myParsList[3] == "amt") {
			//alert(myParsList[4] + "\n" + parseFloat($('subtotal').value));
			if (myParsList[4] <= parseFloat($('subtotal').value)) {
				var newSubTotal = ($('subtotal').value - myParsList[2]).toFixed(2);
				$('validateLink').innerHTML = "<img src='images/coupon/coupon-checkgreen.jpg'>"; // change check link
				$('couponResult').innerHTML = "<font color='green'><b>&nbsp;&nbsp;$" + myParsList[2] + "&nbsp;Off!</b></font>"; // change check link
				$('couponValue').value = "$" + myParsList[2] + " Off"; // populate hidden field
				$('div_subtotal').innerHTML = "<font color='red'>"+newSubTotal+"</font>"; // change visual displayed Total
				$('subtotal').value = newSubTotal; // change hidden input value for subtotal
				var newOrderTotal = (parseFloat($('shipping').value) + parseFloat($('tax').value) + parseFloat(newSubTotal)).toFixed(2);
				$('div_total').innerHTML = "<font color='red'>"+newOrderTotal+"</font>";
				$('order_total').value = newOrderTotal; // change hidden input value for order_total
				$('ustotal').value = newOrderTotal;
			}
			else {
				alert("This coupon is only valid for orders of $" + myParsList[4] + " or more.\nPlease purchase more products.");
			}
		}
	}
	else {
		alert("You have entered an incorrect Coupon, Please try again!")
	}
}
function resetValues()
{
	$('subtotal').value = $('div_subtotal').innerHTML;
	$('order_total').value = $('div_total').innerHTML;
	//$('ustotal').value = $('div_total').innerHTML;
	$('tax').value = "0";
	$('shipping').value = "0";
}

//var myGlobalHandlers = {
//	onCreate: function(){
//		Element.show('systemWorking');
//	},
//
//	onComplete: function() {
//		if(Ajax.activeRequestCount == 0){
//			Element.hide('systemWorking');
//		}
//	}
//};
//Ajax.Responders.register(myGlobalHandlers);
function fillCustPrice (custQty){
	if(!(IsNumeric(custQty))){alert("Please enter a numeric quantity"); return;}
	if(custQty == ''){alert("Please enter a numeric quantity"); return;}
	custQty = parseInt(custQty);
	if($('capacity')){
		for (var i=0; i<$('capacity').length; i++){
			//alert($('capacity')[i].text);
			var capacity = $('capacity')[i].text;
			//alert(capacity);
			//var minMax = findMinMax(capacity);
			//var qMinQMax = findQMinQMax(capacity);
			//var ppu = getPricePerUnit(minMax,qMinQMax,custQty);
			var valueToChange = capacity+"_custom";
			//$(valueToChange).innerHTML = "<a href='/?fuseaction=quote.add_cart&productsid="+$('productsid').value+"&inst=1&id=-1&quantity="+custQty+"&capacity="+capacity+"' style='font-size: 11px;'>"+"$"+ppu+"</a>";
			fillPPU(custQty,capacity,valueToChange);
		}
	}
	else {
		var capacity = $('capacityId').value;
		//var minMax = findMinMax(capacity);
		//var qMinQMax = findQMinQMax(capacity);
		//var ppu = getPricePerUnit(minMax,qMinQMax,custQty);
		var valueToChange = capacity+"_custom";
		//$(valueToChange).innerHTML = "<a href='/?fuseaction=quote.add_cart&productsid="+$('productsid').value+"&inst=1&id=-1&quantity="+custQty+"&capacity="+capacity+"' style='font-size: 11px;'>"+"$"+ppu+"</a>";
		fillPPU(custQty,capacity,valueToChange);
	}
}
function IsNumeric(sText){
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++)
      {
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1)
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}
function fillPPU(qty,cap,chng)
{
	var id = $('productsid').value;
	var url = '?fuseaction=budget.ppu'
	var pars = 'id=' + id + '&q=' + qty +"&c="+cap;
	var myAjax = new Ajax.Updater({success: chng},url,{method: 'get',parameters: pars,onFailure: reportError});
}


