/*----------------------------------------------------------------
	general functions for form checking
----------------------------------------------------------------*/
function checkmail(email)
{	
	return email.indexOf(".") && (email.indexOf("@") > 0)
}	

/*----------------------------------------------------------------
	specific functions for file addquestionnaire.xsl
	TODO: include into xslt-file or separate in an extra file
----------------------------------------------------------------*/
function validateAddQuestFormData()
{
	ok = true;
	if (document.fillout.name.value == '')
	{
		alert('questionnaire name is not set');
		ok = false;
	}
	
		if (document.fillout.desc.value == '')
	{
		alert('no questionnaire description entered');
		ok = false;		
	}
	
	return ok;
}

/*----------------------------------------------------------------
	specific functions for adding new user
	TODO: separate in an extra file
----------------------------------------------------------------*/
function validateFormData()
{	
	ok = true;
	email = document.fillout.contact.value;	
	if (!checkmail(email))
	{
		alert(email + ' is not a valid email');
		ok = false;
	}
	
	if (document.fillout.name.value == '')
	{
		alert('group name is not set');
		ok = false;
	}

	if (document.fillout.desc.value == '')
	{
		alert('no group description entered');
		ok = false;		
	}
	
	if (document.fillout.pw.value == '')
	{
		alert('no password entered');
		ok = false;		
	}
	
	/*
	if (document.fillout.admin_pw.value != document.fillout.admin_pw2.value)
	{
		alert('passwords entered are not equal!');
		ok = false;		
	}
	*/
	return ok;
}	

/*
 * All form checking for mandatory answers has been moved to the server side
 */
function checkMandatoryQuestionFilledout(isMandatory, qType, style, errorMsg, defAnswerText, numberExpErr, numberInRangeErr)
{
	setTimeNeeded();	
	return true;
}
/*
	check on the client side if a question was filled out completely
	(only if question is not mandatory)
	parameters:
		+ isMandatory: if not true, do not check if form is completely filled out
		+ qType: the question type
		+ errorMsg: the error message which should be displayed when the form is not correctly filled out
	Return:
		+ true: if all fields are filles out, so submitting can be proceeded
		+ false: if one mandatory field was not filled out 
	TODO repair this method to enable client sided feedback
*/
function checkMandatoryQuestionFilledout_old(isMandatory, qType, style, errorMsg, defAnswerText, numberExpErr, numberInRangeErr)
{
	//alert(isMandatory + ";" + qType +  ";" + errorMsg  +  ";" + defAnswerText);
	if (!isMandatory)
	{
		//setTimeNeeded();
		return true;
	}
	
	if (qType == 'openEndedQ')
	{
		val = document.forms["fillout"].openText.value;
		if (val == '' || val == defAnswerText)
		{
			addError(errorMsg);
			return false;
		} else
		{
			number_field = document.getElementById('number_exp');							
			if (number_field != null)
			{
				number_val = number_field.value;							
				if (!isNumber(number_val))
				{
					addError(numberExpErr);
					return false;
				}				
			} 
			setTimeNeeded();
			return true;
		}
	} else if (qType == 'questionMatrix')
	{
		subQIdx = 1;
		hasRadio = true;
		while (hasRadio)
		{
			altName = "alternatives_" + subQIdx;
			radioBoxes = document.getElementsByName(altName);
			if (radioBoxes.length == 0)
			{
				hasRadio = false;
			}
			if (hasRadio && !isOneRadioBoxOn(radioBoxes))
			{
				addError(errorMsg);
				return false;
			}

			subQIdx = subQIdx +	1;
		}
	} else if (qType == 'openendedMatrixQ') /* TODO currently only checking of number types is implemented */
	{
		allIds = getInputElemsStartingWithId('number_exp');
		str = "";
		for (i = 0; i< allIds.length;i = i + 1)
		{
			val = allIds[i].value;
			if (val == '' || val == defAnswerText)
			{
				addError(errorMsg);
				return false;
			} else if (!isNumber(val))
			{
				addError(numberExpErr);
				return false;
			}				
		}
	} else if (qType == 'intervalQMatrix' || qType == 'semanticDiffMat')
	{
		
		allIds = getInputElemsStartingWithId('intervals');
		if (allIds.length == 0)
		{
			allIds = getElemsStartingWithId('select', 'intervals');
		}
		
		for (i = 0; i< allIds.length;i++)
		{
			val = allIds[i].value;
			//alert(val);
			if (val == '' || val == -1)
			{
				addError(errorMsg);
				return false;
			} else if (!isNumber(val))
			{
				addError(numberExpErr);
				return false;
			}
			if (style == 'text_slider' && (val < 1 || val > 10))
		
			{
				addError(numberInRangeErr + ": 1-10");
				return false;
			}				
		}
		
	} else if (qType = 'closedRankingQ')
	{
	}
 
	//setTimeNeeded();
	return true; 
}

function setTimeNeeded()
{
	//everything went ok, so set the time needed
	
	timeEnd = new Date().getTime();
	needed = timeEnd - timeStart;	
	f =  document.getElementById('fillout');	
	f.duration.value = needed;
}
/*
Add an error string entry to the error div box
*/
function addError(errorMsg)
{
	var errorDiv = document.getElementById("error_wrap");	
	errorDiv.style.display = "block";
	var error = '<div id="errors" class="errors"><table><tr><td class="error_text">' + errorMsg +  '</td></tr></table></div>';		
	//errorDiv.innerHTML = error + errorDiv.innerHTML;
	errorDiv.innerHTML = error;		
}
