// CLASS ArtToggle
// This is an object which
// simulates the functionality of a radio button but with graphical buttons.
// if the prefix is "q1", the ArtToggle expects the following to exist:
// - images named q1_img1 and q1_img2
// the state of the toggle will be 0 if nothing is selected, 1 if the 
// first button is selected, and 2 if the second button is selected.

// constructor for ArtToggle class.  Takes a prefix used to identify the toggle
// and images which are the
// on and off states for the first and second buttons in the toggle.
// if the second on and off state are passed in as "false", they will be set equal
// to the first ones.

function ArtToggle(prefix, onState1, offState1, onState2, offState2) {
	this.button1 = prefix + "_img1";
	this.button2 = prefix + "_img2";
	this.onState1 = onState1;
	this.offState1 = offState1;
	this.name = prefix;
	if (onState2) {
		this.onState2 = onState2;
	} else {
		this.onState2 = onState1;
	}
	if (offState2) {
		this.offState2 = offState2;
	} else {
		this.offState2 = offState1;
	}

	this.state = 0;		// 0 is unselected; 1 is button1; 2 is button2.
}

new ArtToggle("tmp", null, null, null, null);
// onclick functionality for each button of the toggle.  
// takes a number, 1 or 2, which identifies the button.
function ArtToggle_buttonClicked(buttonID) {
	if (this.state == buttonID) return;
	this.state = buttonID;
	if (buttonID == 1) {
		swapImage(this.button1, this.onState1);
		swapImage(this.button2, this.offState2);
	}
	else if (buttonID == 2) {
		swapImage(this.button2, this.onState2);
		swapImage(this.button1, this.offState1);
	}
}
ArtToggle.prototype.buttonClicked = ArtToggle_buttonClicked;



// CLASS ArtToggleGroup -- store a collection of ArtToggles, handle submitting
// them via opening a popup and sending the appropriate string.
function ArtToggleGroup(arrToggle, strAction, pollID) {
	this.toggles = arrToggle;			//array of toggle objects
	this.action = strAction;			//url of the page to submit to
	this.pollID = pollID;
}

new ArtToggleGroup(new Array(), null, null);

function ArtToggleGroup_submit() {
	var strResult = this.action;
	if (!this.validate()) { return; }
	strResult += "?pollID=" + this.pollID;
	for (var i = 0; i < this.toggles.length; i++) {
		var tog = this.toggles[i];
		strResult += "&" + tog.name + "=" + tog.state;	
	}
	this.popResults(strResult);
}
ArtToggleGroup.prototype.submit = ArtToggleGroup_submit;

function ArtToggleGroup_validate() {
	for (var i = 0; i < this.toggles.length; i++) {
		var tog = this.toggles[i];
		if (tog.state == 0) {
			alert ("Please make sure you have voted on every question.");
			return false;
		}
	}
	return true;
}

ArtToggleGroup.prototype.validate = ArtToggleGroup_validate;

function ArtToggleGroup_popResults(path) {
	popWindow = window.open(path, this.prefix,'width=400,height=325,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0');
}

ArtToggleGroup.prototype.popResults = ArtToggleGroup_popResults;

