/*
** View Mode Core
**
** @author
** @package
** @version 1.3
*/

/*
** INIT
*/
$(document).ready(function()
{ 
	$("#fireForm").jqm({ modal: true });
	
	// Add asterix to required fields
	addRequiredLabel();
})

/*
** Add asterix to label of
** required questions.
*/
function addRequiredLabel()
{
	var reqHtml = "&nbsp;<span class=\"field-required\">*</span>";
	var tbody = document.getElementById("form-tbody");   // Get tbody (contains questions)
	
	for(var i = 0; i < tbody.rows.length; i++)
	{
		var qRow = tbody.rows[i];
		
		// Skip spacer rows
		if(qRow.getAttribute("class") == "question-row")
		{
			var qid = getNum(tbody.rows[i].getAttribute("id"));
			
			// Check if question required
			if(questions[qid]["answer_required"] == "1")
			{
				// Append reqHtml to label
				$("#fireFormRow" + qid).find(".question-label label").append(reqHtml);   
			}
		}
	}
	
	return false;
}

/*
** Validates, posts response data 
** (Ajax) to fireformsubmit.php.
*/
function submitFireForm(formId)
{
	var isValid = true;
	
	/** Call validate function for each question ****/
	/*for(var i in questions)
	{
		if(typeof(questions[i]) != 'function')
		{
			var q = questions[i];
			
			if(!funcValidate[q.question_type](q.id)) isValid = false;
		}
	}*/
	
	if(isValid)
	{
		// Ajax options
		var options =
		{
	    	dataType: 'json',
	    	type: 'POST',
			error: function(data, status, e) 
			{
				var err = config.error.unexpected;
				
				if(typeof(e) == 'string') err += '\n\nError Details:' + e;
				
				fireFormAjaxError(err);
			},
			beforeSubmit: function() { /*fireFormAjaxStart();*/ },
			success: function(json)
			{
				if(typeof(json) != 'object' || typeof(json.error) == 'undefined') fireFormAjaxError(config.error.unexpected);
				else if(!empty(json.error))
				{
					var invalidQuestions = [];
					
					/** Loop invalid questions in json response ****/
					if(typeof(json.invalid_questions) != 'undefined')
					{
						for(var i in json.invalid_questions)
						{
							if(typeof(json.invalid_questions[i]) != 'function')
							{
								var q = questions[json.invalid_questions[i]];
								invalidQuestions[invalidQuestions.length] = q.id;	
								
								funcShowErrors[q.question_type](q.id);							
							}						
						}
					}
				
					for(var i in questions)
					{
						if(typeof(questions[i]) != 'function')
						{
							if(!invalidQuestions.inArray(i, true))
							{
								var q = questions[i];
								funcHideErrors[q.question_type](q.id);	
							}
						}
					}
				
					fireFormAjaxError(json.error);
				}
				else   // Successful; redir to landing
				{
					//document.location.href = json.url;
					parent.location.href = json.url;   // Redir PARENT (not iframe - CuBit module version)
					fireFormAjaxComplete();
				}        	
			}
		};
		 
		$('#fireFormForm' + formId).ajaxSubmit(options);			
	}
	else
	{
		var error = new Array(config.error.invalid1, config.error.invalid2);
		fireFormAjaxError(error);
	}

	return false;
}
