// JavaScript Document

function my_ajax_update(target,url,pars,timer) 
{
	
	var myAjax = new Ajax.Updater(target, url, {
			method: 'get', 
			parameters: pars,
			onSuccess: function(transport)
			{
				var response = transport.responseText || "";
			},
			onComplete: function()
			{     
				//alert('Request Completed'); 
				//Print flight Array and replace innerHtml of summary div.
			}
		});
	if (timer > 0) setTimeout("my_ajax('"+target+"','"+url+"','"+pars+"',"+timer+")",timer);
}


function my_ajax_request(url,pars)
{
	var myAjax = new Ajax.Request(url, {
		method: 'post',
		postBody: pars,
		evalScripts:true,
		asynchronous: true,
	    onSuccess: function(transport)
		{
			var response = transport.responseText || "";
			//debug("response: " + response); 
			//Parse Values and Add them to Array
			if (response != "")
			{
				householdDisplay(transport.responseText);
				
			} else
			{
				alert('Please select from all drop down selections.');	
			}
		},
		onComplete: function()
		{
			//Print innerHtml of summary div.
			//householdDisplay();
		}
	}
	)
}

function ajaxRequest(url,data) {  
	var aj = new Ajax.Request(  
		url, {  
			method:'get',   
			parameters: data,   
			onComplete: getResponse  
		}  
	);  
} 

function getResponse(oReq) {  
	$('result').innerHTML = oReq.responseText;  
} 

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function householdDisplay( household )
{
	var summaryDiv = document.getElementById("summary");
	var price = 0.00; 
	var carbon_balance = 0.00;
	var cParsed = household.split(":");
		
	//carbon_balance += cParsed[0];
	//price += cParsed[1];
    carbon_balance += parseFloat(cParsed[1]);
	price += parseFloat(cParsed[2]);
		
	//Build Form and Table and Output Flight Array
	var summaryHtml = "";
	if( price!=0 ){ 
		//summaryHTML = "<form action='http://www.carbonfriendly.com/ecommerce/dispatcher.php' method='post' name='payment_form' id='payment_form' target='_parent'>";
		summaryHTML = "<form action='' method='post' name='payment_form' id='payment_form' target='_parent'>";
		summaryHTML += "<table width='342px' border='0' style='border:1px solid #000; background-color: #FFF;'>";
		summaryHTML += "<tr><th style='width:50%; text-align:left;'></th><th style='width:25%; text-align:left;' nowrap>CO2 (t)</th><th style='text-align:left; width:25%;'>Cost</th></tr>";


		summaryHTML += "<tr style=' font-size: 10px;'>";
		summaryHTML += "<td style='text-align:left;' nowrap><b>Totals</b></td>";
		summaryHTML += "<td style='text-align:left;' nowrap>"+Math.round(carbon_balance*100)/100+"</td>";
		//summaryHTML += "<td style='text-align:right;' nowrap>Cost: </td>";
		summaryHTML += "<td style='text-align:left;' nowrap>$"+Math.round(price*100)/100+" USD</td>";
		summaryHTML += "</tr>";

		summaryHTML += "</table>";
		//Create Hidden Inputs for Payment Form
		summaryHTML += "<div>";
		summaryHTML += "<input type='hidden' name='carbon_balance' value='"+ carbon_balance +"' />";
		summaryHTML += "<input type='hidden' name='price' value='"+ price +"' />";
		summaryHTML += "<input type='hidden' name='page_referrer' value='ajax_widget' />";
		summaryHTML += "<input type='hidden' name='offset_name' value='Household Offset' />";
		//summaryHTML += "<input type='submit' value='OFFSET NOW'>";
		summaryHTML += "</div>";
		summaryHTML += "</form>";
		//summaryHTML += "<a href=\"#\" onclick=\"popup('popUpDiv')\">How Was This Calculated?</a>";

		var newCarbon = (Math.round(carbon_balance*100)/100);
		var newOffset = (Math.round(price*100)/100);
		ajaxRequest('/carbon-calculator/widgets/inc/updateCCResults.php', 'widget=household&userID='+ $F('user_id') +'&uniqueID='+ $F('unique_id') +'&carbon='+ newCarbon +'&offset='+ newOffset);

	}
	else
	{
		var newCarbon = 0;
		var newOffset = 0;
		ajaxRequest('/carbon-calculator/widgets/inc/updateCCResults.php', 'widget=household&userID='+ $F('user_id') +'&uniqueID='+ $F('unique_id') +'&carbon='+ newCarbon +'&offset='+ newOffset);

		summaryHTML = "<div id='contentx'><div id='add_text'></div></div>";
	}
	summaryDiv.innerHTML = summaryHTML;
}

function resetHouseholdData(){

	var newCarbon = 0;
	var newOffset = 0;
	var summaryDiv = document.getElementById("summary");

	ajaxRequest('/carbon-calculator/widgets/inc/updateCCResults.php', 'widget=household&userID='+ $F('user_id') +'&uniqueID='+ $F('unique_id') +'&carbon='+ newCarbon +'&offset='+ newOffset);
	summaryHTML = "<div id='contentx'><div id='add_text'></div></div>";
	summaryDiv.innerHTML = summaryHTML;

	document.offset_form.reset();
}