<!--

    ////////////////////////////////////////////////////////////////////////////////
    // Functions used by yesno.php, as part of index.ihtml
    ///////////////////////////////////////////////////////////////////////////////

    // if there are incomplete questionnaires for current user,
    // opens yesno.ihtml in a popup window
    function yesno(aid, modulename, qid) {        
	if (aid) { 
	    location = "index.php?page=" + modulename + "/yesno&qid=" + qid + "&aid=" + aid;
	} else {	    
	    location = "index.php?page=" + modulename + "/questionnaire&qid=" + qid;	    
	}
	return false;
    }
    
    ///////////////////////////////////////////////////////////////////////////////
    // Functions used by questionnaire.ihtml
    ///////////////////////////////////////////////////////////////////////////////

    function check_name() {	
	name = document.questionnaire_form.answer_head_user_id.value;
	if (name == "") {
	    alert("Please enter your name in the 'Name' field");
	    return false;
	} else {        
	    return true;
	}    
    }

    function check_forms(sList) {	
	if (!check_name()) {
	    return false;
	} else {    
	    if (sList.length > 0) {
		qnos = sList.split(",");
		for (i = 0; i < qnos.length; i++) {
		    eval("type = document.questionnaire_form.question_" + qnos[i] + ".type");			    
		    if (type == "checkbox") {
			eval("no_answer = document.questionnaire_form.question_" + qnos[i].slice(0, qnos[i].indexOf("_")) + "_no_answer.checked");		
		    } else {
			eval("no_answer = document.questionnaire_form.question_" + qnos[i] + "_no_answer.checked");		
		    }	
		    if (no_answer) { 
		    	continue; 
		    }
		    // if checkbox, must check all checkboxes for that question
		    // to check if at least one answer is checked
		    if (type == "checkbox") {
			num = qnos[i].slice(0, qnos[i].indexOf("_"));		    
			nums = sList.split(",");
			checked = false;						
			for (j = 0; j < nums.length; j++) { 		    			
			    if (nums[j].slice(0, nums[j].indexOf("_")) == num) {			
				eval("checked = ((checked) || (document.questionnaire_form.question_" + nums[j] + ".checked))");		
			    }    
			}
			if (!checked) {
			    alert("Please enter a value for question " + num + " before submitting");
			    return false;
			}					    		    
		    } else {		
			eval("value = document.questionnaire_form.question_" + qnos[i] + ".value");		
			if (value == "") {
			    alert("Please enter a value for question " + qnos[i] + " before submitting");
			    return false;
			}	
		    }    		    
		}
	    }	
	}    
	return true;
    }

    function validate_check_forms(sList,dList) {	
	if (!check_name()) {
	    return false;
	} else {    
	    if (sList.length > 0) {
		var qnos = sList.split(",");
		if (!dList)
		    dList = sList;
		var dnos = dList.split(",");
		for (i = 0; i < qnos.length; i++) {
		    eval("var question = document.questionnaire_form.question_" + qnos[i]);
		    var type = question.type;
		    var length = question.length;
		    if (type == "checkbox") {
			eval("no_answer = document.questionnaire_form.question_" + qnos[i].slice(0, qnos[i].indexOf("_")) + "_no_answer.checked");		
		    } else {
			eval("no_answer = document.questionnaire_form.question_" + qnos[i] + "_no_answer.checked");		
		    }	
		    if (no_answer) { 
		    	continue; 
		    }
		    if (length > 0) {
			    var j=1;
			    eval("current = document.questionnaire_form.question_" + qnos[i] + "["+j+"].type");
			    if (current=="radio"){
				value = "";
				eval("radioGroup = document.questionnaire_form.question_" + qnos[i]);
				for(j=0;j<radioGroup.length;j++){
				    if (radioGroup[j].checked){
					value=radioGroup[j].value;
				    }
				}
				if (value==""){
				    eval("document.questionnaire_form.question_" + qnos[i] + "_no_answer.checked = 'yes';");		
				}
			    }
		    }
		    // if checkbox, must check all checkboxes for that question
		    // to check if at least one answer is checked
		    if (type == "checkbox") {
			num = qnos[i].slice(0, qnos[i].indexOf("_"));		    
			nums = sList.split(",");
			checked = false;						
			for (j = 0; j < nums.length; j++) { 		    			
			    if (nums[j].slice(0, nums[j].indexOf("_")) == num) {			
				eval("checked = ((checked) || (document.questionnaire_form.question_" + nums[j] + ".checked))");		
			    }    
			}
			if (!checked) {
			    eval("document.questionnaire_form.question_" + qnos[i] + "_no_answer.checked = 'yes';");		
			}					    		    
		    } 
		    else {		
			eval("value = document.questionnaire_form.question_" + qnos[i] + ".value");		
		    }    		    
		    if (value == "") {
			eval("document.questionnaire_form.question_" + qnos[i] + "_no_answer.checked = 'yes';");		
		    }	
		}
	    }	
	}    
	return true;
    }

    function submit_questionnaire(sList) {	
    	if (check_forms(sList)) {
	    document.questionnaire_form.answer_head_status.value = "C";
	    document.questionnaire_form.submit();	
	}	
	return false;    
    }

    function save_questionnaire() {
	if (!check_name()) {
	    return false;
	} else if (confirm("Are you sure you want to save this questionnaire and return to it another time?")) {
	    document.questionnaire_form.answer_head_status.value = "I";
	    document.questionnaire_form.submit();
	}
	return false;
    }

    function save_questionnaire_compact(modulename, qid, aid, compact, user_id) {
	document.questionnaire_form.answer_head_status.value = "I";
	document.questionnaire_form.page.value = modulename + "/questionnaire";
	document.questionnaire_form.qid.value = qid;
	document.questionnaire_form.aid.value = aid;
	document.questionnaire_form.compact.value = compact;
	document.questionnaire_form.user_id.value = user_id;
	document.questionnaire_form.submit();
	return false;
    }

    ///////////////////////////////////////////////////////////////////////////////
    // Functions used by questionnaire_form.ihtml
    ///////////////////////////////////////////////////////////////////////////////

    // Called when 'Delete' button pressed, sets $func to 'delete'
    function set_questionnaire_func_delete() {	
	document.questionnaire_form.func.value = "questionnaireDelete";
	return true;    
    }

    function swap(func_name, question_id) {	
	document.questionnaire_form.func.value = func_name;	    
	document.questionnaire_form.page.value = "questionnaire/questionnaire_form";	    
	document.questionnaire_form.question_id.value = question_id;	    
	document.questionnaire_form.submit();
	return false;    
    }

    ///////////////////////////////////////////////////////////////////////////////
    // Functions used by question_form.ihtml
    ///////////////////////////////////////////////////////////////////////////////

    // This validation function checks values have been entered into the form fields
    function check_question_form() {
	title = document.question_form.question_title.value;
	type = document.question_form.question_type.value;
	if (title == "") {
	    alert("Please enter a question");
	    return false;
	} else if (type == "") {
	    alert("Please enter an answer type for the question");
	    return false;
	} else {
	    return true;    
	}    
    }

    function submit_checked_question_form() {
	if (check_question_form()) {
	    document.question_form.submit();
	}    
	return false;
    }
    
    function is_numeric(value, lower, upper) {
	len = value.length;
	dot = false;
	if (len == 0) {
	    return true;
	} else {
	    for (i = 0; i < len; i++) {
		if (value.substring(i,i+1) == ".") {
		    if (!dot) {
			dot = true;
		    } else {
			return false;
		    }
		} else {
		    val = parseInt(value.substring(i,i+1));
		    if ((isNaN(val)) || (val < lower) || (val > upper)) {
			return false;		    
		    }
		}        
	    }
	    return true;
	}    
    }

    function check_numeric(value, lower, upper) {
	if (!is_numeric(value, lower, upper)) {
	    alert("Value must consist only of digts and at most one decimal");
	    return false;
	} else {
	    return true;
	}
    }    

    function check_min(dflt, min, max, type) {
	if (!check_numeric(min, 0, 9)) {
	    return false;
	} 
	
	if (min.length > 0) {
	    min = parseFloat(min);	    	    
	    if (dflt.length > 0) {
		if (min > parseFloat(dflt)) {
		    alert("Value must be less than or equal to " + dflt);
		    return false;		
		}		
	    }
	    if (max.length > 0) {
		if (min > parseFloat(max)) {
		    alert("Value must be less than or equal to " + max);
		    return false;		
		}		
	    }
	}
	return true;
    }    
    
    function check_max(dflt, min, max, type) {
	if (!check_numeric(max, 0, 9)) {
	    return false;
	} 
	if (max.length > 0) {
	    max = parseFloat(max);	    	    
	    if (dflt.length > 0) {
		if (max < parseFloat(dflt)) {
		    alert("Value must be greater than or equal to " + dflt);
		    return false;		
		}		
	    }
	    if (min.length > 0) {
		if (max < parseFloat(min)) {
		    alert("Value must be greater than or equal to " + min);
		    return false;		
		}		
	    }
	}
	return true;
    }    

    function check_default(dflt, min, max, type, dec) {		    
	if (!check_numeric(dflt, 0, 9)) {
	    return false;
	} 
	
	if (dflt.length > 0) {
	    if (dflt < parseInt(min)) {
		alert("Value must be greater than or equal to " + min);
		return false;		
	    }		
	    if (dflt > parseInt(max)) {
		alert("Value must be less than or equal to " + max);
		return false;		
	    }		

	    if (dec > 0) {
		pos = dflt.indexOf(".");
		if (pos >= 0) {
		    decimal = dflt.substring(pos+1, dflt.length);
		    if (decimal.length > dec) {
			alert("No more than " + dec + " decimals allowed for this field");
			return false;
		    }
		}	    
	    }		    
	}
	return true;
    }

    function default_warning(dflt, min, max, type, dec) {		    
	if (!check_numeric(dflt, 0, 9)) {
	} 
	
	if (dflt.length > 0) {
	    if (dflt < parseInt(min)) {
		alert("Value is expected to be greater than or equal to " + min);
	    }		
	    if (dflt > parseInt(max)) {
		alert("Value is expected to be less than or equal to " + max);
	    }		

	    if (dec > 0) {
		pos = dflt.indexOf(".");
		if (pos >= 0) {
		    decimal = dflt.substring(pos+1, dflt.length);
		    if (decimal.length > dec) {
			alert("No more than " + dec + " decimals allowed for this field");
		    }
		}	    
	    }		    
	}
	return true;
    }

    function validate_date(date) {
	dd = date.substring(0,2);
	mm = date.substring(3,5);				
	yyyy = date.substring(6,10);
	if ((!is_numeric(dd, "0", "9"))  || (!is_numeric(mm, "0", "9")) || (!is_numeric(yyyy, "0", "9"))) {
	    alert("Date must be entered in 'DD/MM/YYYY' format");
	    return false;
	} else if ((dd < "01") || (dd > "31")) {
	    alert("Day must be between '01' and '31'");
	    return false;
	} else if ((mm < "01") || (mm > "12")) {
	    alert("Month must be between '01' and '12'");
	    return false;
	} else if ((yyyy < "0000") || (yyyy > "9999")) {
	    alert("Year must be between '0000' and '9999'");
	    return false;
	} else {	
	    return true;
	}
    }

    function format_date(date) {
	//alert(date);
	if (date.length == 0) {
	    return date;
	} else {    
	    dd = date.substring(0,2);	
	    mm = date.substring(3,5);	
	    yyyy = date.substring(6,10);
	    return (dd + "/" + mm + "/" + yyyy);
	}    
    }

    function check_date(form_val) {
	eval("val = " + form_val);
	if (val.length == 0) {
	    return true;
	}
	xx = val;
	if (validate_date(val)) {	
	    val = xx;
	    eval(form_val + " = format_date(xx)");	    
	    return true;
	} else {
	    return false;
	}
    }
    
    function day(date) {
	return (parseInt(date.substring(0,1) * 10) + parseInt(date.substring(1,2)));	
    }

    function month(date) {
	return (parseInt(date.substring(3,4) * 10) + parseInt(date.substring(4,5)));	
    }

    function year(date) {
	return (parseInt(date.substring(6,7) * 1000) + (parseInt(date.substring(7,8)) * 100) + (parseInt(date.substring(8,9)) * 10) + parseInt(date.substring(9,10)));	
    }

    function valid_range(date, lower, upper) {	
	future = new Date(2100, 00, 01);	
	t = new Date(year(date), month(date), day(date));
	diff = (future - t);
//	alert(diff);
	if (lower.length > 0) {
	    lt = new Date(year(lower), month(lower), day(lower));
	    start = (future - lt);
	    if (diff > start) {
		alert("Please enter a date on or later than " + lower);	
		return false;
	    }
	}	
	if (upper.length > 0) {
	    ut = new Date(year(upper), month(upper), day(upper));
	    end = (future - ut);
	    if (diff < end) {		
		alert("Please enter a date on or prior to " + upper);	
		return false;
	    }
	}			
	return true;	
    }

    function check_date_range(form_val, lower, upper) {	
	eval("val = " + form_val);	
	if (val.length != 10) {
	    alert("Please enter a date in the correct format");
	    return false;
	}
	if (val.length == 0) {
	    return true;
	}
	xx = val;
	if (validate_date(val)) {	    
	    val = xx;
	    if (valid_range(format_date(val), format_date(lower), format_date(upper))) {
		eval(form_val + " = format_date(val)");
		return true;
	    } else {
		return false;
	    }	
	} else {
	    return false;
	}
    }

    function soft_valid_range(date, lower, upper) {	
	future = new Date(2100, 00, 01);	
	t = new Date(year(date), month(date), day(date));
	diff = (future - t);
//	alert(diff);
	if (lower.length > 0) {
	    lt = new Date(year(lower), month(lower), day(lower));
	    start = (future - lt);
	    if (diff > start) {
		alert("Date is expected to be on or later than " + lower);	
//		return false;
	    }
	}	
	if (upper.length > 0) {
	    ut = new Date(year(upper), month(upper), day(upper));
	    end = (future - ut);
	    if (diff < end) {		
		alert("Date is expected to be on or prior to " + upper);	
//		return false;
	    }
	}			
	return true;	
    }
    function warning_date_range(form_val, lower, upper) {	
	eval("val = " + form_val);	
	if (val.length != 10) {
	    alert("Please enter a date in the correct format");
	    return false;
	}
	if (val.length == 0) {
	    return true;
	}
	xx = val;
	if (validate_date(val)) {	    
	    val = xx;
	    if (soft_valid_range(format_date(val), format_date(lower), format_date(upper))) {
		eval(form_val + " = format_date(val)");
		return true;
	    } else {
		return false;
	    }	
	} else {
	    return false;
	}
    }
    function validate_time(time) {
	hh = time.substring(0,2);
	mm = time.substring(3,5);			
	if ((!is_numeric(hh, "0", "9"))  || (!is_numeric(mm, "0", "9"))) {
	    alert("Time must be entered in 'HH:MM' format");
	    return false;
	} else if ((hh < "00") || (hh > "23")) {
	    alert("Hour must be between '00' and '23'");
	    return false;
	} else if ((mm < "00") || (mm > "59")) {
	    alert("Minute must be between '00' and '59'");
	    return false;
	} else {
	    return true;
	}
    }

    function format_time(time) {
	hh = time.substring(0,2);	
	mm = time.substring(3,5);	
	return (hh + ":" + mm);
    }
    
    function check_time(form_val) {
	eval("val = " + form_val);	
	if (val.length == 0) {
	    return true;
	}
	xx = val;
	if (validate_time(val)) {
	    val = xx;
	    eval(form_val + " = format_time(val)");
	    return true;
	} else {
	    return false;
	}
    }
    
    function submit_question_form(type, statistical_data) {    
	if (type == "date") {    
	    date = document.question_form.answer_1.value;
	    if (date.length > 0) {
		if (validate_date(date)) {
		    document.question_form.answer_1.value = format_date(date);		    
		}
	    }	   
	    document.question_form.submit(); 
	} else if (type == "time") {    
	    time = document.question_form.answer_1.value;
	    if (time.length > 0) {
		if (validate_time(time)) {
		    document.question_form.answer_1.value = format_time(time);		    
		}
	    }	  
	    document.question_form.submit();
	} else {
	    if ((statistical_data) && ((type == "select") || (type == "multiple"))) {
		max = document.question_form.question_num_answers.value;
		for (i = 1; i <= max; i++) {
		    eval("value = document.question_form.answer_" + i + "_numeric.value");		
		    if (value == "") {
			alert("Please enter a Numeric value for answer " + i + " before submitting");
			return false;
		    }
		    eval("value = document.question_form.answer_" + i + ".value");		
		    if (value == "") {
			alert("Please enter a value for answer " + i + " before submitting");
			return false;
		    }
		}    
	    }	
	    document.question_form.submit();
	}    
	return false;
    }
            
    // Called when 'Delete' button pressed, sets $func to 'delete'
    function set_question_func_delete() {	
	document.question_form.func.value = "questionDelete";
	document.question_form.page.value = "questionnaire/questionnaire_form";	    
	document.question_form.submit();
	return false;    
    }
    
    function disable_num_answers() {	
	type = document.question_form.question_type.value;
	if ((type == "multiple") || (type == "select")) {
	    document.question_form.question_num_answers.disabled = false;
	} else {
	    document.question_form.question_num_answers.value = 1;
	    document.question_form.question_num_answers.disabled = true;
	}
    }

    ///////////////////////////////////////////////////////////////////////////////
    // Functions used by login_user_list_form.ihtml
    ///////////////////////////////////////////////////////////////////////////////

    function change_location(modulename, pagename) {	
	shopper_group_id = document.user_list_form.shopper_group_id.value;
	if (shopper_group_id != "") {
	    loc = document.user_list_form.location.value;	
	    location.href = "?page=" + modulename + "/" + pagename + "&shopper_group_id=" + shopper_group_id + "&location=" + loc;
	}
	return true;    
    }

    function change_qid_location(modulename, pagename) {	
	shopper_group_id = document.user_list_form.shopper_group_id.value;
	qid = document.user_list_form.qid.value;
	if (shopper_group_id != "") {
	    loc = document.user_list_form.location.value;	
	    location.href = "?page=" + modulename + "/" + pagename + "&shopper_group_id=" + shopper_group_id + "&location=" + loc + "&qid=" + qid;
	}
	return true;    
    }

    function login_user(user_id) {	
	document.user_list_form.group_user_id.value = user_id;
	document.user_list_form.submit();
	return false;    
    }

    function set_other_user(user_id) {	
	document.user_list_form.other_user_id.value = user_id;
	document.user_list_form.submit();
	return false;    
    }


    ///////////////////////////////////////////////////////////////////////////////
    // Functions used by questionnaire_export.ihtml
    ///////////////////////////////////////////////////////////////////////////////

    function select_all(num) {	
	all = document.export_form.checkbox_all.checked;
	for (i = 1; i <= num; i++) {
	    eval("document.export_form.checkbox_" + i + ".checked = " + all);
	}
	return true;    
    }

    ///////////////////////////////////////////////////////////////////////////////
    // Used by admin/user_form.ihtml
    ///////////////////////////////////////////////////////////////////////////////
    function disable_location() {	
	if (document.form.perms.value == "locationadmin") {
	    document.form.location.disabled = false;
	} else {
	    document.form.location.value = "";
	    document.form.location.disabled = true;
	}
	return true;
    }

    ///////////////////////////////////////////////////////////////////////////////
    // Used by shopper/shopper_form.ihtml
    ///////////////////////////////////////////////////////////////////////////////
    function post_vars() {	
	document.form.func.value = "checkConfidential";
	document.form.submit();
	return false;
    }

//-->    	
