/* 

   Conservation International CO2 Calculator
   Copyright 2006 Conservation International
   Portions Copyright 2006, 2005, 2004 Sustainable By Design
   
   File: javascript/misc.js

*/


/* ******************************** */
/* FUNCTION: SetInitialCalculator   */
/* ******************************** */

function SetInitialCalculator () {

	currentCalculator = 'intro';

	queryString = location.search.substring(1);
	
	queryPairs = queryString.split("&");

	for (i = 0; i < queryPairs.length; i ++) {
	
		queryPair = queryPairs[i];
	
		equalPosition = queryPair.indexOf('=');
		
		if (equalPosition == -1) continue;
		
		arg = queryPair.substring(0, equalPosition);
		
		value = queryPair.substring(equalPosition + 1);
		
		if (arg == 'calculator') {
		
			for (j = 0; j < tabs.length; j ++) {
			
				if (tabs[j] == value) {
		
					currentCalculator = value;
					
					Tab (currentCalculator);
				}
			}
		}
	}
}


/* ******************************** */
/* FUNCTION: PreloadImages          */
/* ******************************** */

function PreloadImages () {


}


/* ******************************** */
/* FUNCTION: GetRadioButtonValue    */
/* ******************************** */

function GetRadioButtonValue (radioButton) {

	radioLength = radioButton.length;
	
	if (radioLength == undefined) {
	
		if (radioButton.checked) {
		
			return radioButton.value;
		}
			
		else {
		
			return "";
		}
	}
			
	for (i = 0; i < radioLength; i ++) {
	
		if (radioButton[i].checked) {
		
			return radioButton[i].value;
		}
	}
	
	return "";
}


/* ************************ */
/* FUNCTION: FormatFloat    */
/* ************************ */

function FormatFloat (number, format) {

	/* digits to display after the decimal point */
	
	if (format == 'integer') {
	
		desiredPrecision = 0;
	}

	else {
	
		if (format == null) {
	
			desiredPrecision = 1;
		}

		else {
		
			desiredPrecision = format;
		}
	}
	
	/* for floats...*/
	
	if (desiredPrecision > 0) {
	
		/* convert number to string */
		
		number = number.toString();
		
		/* if the number is an integer, add decimal point */
		
		if (number.indexOf('.') == -1) {
		
			number = number + '.';
		}
	
		/* get the number of digits after the decimal point */
		
		currentPrecision = number.length - number.indexOf('.') - 1;
		
		/* calculate precision variance */
		
		precisionVariance = (desiredPrecision - currentPrecision);
	
		/* if number is too precise, whack it off */
		
		if (precisionVariance < 0) {
		
			number = number.substr(0, (number.length + precisionVariance));
		}
		
		/* if number isn't precise enough, add trailing zeroes */
		
		for (i = 0; i < precisionVariance; i ++) {
		
			number = number + '0';
		}

		/* if zero */
		
		if (number == 0) {
		
			number = "0.0";
		}
		
	}
	
	/* for integers */
	
	else {
	
		number = parseInt (number);
	}
	
	return (number);
}


/* ************************ */
/* FUNCTION: UpdateSubtotal */
/* ************************ */

function UpdateSubtotal (divID, value) {

	subtotalText = FormatFloat (value) + ' <span class="subtotalUnits">tons of CO<span style="font-size:7px;">2</span> / year</span>';

	document.getElementById(divID).innerHTML = subtotalText;
}


/* ************************ */
/* FUNCTION: UpdateTotal    */
/* ************************ */

function UpdateTotal (divID, value, absolute) {

	if (absolute) {
	
		units = 'tons of CO<span style="font-size:7px;">2</span>';
	}
	
	else {
	
		units = 'tons of CO<span style="font-size:7px;">2</span> / year';
	}

	totalText = FormatFloat (value) + ' <span class="subtotalUnits totalUnits">' + units + '</span>';

	document.getElementById(divID).innerHTML = totalText;
}


/* ***************************** */
/* FUNCTION: UpdateTotalNotes    */
/* ***************************** */

function UpdateTotalNotes (divID, value) {
	
	donation_url = '<a href="https://secure2.convio.net/cintl/site/Donation2?df_id=1160&1160.donation=form1&PREFILL_AMOUNT=' + FormatFloat (value, 'integer') + '">';
	
	// gift offsets
	if (divID=="gift_total_notes"){
		donation_url = '<a href="https://secure2.convio.net/cintl/site/Donation2?df_id=1361&1361.donation=form1&PREFILL_AMOUNT=' + FormatFloat (value, 'integer') + '">';
	};
	
	totalText = 'Offset your CO<span style="font-size:7px;">2</span> emissions with a <b>$' + FormatFloat(value, 'integer') + '</b> donation<img src="/xp/CIWEB/images/cc/spacer.gif" width=10 height=1 style="vertical-align:middle;">';
	
	totalText = totalText + donation_url;
	
	totalText = totalText + '<img src="/xp/CIWEB/images/cc/offset-now.gif" border="0" width="60" height="13" style="vertical-align:middle;"></a>';

	//document.getElementById(divID).innerHTML = totalText;
}


/* ***************************** */
/* FUNCTION: ResetSelectMenus    */
/* ***************************** */

function ResetSelectMenus () {

	f = document.theForm;

	/* HOME ENERGY */

	f.personal_home_residence.selectedIndex        = 0;
	f.personal_home_simple_occupants.selectedIndex = 0;

	f.personal_home_detailed_state.selectedIndex     = 0;
	f.personal_home_detailed_occupants.selectedIndex = 0;
	f.personal_home_electricity.selectedIndex        = 0;
	f.personal_home_gas.selectedIndex                = 0;
	f.personal_home_oil.selectedIndex                = 0;
	f.personal_home_propane.selectedIndex            = 0;
	f.personal_home_wood.selectedIndex               = 0;

	/* CAR TRAVEL */

	f.personal_car_class.selectedIndex          = 0;
	f.personal_car_miles_simple.selectedIndex   = 0;
	f.personal_car_mpg.selectedIndex            = 0;
	f.personal_car_miles_detailed.selectedIndex = 0;

	/* AIR TRAVEL */

	f.personal_air_short.selectedIndex = 0;
	f.personal_air_long.selectedIndex  = 0;

	/* DIET */
	
	f.personal_diet.selectedIndex = 0;

	/* RECALCULATE */
	
	Calculate ();
}


/* ***************************** */
/* FUNCTION: Popup               */
/* ***************************** */

function Popup (fileName) {

	args = "width=600,height=400,resizable=yes,scrollbars=yes";

	if (typeof (popup_windows[name]) != "object") {
	
		popup_windows[name] = window.open (fileName, fileName, args);
	}
	
	else {
	
		if (! popup_windows[name].closed) {
		
			popup_windows[name].location.href = fileName;
		}
	
		else {

			popup_windows[name] = window.open (fileName, fileName, args);
		}
	}

	popup_windows[name].focus();
}

