/*
  
 	------------------------------------------------------
 	FILE:	common.js
 	------------------------------------------------------
 	AUTHOR:	Brendon Ryniker
 	EMAIL:	brendon@catalyst.net.nz
 	DATE:	Mon Jan 12 13:05:54 2004 
 	------------------------------------------------------
 	APPLICATION: EEC Virtual Election Calculator
 	------------------------------------------------------
 	DEPENDANCIES:
 	------------------------------------------------------
 	FUNCTIONALITY:
	
	General client-side functionality comprising parts of 
	the Virtual Election web application.
 	------------------------------------------------------
*/

// ==================================================
	function findValue (form, name) {
// ==================================================
        var index;
        for (index = 0; index < form.elements.length; index++) {
            if (form.elements[index].name == name) {
            return form.elements[index].value;
        }
        }
    }

// ==================================================
    function focusOn(form,name) {
// ==================================================
        var index;
        for (index = 0; index < form.elements.length; index++) {
            if (form.elements[index].name == name) {
            form.elements[index].focus();
        }
        }
    }

// ==================================================
    function readPartyVoteMethod (form) {
// ==================================================
        return form.asPercentage[0].checked;
    }

// ==================================================
    function checkPartyVoteAsPercent(form, partyName, fieldName) {
// ==================================================
        var fieldValue = findValue(form, fieldName);
        var numberRE = /^\s*[0-9]*\.?[0-9]*\s*$/;
        var blankRE = /^\s*$/;
        if (!numberRE.test(fieldValue) || blankRE.test(fieldValue)) {
        focusOn(form, fieldName);
        alert("Party Vote for "+partyName+" must be a percentage (0.0 to 100.0) - was " + fieldValue);
        return false;
        }
        return true;
    }

// ==================================================
    function checkPartyVoteAsCount(form, partyName, fieldName) {
// ==================================================
        var fieldValue = findValue(form, fieldName);
        var numberRE = /^\s*[0-9]+\s*$/;
        if (!numberRE.test(fieldValue)) {
        focusOn(form, fieldName);
        alert("Party Vote for "+partyName+" must be an integer greater than or equal to 0");
        return false;
        }
        return true;
    }

// ==================================================
    function checkElectorateVote(form, partyName, fieldName) {
// ==================================================
        var fieldValue = findValue(form, fieldName);
        var numberRE = /^\s*[0-9]+\s*$/;
        if (!numberRE.test(fieldValue)) {
        focusOn(form, fieldName);
        alert("Electorate Vote for "+partyName+" must be an integer greater than or equal to 0");
        return false;
        }
        return true;
    }

// ==================================================
function checkElectionForm (form,numElectorates) {
// ==================================================
        var partyCount = form.partyCount.value - 0;
        var index;
        var partyName;
        var fieldName;
        var partyPercentages;
        var partyVoteTotal = 0;
        var electorateTotal = 0;
        var blankRE = /^\s*$/;

        partyPercentages = readPartyVoteMethod(form);

		
        for (index = 0; index < partyCount; index++) {
        partyName = findValue(form, "partyName_"+index);

        if (partyPercentages) {
            if (!checkPartyVoteAsPercent(form, partyName, "partyVote_"+index)) return false;
        } else {
            if (!checkPartyVoteAsCount(form, partyName, "partyVote_"+index)) return false;
        }
        if (!checkElectorateVote(form, partyName, "electorateSeats_"+index)) return false;
        partyVoteTotal += (findValue(form, "partyVote_"+index) - 0);
        electorateTotal += (findValue(form, "electorateSeats_"+index) - 0);
        }
	
        partyCount = form.optPartyCount.value - 0;
        for (index = 0; index < partyCount; index++) {
        partyName = findValue(form, "partyName_opt_"+index);
        if (blankRE.test(partyName)) continue;
        if (partyPercentages) {
            if (!checkPartyVoteAsPercent(form, partyName, "partyVote_opt_"+index)) return false;
        } else {
            if (!checkPartyVoteAsCount(form, partyName, "partyVote_opt_"+index)) return false;
        }
        if (!checkElectorateVote(form, partyName, "electorateSeats_opt_"+index)) return false;
        partyVoteTotal += (findValue(form, "partyVote_opt_"+index) - 0);
        electorateTotal += (findValue(form, "electorateSeats_opt_"+index) - 0);
        }

        if (partyPercentages) {
        if (partyVoteTotal != 100) {
            if (! confirm("The Party Vote percentages add up to "+partyVoteTotal+", not 100.0.\nDo you wish to continue?")) {
            return false;
            }
        }
        } else {
        if (partyVoteTotal < 1) {
            alert("Party Votes must add up to at least one");
            return false;
        }
        }

        if (electorateTotal > numElectorates) {
        alert("The number of electorate seats must not be greater than " + numElectorates);
        return false;
        }
        return true;
    }

// ==================================================
    function truncateDecimalInput (field) {
// ==================================================
        // Don't 'optimise' the 10 * 10 into 100.  Javascript seems to have nondeterministic maths
        // that will introduce an error if the value is simply multiplied by 100.
        field.value = Math.floor(field.value * 10 * 10) / 100;
        return 1;
    }

