/*
'**
' @name         functions.js
'
' @describe     Javascript functions
'
' @author		Laurent Constantin
'
' @revised	    01/2003
'*
*/


/*
'**
' @definition   function open_upload(survey_id, question_id, answer_id, preview_mode, tmp_mode, url_base)
'
' @describe     Open the upload file page
'
' @author       Laurent Constantin
'
' @revised      08/2004
'
' @returns      
'*
*/
function open_upload(survey_id, question_id, answer_id, respondent_id, preview_mode, tmp_mode, url_base) {

	upload_win = window.top.open(url_base + 'answer_upload.asp?survey_id=' + survey_id + '&question_id=' + question_id + '&answer_id=' + answer_id + '&respondent_id=' + respondent_id + '&preview_mode=' + preview_mode + '&tmp_mode=' + tmp_mode, 'upload_win', 'width=500px, height=150px, left=100px, top=100px, resizable=yes, status=no, maximize=no, minimize=no');

	if (upload_win)
		upload_win.focus();
}


/*
'**
' @definition   function scroll_page(scroll_values)
'
' @describe     Scroll the current page to x, y values passed as parameters (scroll_values = x_y)
'
' @author       Laurent Constantin
'
' @revised      07/2004
'
' @returns      
'*
*/
function scroll_page(scroll_values) {
	var tmp = scroll_values.split('_');
	
	if (isNaN(tmp[0]))
		scrollX = 0;
	else
		scrollX = tmp[0];
		
	if (isNaN(tmp[1]))
		scrollY = 0;
	else
		scrollY = tmp[1];

	window.scrollTo(scrollX, scrollY);
}


/*
'**
' @definition   function check_survey_fields()
'
' @describe     This function check if at least one answer for all mandatory fields are selected
'
' @author       Laurent Constantin
'
' @revised      01/2003
'
' @returns      boolean: return true if all mandatory fields are fill up
'*
*/
function check_survey_fields() {
	mandatory_questions = window.document.frm_survey.mandatory_questions.value;

	global_answer_selected = true;
	
	if (mandatory_questions != '') {
		mandatory_questions_array = mandatory_questions.split(';');
		
		for (var i=0; i < mandatory_questions_array.length; i++) {
			mandatory_answers = eval('window.document.frm_survey.mandatory_answers_' + mandatory_questions_array[i] + '.value');
			mandatory_answers_array = mandatory_answers.split(';');
	
			answer_selected = false;

			for (var j=0; j < mandatory_answers_array.length; j++) {
				answer_obj = eval('window.document.frm_survey.answer_' + mandatory_questions_array[i] + '_' + mandatory_answers_array[j]);
	
				if (answer_obj) {
					switch (answer_obj.type) {
						case "checkbox":
							if (answer_obj.checked)
								answer_selected = true;
							break;
	
						case "text":
							if (answer_obj.value != '')
								answer_selected = true;
							break;
	
						case "textarea":
							if (answer_obj.value != '')
								answer_selected = true;
							break;
	
						case "select-one":
							if (answer_obj.selectedIndex > 0)
								answer_selected = true;
							break;
	
						case "select-multiple":
							if (answer_obj.selectedIndex >= 0)
								answer_selected = true;
							break;
	
						case "hidden":
							if (answer_obj.value == 'uploaded_file_ok')
								answer_selected = true;
							break;
					}
				}
				
				// if this object doesn't exist -> it's a radio button
				else {
					answer_obj = eval('window.document.frm_survey.answer_' + mandatory_questions_array[i]);

					for (var k = 0; k < answer_obj.length; k++) {
						if (answer_obj[k].checked) {
							answer_selected = true;
							break;
						}
					}
				}
	
				// quit the loop if at least one answer is selected
				if (answer_selected == true)
					break;
			}

			// save the global answer value
			if (answer_selected == false)
				global_answer_selected = false;

			// check if the browser is Netscape or IE
			if (is.ns == false) {
				// put in red the asterisk of the mandatory question without answer selected
				no_selected_question_obj = eval('mandatory_question_' + mandatory_questions_array[i]);
				if (no_selected_question_obj) {
					if (answer_selected == false)
						no_selected_question_obj.style.color = '#FF0000';
					else
						no_selected_question_obj.style.color = '#05669d';
				}
			}
			// for netscape -> change the image src for the asterisk
			else {
				if (answer_selected == false)
					document.images['asterisk_question_' + mandatory_questions_array[i]].src = 'images/asterisk_red.gif'
				else
					document.images['asterisk_question_' + mandatory_questions_array[i]].src = 'images/asterisk_blue.gif'
			}
		}
	}


	if (global_answer_selected == true) {
		// check the date format of all date object
		if (check_date_format() == true) {
			return true;
		}
		else {
			alert(msg_55);
			return false;
		}
	}
	else {
		alert(msg_56);
		return false;
	}
}


function check_date_format() {
	date_questions = window.document.frm_survey.date_questions.value;

	if (date_questions != '') {
		date_questions_array = date_questions.split(';');
	
		err_msg = '';
		check_date_failed = false;
		
		for (var i=0; i < date_questions_array.length; i++) {
			date_answers = eval('window.document.frm_survey.answer_' + date_questions_array[i] + '.value');
	
			if (date_answers != '') {
				err_msg = is_valid_date(date_answers, "/");

				// put in red the question
				tmp_array = date_questions_array[i].split('_');

				// check if the browser is Netscape or IE
				if (is.ns == false) {
					no_selected_question_obj = eval('mandatory_question_' + tmp_array[0]);
					if (no_selected_question_obj) {
						if (err_msg != '')
							no_selected_question_obj.style.color = '#FF0000';
						else
							no_selected_question_obj.style.color = '#05669d';
					}
				}
				// for netscape -> change the image src for the asterisk
				else {
					if (err_msg != '')
						document.images['asterisk_question_' + mandatory_questions_array[i]].src = 'images/asterisk_red.gif'
					else
						document.images['asterisk_question_' + mandatory_questions_array[i]].src = 'images/asterisk_blue.gif'
				}
								
				if (check_date_failed == false && err_msg != '')
					check_date_failed = true;
			}
		}
		
		if (check_date_failed == false) {
			return true;
		}
		else {
			return false;
		}
	}
	
	// if there is no date
	else {
		return true;
	}
}


function is_valid_date(test_date, sep) {
	// checks if date passed is in valid dd/mm/yyyy format (separator can be choose)
	var reason = '';

    if (test_date.length >= 8 && test_date.length <= 10) {
        if (test_date.indexOf(sep) > 0) {
			date_array = test_date.split(sep);
            var date  = date_array[0];
            var month = date_array[1];
            var year  = date_array[2];

            var test = new Date(year,month-1,date);

            if (year == y2k(test.getYear()) && (month-1 == test.getMonth()) && (date == test.getDate())) {
                reason = '';
            }
            else {
                reason = 'The date you entered is not valid.\nPlease enter a valid date.';
            }
        }
        else {
            reason = 'Invalid date format.\nPlease use this format to enter dates: dd/mm/yyyy';
        }
    }
    else {
        reason = 'Invalid date format.\nPlease use this format to enter dates: dd/mm/yyyy';
    }
    
    return reason;
}


function y2k(number) {
	return (number < 1000) ? number + 1900 : number;
}

// BrowserCheck Object
function BrowserCheck() {
	var b = navigator.appName
	if (b=="Netscape") this.b = "ns"
	else if (b=="Microsoft Internet Explorer") this.b = "ie"
	else this.b = b
	this.version = navigator.appVersion
	this.v = parseInt(this.version)
	this.ns = (this.b=="ns" && this.v>=4)
	this.ns4 = (this.b=="ns" && this.v==4)
	this.ns5 = (this.b=="ns" && this.v==5)
	this.ie = (this.b=="ie" && this.v>=4)
	this.ie4 = (this.version.indexOf('MSIE 4')>0)
	this.ie5 = (this.version.indexOf('MSIE 5')>0)
	this.min = (this.ns||this.ie)
}
is = new BrowserCheck()


/*
'**
' @definition   function check_textarea_fields()
'
' @describe     This function check all textarea field and control if the number of character is not more than 8000
'
' @author       Laurent Constantin
'
' @revised      08/2003
'
' @returns      boolean: return true if all textarea text are less than 8000 chars
'*
*/
function check_textarea_fields() {
	textarea_questions = window.document.frm_survey.textarea_questions.value;

	global_answer_selected = true;
	
	if (textarea_questions != '') {
		textarea_questions_array = textarea_questions.split(';');
		
		for (var i=0; i < textarea_questions_array.length; i++) {
			textarea_answer = eval('window.document.frm_survey.answer_' + textarea_questions_array[i] + '.value');
			
			if (textarea_answer.length > 8000) {
				answer_selected = false;
				global_answer_selected = false;
			}
			else {
				answer_selected = true;
			}

			textarea_questions_nbr_array = textarea_questions_array[i].split('_');

			// check if the browser is Netscape or IE
			if (is.ns == false) {
				// put in red the asterisk of the textarea question with more than 8000 chars inside
				no_selected_question_obj = eval('mandatory_question_' + textarea_questions_nbr_array[0]);
				if (no_selected_question_obj) {
					if (answer_selected == false)
						no_selected_question_obj.style.color = '#FF0000';
					else
						no_selected_question_obj.style.color = '#05669d';
				}
			}
			// for netscape -> change the image src for the asterisk
			else {
				if (answer_selected == false)
					document.images['asterisk_question_' + textarea_questions_nbr_array[0]].src = 'images/asterisk_red.gif'
				else
					document.images['asterisk_question_' + textarea_questions_nbr_array[0]].src = 'images/asterisk_trans.gif'
			}
		}

		
		if (global_answer_selected == true) {
			return true;	
		}
		else {
			alert(msg_57);
			return false;
		}
	}
	else {
		return true;
	}
}


/*
'**
' @definition   function key_press()
'
' @describe     This function prevent form submission by using ENTER Key on input element except for textarea, button and submit
'
' @author       Laurent Constantin
'
' @revised      09/2003
'
' @returns      
'*
*/
function key_press() {
	// check if the browser is Netscape or IE
	if (is.ie == true) {
		if (window.event.keyCode == 13 && window.event.srcElement.type != 'textarea' && window.event.srcElement.type != 'button' && window.event.srcElement.type != 'submit')
			window.event.keyCode = 0;
	}
} 


/*
'**
' @definition   function is_email()
'
' @describe     This function check an email address format
'
' @author       http://webreference.com/js/tips/990928.html
'
' @revised      09/2003
'
' @returns      boolean according to the string passed as parameter
'*
*/
function is_email(str) {
	var supported = 0;
	if (window.RegExp) {
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) supported = 1;
	}
	
	if (!supported) 
		return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	return (!r1.test(str) && r2.test(str));
}


/*
'**
' @definition   function check_email_fields()
'
' @describe     This function check all email field and control if the format of the email address is correct
'
' @author       Laurent Constantin
'
' @revised      09/2003
'
' @returns      boolean: return true if all email address are correctly formated
'*
*/
function check_email_fields() {
	email_questions = window.document.frm_survey.email_questions.value;

	global_answer_selected = true;
	
	if (email_questions != '') {
		email_questions_array = email_questions.split(';');
		
		for (var i=0; i < email_questions_array.length; i++) {
			email_answer = eval('window.document.frm_survey.answer_' + email_questions_array[i] + '.value');
			
			if (email_answer != '') {
				if (is_email(email_answer) == false) {
					answer_selected = false;
					global_answer_selected = false;
				}
				else {
					answer_selected = true;
				}
	
				email_questions_nbr_array = email_questions_array[i].split('_');

				// check if the browser is Netscape or IE
				if (is.ns == false) {
					// put in red the asterisk of the textarea question with more than 8000 chars inside
					no_selected_question_obj = eval('mandatory_question_' + email_questions_nbr_array[0]);
					if (no_selected_question_obj) {
						if (answer_selected == false)
							no_selected_question_obj.style.color = '#FF0000';
						else
							no_selected_question_obj.style.color = '#05669d';
					}
				}
				// for netscape -> change the image src for the asterisk
				else {
					if (answer_selected == false)
						document.images['asterisk_question_' + email_questions_nbr_array[0]].src = 'images/asterisk_red.gif'
					else
						document.images['asterisk_question_' + email_questions_nbr_array[0]].src = 'images/asterisk_trans.gif'
				}
			}
		}

		
		if (global_answer_selected == true) {
			return true;	
		}
		else {
			alert(msg_58);
			return false;
		}
	}
	else {
		return true;
	}
}


/*
'**
' @definition   function check_number_fields()
'
' @describe     This function check all number field and control if it's a number integer or floating depending of the flag answer_floating
'
' @author       Laurent Constantin
'
' @revised      06/2004
'
' @returns      boolean: return true if all numbers are correctly formated
'*
*/
function check_number_fields(can_be_floating) {
	if (can_be_floating == true) {
		number_questions = window.document.frm_survey.floating_questions.value;
		floating_decimal = window.document.frm_survey.floating_decimal.value;
	}
	else
		number_questions = window.document.frm_survey.integer_questions.value;

	global_answer_selected = true;

	if (number_questions != '') {
		number_questions_array = number_questions.split(';');
		
		for (var i=0; i < number_questions_array.length; i++) {
			number_answer = eval('window.document.frm_survey.answer_' + number_questions_array[i] + '.value');

			if (number_answer != '') {
				if (isNaN(number_answer) == true) {
					answer_selected = false;
					global_answer_selected = false;
				}
				else {
					if (can_be_floating == true) {
						floating_decimal_array = floating_decimal.split(';');
						
						tmp_array = number_answer.split('.');
						number_decimal = tmp_array[parseInt(tmp_array.length-1)].length;
						if (tmp_array.length > 1 && number_decimal != floating_decimal_array[i]) {
							answer_selected = false;
							global_answer_selected = false;
						}
						else
							answer_selected = true;
					}
					else {
						if (parseInt(number_answer) != number_answer) {
							answer_selected = false;
							global_answer_selected = false;
						}
						else
							answer_selected = true;
					}
				}
	
				number_questions_nbr_array = number_questions_array[i].split('_');

				// check if the browser is Netscape or IE
				if (is.ns == false) {
					// put in red the asterisk of the textarea question with more than 8000 chars inside
					no_selected_question_obj = eval('mandatory_question_' + number_questions_nbr_array[0]);
					if (no_selected_question_obj) {
						if (answer_selected == false)
							no_selected_question_obj.style.color = '#FF0000';
						else
							no_selected_question_obj.style.color = '#05669d';
					}
				}
				// for netscape -> change the image src for the asterisk
				else {
					if (answer_selected == false)
						document.images['asterisk_question_' + number_questions_nbr_array[0]].src = 'images/asterisk_red.gif'
					else
						document.images['asterisk_question_' + number_questions_nbr_array[0]].src = 'images/asterisk_trans.gif'
				}
			}
		}

		
		if (global_answer_selected == true) {
			return true;	
		}
		else {
			alert(msg_59);
			return false;
		}
	}
	else {
		return true;
	}
}


/*
'**
' @definition   function check_max_answers()
'
' @describe     This function check all max answer (number of maximum checkbox can be selected)
'
' @author       Laurent Constantin
'
' @revised      10/2003
'
' @returns      boolean: return true if all max answers are correct
'*
*/
function check_max_answers() {
	max_answer_questions = window.document.frm_survey.max_answer_questions.value;

	global_answer_selected = true;
	nbr_of_checked = 0;
	last_question_id = 0;
	
	if (max_answer_questions != '') {
		max_answer_questions_array = max_answer_questions.split(';');

		for (var i = 0; i < window.document.frm_survey.elements.length; i++) {
			if (window.document.frm_survey.elements[i].type == 'checkbox') {
				tmp_array = window.document.frm_survey.elements[i].name.split('_');

				if (tmp_array[1] != last_question_id && last_question_id != 0) {
					// look for the max answer for this question
					for (var j = 0; j < max_answer_questions_array.length; j++) {
						tmp_array2 = max_answer_questions_array[j].split(':');
						if (tmp_array2[0] == last_question_id)
							break;
					}
					
//alert(last_question_id + ' -> ' + nbr_of_checked + ' => ' + tmp_array2[1]);
					if (nbr_of_checked > tmp_array2[1]) {
						answer_selected = false;
						global_answer_selected = false;
					}
					else {
						answer_selected = true;
					}

					// check if the browser is Netscape or IE
					if (is.ns == false) {
						// put in red the asterisk of the textarea question with more than 8000 chars inside
						no_selected_question_obj = eval('mandatory_question_' + last_question_id);
						if (no_selected_question_obj) {
							if (answer_selected == false)
								no_selected_question_obj.style.color = '#FF0000';
							else
								no_selected_question_obj.style.color = '#05669d';
						}
					}
					// for netscape -> change the image src for the asterisk
					else {
						if (answer_selected == false)
							document.images['asterisk_question_' + last_question_id].src = 'images/asterisk_red.gif'
						else
							document.images['asterisk_question_' + last_question_id].src = 'images/asterisk_trans.gif'
					}

					nbr_of_checked = 0;
				}

				if (max_answer_questions.indexOf(tmp_array[1] + ':') >= 0) {
					if (window.document.frm_survey.elements[i].checked == true)
						nbr_of_checked = nbr_of_checked + 1;
				}
				
				last_question_id = tmp_array[1];
			}
		}


		// look for the max answer for this question
		for (var j = 0; j < max_answer_questions_array.length; j++) {
			tmp_array2 = max_answer_questions_array[j].split(':');
			if (tmp_array2[0] == last_question_id)
				break;
		}
//alert(last_question_id + ' -> ' + nbr_of_checked + ' => ' + tmp_array2[1]);
		if (nbr_of_checked > tmp_array2[1]) {
			answer_selected = false;
			global_answer_selected = false;
		}
		else {
			answer_selected = true;
		}

		// check if the browser is Netscape or IE
		if (is.ns == false) {
			// put in red the asterisk of the textarea question with more than 8000 chars inside
			if (last_question_id != 0) {
				no_selected_question_obj = eval('mandatory_question_' + last_question_id);
				if (no_selected_question_obj) {
					if (answer_selected == false)
						no_selected_question_obj.style.color = '#FF0000';
					else
						no_selected_question_obj.style.color = '#05669d';
				}
			}
		}
		// for netscape -> change the image src for the asterisk
		else {
			if (answer_selected == false)
				document.images['asterisk_question_' + last_question_id].src = 'images/asterisk_red.gif'
			else
				document.images['asterisk_question_' + last_question_id].src = 'images/asterisk_trans.gif'
		}


		if (global_answer_selected == true) {
			return true;	
		}
		else {
			alert(msg_60);
			return false;
		}
	}
	else {
		return true;
	}
}


/*
'**
' @definition   function check_max_answers()
'
' @describe     Generic function to remove leading and trailing white spaces in any given string input.
'
' @author       Khoa Tran
'
' @revised      10/2003
'
' @returns      return the string
'*
*/
function trimSpaces(myString) 
{ 
    while (myString.substring(0,1) == ' ')
    { 
        myString = myString.substring(1, myString.length);
	}
	
    while (myString.substring(myString.length-1,myString.length) == ' ')
    {    
		myString = myString.substring(0, myString.length-1);
    }
   return myString;
}


/*
'**
' @definition   function imageOn(imagex), imageOff(imagex)
'
' @describe     Function for the help rollover
'
' @author       Laurent Constantin
'
' @revised      11/2003
'
' @returns      
'*
*/
btn_help_on = new Image(56, 49);
btn_help_on.src = "images/btn_help_on.gif";
btn_help_off = new Image(56, 49);
btn_help_off.src = "images/btn_help_off.gif";

function imageOn(imagex) {
	ion = eval(imagex + "_on.src");
	document [imagex].src = ion;  
}


function imageOff(imagex) {
	iof = eval(imagex + "_off.src");
	document [imagex].src = iof;
}


/*
'**
' @definition   function open_help(anchor)
'
' @describe     Open the help file with the anchor passed as parameter
'
' @author       Laurent Constantin
'
' @revised      11/2003
'
' @returns      
'*
*/
function open_help(site_host, anchor) {
	var help_link = site_host + 'help/DataCol.htm';
	
	if (anchor != '')
		help_link = help_link + '#' + anchor;

	help_win = window.top.open(help_link, 'help_win', 'toolbar=yes, scrollbars=yes, status=yes, resizable=yes, width=700px, height=500px, left=100px, top=100px');

	if (help_win)
		help_win.focus();
}
