// 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)
{
	var re2 = new RegExp("choicey", "g");
	var re3 = new RegExp("redirects_to", "g");
	var re4 = new RegExp("customise_behaviour", "g");

	var html = $(from_id).innerHTML;
	html = html.replace(re2, "choice_" + j);
	html = html.replace(re3, "redirects_to_" + j);
	html = html.replace(re4, "customise_behaviour_" + j);
	
	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)
{
	var re = new RegExp("comparatory", "g");

	var html = $(from_id).innerHTML;
	html = html.replace(re, "comparator][" + m );
	
	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 confirm_send_invitation()
{
	var text = "Are you sure you want to send the invitation to this respondent?";
	if (number_of_respondents > 1)
	{
		var text = "Are you sure you want to send the invitation to these "+number_of_respondents+" respondents?";		
	}
	
	if (confirm(text))
	{ 
		disable_submit_tag("submit_form_tag");
		return true;
	} 
	else
	{ 
		return false;
	}
	return false;
}

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;
}

function insertAtCaret(obj, text) {
	if(document.selection) {
		obj.focus();
		var orig = obj.value.replace(/\r\n/g, "\n");
		var range = document.selection.createRange();

		if(range.parentElement() != obj) {
			return false;
		}

		range.text = text;
		
		var actual = tmp = obj.value.replace(/\r\n/g, "\n");

		for(var diff = 0; diff < orig.length; diff++) {
			if(orig.charAt(diff) != actual.charAt(diff)) break;
		}

		for(var index = 0, start = 0; 
			tmp.match(text) 
				&& (tmp = tmp.replace(text, "")) 
				&& index <= diff; 
			index = start + text.length
		) {
			start = actual.indexOf(text, index);
		}
	} else if(obj.selectionStart) {
		var start = obj.selectionStart;
		var end   = obj.selectionEnd;

		obj.value = obj.value.substr(0, start) 
			+ text 
			+ obj.value.substr(end, obj.value.length);
	}
	
	if(start != null) {
		setCaretTo(obj, start + text.length);
	} else {
		obj.value += text;
	}
}

function setCaretTo(obj, pos) {
	if(obj.createTextRange) {
		var range = obj.createTextRange();
		range.move('character', pos);
		range.select();
	} else if(obj.selectionStart) {
		obj.focus();
		obj.setSelectionRange(pos, pos);
	}
}

function insertHTMLtoTinyMCE(html) {
    tinyMCE.execInstanceCommand("mce_editor_0","mceInsertContent",false,html);
}