//global vars which will be updated as the user creates the questions
var q_index = 1;
var target = '';

//prices for services
var setup = 2000;
var questions = 300;
var choices = 50;
var onsitebrf = 500;
var confcall = 125;
var msg = 100;
var extra = 0;


/*used in 1.html for creating the question form
all html elements are created via the DOM methods */

function outputQuestion(index,questiontxt) {
	if (questiontxt == null) { questiontxt = ''; }
	var wrapper = document.createElement("div");
	wrapper.setAttribute("id","question" + q_index);
	var bg;	
	if (index % 2 == 0) {
		wrapper.className = "even"; //for IE
		wrapper.setAttribute("class","even");
		bg = "2";
	}else {
		wrapper.className = "odd"; //for IE
		wrapper.setAttribute("class","odd");
		bg = "";
	}
	var frame = document.createElement("div");
	//var divname = "div"+q_index;
	var index = q_index;
	frame.innerHTML = "<table width=\"100%\"><tr><td><h3 class=\"open\" id=\"qtxt"+q_index+"\">Poll Question "+q_index+"</h3></td>" 
			+ "<td id=\"buttontxt"+q_index+"\" width=\"130\" align=\"right\" class=\"buttonlabel"+bg+"\"></td><td align=\"right\" width=\"110\" class=\"bttn\" style=\"vertical-align: middle;\">"
			+ "<img src=\"images/add"+bg+".gif\" alt=\"Add Question\" class=\"button\" onmouseover=\"buttonText('buttontxt"+index+"','add question');\" onmouseout=\"buttonText('buttontxt"+index+"','');\" onclick=\"outputQuestion(q_index); buttonText('buttontxt"+index+"','');\"/>"
			+ "<img src=\"images/minimize"+bg+".gif\" alt=\"Minimize\" class=\"button\" onmouseover=\"buttonText('buttontxt"+index+"','minimize');\" onmouseout=\"buttonText('buttontxt"+index+"','');\" onclick=\"toggleQuestion(this, '"+bg+"',"+q_index+",'buttontxt"+index+"'); buttonText('buttontxt"+index+"','');\"/>"
			+ "<img src=\"images/close"+bg+".gif\" alt=\"Remove Question\" class=\"button\" onclick=\"removeQuestion("+index+");\" onmouseover=\"buttonText('buttontxt"+index+"','remove');\" onmouseout=\"buttonText('buttontxt"+index+"','');\"/>"
			+ "</td></tr></table>";
	wrapper.appendChild(frame);
	var question = document.createElement("div");
	question.setAttribute("id","qdiv"+q_index);
	question.setAttribute("class","open");
	question.className = "open";
	wrapper.appendChild(question);
	
	var questionbox = document.createElement("textarea");
	questionbox.setAttribute("rows","4");
	questionbox.setAttribute("cols","50");
	questionbox.setAttribute("name","Q" + q_index);
	questionbox.setAttribute("id","Q" + q_index);
	questionbox.appendChild(document.createTextNode(questiontxt));
	question.appendChild(questionbox);
	
	question.appendChild(document.createElement("br"));
	question.appendChild(document.createElement("br"));
	
	var max_choices = document.createElement("input");
	max_choices.setAttribute("type","hidden");
	max_choices.setAttribute("name","max" + q_index);
	max_choices.setAttribute("id","max" + q_index);
	max_choices.setAttribute("value", 1);
	question.appendChild(max_choices);

	var del = document.createElement("input");
	del.setAttribute("type","hidden");
	del.setAttribute("name","D"+q_index);
	del.setAttribute("id","D"+q_index);
	del.setAttribute("value","0");
	question.appendChild(del);

	var nextdiv = document.createElement("div");
	nextdiv.innerHTML = "<table><tr><td id=\"div"+index+"\"></td>"
		+ "<td style=\"vertical-align: bottom;\"><table><tr>"
		+ "<td><img src=\"images/add"+bg+".gif\" alt=\"Add Choice\" class=\"button2\" onmouseover=\"buttonText('choicetxt"+index+"','add choice');\" onmouseout=\"buttonText('choicetxt"+index+"','');\" onclick=\"outputChoice('max"+index+"','div"+index+"');\" />"
		+ "<img src=\"images/close"+bg+".gif\" alt=\"Remove Choice\" class=\"button2\" onmouseover=\"buttonText('choicetxt"+index+"','remove choice');\" onmouseout=\"buttonText('choicetxt"+index+"','');\" onclick=\"removeChoice('max"+index+"','div"+index+"');\" /></td>"
		+ "<td class=\"buttonlabel"+bg+"\" width=\"100\" align=\"left\" id=\"choicetxt"+index+"\"></td></tr></table>"
		+"</td></tr></table>";
	question.appendChild(nextdiv);
	q_index++;
	document.getElementById("questiondiv").appendChild(wrapper);

	if (questiontxt == '') {
		//output three answers to start out
		outputChoice(max_choices.id,"div"+index);
		outputChoice(max_choices.id,"div"+index);
		outputChoice(max_choices.id,"div"+index);
	}
}

function removeQuestion(num) {
	var remove = confirm("Remove Question "+num+"?");
	if (remove) {
		document.getElementById('questiondiv').removeChild(document.getElementById('question'+num));
	}else{
		//do nothing	
	}	
}

function outputChoice(maxid,divid,answertxt) {
	if (answertxt == null) { answertxt = ''; }
	var max = document.getElementById(maxid); 
	var div = document.getElementById(divid);
	var index = div.id.slice(3); //get the question number
	
	var wrapper = document.createElement("div");
	wrapper.className = "choice";

	var header = document.createElement("h4");
	header.appendChild(document.createTextNode("Choice " + max.value));
	wrapper.appendChild(header);

	wrapper.appendChild(document.createElement("br"));

	var choice = document.createElement("input");
	choice.setAttribute("type","text");
	choice.setAttribute("size","50");
	choice.setAttribute("name","A" + index +"-"+ max.value);
	choice.setAttribute("id","A" + index +"-"+ max.value);
	choice.setAttribute("value",answertxt);
	wrapper.appendChild(choice);

	div.appendChild(wrapper);	
	max.value++;
}

function removeChoice(maxid,divid) {
	var max = document.getElementById(maxid); 
	var div = document.getElementById(divid);
	if (max.value > 3) {
		div.removeChild(div.childNodes[div.childNodes.length-1]);
		max.value -= 1;
	}else{ alert('You must have at least two choices for a question!'); }	
}

function buttonText(id, text) {
	var area = document.getElementById(id);
	if (area.innerHTML == '') {
		area.innerHTML = text;
	}else{
		area.innerHTML = '';
	}
}

function toggleQuestion(img, bg, num, area) {
	var container = document.getElementById("qdiv"+num);
	var question = document.getElementById("qtxt"+num);
	var max = document.getElementById("max"+num);
	var del = document.getElementById("D"+num);
	if (container.className == 'closed') {
		img.src="images/minimize"+bg+".gif";
		img.onmouseover = function(e) { 
			e=e||window.event;
			buttonText(area,'minimize');
		};
		img.onmouseout = function(e) { 
			e=e||window.event;
			buttonText(area,'');
		};
		img.alt="Minimize";
		container.className = 'open';
		question.style.color = "#000000";
		del.value = 0; //we now want the minimized question to still be stored in db
		for (i=1; i<container.childNodes.length; i++) {
			var child = container.childNodes[i];
			if (child.tagName == 'input' || child.tagName == 'textarea') {
				child.style.display='inline';
			}
		}	
	}
	else if (container.className == 'open') {
		img.src = "images/maximize"+bg+".gif";
		img.onmouseover = function(e) { 
			e=e||window.event;
			buttonText(area,'maximize');
		};
		img.onmouseout = function(e) { 
			e=e||window.event;
			buttonText(area,'');
		};
		img.alt="Maximize";
		container.className = 'closed';
		question.style.color = "#666666";
		del.value = 0;
		for (i=1; i<container.childNodes.length; i++) {
			var child = container.childNodes[i];
			if (child.tagName == 'input' || child.tagName == 'textarea') {
				child.style.display='none';
			}
		}
	}
}

function openForm(data,step) {
	switch (step) {
		case 0:
			if (data != null) {
				var arrdata = data.split("|");
				var index = 0;
				for (i=0; i<arrdata.length; i++) {
					temp = arrdata[i].split("=");
					if (temp[0].substring(0,1) == "Q") {
						index++;
						outputQuestion(index,temp[1]);
					}else if (temp[0].substring(0,1) == "A") {
						outputChoice("max"+index,"div"+index,temp[1]);
					}else{
						//do nothing
					}
				}
			}else{
				outputQuestion(q_index);
			}
		break;

		case 1:
			if (data != null) {
				var arrdata = data.split("|");
				var first = arrdata.shift();
				var targetregion = first.split("=");
				if (targetregion[0] != 'target') { //filter out bad form data
					first = arrdata.shift();
					targetregion = first.split("=");
				}
				switch (targetregion[1]) {
					case 'national':
						var select = document.getElementById("select1");
						select.setAttribute("checked","checked");
					break;
					case 'state':
						var select = document.getElementById("select2");
						select.setAttribute("checked","checked");
						var target2 = document.getElementById("target2");
						target2.style.display = 'block';
						target = 'target2';
						
						var statevalue = arrdata.shift().split("=")[1];
						selected('state',statevalue);
					break;
					case 'cd':
						var select = document.getElementById("select3");
						select.setAttribute("checked","checked");
						var target2 = document.getElementById("target3");
						target2.style.display = 'block';
						target = 'target3';
						
						var cdvalue = arrdata.shift().split("=")[1];
						selected('cd',cdvalue);

						cdvalue = arrdata.shift().split("=");
						document.getElementById(cdvalue[0]).value = cdvalue[1];
					break;
					case 'ld':
						var select = document.getElementById("select4");
						select.setAttribute("checked","checked");
						var target2 = document.getElementById("target4");
						target2.style.display = 'block';
						target = 'target4';
						
						var ldvalue = arrdata.shift().split("=")[1];
						selected('ld',ldvalue);

						ldvalue = arrdata.shift().split("=");
						document.getElementById(ldvalue[0]).value = ldvalue[1];
					break;
					case 'county':
						var select = document.getElementById("select5");
						select.setAttribute("checked","checked");
						var target2 = document.getElementById("target5");
						target2.style.display = 'block';
						target = 'target5';
						
						var countyvalue = arrdata.shift().split("=")[1];
						selected('county',countyvalue);

						countyvalue = arrdata.shift().split("=");
						document.getElementById(countyvalue[0]).value = countyvalue[1];
					break;
					case 'city':
						var select = document.getElementById("select6");
						select.setAttribute("checked","checked");
						var target2 = document.getElementById("target6");
						target2.style.display = 'block';
						target = 'target6';
						
						var cityvalue = arrdata.shift().split("=")[1];
						selected('city',cityvalue);

						cityvalue = arrdata.shift().split("=");
						document.getElementById(cityvalue[0]).value = cityvalue[1];
					break;
					case 'other':
						var select = document.getElementById("select7");
						select.setAttribute("checked","checked");
						var target2 = document.getElementById("target7");
						target2.style.display = 'block';
						target = 'target7';
						
						var othervalue = arrdata.shift().split("=")[1];
						document.getElementById('other_comments').value = othervalue;
					break;
				}
			}else{
				//do nothing
			}
		break;

		case 2:
			if (data != null) {
				var arrdata = data.split("|");
				temp = arrdata.shift().split("=");
				while (temp != null) { //loop through the form data	
					if (temp[0] == 'sample_size' || temp[0] == 'target') {
						selected(temp[0],temp[1]);	
					}else if (temp[0] == 'other') {
						document.getElementById(temp[0]).value = temp[1];
					}else{
						var split = temp[0].split("_");
						if (split[1] != null) {
							var box = split[0]+" "+split[1];
						}else{
							var box = temp[0];
						}
						var checkbox = document.getElementById(box);
						checkbox.setAttribute("checked","checked");
					}
					temp = arrdata.shift();
					if (temp != null) { temp = temp.split("="); }
				}
			}	
		break;

		case 3:
			if (data != null) {
				var arrdata = data.split("|");
				temp = arrdata.shift().split("=");
				while (temp != null) { //loop through the form data
				
					var formelement = document.getElementById(temp[0]);
					if (formelement.tagName == "INPUT" && formelement.type == "checkbox") {
						formelement.setAttribute("checked","checked");
						formelement.checked
					}else if (formelement.tagName == "INPUT" && formelement.type == "text") {
						formelement.value = temp[1];
					}else{ //textarea
						formelement.value = temp[1];
					}
					temp = arrdata.shift();
					if (temp != null) { temp = temp.split("="); }
				}
			}
		break;
		
		default:
			//do nothing
		break;
	}
}

function selected(id,optionvalue) {
	var localselect = document.getElementById(id);
	var options = optionvalue.split('#');
	if (options.length > 1) {
	for (k=0; k<options.length-1; k++) {
		var nextoption = options[k];
		for (j=0; j<localselect.childNodes.length; j++) {
			option = localselect.childNodes[j];
			if (option.value == nextoption) {
				option.setAttribute("selected","selected"); //IE
				option.selected = true; //Firefox, Safari
			}
		}
	}	
	}else{
		for(j=0; j<localselect.childNodes.length; j++) {
			option = localselect.childNodes[j];
			if (option.value == options) {
				option.setAttribute("selected","selected"); //IE
				option.selected = true; //Firefox, Safari
			}
		}
	}
}


function localinfo(input) {
	var textbox = document.getElementById('other');
	if (input.value == 'other') {
		textbox.style.display = "inline";
	}else{
		if ( textbox.style.display == "inline" ) {
			textbox.style.display = "none";
		}	
	}
}

function saveQuit(id, value) {
	//changes the value of hidden to 'step[x]'
	document.getElementById(id).value=value;
	document.getElementById(value).submit();
}

function showDetails(details) {
	if (target != '') {
		var olddiv = document.getElementById(target);
		olddiv.style.display = 'none';
		var form = document.getElementById('step2');
		form.reset();
	}	
	if (details != 'none') {
		var div = document.getElementById(details);
		div.style.display = 'block';
		var num = div.id.slice(6);
		target = details;
		document.getElementById('select'+num).checked='checked';
	}else{
		document.getElementById('select1').checked='checked';
	}
}

function accountInfo(radio) {
	var login = document.getElementById('login');
	var create = document.getElementById('create');
	switch (radio.value) {
	case "yes":
		login.style.display = "inline";
		if (create.style.display == "inline") {
			create.style.display = "none";
		}
		break;
	case "no":
		create.style.display = "inline";
		if (login.style.display == "inline") {
			login.style.display = "none";
		}
		break;
	}
}

function update(cost,checkbox) {
	/*
	var para = document.getElementById('extra');
	var h3 = document.getElementById('totalcost');
	if (checkbox.className == 'off') {
		checkbox.className = 'on';
		extra += cost;
	}else{
		checkbox.className = 'off';
		extra -= cost;
	}
	para.innerHTML = "$"+extra;	
	h3.innerHTML="$"+(extra+poll);
	*/
}

function colorStep() {
	var step = document.getElementById('step').value;
	if (document.all) { step++; }
	
	switch (step*1) {
	case 1: document.getElementById('stepone').style.color='#99ccff'; break
	case 2: document.getElementById('steptwo').style.color='#99ccff'; break
	case 3: document.getElementById('stepthree').style.color='#99ccff'; break
	case 4: document.getElementById('stepfour').style.color='#99ccff'; break
	}
}

function toggleStyle(id) {
	var element = document.getElementById(id);
	if (element.className != 'price') {
	if (!element.className || element.className == 'inline') {
		element.className="selected";
	}else{
		element.className="inline"; 
	}
	}
}

function toggleDisplay(divid) {
	var container = document.getElementById(divid);
	if (container.className == 'closed') {
		container.className = 'open';
	}
	else if (container.className == 'open') {
		container.className = 'closed';
	}
}

function resetForm1() {
	var r = confirm("Reset all fields?");
	if (r) {
		var qs = document.getElementById('questiondiv');
		qs.innerHTML = '';	
		q_index = 1;
		outputQuestion(q_index);
	}else{  
		//do nothing	
	}	
}

function resetForm2() {
	var r = confirm("Reset all fields?");
	if (r) {
		document.getElementById('step2').reset();
		document.getElementById(target).style.display = "none";
	}else{
		//do nothing
	}
}

function resetForm3() {
	var r = confirm("Reset all fields?");
	if (r) {
		document.getElementById('step3').reset();
	}else{
		//do nothing
	}
}

function resetForm4() {
	var r = confirm("Reset all fields?");
	if (r) {
		document.getElementById('step4').reset();
	}else{
		//do nothing
	}
}

function closebox(id) {
	var container = document.getElementById(id);
	container.className="closed";
}

function removePoll(pollid) {
	var r = confirm("Permanently delete poll?");
	if (r) {
		window.location="http://constituentdynamics.com/accounts/?r="+pollid;	
	}else{
		//do nothing	
	}
}

function resumePoll(pollid,laststep) {
	if (laststep == '') { laststep = 1; }
	window.location="http://constituentdynamics.com/poll/?poll="+pollid+"&step="+laststep;
}
