var OrgDNA = {
	simulation_id: 0,
	url_alias: '',
	windows: {},
	check_form: true,
	round: 0,

	Actions: {
		tag_id: null,
		current_id: 1,
		max_id: 28,
		next: function() {
			OrgDNA.Actions.setNewID(OrgDNA.Actions.current_id + 1);
			return false;
		},
		prev: function() {
			OrgDNA.Actions.setNewID(OrgDNA.Actions.current_id - 1);
			return false;
		},
		setNewID: function(new_id) {
			new_id = parseInt(new_id);
			if (new_id > OrgDNA.Actions.max_id) new_id = 1;
			if (new_id < 1) new_id = OrgDNA.Actions.max_id;
			$('action_card_'+OrgDNA.Actions.current_id).hide();
			$('action_card_'+new_id).show();
			OrgDNA.Actions.current_id = new_id;
			$('action_number').innerHTML = new_id;
		},


		emptyFunction: function() {}
	},
	
	changeAction: function(action_el) {
		var value = $F(action_el.id);
		if (value) {
			OrgDNA.Actions.setNewID(value);
		}
	},
	
	showInsights: function(url, width, height, attributes) {
		if (!url.length) return;
		// var show = OrgDNA.getRadioValue('includeinsights_yes');
		var show = 'yes';
		var show_get_var = (show == 'no' ? '0' : '1');
		url = OrgDNAUtil.addGetVarToURL('show',show_get_var,url);
		
		// add selected insights
		var checked, checkbox_el, action_number;
		var error_text = '';
		var checked_offset = 0;
		var checked_action_numbers = new Array();
		for (action_number=1; action_number <= 28; action_number++) {
			// ignore previously checked insights
			checkbox_el = $('insight_'+action_number);
			if (!checkbox_el) continue;
			
			// if this checkbox is not for this round, then ignore it
			if (checkbox_el.value != OrgDNA.round) continue;
			
			checked = checkbox_el.checked;
			if (checked) {
				// make sure we haven't checked more than 3
				if (checked_offset >= 3) {
					error_text += "More than three insights were selected.";
					break;
				}
				
				// save the action numbers
				checked_action_numbers[checked_offset] = action_number;
				++checked_offset;
			}
		};
		
		if (error_text.length) {
			alert("Please correct the following errors to continue:\n"+error_text);
			return false;
		}

		for (var i=0; i < checked_action_numbers.length; i++) {
			action_number = checked_action_numbers[i]

			// add the action number to the id
			url = OrgDNAUtil.addGetVarToURL('iid-'+i+'',action_number,url);

			// disable the field
			checkbox_el = $('insight_'+action_number);
			checkbox_el.disable();
		};

		// alert('show='+show+' ('+show_get_var+') url='+url+''); return false;
		OrgDNA.newWin('Insights', url, width, height, attributes);
		return false;
	},
	
	showOneInsight: function(url, action_number, width, height, attributes) {
		if (!url.length) return;
		url = OrgDNAUtil.addGetVarToURL('iid-0',action_number,url);
		OrgDNA.newWin('Insights', url, width, height, attributes);
		return false;
	},

	oepnPrintPofile: function(url, width, height, attributes) {
		if (!url.length) return;

		OrgDNA.newWin('PrintProfile', url, width, height, attributes);
		return false;
	},

	showActions: function(url, width, height, attributes) {
		if (!url.length) return;
		OrgDNA.newWin('Actions', url, width, height, attributes);
		
		return false;
	},
	
	getRadioValue: function(radio_id) {
		var frm = $(radio_id).form;
		var radio_name = $(radio_id).name;
		return $F($(frm).getInputs('radio', radio_name).find(
			function(re) {return re.checked;}
		));
	},
	
	submitGamePlay1Form: function() {
		// check duplicate actions
		var checked_count=0;

		var error_text = '';
		var year = OrgDNA.round;
		
		var action_checkbox;
		for (var action_number=1; action_number <= 28; action_number++) {
			action_checkbox = $F('a'+action_number+'y'+year);
			// alert('action_checkbox '+'a'+action_number+'y'+year+' was  '+action_checkbox);
			if (action_checkbox == null) {
				continue;
			}
			++checked_count;
		}

		if (checked_count < 0 || checked_count > 5) {
			error_text += 'Please check no more than 5 actions for this year.'+"\n";
		}

		if (error_text.length) {
			alert("Please correct the following errors to continue:\n"+error_text);
			return false;
		}
		
		
		// all actions are ok
		$('orgdna_form').submit();
		return false;
	},
	
	submitGamePlay2Form: function() {
		$('orgdna_form').submit();
		return false;
	},

	submitResultsForm: function() {
		$('orgdna_form').submit();
		return false;
	},
	
	checkPleaseWaitForm: function() {
		if (!OrgDNA.check_form) return false;
		OrgDNA.checkForm('orgdna_pleasewait');
	},

	checkResultsForm: function() {
		if (!OrgDNA.check_form) return false;
		OrgDNA.checkForm('orgdna_results');
	},
	
	checkForm: function(stage) {
		var method = 'checkPlayerAdvancement';
		var id = OrgDNA.tag_id;
		var component = 'orgdna_simulation_display';
		var sid = OrgDNAUtil.getCookie('sid');
		var url_alias = OrgDNA.url_alias;
		
		new Ajax.Updater('response_container', '/_awc_ajax', {evalScripts: true, parameters: 'method='+method+'&sid='+sid+'&id='+id+'&component='+component+'&url_alias='+url_alias+'&stage='+stage+''});
		return false;
	},
	
	submitPleaseWaitForm: function() {
		$('orgdna_form').submit();
		return false;
	},

	submitResultsForm: function() {
		$('orgdna_form').submit();
		return false;
	},
	
	newWin: function(id, url, width, height, attributes) {
		OrgDNA.windows[id] = window.open(url, id, 'width='+width+',height='+height+','+attributes); 
		OrgDNA.windows[id].focus();
	},
	

	emptyFunction: function() {}
};

var OrgDNAUtil = {
	addGetVarToURL: function(new_key, new_val, url) {
		var url_string = ''+url;
		var out_url_string = '';
		var offset = url_string.indexOf('?');
		var key_replaced = false;
		var get_string = false;
		if (offset > 0) {
			out_url_string = url_string.substring(0,offset+1);
			get_string = url_string.substring(offset+1);
			var fields = get_string.split('&');
			for (var f = 0; f < fields.length; f++) {
				if (f > 0) out_url_string += '&';
				var field = fields[f].split('=');
				var key = unescape(field[0].replace(/\+/g, ' '));
				if (key == new_key) {
					key_replaced = true;
					out_url_string += field[0] + '=' + encodeURIComponent(new_val).replace(/%2B/g,'+');
				} else {
					out_url_string += field[0] + '=' + field[1];
				}
			}
		} else {
			out_url_string = url_string;
		}
		if (!key_replaced) {
			out_url_string += (get_string ? '&' : '?') + encodeURIComponent(new_key).replace(/%2B/g,'+') + '=' + encodeURIComponent(new_val).replace(/%2B/g,'+');
		}
		return out_url_string;
	},
	
	getCookie: function(name) {
		var start = document.cookie.indexOf( name + "=" );
		var len = start + name.length + 1;
		if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
			return null;
		}
		if ( start == -1 ) return null;
		var end = document.cookie.indexOf( ";", len );
		if ( end == -1 ) end = document.cookie.length;
		return unescape( document.cookie.substring( len, end ) );
	}
	
}


var OrgDNAFac = {
	current_panel: 1,
	current_team: '',

	switchPanel: function(new_panel) {
		// call ajax
		OrgDNAFac.current_panel = new_panel;
		OrgDNAFac.refreshPanel();
		return false;
	},
		
	teamPulldownChanged: function() {
		var new_team = $('orgdna_team').value;
		OrgDNAFac.switchTeam(new_team);
	},
	
	switchTeam: function(new_team) {
		OrgDNAFac.current_team = new_team;
		OrgDNAFac.refreshPanel();
		return false;
	},
	
	refreshPanel: function() {
		var method = 'buildFacPanel';
		var id = OrgDNA.tag_id;
		var component = 'orgdna_simulation_display';
		var team = OrgDNAFac.current_team;
		var simulation_id = OrgDNA.simulation_id;
		var panel = OrgDNAFac.current_panel;
		
		new Ajax.Updater('panel_container', '/_awc_ajax', {parameters: 'method='+method+'&id='+id+'&component='+component+'&panel=panel'+panel+'&team='+team+'&simulation_id='+simulation_id+''});
		return false;
	},
	
	showResults: function(round) {
		// allow all players to move on to the results
		OrgDNAFac.updateApproval('results',round);
	},

	continueToRound: function(round) {
		// allow all players to move on to the next round
		OrgDNAFac.updateApproval('completion',round);
	},

	updateApproval: function(type, round) {
		$('response_container').update("updating...");
		var method = 'updateFacApproval';
		var id = OrgDNA.tag_id;
		var component = 'orgdna_simulation_display';
		var simulation_id = OrgDNA.simulation_id;
		
		new Ajax.Updater('response_container', '/_awc_ajax', {parameters: 'method='+method+'&id='+id+'&component='+component+'&type='+type+'&round='+round+'&simulation_id='+simulation_id+''});
		return false;
	}
	
}

