/* 
umr portal javascript
@author Cass Cox, 2002/2004
*/

function clearelec(item) {
	item.electorates__electorate.value="";
}

// CODE FOR NAVIGATION

function goToURL(finalURL) {
	window.open(finalURL);
}

function doCurCon() {
	var curAmount = document.forms[0].curAmount.value
	var curFrom = document.forms[0].curFrom.options[document.forms[0].curFrom.selectedIndex].value
	var curTo = document.forms[0].curTo.options[document.forms[0].curTo.selectedIndex].value
	var currencyURL = "http://au.finance.yahoo.com/m5?a=" + curAmount + "&s=" + curFrom + "&t=" + curTo
	window.open(currencyURL,"currency")
}

var allflag = true;
var iterate = 0;

/* portal specific view / hide */
function view(ident) {
	if (allflag) hideall(ident);
	if (document.getElementById){
		element = document.getElementById(ident);
		if (element.style.display == "none") {
			element.style.display = "";
		}
		else { 
			element.style.display = "none";
		}
	}
	allflag=true;
}

function subview(ident) {
	if (document.getElementById){
		element = document.getElementById(ident);
		if (element.style.display == "none") {
			element.style.display = "";
		}
		else { 
			element.style.display = "none";
		}
	}
}

var hideID = new Array("umr","news","pol","elec","comp","misc","moe","cur","aec");
function hideall(ident) {
	allflag = false;
	iterate=0
	var target;
	var element;
	for (i = 0; i < hideID.length; i++) {
		if (hideID[i] == ident) {
			// do nothing
		}
		else {
			target = hideID[i];
			element = document.getElementById(target);
			element.style.display = "none"; 
		}
	}
}

var showID = new Array("umr","news","pol","elec","comp","misc","aec");
function showall() {
	var target;
	var element;
	for (i = 0; i < showID.length; i++) {
		target = showID[i];
		element = document.getElementById(target);
		element.style.display = ""; 
		}
}


/* CalcConfIntPrn.html javascript calculator
   copyright 2003 mark mitchell 
   GNU General Public License
*/
var confLevel = 1.96;
var sResultsWhole = false;

function clearFields(obj) {
  obj.population.value = "1000000";
  obj.sampleSize.value = "";
  obj.percentage.value = "50";
  obj.margin.value = "";
  obj.confInt.value = "";
}

function compute(obj) {
 obj.margin.value = "";
 obj.confInt.value = "";
 if ((obj.population.value == "") ||
  (obj.sampleSize.value == "")) {
  (obj.percentage.value == "") ||
  alert("All values must be filled in");
  return true;
 }
 obj.population.value = removeCommas(obj.population.value);
 obj.sampleSize.value = removeCommas(obj.sampleSize.value);
 obj.percentage.value = removeCommas(obj.percentage.value);
 var population = eval(obj.population.value);
 var sampleSize = eval(obj.sampleSize.value);
 var percentage = eval(obj.percentage.value) / 100.0;

 if (population <= 1) {
  alert("Population must be greater than 1");
  return true;
 }
 if ((sampleSize < 1) ||
  (sampleSize > population)) {
  alert("Sample size must be between 1 and the population");
  return true;
 }
 if ((percentage < 0.0) ||
  (percentage > 1.0)) {
  alert("Percentage must be between 0 and 100");
  return true;
 }

 var pq = percentage * (1.0 - percentage);
 var confInt = 100.0 * confLevel * Math.sqrt(pq * (population - sampleSize) /
  (population - 1.0) / sampleSize);

 // round off to hundredths place
 if (sResultsWhole) {
  confInt = confInt / 100.0;
  obj.margin.value = Math.round(confInt * population + 0.05);
  obj.confInt.value = Math.round((percentage - confInt) * population + 0.05) +
   " to " + Math.round((percentage + confInt) * population + 0.05);
 } else {
  percentage = 100.0 * percentage;
  obj.margin.value = Math.round(confInt * 100.0) / 100.0;
  obj.confInt.value = Math.round((percentage - confInt) * 100.0) / 100.0 + 
   " to " + Math.round((percentage + confInt) * 100.0) / 100.0 ;
 }
}

function confLevel90 (obj) {
	confLevel = 1.64;
}

function confLevel95 (obj) {
 confLevel = 1.96;
}

function confLevel99 (obj) {
 confLevel = 2.576;
}

function removeCommas (s) {
 var t = "";
 var i;
 var c, comma = ",";
 for (i = 0; i < s.length; i++) {
  c = s.charAt(i);
  if (c != comma) t += c;
 }
 return t;
}

function resultsPercent (obj) {
 location.href = "/calc_conf_int_prn.php?percentage=true";
}

function resultsWhole (obj) {
 location.href = "/calc_conf_int_prn.php";
}
