//Hide cookies & Js warning message...
//
$(function() {
 checkForCookies();
});

/*
**Jquery onload
*/

var formSubmitted = false;
$(document).ready(function() {
	initSurvey();
});

function initSurvey()
{
	/* Stop double click form submission
	**
	*/
	$("#NEXT_PAGE, #PREVIOUS_PAGE, #LAST_PAGE").click(function(){
		$("#navigationButtons").hide();
	});
	
	/* Star rating transformation
	**
	*/
	$(".stars-wrap").stars();
	
	/*
	** Disable enter key on text inputs & selects
	*/
	$("input,select").keypress(function(e) {return e.which!= 13;});
}

function checkForCookies()
{
	var KEY = '43wEt34tA';
	$.cookie('test_cookies', KEY );
	if($.cookie('test_cookies')==KEY){
		$("#JsCookieWarning").hide();
	}
	
	$.cookie('test_cookies', null );
}

/* Stop event Propagation used on checkbox inside td with onclick
**
*/
function stopProp(event)
{
	if (event.stopPropagation)
		event.stopPropagation();
	else event.cancelBubble = true;
}

/* Return the value of the radio button that is checked
** return an empty string if none are checked, or
** there are no radio buttons
*/
function getCheckedValue(radioObj)
{
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

/* set the radio button with the given value as being checked
** do nothing if there are no radio buttons
** if the given value does not exist, all the radio buttons
** are reset to unchecked
**
** If toggle is set then value is inverted
** this is used for checkboxes
*/
function setCheckedValue(event, radioName, newValue, toggle)
{
	// Don't propogate the event to the document
	if (event.stopPropagation) {
	  event.stopPropagation();   // W3C model
	} else {
	  event.cancelBubble = true; // IE model
	}

	var radioObj = document.getElementsByName(radioName);

	if(!radioObj)
		return;
	
	var radioLength = radioObj.length;
	
	if(radioLength == undefined)
	{
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	
	for(var i = 0; i < radioLength; i++)
	{
		if(radioObj[i].value == newValue.toString())
		{
			if (toggle==true)
			{
				radioObj[i].checked = !radioObj[i].checked;
			}
			else
				radioObj[i].checked = true;
		}
	}

	/*
	**
	** Limit number of choices (multiple choice)
	**
	** TODO:	finish this implentation, you will need to disable elements that are not selected
	** Issues:	this function is only fired from a table row. We also need to fire the validation from the checkbox
	** info:	Recomends getting the row to fire the checkbox then calling validation function
	** see:     http://www.learningjquery.com/2008/12/quick-tip-click-table-row-to-trigger-a-checkbox-click
	**
	*/
	/*	
	//Get num checked
	//
	var numChecked=0;
	for(var i = 0; i < radioLength; i++){
		if(radioObj[i].checked) {
			numChecked++;
		}
	}
	
	//Get the number allowed
	//
	var inputName = $('[name="'+radioName+'"]').attr('name');
	inputName = "#SETTING_MAX_NUM_"+inputName;
	inputName = inputName.slice(0, -2);//remove []
	var maxNum = $(inputName).val();
	
	alert("numChecked="+numChecked);
	alert("maxNum="+maxNum);
	*/

}


/* Set the select box with the given value as being selected
** do nothing if there are no options
** if the given value does not exist, the select box is reset
*/
function setSelectValue(selectName, newValue)
{
	var selectObj = document.getElementsByName(selectName)[0];

	if(!selectObj)
		return;
		
	var selectLength = selectObj.options.length;

	for(var i = 0; i < selectLength; i++)
	{
		selectObj[i].selected = false;
		if(selectObj[i].value == newValue.toString())
			selectObj[i].selected= true;
	}
}

/* Clear matrixx style questions
**
*/
function clearMatrix(qNum,num)
{
	/*num is not used any more, it dosent work with piped ranking*/
	var num= $( "[name^=q"+qNum+"-]" ).length;
	for(var item=0;item<num;item++){
		$("[name=q"+qNum+"-"+item+"\\[\\]]").removeAttr('checked');
	}
	
	/*while(num--)
	{
		var a = document.getElementsByName('q'+qNum+'-'+num+'[]');
		var i = a.length;
		
		while (i--)
		{
			if(a[i]!=null)
			{
				if(a[i].type=='radio' || a[i].type=='checkbox')
				{
					a[i].checked = false;
				}
			}
		}
	}*/
}

/* Clear radio and checkbox questions
**
*/
function clearButtons(buttonGroup)
{
	var a = document.getElementsByName(buttonGroup);
	for (i=0; i < a.length; i++)
	{
		if (a[i].checked == true)
		{
			a[i].checked = false;
		}
	}
} 
