var survey_formId;
var survey_validator;


function pageReload() {
  $('page_form').innerHTML = "<img src='/loading.gif' /> Redirecting...";
  window.location.reload();
}


/* Allow to use console.log() for IE */
if (typeof(console) == 'undefined') {
	var console = {log: function(s) {alert(s);}}
}


function survey_register_validation(formId)
{
	survey_formId = formId; /* remember last used formId */
	survey_validator = new Validation(formId,{
		immediate : true
	});
}

Object.extend(Validation, {
	isMatrix: function(name, elm) {
		return ((name=='validate-one-required-in-tablerow') && (elm.tagName.toUpperCase()=='INPUT'));
	},
	
	buildAdviceRow : function(name, elm, errorMsg) {
		// Special Handling for matrix (or validation in a table)
		if (this.isMatrix(name, elm)) {
			advice = '<div class="validation-advice" id="advice-' + name + '-' + Validation.getElmID(elm) +'" style="display:none">' + errorMsg + '</div>';
			var p = elm.parentNode;
			if(p) {
				if (p.tagName == 'TD') {
					// Adds a full row
					p  = p.parentNode; // TR
					nb = p.cells.length;
					advice = "<tr><td colspan='"+nb+"'>"+advice+"</td></tr>";
					new Insertion.After(p, advice);
				}
				else {
					new Insertion.Bottom(p, advice);
				}
				
			} else {
				new Insertion.After(elm, advice);
			}
			return Validation.getAdvice(name, elm);
		}
		else {
			// Regular execution
			return Validation._buildAdvice_real(name, elm, errorMsg);
		}
	}, 
	
	_showAdvice_normal : Validation.showAdvice,
	
	showAdviceRow : function(name, elm, v, useTitle) {
		var advice = Validation.getAdvice(name, elm);
		if(advice == null) {
			var errorMsg = useTitle ? ((elm && elm.title) ? elm.title : v.error) : v.error;
			advice = Validation.buildAdviceRow(name, elm, errorMsg);
		}
		var trEl = elm.parentNode.parentNode;
		Element.show(trEl);
		
		if(typeof Effect == 'undefined') {
			advice.style.display = 'block';
		} else {
			new Effect.Appear(advice, {duration : 1 });
		}
	},

	showAdvice : function(name, elm, v, useTitle) {
		if (this.isMatrix(name, elm)) {
			Validation.showAdviceRow(name, elm, v, useTitle);
		}
		else {
			// Normal execution
			Validation._showAdvice_normal(name, elm, v, useTitle);
		}
	},
	
	_hideAdvice_normal : Validation.hideAdvice,
	
	hideAdviceRow : function(name, elm) {
		var advice = Validation.getAdvice(name, elm);
		if(advice != null) {
			var trEl = advice.parentNode.parentNode;
			Element.hide(trEl);
			advice.hide();
		}
	},
	
	hideAdvice : function(name, elm) {
		if (this.isMatrix(name, elm)) {
			Validation.hideAdviceRow(name, elm);
		}
		else {
			// Normal execution
			Validation._hideAdvice_normal(name, elm);
		}
	}
});




function survey_unregister_validation()
{
	/*
		NOT COMPLETE!! do NOT USE
		
		In order for this to work, we would need to modify validation.js
		to keep track of registered events through Event.observe
		and add a function like Validation.cancel()
	*/
	if (!typeof(survey_validator) != 'undefined') {
		//console.log('resseting validation');
		survey_validator.reset();
		survey_validator = null;
	}
}


function survey_q_open_changed(openEl, qId, renderType)
{
	//console.log('q_open_changed');
	if (renderType == 'TextArea') {
		valid = (openEl.value.strip().length > 0);
	}
	else {
		valid = (openEl.value.strip().length > 0);
	}
	if (valid) {
		//survey_unregister_validation();
		survey_clear_q_dna(qId);
		survey_clear_q_other(qId);
	}
}

function survey_q_radio_changed(radioEl, qId, renderType)
{
	//console.log('q_radio_changed');
	survey_clear_q_other(qId);
}

function survey_q_checkbox_changed(chkEl, qId, renderType)
{
	//console.log('q_checkbox_changed');
	//console.log(chkEl);
	//console.log(qId);
	choices = survey_q_checkbox_get_checked_elements(qId);
	if (choices.length > 0) {
		survey_clear_q_dna(qId);
	}
}

function survey_q_checkbox_get_checked_elements(qId)
{
	var choices = [];
	var type = 'checkbox';
	// var type = null;
	Form.getInputs(survey_formId, type, 'Q_'+qId+'[]').each(function(checkboxEl) {
		if (checkboxEl.checked) {
			//console.log(checkboxEl);
			choices.push(checkboxEl);
		}
	});
	return choices;
}


function survey_q_multi_choice_changed(cEl, qId, renderType)
{
	//console.log('q_multi_choice_changed');
	
	choices = survey_q_multi_choice_get_checked_elements(qId, renderType);
	if (choices.length > 0) {
		survey_clear_q_dna(qId);
		survey_clear_q_other(qId);
	}
}


function survey_q_multi_choice_get_checked_elements(qId, renderType)
{
	var choices = [];
	//var type = 'checkbox';
	var type = null;
	Form.getInputs(survey_formId, type, 'Q_'+qId+'[]').each(function(checkboxEl) {
		if (checkboxEl.checked) {
			//console.log(checkboxEl);
			choices.push(checkboxEl);
		}
	});
	return choices;
}


function survey_q_other_changed(otherEl, qId, renderType)
{
	//console.log('survey_q_other_changed()');
	//console.log(otherEl);
	//if (typeof(console)!='undefined') {console.log(otherEl.checked)} else {alert(otherEl.checked);}
	//if (typeof(console)!='undefined') {console.log(qId)} else {alert(qId);}
	//if (typeof(console)!='undefined') {console.log(renderType)} else {alert(renderType);}
	
	if (otherEl.checked) {
		survey_clear_q_dna(qId);
		if (renderType != "Checkboxes")
			survey_clear_q_answer(qId, renderType);
		//alert(i18n_lang);
		aId = "Q_" + qId + "_other_value";
		Field.focus(aId);
	}
}

function survey_q_other_value_focus(otherTextEl, qId, renderType)
{
	//console.log('q_other_value_focus()');
	aId = "Q_" + qId + "_other";
	aEl = $(aId);
	if (aEl && !aEl.checked) {
		aEl.checked = "checked";
		survey_q_other_changed(aEl, qId, renderType);
	}
	Field.focus(otherTextEl);
}


function survey_q_dna_changed(dnaEl, qId, renderType)
{
	//console.log('survey_q_dna_click()');
	if (dnaEl.checked) {
		survey_clear_q_answer(qId, renderType);
		survey_clear_q_other(qId);
	}
}


function survey_clear_q_dna(qId)
{
	//console.log('survey_clear_q_dna()');
	aId = "Q_" + qId + "_dna";
	if (aEl = $(aId)) {
		aEl.checked = false;
	}
}


function survey_clear_q_other(qId)
{
	//console.log('survey_clear_q_other()');
	aId = "Q_" + qId + "_other";
	if (aEl = $(aId)) {
		aEl.checked = false;
	}
	
	aId = "Q_" + qId + "_other_value";
	if (aEl = $(aId)) {
		//alert('resetting q_other value');
		aEl.value = "";
	}
	
	aId = "advice-validate-empty-string-Q_" + qId + "_other_value";
	if (aEl = $(aId)) {
		aEl.style.display = "none";
	}
}


function survey_clear_q_answer(qId, renderType)
{
	//console.log("survey_clear_q_answer(qId="+qId+", renderType="+renderType+")");
	switch (renderType)
	{
		case 'MatrixCheckboxes': survey_clear_q_multi_choices_question(qId, renderType); break;
		case 'TextArea': survey_clear_q_open_question(qId, renderType); break;
		case 'Open': survey_clear_q_open_question(qId, renderType); break;
		case 'Checkboxes': survey_clear_q_checkboxes(qId); break;
		default: break;
	}
}


function survey_clear_q_open_question(qId, renderType)
{
	//console.log('survey_clear_open_question()');
	aId = "Q_" + qId;
	if (aEl = $(aId)) {
		if (renderType == 'TextArea') {
			aEl.value = "";
		}
		else {
			aEl.value = "";
		}
		//if (Element.hasClassName(aEl, 'required')) {
		//	Element.removeClassName(aEl, 'required');
		//}
	}
	
	aId = "advice-validate-empty-string-Q_" + qId;
	if (aEl = $(aId)) {
		aEl.style.display = "none";
	}
}


function survey_clear_q_checkboxes(qId)
{
	survey_q_checkbox_get_checked_elements(qId).each(function(el){
		el.checked = false;
	});
}

function survey_clear_q_multi_choices_question(qId, renderType)
{
	//console.log('survey_clear_q_multi_choices_question()');
	
	survey_q_multi_choice_get_checked_elements(qId, renderType).each(function(el){
		el.checked = false;
	});
}
