var aValidateAnswers=new Array();
var aRequiredAnswers=new Array();
var aValidateMinMax =new Array();
var sQuestionText="";

function doValidateSurvey(theForm){
	for (sQuestion in aValidateMinMax){
	 if(typeof(aValidateMinMax[sQuestion])!="function"){
		var sParam=aValidateMinMax[sQuestion];
		var aParam=sParam.split(",");
		var sQuestionType=aParam[0];
		var iMin=parseInt(aParam[1]);
		var iMax=parseInt(aParam[2]);
		var oElement=eval("theForm."+sQuestion);
		var iChecked=0;

		sQuestionText=eval("theForm."+sQuestion+"_text.value");
		for(var i=0;i<oElement.length;i++) if(oElement[i].checked)iChecked++;

		if((iMin==iMax)&&(iChecked!=iMin)){
			alert("Please choose "+iMin+" response(s) for question '"+sQuestionText+"'.");
			oElement[0].focus();
			return false;
		}

		if(iChecked<iMin){
			alert("Please choose at least "+iMin+" response(s) for question '"+sQuestionText+"'.");
			oElement[0].focus();
			return false;
		}

		if((iMax>0)&&(iChecked>iMax)){
			alert("You can only select a maximum of "+iMax+" responses for question '"+sQuestionText+"'.");
			oElement[0].focus();
			return false;
		}
	 }
	};

	for (sQuestion in aRequiredAnswers){
	 if(typeof(aRequiredAnswers[sQuestion])!="function"){
		sQuestionText=eval("theForm."+sQuestion+"_text.value");
		var sQuestionType=aRequiredAnswers[sQuestion];
		if((sQuestionType!="P1")&&(sQuestionType!="PR")&&(sQuestionType!="R")&&(sQuestionType!="SR")){
			if(!doRequiredAnswers(theForm,sQuestion)) return false;
		}
	 }
	}

	for (sQuestion in aValidateAnswers){
	 if(typeof(aValidateAnswers[sQuestion])!="function"){
		sQuestionText=eval("theForm."+sQuestion+"_text.value");
		var sQuestionType=aValidateAnswers[sQuestion];
		if(sQuestionType=="P1"){
			if(!doValidatePercentage100(theForm,sQuestion)) return false;
		}

		if(sQuestionType=="PR"){
			if(!doValidatePercentage(theForm,sQuestion)) return false;
		}
		
		if((sQuestionType=="R")||(sQuestionType=="SR")){
			if(!doValidateMatrix(theForm,sQuestion)) return false;
		}
	 }
	};
	return true;
}

function doRequiredAnswers(theForm,sQuestion){
	var focusElement=null;
	var oElement=eval("theForm."+sQuestion);
	if(oElement){
		if((oElement.type=="text")||(oElement.type=="textarea")){
			if(Trim(oElement.value)==""){
				oElement.focus();
				alert("Answering the question '"+sQuestionText+"' is required! Please enter the requested information.");
				return false;
			}
		}else if(oElement.type=="select-one"){
			if(oElement.value==""){
				oElement.focus();
				alert("Please answer the question ["+sQuestionText+"].\n\nAll questions marked with \"*\" must be answered.");
				return false;
			}
		}else{
			var isChecked=false;
			for(var i=0;i<oElement.length;i++){
				if(oElement[i].checked){isChecked=true;break}
			}
			if(!isChecked){
				oElement[0].focus();
				alert("Please answer question ["+sQuestionText+"].\n\nAll questions marked with \"*\" must be answered.");
				return false;
			}
		}
	}
	return true;
}

function doValidateMatrix(theForm,sQuestion){
	var bAnswered=true;
	var lastCBGroup="";
	var focusElement=null;
	for(var i=0;i<theForm.elements.length;i++){
		if(theForm.elements[i].name.indexOf(sQuestion+"A")==0){
			if(!focusElement) focusElement=theForm.elements[i];
			var iValue=theForm.elements[i].value;
			if(lastCBGroup!=theForm.elements[i].name){
				if(!bAnswered){
					if(focusElement) focusElement.focus();
					alert("Please answer all questions in section ["+sQuestionText+"].\n\nAll questions marked with \"*\" must be answered.");
					return false;
				}
				bAnswered=false;
			}
			if(theForm.elements[i].checked){
				bAnswered=true;
			}
			lastCBGroup=theForm.elements[i].name;
		}
	};
	
	if(!bAnswered){
			if(focusElement) focusElement.focus();
			alert("Please answer all questions in section ["+sQuestionText+"].\n\nAll questions marked with \"*\" must be answered.");
			return false;
	}
	return true;
}

function doValidatePercentage(theForm,sQuestion){
	var bAnswered=false;
	var focusElement=null;
	for(var i=0;i<theForm.elements.length;i++){
		if(theForm.elements[i].name.indexOf(sQuestion+"A")==0){
			if(!focusElement) focusElement=theForm.elements[i];
			var iValue=theForm.elements[i].value;
			if((isNaN(iValue))||(iValue=="")){
				if(iValue!=""){
					if(focusElement) focusElement.focus();
					alert("Please answer question ["+sQuestionText+"] correctly.\n\nPlease enter a valid number between 0 and 100, or leave empty not applicable.");
					return false;
				}
			}else{
				iValue=parseInt(iValue);
				if((iValue<0)||(iValue>100)){
					if(focusElement) focusElement.focus();
					alert("Please answer question ["+sQuestionText+"] correctly.\n\nPlease enter a value between 0 and 100, or leave empty not applicable.");
					return false;
				}else{
					bAnswered=true;
				}
			}
		}
	};
	
	if(aRequiredAnswers[sQuestion]){
		if(!bAnswered){
				alert("Please answer question ["+sQuestionText+"].\n\nAll questions marked with \"*\" must be answered.");
				return false;		
		}
	}
	return true;
}

function doValidatePercentage100(theForm,sQuestion){
	var iSum=0;
	var bAnswered=false;
	var focusElement=null;
	for(var i=0;i<theForm.elements.length;i++){
		if(theForm.elements[i].name.indexOf(sQuestion+"A")==0){
			if(!focusElement) focusElement=theForm.elements[i];
			var iValue=theForm.elements[i].value;
			if((!isNaN(iValue))&&(iValue!="")){
				iValue=parseInt(iValue);
				iSum+=iValue;
				bAnswered=true;
			}else{
				if(iValue!=""){
					if(focusElement) focusElement.focus();
					alert("Please answer question ["+sQuestionText+"] correctly.\n\nPlease enter a valid number between 0 and 100, or leave empty not applicable.");
					return false;
				}
			}
			
		}
	};
	
	if(aRequiredAnswers[sQuestion]){
		if(!bAnswered){
				if(focusElement) focusElement.focus();
				alert("Please answer question ["+sQuestionText+"].\n\nAll questions marked with \"*\" must be answered.");
				return false;		
		}
	}

	if(!bAnswered) return true;
	
	if(iSum!=100){
		if(focusElement) focusElement.focus();
		alert("Please answer question ["+sQuestionText+"] correctly.\n\nThe sum of all percentages for this question must equal to 100%\n\nYour current entries add up to "+iSum+"%.\n\nPlease correct your entries.");
		return false;
	}

	return true;
}


//string functions:
function Trim(str) { 
	str = str + "";
	return RTrim(LTrim(str))
}

function LTrim(str) { 
	str = str + "";
	while (str.substring(0,1)==" ") str = str.substring(1);
	return str;
}

function RTrim(str){ 
	str = str + "";
	while (str.substring(str.length-1)==" ") str = str.substring(0,str.length-1);
	return str;
}
