// All function related to the Survey Design

function WindowResize()
{
	try
	{
		switch (WSize)
		{
			case "full-size": 
				WWidth  = screen.availWidth*2/3;
				WHeight = screen.availHeight;
				break;
			case "medium-size": 
				WWidth  = screen.availWidth/2;
				WHeight = screen.availHeight/2;
				break;
			case "custom-size": 
				WWidth  = Math.min(screen.availWidth, WWidth);
				WHeight = Math.min(screen.availHeight, WHeight);
				break;
        }
        
        var left = screen.availWidth/2 - WWidth/2;        
        var top = screen.availTop;
        if (top == null) // ie, let's reduce a little the size of the window and center it in the screen
        {
			top = screen.Height - screen.availHeight;
			WHeight -= top;
		}
        window.moveTo(left, top);				
		window.resizeTo(WWidth , WHeight);
	}
	catch (e) {};
}

function AddQuestion(qArray,id,description,maxsel,qtype,qShowMode,relTextId,no,linkNo,qIsMandatory)
{
		qArray.questions[qArray.Count]= new Question(id,description,maxsel,qtype,qShowMode,relTextId,no,linkNo,qIsMandatory);
		qArray.Count++;
}

function Question(id, description, maxsel, qtype, qShowMode, relTextId, no, linkNo, qIsMandatory)
{
	this.QuestionId		= id;
	this.Description	= description;
	this.MaxSelections	= maxsel;
	this.QuestionType	= qtype;
	this.qShowMode		= qShowMode;
	this.RelTextId		= relTextId;
	this.QuestionNo		= no;
	this.linkNo			= linkNo;
	this.qIsMandatory	= qIsMandatory;
	this.Answers		= new AnswerArray();
	this.Options		= new OptionArray();			
}

function QuestionArray()
{
	this.Count = 0;
	this.questions = new Array();
}

function AddAnswer(aArray,id,description,hasRelText,mainCtlId,relTextId,relPromptText)
{
	aArray.answers[aArray.Count] = new Answer(id,description,hasRelText,mainCtlId,relTextId,relPromptText);
	aArray.Count++;
}

function AnswerArray()
{
	this.Count = 0;
	this.answers = new Array();
}

function Answer(id, description, hasRelText, mainCtlId, relTextId, relPromptText)
{
	this.AnswerId			= id;
	this.Description		= description;
	this.HasRelatedText		= hasRelText;
	this.RelatedPromptText	= relPromptText;
	this.MainControlId		= mainCtlId;
	this.RelTextId			= relTextId;
}

function AddOption(optionsArray, id, description, prefixCtrlId, optionsAmount)
{
	optionsArray.options[optionsArray.Count] = new Option(id, description, prefixCtrlId, optionsAmount);
	optionsArray.Count++;
}

// creates a new array of options
function OptionArray()
{
	this.Count = 0;
	this.options = new Array();
}

// creates a new options set
function Option(id, description, prefixCtrlId, optionsAmount)
{
	this.AnswerId      = id;
	this.Description   = description;
	this.PrefixCtrlId  = prefixCtrlId;
	this.OptionsAmount = optionsAmount;
}

// Events Management
function UpdateCBRelText(quest, answ)
{
	var cb = document.activeElement;
	var idx = cb.id.indexOf("CBAnswerQId" + quest + "AId" + answ);
	var relTextId = cb.id.substring(0,idx);
	var relText = document.getElementById(relTextId + "AnswerRelTextQId" + quest + "AId" + answ);
	if (relText != null) 
		relText.disabled = !relText.disabled;
}
	
function UpdateRBRelText(bId, quest, answ)
{	
	isNS = (navigator.appName.indexOf('Netscape') > -1) ? true : false;		
	var idx;
	var reltextID;
	var rb;
	if (isNS)
	{
		var test = 'RBAnswerQId' + quest + 'AId' + answ;
		idx = bId.indexOf('RBAnswerQId' + quest + 'AId' + answ);
		reltextID = 'SurveyCtl_SurveyRepeater_Item' + quest;
	}
	else
	{	
		rb = document.activeElement;
		idx = rb.id.indexOf('RBAnswerQId' + quest + 'AId' + answ);
		reltextID = rb.id.substring(0,idx);	
	}		
	var relText = document.getElementById(reltextID + "AnswerRelTextQId" + quest + "AId" + answ);
	if (relText != null) 
	{	
		relText.disabled = false;
	}
	for (var i = 0; i < questionArray.Count; i++)
	{
		if (questionArray.questions[i].QuestionId == quest)
		{
			for (var j = 0; j < questionArray.questions[i].Answers.Count; j++)
			{
				var answer = questionArray.questions[i].Answers.answers[j];
				var AnswerId = answer.AnswerId;
				if (AnswerId != answ)
				{	
					relText = document.getElementById(reltextID + "AnswerRelTextQId" + quest + "AId" + AnswerId);
					if (relText != null) 
						relText.disabled = true;
				}
			}
		}
	}
}

// process a single question
function CanSubmitSingleQuestion(question)
{
	var obj = document.getElementById(SurveyControlClientId + "_" + question.RelTextId);
	return ((obj != null && obj.value != "") || question.qIsMandatory == "False");
}

// process a dropdownlist question
function CanSubmitDropDownListQuestion(question)
{
	var obj = document.getElementById(SurveyControlClientId + "_" + "DropDownListQId" + question.QuestionId);
	return ((obj != null && obj.selectedIndex >= 1) || question.qIsMandatory == "False");
}


// process a radio button question
function CanSubmitRadioButtonQuestion(question)
{
	// skipping analyzing the answers if question is not mandatory
	if (question.qIsMandatory == "False")
		return true;
	
	for (var j = 0; j < question.Answers.Count; j++)
	{
		var answer = question.Answers.answers[j];		
		var obj = document.getElementById(SurveyControlClientId + "_" + answer.MainControlId);
		
		if (obj != null && obj.checked == true) 
			return true;
	}
	
	//no radio buttons checked...
	return false;
}

// process a checkbox question
function CanSubmitCheckBoxQuestion(question)
{
	// skipping analyzing the answers if question is not mandatory
	if (question.qIsMandatory == "False")
		return true;
	
	for (var j = 0; j < question.Answers.Count; j++)
	{
		var answer = question.Answers.answers[j];		
		var obj = document.getElementById(SurveyControlClientId + "_" + answer.MainControlId);
		
		if (obj != null && obj.checked == true) 
			return true;
	}
	
	//no checkboxes checked...
	return false;
}

// process a rate question
function CanSubmitRateQuestion(question)
{
	// skipping analyzing the answers if question is not mandatory
	if (question.qIsMandatory == "False")
		return true;
	
	var allOptionsChecked = false;

	// check all options, one by one, at least one must have been selected
	for (var j = 0; j < question.Options.Count; j++)
	{
		var option = question.Options.options[j];
		var prefix = SurveyControlClientId + "_" + option.PrefixCtrlId;
		allOptionsChecked = false;
		
		for (var k = 0; k < option.OptionsAmount; k++)
		{
			var obj = document.getElementById(prefix + k);
			allOptionsChecked = allOptionsChecked || (obj != null && obj.checked == true);
			if (allOptionsChecked)
				break;
		}
		
		if (!allOptionsChecked)
			return false;
	}
				
	return allOptionsChecked;
}

function CanSubmit() 
{	
	for (var i = 0; i < questionArray.Count; i++)
	{
		var AnswersChecked	= 0;
		
		//simple question found, only a textbox
		if (questionArray.questions[i].QuestionType == "True" && CanSubmitSingleQuestion(questionArray.questions[i])) 
			AnswersChecked++;		
		
		//dropdownlist question found
		if (questionArray.questions[i].qShowMode == "dropdown" && CanSubmitDropDownListQuestion(questionArray.questions[i])) 
			AnswersChecked++;
		
		//radio button question found
		if (questionArray.questions[i].qShowMode == "radio" && CanSubmitRadioButtonQuestion(questionArray.questions[i])) 
			AnswersChecked++;
		
		//checkbox question found
		if (questionArray.questions[i].qShowMode == "checkbox" && CanSubmitCheckBoxQuestion(questionArray.questions[i]))
			AnswersChecked++;		
			
		//rate question found
		if (questionArray.questions[i].qShowMode == "rate" && CanSubmitRateQuestion(questionArray.questions[i])) 
			AnswersChecked++;		
		
		if (AnswersChecked == 0)
		{
			alert("Please, answer question " + questionArray.questions[i].QuestionNo + ".");
			window.location.href = windowLocation + "#" + SurveyControlClientId + "_" + questionArray.questions[i].linkNo;
			return false;
		}			
	}
}