//This function adds the form elements
<!--

//var maxval=100;
//var outlier = [998,999];

var percent_sums = new Array;
var percent_sums_isRankingQuestion = new Array;
var percent_sums_numFieldsToRank = new Array;

var percent_sums = new Array;
var percent_sums_names = new Array;

var percent_sum_fields = new Array;
var num_percent_sums = 0;
var check_percent_sum = 0;
var responseAction = "";


setInterval("total();",250);

function checkForm() {
//called onsubmit.

	if ( useAjax == 1 ) {
		checkAjax(responseAction);
		return false;
	}


	for (i=0; i < percent_sums_isRankingQuestion.length; i++) {
	//if there are any ranking questions, we'll check them here...
			
		if (percent_sums_isRankingQuestion[i] == 1) {

			res = checkRankingQuestion(i);	
			if (res == false){

				return false;
			}

		}
		else {
			res = checkPercentSum(i);
			if (res == false){

				return false;
			}

		}
	}

	return true;

}



document.customform.onsubmit = checkForm; 

function checkPercentSum(j){

	var i;
	var ref,totalval,remtotal, totalfieldname;
	totalval = 0;
	bad = 0;

	currquestion = percent_sum_fields[j];
	totalfieldname = percent_sums[j];


	for(i=0; i < currquestion.length; i++)
	{

		ref = currquestion[i];
		ls = document.customform[ref].value-0;
		totalval += ls;

	}

	if ( totalval >= 101 || totalval <= 99){

		bad = 1;
		alert("Numbers must add up to 100% for question \""+percent_sums_names[j]+"\"");
      	}								



	if (bad) {
		return false;
	}

	return true;


}



function checkRankingQuestion(j){

	var i;
	var ref,totalval,remtotal, totalfieldname;
	totalval = 0;
	bad = 0;

	currquestion = percent_sum_fields[j];

	totalfieldname = percent_sums[j];

	for(i=0;i < currquestion.length; i++)
	{
		//go through fields in a current question
		ref = currquestion[i];
//alert( document.customform[ref].value +" > "+percent_sums_numFieldsToRank[j]);
	
	ls = 	document.customform[ref].value-0;
	rs = percent_sums_numFieldsToRank[j]-0;

		if ( ls > rs ||  document.customform[ref].value-0 < 1){


			alert("Numbers must be from 1 to "+percent_sums_numFieldsToRank[j]+" for question \""+percent_sums_names[j]+"\"");				
//				document.customform[ref].value = 1;
				bad = 1;
				i = 1000;
             		}								
		else {
			for (n=0; n < i; n++) {

				if (document.customform[currquestion[n]].value == document.customform[ref].value ) 	{

				alert("Duplicate number "+document.customform[currquestion[n]].value+". Each number must be unique for question \""+percent_sums_names[j]+"\"");
	
					n = 1000; //end loop	
//					document.customform[ref].value = 1;
					bad = 1;
					i = 1000;
				}

			}

		}	
	}

	if (bad) {
		return false;
	}

	return true;

}



function addPercentSum(total_field_name, isRankingQuestion, numFieldsToRank, field_text){
//must be called before addPercentSumField
//the reason we do this is so we can tell which field to add the total to


	num_percent_sums++;

	index = num_percent_sums - 1;	
	percent_sums[index] = total_field_name;
	percent_sums_names[index] = field_text;
	percent_sums_isRankingQuestion[index] = isRankingQuestion;
	percent_sums_numFieldsToRank[index] = numFieldsToRank;

	percent_sum_fields[index] = new Array();

}

function addPercentSumField(fieldname) {
//call addPercentSum first

	index = num_percent_sums - 1;
	topindex = percent_sum_fields[index].length;

	percent_sum_fields[index][topindex] = fieldname;
}


function total() {

	var i,j, bad;
	var ref,totalval,remtotal, totalfieldname;

	totalval = 0;

	j = 0;

	for (j = 0; j < percent_sums.length; j++) {
		// go through percent sum fields

	totalval = 0;


	currquestion = percent_sum_fields[j];
	totalfieldname = percent_sums[j];

	for(i=0;i < currquestion.length; i++)
	{
		//go through fields in a current question
		bad = 0;
		ref = currquestion[i];

			if( isNaN(document.customform[ref].value)){
				alert(document.customform[ref].value + " is not a number. Please re-enter.");
				document.customform[ref].value = 0;
			}
			else if( document.customform[ref].value < 0 ){
				alert("You entered a negative number.  Please re-enter a positive number.");
				document.customform[ref].value = 0;

			}


		if (bad == 0) totalval += (document.customform[ref].value * 1);


	} //i looped through a percent sum questions fields


	document.customform[totalfieldname].value = totalval;



} //j looped through percent sum questions

//   	return totalval;

}




function cform_validate_percentage(element){
	//text element

	num = new Number(element.value);

	if (!element.value || isNaN(num) || num < 0 || num > 100 ){

		alert(element.value +": invalid percentage");
		element.focus();
		return false;

	}
		
	
	return true;

}


function cform_validate_dollar_value(element) {

	num = new Number(element.value);
	if (!element.value || isNaN(num) || num < 0){

		alert(element.value +": invalid dollar value");
		element.focus();
		return false;

	
	}

	return true;
}

function cform_validate_number(element) {

	num = new Number(element.value);
	if (!element.value || isNaN(num)){

		alert(element.value +": invalid number");
		element.focus();
		return false;

	
	}

	return true;
}

function cform_validate_email(element) {

	email = element.value;	

//	email =~ /.*\@.*\..*/g
//	my regexp = new RegExp();
	
	if (!email || !email.match(/.*\@.*\..*/)){

		alert(element.value +": email address");
		element.focus();
		return false;

	
	}

	return true;
}



//-->



