// $Id: ajaxenquete.js,v 1.14 2007/05/07 07:25:55 yabuta Exp $

Hyjax.Component.enquete = Class.create();
Hyjax.Component.enquete.prototype = {
	initialize: function(url) {
		Hyjax.EventHandler.register(this);
		var jenqdir = "ajaxenq";
		if(url.indexOf("?") != -1){
			this.url = url.substring(0,url.indexOf("?"));
			this.param = url.substring(url.indexOf("?")+1,url.length);
		}
		else if(url.indexOf(jenqdir) != -1) {
			this.url = url.substring(0,url.indexOf(jenqdir)) + jenqdir + "/";
			this.param = url.substring(url.indexOf(jenqdir) + jenqdir.length + 1, url.length);
			if (this.param.length > 0) {
				this.param = this.param.split("/");
				this.param = this.param.join("&");
			}
		}else{
			this.url = url;
		}
	},
	eLoad: function(node) {
		new Ajax.Request(this.url, {method: 'post', parameters: this.param, onComplete: this.jenqueteDataDisplay});
	},
	jenqueteDataDisplay: function(responseHttpObj) {

		var str = responseHttpObj.responseText;
		$("include_jenquete").innerHTML = str;

	}

}

function res_jenq_callAjax(form,type,st){

	var ans;

	if(document.forms[form].vote.value){
		document.forms[form].vote.disabled=true;
	}

	if(document.forms[form].ans){
		if(type=="radio"){
			for (i=0; i<document.forms[form].ans.length; i++){
				if (document.forms[form].ans[i].checked){
					 ans = document.forms[form].ans[i].value;
				}
			}
		}else{
			ans = document.forms[form].ans.value;
		}
	}

	if(ans == null){
		ans = '99';
	}

	var parts;
	parts = res_jenq_makeparam(form, null, null, null, null, null, ans);
	res_jenq_Ajax(parts);

}

//結果を見る
function res_jenq_callResult(form){

	var parts;
	parts = res_jenq_makeparam(form, null, null, null, null, null, '');
	res_jenq_Ajax(parts);

}

//他のアンケート
function res_jenq_callOther(form){

	var parts;
	parts = res_jenq_makeparam(form, null, null, null, 2, null, '');
	res_jenq_Ajax(parts);

}

//設問画面＆過去結果画面
function res_jenq_callrest(form,id,st){

	var parts;
	parts = res_jenq_makeparam(form, id, null, null, st, null,'');
	res_jenq_Ajax(parts);

}

function res_jenq_makeparam(form, id, dev, pos, st, wh, ans) {

	var obj = document.forms[form];

	if(id == null && obj.qid) id = obj.qid.value;
	if(dev == null && obj.dev) dev = obj.dev.value;
	if(pos == null && obj.pos) pos = obj.pos.value;
	if(st == null && obj.st) st = obj.st.value;
	if(wh == null && obj.wh) wh = obj.wh.value;
	if(obj.mt) mt = obj.mt.value;
	if(obj.mr) mr = obj.mr.value;
	if(obj.mt) mb = obj.mb.value;
	if(obj.ml) ml = obj.ml.value;

	var parts = "id="+id+"&dev="+dev+"&pos="+pos+"&st="+st+"&wh="+wh+"&ans="+ans
				+"&mt="+mt+"&mr="+mr+"&mb="+mb+"&ml="+ml;
	return parts;

}	

//Ajaxページ表示
function res_jenq_Ajax(parts){

	new Ajax.Request(
		'/ajax_res/ajaxenq/',
		{
			method: 'post',
			parameters: parts,
			onComplete: function(responseHttpObj) {
				var str = responseHttpObj.responseText;
				$("include_jenquete").innerHTML = str;
			}
		}
	);

}
