// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function update_form(value, div_id)
{
	var to_div = $(div_id);

	new Effect.Opacity(to_div, {duration: 0.2, from: 1.0, to: 0.0});
	
	if (value=="")
	{ 
		value = 0;
	}
	
	setTimeout("step_two("+value+", '"+div_id+"');", 200); 
	
}


function step_two(value, div_id)
{
	var to_div = $(div_id);
	
	if (value==0)
	{ 
		to_div.innerHTML = "";
		return;
	}
	
	var from_div = $("partial_form_"+ value);
	html = from_div.innerHTML;
	
	// replace a few things...
	html = html.replace(new RegExp("partial_form_preferred_image_question", "g"), "partial_form_preferred_image_question_");
	html = html.replace(new RegExp("rate_a_phrase_choices", "g"), "rate_a_phrase_choices_");
	
	to_div.innerHTML = html;	
	
	new Effect.Opacity(to_div, {duration: 0.2, from: 0.0, to: 1.0});
}

function insert_image_form(to_id, from_id)
{
	var re = new RegExp("uploaded_data", "g");
	var re2 = new RegExp("customize_behaviour", "g");
	var re3 = new RegExp("redirects_to", "g");
	var html = $(from_id).innerHTML;
	html = html.replace(re, "uploaded_data_" + i );
	html = html.replace(re2, "customize_behaviour_" + i);
	html = html.replace(re3, "redirects_to_" + i);
	new Insertion.Bottom(to_id, html);	
	
	i = i + 1;
}

function insert_rate_a_phrase_choice_form(to_id, from_id, value, redirect)
{
	
	var re2 = new RegExp("choicey", "g");
	var re3 = new RegExp("redirects_to", "g");
	var re4 = new RegExp("customise_behaviour", "g");
	var re5 = new RegExp("insertvalue", "g");
	var re6 = new RegExp("insertredirect", "g");
	
	var html = $(from_id).innerHTML;
	if (redirect == -1 || redirect == null) {
		redirect = "";
		var re7 = new RegExp('checked="checked" id="question_customise_behaviour', 'g');
		html = html.replace(re7, 'id="question_customise_behaviour');
	}
	if (value == -1 || value == null) {
		value = "";
	}
	html = html.replace(re2, "choice_" + j);
	html = html.replace(re3, "redirects_to_" + j);
	html = html.replace(re4, "customise_behaviour_" + j);
	html = html.replace(re5, value);
	html = html.replace(re6, redirect);
	
	new Insertion.Bottom(to_id, html);	
	
	j = j + 1;	
}

function insert_recommend_a_friend_form(to_id, from_id)
{
	var re = new RegExp("questionnamey", "g");
	var re2 = new RegExp("questionemaily", "g");

	var html = $(from_id).innerHTML;
	html = html.replace(re, "question[name][" + k + "]");
	html = html.replace(re2, "question[email][" + k + "]");
	
	new Insertion.Bottom(to_id, html);	
	
	k = k + 1;	
}

function insert_multiple_rate_a_phrase_choice_form(to_id, from_id, comparator)
{
	var re = new RegExp("comparatory", "g");
	var re2 = new RegExp("insertComp", "g");

	var html = $(from_id).innerHTML;
	if (comparator == -1 || comparator == null) {
		comparator = "";
	}
	html = html.replace(re, "comparator][" + m );
	html = html.replace(re2, comparator);
	
	new Insertion.Bottom(to_id, html);	
	
	m = m + 1;	
}

function update_next_sibling_display(object)
{
	var object_to_control_display = object.parentNode.nextSiblings()[0];

	if (object_to_control_display.style.display == "block")
	{
		if (object.checked == false) { object_to_control_display.style.display = "none"; }
	}
	else
	{
		if (object.checked == true) { object_to_control_display.style.display = "block"; }
	}
}

function confirm_form_activation()
{
	// return true if there is no active box, it means that there is no question in that survey
	if ($('survey_active')==null) { disable_submit_tag("update_survey_submit_tag"); return true; }

  // return true if already active, but should never happen
  if (before_survey_active==1) { disable_submit_tag("update_survey_submit_tag"); return true; }

  // confirmation dialog
  if ($('survey_active').checked)
  { 
	  if (confirm("Are you sure to activate this survey? You will then not be able to edit it"))
	  {
	    disable_submit_tag("update_survey_submit_tag");
      return true;
    }
    else
		{
			return false;
		}
  }
  
  disable_submit_tag("update_survey_submit_tag");
  return true;
}

function disable_submit_tag(id)
{
	$(id).disabled=true;
	$(id).value='Please wait...';	
}

function one_radio_button_is_checked()
{
  var inputs = document.getElementsByTagName('input');
   
  var one_is_checked = false;
  for (var i=0; i<inputs.length; i++){
		if ((String(inputs[i].getAttribute('type'))=="radio" || String(inputs[i].getAttribute('type'))=="checkbox") && inputs[i].checked) { one_is_checked = true; }
  }

  return one_is_checked;
}
