function uniqueInteger( ) {
    return uniqueInteger.counter++;
}


function changeSurveyQuestionType(id, mode){
    var mode=mode;

    Event.observe('SurveyQuestionType', 'change', function(){selectQuestionType($(id))}, false);
    Event.observe('addNewAnswer', 'click', function(){addNewAnswer($(id),mode)}, false);

    getId('SurveyQuestion').onkeyup = unlockLockSaveButton;
}


function formValidate(){

    var question_type = $('SurveyQuestionType').getValue();
    var key = uniqueInteger();

    if(question_type != 1000000 && key < 3)
	{
    	alert("Please create answers for this question.");
    	uniqueInteger.counter = uniqueInteger.counter - 1;
    	return false;
    }
    return true;
}


function selectQuestionType(obj){

	var question_type = $('SurveyQuestionType').getValue();

	if(question_type != 1000000)
	{
      	 getId('addNewAnswer').disabled = false;
	}
	else
	{
     	getId('addNewAnswer').disabled = true;
	}
}


function addNewAnswer(obj, mode){

	var question_type = $('SurveyQuestionType').getValue();

    if(question_type != 1000000)
	{
    	var key = uniqueInteger();
    }

    if (key > 50)
    {
        alert("Are you all right? You cannot add more than 50 answers");
        getId('addNewAnswer').disabled = true;
        return;
    }

	if(question_type != 1000000)
	{
	     var input_text = '<label id="SurveyAnswerLabel_'+key+'"><br /><br />Answer '+key+':</label> <input type="text"  style="width: 300px;" class="text" id="SurveyAnswer_'+key+'" name="data[Survey][p_answer_'+key+']" value=""> ';

	}

    if (mode=='edit')
       new Insertion.Before('AnswersNumber', input_text);
    else
	   new Insertion.Before(obj, input_text);
}


function unlockLockSaveButton()
{
    var question_field = $('SurveyQuestion').getValue();
    if (question_field.length > 3)
    {
    	getId('SaveButton').disabled = false;
    }
    else
    {
        getId('SaveButton').disabled = true;
    }
}