/*
 *	$Rev: 1.2 $
 *	$Date: 2004-09-08 14:35:47 +1000 (Wed, 08 Sep 2004) $
 *	$Name:  $
 *	$Author: markl $
 */

function validateForm(){
var radioBtn = new Array; // debug variable only
var NotAnswered=0;
for (j=1; j<document.CFForm_1.elements.length; j++){

  switch(document.CFForm_1.elements[j].type){
  	case "radio": //Check if radio button has been answered
		var OK = false;
		var i=j;
		re = "optional";

		//Check for optional question
		if (eval("document.CFForm_1.elements[" + j + "].id").search(re) == -1 ){	
			do{
				if (eval("document.CFForm_1.elements[" + i + "].checked")) {
					//Check that not blank - this catches RBwOther blank text fields.
					if( trim(eval("document.CFForm_1.elements[" + i + "].value")) != ""){
						OK = true;
					}
				}
				i++;
			// the code below is necessary to work around a problem in Mozilla
			// Mozilla includes the label tags of the radio buttons in the elements[] array
			// of the document.form object, with undefined properties eg:  
			// type = 'undefined' and name = 'undefined'
				if(eval("!document.CFForm_1.elements[" + i +"].type")) {
					i++; // skip the label
				}
			}
			while(eval("document.CFForm_1.elements[" + j +"].name")== eval("document.CFForm_1.elements[" + i +"].name"));
			
			j=i;
			if(!OK){
				NotAnswered++;
			}
		}	
 	break;

  	case "checkbox": //Check if at least one Check box been answered
		var OK = false;
		var i=j;
		re = "optional";
		//Check for optional question
		if (eval("document.CFForm_1.elements[" + j + "].id").search(re) == -1 ){	
			do{
				if(document.CFForm_1.elements[i].type == 'text'){  //Check if Text Line
					if(eval("document.CFForm_1.elements[" + i + "].value")!=""){
						OK = true;
					}
				}
				else{
					if(eval("document.CFForm_1.elements[" + i + "].checked")) {
						OK = true;
					}
		// the code below is necessary to work around a problem in Mozilla
		// Mozilla includes the label tags of the radio buttons in the elements[] array
		// of the document.form object, with undefined properties eg:  
		// type = 'undefined' and name = 'undefined'
					if(eval("!document.CFForm_1.elements[" + i +"].type")) {
						i++; // skip the label
					}
				}
				i++;
			}
			while(eval("document.CFForm_1.elements[" + j +"].name")== eval("document.CFForm_1.elements[" + i +"].name"));
			j=i;
			if(!OK){
				NotAnswered++;
			}
		}
 	break;

	case "select-one"://Check that all DropDownBoxes have been answered.
		if (document.CFForm_1.elements[j].id!="optional"){
			if (eval("document.CFForm_1.elements[" + j + "].value")==""){
				NotAnswered++;
			}
		}
	break;

	case "text":
		if (eval("document.CFForm_1.elements[" + j + "].id")!="optional"){	
			if (ltrim(eval("document.CFForm_1.elements[" + j + "].value"))==""){
				NotAnswered++;
			}
		}
	break;
	
	case "textarea": 
		if (eval("document.CFForm_1.elements[" + j + "].id")!="optional"){	
			if (ltrim(eval("document.CFForm_1.elements[" + j + "].value"))==""){
				NotAnswered++;
			}
		}
	break;
	

    default :
  }
}
if(NotAnswered==0){
	return true;
	}
else if(NotAnswered==1){
	alert(LNG_JS_PLEASEANSWERALL +"\n"+ LNG_JS_ONEUNANSWERED);
	return false
	}
else{
	alert(LNG_JS_PLEASEANSWERALL +"\n"+ LNG_JS_MULTUNANSWERED1 +" "+ NotAnswered +" "+ LNG_JS_MULTUNANSWERED2);
	return false
	}
}


function CheckDupes(question,instance) {

	eval('Obj = document.CFForm_1.response'+question);
	if(Obj[instance].selectedIndex) {
	// alert(question + ", " + instance + ", " + Obj[instance].selectedIndex);	
		for(i=1;i<Obj.length;i++) {
			if((Obj[i].selectedIndex==Obj[instance].selectedIndex)&&(i!=instance)) {
				Obj[i].selectedIndex=0;
			}
		}		
	}
}


function ltrim(s){
	return s.replace( /^\s*/, "" );
}

function rtrim(s){
	return s.replace( /\s*$/, "" );
}

function trim(s){
	return rtrim(ltrim(s));
}


function SectionLoader(vStatus,vClearSection){
	var vClearSection;
	// This function requires 1 variables 1 hidden form element(s) on the host page
	if(vClearSection != 'undefined'){
		document.forms[0].ClearSection.value = vClearSection;
	}
	if(vStatus != '') {
		document.forms[0].LoadSection.value = vStatus;
	}
	document.forms[0].submit();
} // end function SectionLoader


// Functions for Radio Button w/Other
function SelectOther(el){ // This selects the correct radio button when the text area is clicked on
	el[el.length-1].checked = true;
}
function PopulateRadioButton(el,elOther){ //This sets the RB value when the text area loses focus
	el[el.length-1].value = elOther.value;
}
function ClearOther(el){ //Clears the text box when another radio button is selected
	el.value="";
}
// End of functions for Radio Button w/Other

// Currently this function is used for the related DDL question type only
function catValues (field1, field2) {
	if(field1 && field2)
	return field1[field1.selectedIndex].value + ', ' + field2[field2.selectedIndex].value;
	else return "";
}