var cal = new CalendarPopup();
cal.setReturnFunction("setMultipleValues");
function setMultipleValues(y,m,d) {
     document.forms[0].date_year.value=y;
     document.forms[0].date_month.selectedIndex=m;
     document.forms[0].date_date.selectedIndex=d;
}
var cal1 = new CalendarPopup();
cal1.setReturnFunction("setMultipleValues1");
function setMultipleValues1(y,m,d) {
     document.forms[0].date_year1.value=y;
     document.forms[0].date_month1.selectedIndex=m;
     document.forms[0].date_date1.selectedIndex=d;
}

function addRowToTable()
{
  var tbl = document.getElementById('oTable');
  var lastRow = tbl.rows.length;
  // if there's no header row in the table, then iteration = lastRow + 1
  var iteration = lastRow-1;
  var row = tbl.insertRow(lastRow-1);

  // left cell
  var cellLeft = row.insertCell(0);
  var textNode = document.createTextNode('Answer'+(iteration-7));
  cellLeft.appendChild(textNode);

  // right cell
  var cellRight = row.insertCell(1);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'answer[]';
  el.id = 'txtRow' + iteration;
  el.size = 40;

  el.onkeypress = keyPressTest;
  cellRight.appendChild(el);

  // select cell
  //var cellRightSel = row.insertCell(2);
  //var sel = document.createElement('select');
  //sel.name = 'selRow' + iteration;
  //sel.options[0] = new Option('text zero', 'value0');
  //sel.options[1] = new Option('text one', 'value1');
  //cellRightSel.appendChild(sel);
}

function keyPressTest(e, obj)
{
  var validateChkb = document.getElementById('chkValidateOnKeyPress');
  if (validateChkb.checked) {
    var displayObj = document.getElementById('spanOutput');
    var key;
    if(window.event) {
      key = window.event.keyCode;
    }
    else if(e.which) {
      key = e.which;
    }
    var objId;
    if (obj != null) {
      objId = obj.id;
    } else {
      objId = this.id;
    }
    displayObj.innerHTML = objId + ' : ' + String.fromCharCode(key);
  }
}

function removeRowFromTable()
{
  var tbl = document.getElementById('oTable');
  var lastRow = tbl.rows.length;
  if (lastRow > 10) tbl.deleteRow(lastRow - 2);
}

function edit(q_id){
  var area = document.getElementById("qBox");
  area.style.display = '';
  xmlHttpGet('/app/questionnaire/index.php?adm=1&question_id='+q_id, 'qBox');

}

function removeAnswerFromQuestion(q_id, a_id){
    var xmlhttp = getXmlhttp();
    var url = "/app/questionnaire/index.php?action=remove_answer&question_id="+ q_id +"&answer_id=" + a_id;
    xmlhttp.open('POST', url, true);
    xmlhttp.onreadystatechange = function(){
     if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
       res = xmlhttp.responseText;
       if(res == 1){
         alert("success");
       }else{
         alert("failed");
       }
     }
    }
   xmlhttp.send(null); 
   // load the box
   edit(q_id);
   // load new list
   loadQuestionnaire('adm');
}


function displayPieChart(params, elementId){
 var xmlhttp = getXmlhttp();
    params = 'answer=' + params;
    var url = "/app/questionnaire/drawgraph.php";
    xmlhttp.open('POST', url, true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", params.length);
    xmlhttp.setRequestHeader("Connection", "close");
    //xmlhttp.setRequestHeader('Content-Type', 'image/png');
    xmlhttp.onreadystatechange = function(){
     if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
      //res = xmlhttp.responseText;
      //var obj = getElementFromName(elementId);
      //if(obj) obj.innerHTML= xmlhttp.responseText;
      //fixPNGImages(elementId);
     }
    }
   xmlhttp.send(params);
}

function Vote(){
    var ulobj=document.getElementById("question");
    var test = ulobj.getElementsByTagName("input");

    len =   test.length;  //document.form1.selection.length;
    var question = "";
    for (i = 0; i <len; i++) {
        if(test[i].checked){
           //alert(i+'   '+test[i].name+'  '+test[i].value);

           if(question.indexOf(test[i].name) >= 0){
               question = question + test[i].value +",";
           }else{
               if(question.length == 0)
                 question = test[i].name + "=" + test[i].value +",";
               else
                 question = question.substr(0, question.length-1) +'@'+ test[i].name + "=" + test[i].value +",";
           }
        }
    }

    if(question.length == 0)	{
			alert("答えを選択してください。");
		} else {
	    var xmlhttp = getXmlhttp();
	    var url = "/app/questionnaire/index.php?action=submit&answer="+question.substr(0, question.length-1);
	    xmlhttp.open('POST', url, true);
	    xmlhttp.onreadystatechange = function(){
	     if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
	       res = xmlhttp.responseText;

	       if(res == 1){
	         alert("投票ありがとうございます。");
	       }else{
	         alert("投票はお一人様1回限りです。");
	         loadQuestionnaire();
	       }
	     }
	    }
	   xmlhttp.send(null);
		}
}

// To post comment from the result page.
function PostComment()	{
  var cmtobj			=	document.getElementById("commentform");
  var commenter		=	document.getElementById("commenter").value;
	commenter		=	encodeURI(commenter);
  var commenttext	=	document.getElementById("commenttext").value;
	commenttext	=	encodeURI(commenttext);
  var qid					=	document.getElementById("qid").value;
  var uid					=	document.getElementById("uid").value;

	if(uid == 99999999)	{
		alert("コメントを投稿いただくにはログインが必要です。");
	} else {
		if(commenttext.length == 0)	{
			alert("コメントが未記入です。");
		}	else	{
			var xmlhttp = getXmlhttp();
			var url = "/app/questionnaire/index.php?action=postcomment&uid="+uid+"&qid="+qid+"&commenter="+commenter+"&commenttext="+commenttext;
			//alert(url);
			xmlhttp.open('POST', url, true);
			xmlhttp.onreadystatechange = function(){
				if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
					res = xmlhttp.responseText;
					//alert(res);
					if(res == 1){
						alert("コメントをありがとうございます。");
						location.reload();
					}else{
						alert("コメントの前に投票をお願いします。");
						loadQuestionnaire();
					}
				}
			}
			xmlhttp.send(null);
		}
	}
}

function loadQuestionnaire(user){
 var area = document.getElementById("questionnaire");
 area.style.display = '';

 if(user == 'adm'){
   url = '/app/questionnaire/index.php';
 }else{
   url = '/app/questionnaire/index.php?from=pf';
 }
  xmlHttpGet(url , 'questionnaire');
}

function openResultGraph(qid) {
  openNewWindow('/app/questionnaire/index.php?result=true&qid='+qid+'&from=pf', 'PieGraph' , 'resizable=yes, toolbar=no, scrollbars=yes, personalbar=no, menubar=no, width=650, height=500');
}

function updateStatus(question_id, status){

    var xmlhttp = getXmlhttp();
    var url = "/app/questionnaire/index.php?action=update_status&question_id=" + question_id + "&status="+status;
    xmlhttp.open('POST', url, true);
    xmlhttp.onreadystatechange = function(){
     if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
       res = xmlhttp.responseText;

       if(res == 1){
         alert("success");
         loadQuestionnaire('adm');
       }else{
         alert("failed");
       }
     }
    }
   xmlhttp.send(null);

}

function search_question(){
    var start_date = document.qForm.date_year.value+document.qForm.date_month.value+document.qForm.date_date.value;
    var end_date   = document.qForm.date_year1.value+document.qForm.date_month1.value+document.qForm.date_date1.value;
    var site_id    = document.qForm.site_id.value;
    var question   = document.qForm.question.value;
    var user_type  = getValue(document.qForm.user_type);
    var status     = getValue(document.qForm.status);

    var params  = "adm=1&start_date="+start_date+"&end_date="+end_date+"&site_id="+site_id+"&question="+question+"&user_type="+user_type+"&status="+status;
    var url = "/app/questionnaire/index.php?"+params;
    xmlHttpGet(url , 'show_questionnaire');
}

function getValue(radioObj){
  if (radioObj)
      len  = radioObj.length;
  else
      return;

  for(var i = 0; i < len; i++) {
      if(radioObj[i].checked) {
         return radioObj[i].value;
      }
  }

}





