/************************************************************************************************
*																								*
*	qbuilder/js/qb_client_funcs.js																*
*	22/06/04 - CM																				*
*																								*
*	Standard JS functions for qBuilder. Requires standard_general.js 							*
*																								*
*************************************************************************************************/

var disabled_controls=new Array();
var response_waiting=new Array();

// Validation code

function gridhead_hover(header_id, state) {
	var gridhead_hover = get_object(header_id);
	gridhead_hover.style.display = (state=="on") ? "block" : "none";
}

function validate_regexp(control_name, regex, errmsg) {
	if(regex!="") {
		if(errmsg=="") errmsg="Answer invalid";
		v = document.getElementById(control_name);
		rex = new RegExp(regex);
		m = rex.exec(v.value);
		if(m) {
			(m!=v.value) ? badmatch=true : badmatch=false;
		} else {
			badmatch=true;
		}
		if(badmatch==true) {
			alert(errmsg);
			setTimeout("document.getElementById('"+control_name+"').focus()", 10);
			setTimeout("document.getElementById('"+control_name+"').select()", 50);
		}
	}
	
}

function check_waiting_response() {
	var r=true;
	if(response_waiting.length>0) {
		alert("Please wait while your responses are saved before you proceed.\nYou should only see this message on slower internet connections\nor if the server is very busy. Try again shortly.");
		r = false;
	}
	return r;
}

function validate_form(form_name, use_cli_routing) {
	var msg;
	var r;
	var rankobj;
	var onlyrankmissed=false;
	msg = "";
	f = document.forms[form_name];
	r = true;
	errlist = new Array();
	errdone = new Array();
	checkdone = new Array();
	checknames = new Array();
	c=0;
	errnum = 0;	

	// CC 030108 - Fix back button validation (required filled questions when going back)
	direction = document.getElementById('direction').value;
	if(direction == 1) {		
		if(use_cli_routing && check_waiting_response()==false) return false;
		for( var i=0;i<f.elements.length; i++) {
			e = f.elements[i];
			if(e.name.substr(0,16)=="module_question_" && (e.optional!="true" || e.name.substr(0,21)=="module_question_rank_") && e.name.substr(0,21)!="module_question_list_" && disabled_controls[e.name]!=true) {
				//alert((e.optional!="true" || e.name.substr(0,21)=="module_question_rank_")+" "+e.type);
				if(e.type=="radio") {
					radio_name=e.name;
					v = false;
					do {
						if(e.checked==true) {
							v=true;
						}
						e.qnum!="" ? n=e.qnum : n="";
						e = f.elements[++i];
						
					} while(radio_name==e.name);
					if(v==false && errdone[e.name]!=true) {
						errlist[errnum++] = n;
						errdone[e.name]=true;
						r = false;
						onlyrankmissed=false;
					}	
					i=i-1;
				} else if((e.type=="checkbox" || e.type=="hidden") && e.name.substr(0,21)=="module_question_check" ) {
					a = e.name.split("_");
					check_name=a[a.length-1];
					e.qnum!="" ? n=e.qnum : n="";		
					if(!(checkdone[check_name])) {
						checkdone[check_name] = new Array();
						checkdone[check_name][0] = false;
						checknames[c++]=check_name;
					}
					if(checkdone[check_name][0]!=true) {
						checkdone[check_name][1]=n;
						checkdone[check_name][2]=e.name;
						if((e.type=="checkbox" && e.checked==true) || (e.type=="hidden" && e.value=="on")) {
							checkdone[check_name][0]=true;
						}
					}
				} else if(((e.type=="text" || e.type=="textarea" || e.type=="password") && e.disabled==false) || e.type=="hidden") {
					verify_obj = get_object("verify_"+e.name);
					if(verify_obj) {
						if(verify_obj.value!=e.value) {
							a = e.name.split("_");
							check_name=-(-a[a.length-1])+0;
							alert(verify_text[check_name]);
							return false;
						}
					}
					e.qnum!="" ? n=e.qnum : n="";
					if(e.type!="hidden") {
						(typeof e.is_other!="undefined") ? is_other = e.is_other : is_other = "";
						if(is_other!="") {
							checktext=false;
							(typeof e.other_val!="undefined") ? other_val = e.other_val : other_val = "";
							if(other_val!="") {
								if(get_object(is_other).value==other_val) checktext=true;
							} else {
								if(get_object(is_other).value=="on") checktext=true;
							}
						} else {
							checktext=true;
						}
					} else if(e.type=="hidden" && e.name.substr(0,21)=="module_question_rank_") {
						
						// I've turned off client validation for ranking questions for now, as I can't make them work locally for testing 
						// MSq 27/05/10
						
						/*
						
						checktext=false;
						rnk_id = e.name.substr(21);
						rank_required_items = get_object('rank_required_items_'+rnk_id);
						rank_min_items = get_object('rank_min_items_'+rnk_id);
						rank_count_obj = get_object('rank_count_'+rnk_id);
						rnk_sel_obj = get_object('rnk_sel_'+rnk_id);
						urnk_sel_obj = get_object('urnk_sel_'+rnk_id);
						no_pref_obj = get_object('module_question_check_'+rank_no_pref_option_id+'_'+rnk_id);
	
						// comment: if isObject(no_pref_obj) then rank no preference is selected! 

					  	var obj_selected = rnk_sel_obj.value-0;
					  	var obj_not_selected = urnk_sel_obj.value-0;
					  	var min_allowed = rank_min_items.value-0;
					  	var max_allowed = rank_max_items.value-0;
					  	var rank_exact = rank_required_items.value-0;
					  	var no_pref = "false";
					  	if (isObject(no_pref_obj) && no_pref_obj.value == "on") {
					  		no_pref = "true";
						}
						var rank_validated = 1;
						
						if (obj_selected < min_allowed) {
							rank_validated = 0;
						}
						if (max_allowed > 0 && obj_selected > max_allowed) {
							rank_validated = 0;
						}
						if (rank_exact > 0 && rank_exact != obj_selected) {
							rank_validated = 0;
						}
						
						
						if (obj_selected == 0 && e.optional) {
							rank_validated = 1;
						}
						if (no_pref == "true") {
							rank_validated = 1;
						}
						
						if(rank_validated == 0) {
						
						
						//This is simply unreadable, so I changed it to the multiple if statements above.
						
						//if((((rnk_sel_obj.value-0)<(rank_min_items.value-0) && ((isObject(no_pref_obj) && no_pref_obj.value != "on") || !(isObject(no_pref_obj)) ) ) ||
						//   ((rank_min_items.value-0)==0 && ((rnk_sel_obj.value-0)<(rank_required_items.value-0) || (rnk_sel_obj.value-0)>(rank_required_items.value-0) )) && 
						//   (e.optional!="true" || (rnk_sel_obj.value-0>0 && e.optional=="true")) &&
						//   !(isObject(no_pref_obj) && no_pref_obj.value == "on"))
					   //	   && e.optional!="true"
					   //	  ){
						  	  
						  	  
						 // Above, added final line && e.optional!=true to make 'optional' disable all ranking validation.. allow anything   
							  
						// if(
						//   ((rnk_sel_obj.value-0)<(rank_min_items.value-0) && ((isObject(no_pref_obj) && no_pref_obj.value != "on") || !(isObject(no_pref_obj)) ) ) ||
						//   ((rank_min_items.value-0)==0 && ((rnk_sel_obj.value-0)<(rank_required_items.value-0) || (rnk_sel_obj.value-0)>(rank_required_items.value-0) )) && 
					   //	   (e.optional!="true" || (rnk_sel_obj.value-0>0 && e.optional=="true")) &&
					   //	   !(isObject(no_pref_obj) && no_pref_obj.value == "on")
					  //	  ){                     
						
						// CC Vicky fix to allow maximum - do this properly instead
						//if(((rnk_sel_obj.value-0)<(rank_min_items.value-0) && ((isObject(no_pref_obj) && no_pref_obj.value != "on") || !(isObject(no_pref_obj)) ) ) || 
					  //	(((rnk_sel_obj.value-0)<(rank_required_items.value-0) || (rnk_sel_obj.value-0)>(rank_required_items.value-0) )) 
					 //	   && (e.optional!="true") 
					//	   && !(isObject(no_pref_obj) && no_pref_obj.value == "on")
				   //		  ){
							
							if(errdone[e.name]!=true) {
								errlist[errnum++] = n;
								errdone[e.name]=true;
								r = false;
								onlyrankmissed=true;
							}
						} else {
							
						} 
						*/
					} else {
						checktext=true;
					}
					if(e.value=="" && errdone[e.name]!=true && checktext==true) {
						errlist[errnum++] = n;
						errdone[e.name]=true;
						r = false;
						onlyrankmissed=false;
					}
				} else if(e.type.substr(0,6)=='select' && e.disabled==false) {
					e.qnum!="" ? n=e.qnum : n="";
					if(e.selectedIndex==0 && errdone[e.name]!=true) {
						errlist[errnum++] = n;
						errdone[e.name]=true;
						r = false;
						onlyrankmissed=false;
					}
				}
			}
		}
		
		for(var i=0;i<checknames.length;i++) {
			if(checkdone[checknames[i]][0]!=true) {
				if(errdone[checkdone[checknames[i]][2]]!=true) {
					errlist[errnum++] = checkdone[checknames[i]][1];
					errdone[checkdone[checknames[i]][2]]=true;
					r = false;
					onlyrankmissed=false;
				}
			}
		}
		
		if(r==false) {
			if((cli_missed_text!="" && cli_missed_text!=null) || (onlyrankmissed==true && rnk_missed_text!="")) {
				msg = (onlyrankmissed==true && rnk_missed_text!="") ? rnk_missed_text : cli_missed_text;			
			} else {
				errs="";
				msg="Question";
				(errnum>1) ? msg += "s " : msg += " ";
				for( var i=0;i<errnum; i++) {
					if(errlist[i]!="" && typeof errlist[i]!="undefined") {
						if(errs.length>0) {
							i==errnum-1 ? errs+=" and " : errs+=", ";
						}
						errs+=errlist[i];
					}
				}
				if(errs!="" && errs!="undefined") msg += errs + " ";
				(errnum>1) ? msg += "were " : msg += "was ";
				msg += "missed!";
			}
			
			alert(unescape(msg));
		}
	}
	return r;
}

function toggle_navigation(show) {
	var nextbutton = get_object("nextbutton");
	var backbutton = get_object("backbutton");
	if(nextbutton)
		nextbutton.style.display = show ? '' : 'none';

	if(backbutton)
		backbutton.style.display = show ? '' : 'none';
	
}
	
function toggle_radio(control_name, value) {
	// ods = off and diasbled
	// odn = on and diasbled
	var im = document.images;
	var no_allow_change=false;
	var cur_im = get_object(control_name+"_"+value);
	var drex = new RegExp("but_radio_ods");
	var is_disabled = (drex.exec(cur_im.src)=="but_radio_ods");
	var a = control_name.split("_");
	var v = get_object(control_name);
	
	if(is_disabled)
		return false;
	
	//var column_lockout_grid_id;
	if(column_lockout_grid_id) {
		var grid_lockout_id = column_lockout_grid_id[a[a.length-1]-0];
		if(grid_lockout_id) {
			if(column_lockout_on[grid_lockout_id]=="off" && value==column_lockout_num[grid_lockout_id]) {
				column_lockout_on[grid_lockout_id]="on";
				toggle_radio_column(a[a.length-1]-0, value, "ods");
			} else {
				if(column_lockout_on[grid_lockout_id]=="on" && v.value==column_lockout_num[grid_lockout_id]) {
					column_lockout_on[grid_lockout_id]="off";
					toggle_radio_column(a[a.length-1]-0, v.value, "off");
				}
			}
		}
	}
	
	if(is_disabled!=true) {
		for( var i=0;i<im.length; i++) {
			if(im[i].name!=null) {
				if(im[i].name.substr(0, control_name.length)==control_name) {
					var rex = new RegExp("but_radio_o");
					var m = rex.exec(im[i].src);
					if(grid_lockout_id) {
						if(column_lockout_on[grid_lockout_id]=="on" && im[i].name.substr(control_name.length+1)==column_lockout_num[grid_lockout_id] && value!=column_lockout_num[grid_lockout_id]) {
							no_allow_change=true;
						} else {
							no_allow_change=false;
						}
					} else {
						no_allow_change=false;
					}
					
					if(no_allow_change==false) {
						if(m=="but_radio_o") {
							im[i].src = img_path + "but_radio_" + ((im[i].name.substr(control_name.length+1)==value) ? "on" : "off")+ ".gif";
						} else {
							im[i].className = "qb_option_image_" + ((im[i].name.substr(control_name.length+1)==value) ? "on" : "off");
						}
					}
				}
			}
		}
		if(no_allow_change==false) {
			c = get_object(control_name);
			c.value = value;
		}
	}

}

function toggle_check_other(control_name, other_name) {
	o = get_object(other_name);
	v = get_object(control_name);		
	im = get_object("img_"+control_name);
	if(o.value!="") {
		v.value="on";
		im.src = img_path + "but_check_on.gif";
	} else if(o.value=="") {
		v.value="";
		im.src = img_path + "but_check_off.gif";			
	}
}



function state_radio_question(module_question_id, disabled) {
	var im = document.images;
	var e;
	var a;
	var radio_option_id;
	var radio_module_question_id;
	var imdis;
	var rex = new RegExp("but_radio_o");
	var is_button;
	var control_name = "module_question_"+pad(module_question_id+"", 9, "0");
	state = (disabled) ? "ods" : "off";
	disabled_controls[control_name]=disabled;
	var v = get_object(control_name);
	v.value="";
	
	for( var i=0;i<im.length; i++) {
		if(im[i].name!=null) {
			if(im[i].name.substr(0,16)=="module_question_" ) {
				a = im[i].name.split("_");
				radio_module_question_id = -(-a[a.length-2]);
				if(radio_module_question_id==module_question_id) {
					is_button = (rex.exec(im[i].src)=="but_radio_o");
					if(is_button==true) {
						im[i].src = img_path + "but_radio_"+state+".gif";
					} else {
						im[i].className = "qb_option_image_"+state;					
					}
				}
			}
		}
	}		
}

function toggle_radio_column(module_question_id, value, state) {
	var im = document.images;
	var e;
	var a;
	var radio_option_id;
	var radio_module_question_id;
	var imdis;
	var rex = new RegExp("but_radio_o");
	var is_button;
	
	for( var i=0;i<im.length; i++) {
		if(im[i].name!=null) {
			if(im[i].name.substr(0,16)=="module_question_" ) {
				a = im[i].name.split("_");
				radio_option_id = -(-a[a.length-1]);
				radio_module_question_id = -(-a[a.length-2]);
				if(radio_option_id==value && radio_module_question_id!=module_question_id) {
					is_button = (rex.exec(im[i].src)=="but_radio_o");
					if(is_button==true) {
						im[i].src = img_path + "but_radio_"+state+".gif";
					} else {
						im[i].className = "qb_option_image_"+state;					
					}
				}
			}
		}
	}		
}

function toggle_check_options(option_id, module_question_id, state) {
	var f = document.forms[limit_form];
	var e;
	var a;
	var check_option_id;
	var check_module_question_id;
	var imdis;
	var rex = new RegExp("but_check_o");
	var is_button;
	
	for( var i=0;i<f.elements.length; i++) {
		e = f.elements[i];
		if(e.name.substr(0,21)=="module_question_check" ) {
			a = e.name.split("_");
			check_option_id = -(-a[a.length-2]);
			check_module_question_id = -(-a[a.length-1]);
			if(check_option_id==option_id && check_module_question_id!=module_question_id) {
				e.value="";
				imdis = get_object("img_"+e.name);
				is_button = (rex.exec(imdis.src)=="but_check_o");					
				if(is_button==true) {
					imdis.src = img_path + "but_check_"+state+".gif";
				} else {
					imdis.className = "qb_check_image_"+state;
				}
			}
		}
	}		
}

function toggle_noneabove(option_id, module_question_id) {
	var f = document.forms[limit_form];
	var e;
	var rex = new RegExp("but_check_o");
	var is_button;
	var imdis;
	var cur_ele = get_object('module_question_check_'+option_id+"_"+module_question_id);
	
	var state="off";
	if(cur_ele.value!="on") {
		f = document.forms[limit_form];
		for( var i=0;i<f.elements.length; i++) {
			e = f.elements[i];
			if(e.name.substr(0,21)=="module_question_check" ) {
				a = e.name.split("_");
				check_option_id = -(-a[a.length-2]);
				check_module_question_id = -(-a[a.length-1]);
				if(check_option_id!=option_id && check_module_question_id==module_question_id && e.value!="") state="ods";
			}
		}		
	
		imdis = get_object("img_"+cur_ele.name);					
		is_button = (rex.exec(imdis.src)=="but_check_o");
		if(is_button==true) {
			imdis.src = img_path + "but_check_"+state+".gif";
		} else {
			imdis.className = "qb_check_image_"+state;
		}			
	}
}

function toggle_noneabove_all(option_id, module_question_id) {
	var f = document.forms[limit_form];
	var e;
	var rex = new RegExp("but_check_o");
	var check_disabled = new RegExp("but_check_ods");
	var is_button;
	var cur_ele = get_object('module_question_check_'+option_id+"_"+module_question_id);
	var imdis = get_object("img_"+cur_ele.name);
	var rex_but = new RegExp("but_check");
	var check_css_disabled = new RegExp("qb_check_image_ods");
	var pass_state=false;
	if(rex_but.exec(imdis.src)) {
		pass_state = check_disabled.exec(imdis.src)!="but_check_ods";	
	} else {
		pass_state = check_css_disabled.exec(imdis.className)!="qb_check_image_ods";	
	}
	if(pass_state) {
		if(cur_ele.value=="on") {
			f = document.forms[limit_form];
			for( var i=0;i<f.elements.length; i++) {
				e = f.elements[i];
				if(e.name.substr(0,21)=="module_question_check" ) {
					a = e.name.split("_");
					check_option_id = -(-a[a.length-2]);
					check_module_question_id = -(-a[a.length-1]);
					if(check_option_id!=option_id && check_module_question_id==module_question_id) e.value="";
				}
			}		
			toggle_check_row(module_question_id, "ods");
		} else {
			toggle_check_row(module_question_id, "off");
		}
	}
}

function toggle_check_row(module_question_id, state) {
	var f = document.forms[limit_form];
	var e;
	var a;
	var check_module_question_id;
	var imdis;
	var rex = new RegExp("but_check_o");
	var is_button;
	f = document.forms[limit_form];
	for( i=0;i<f.elements.length; i++) {
		e = f.elements[i];
		if(e.name.substr(0,21)=="module_question_check" ) {
			a = e.name.split("_");
			check_module_question_id = -(-a[a.length-1]);
			if(check_module_question_id==module_question_id && e.value!="on") {
				imdis = get_object("img_"+e.name);					
				is_button = (rex.exec(imdis.src)=="but_check_o");
				if(is_button==true) {
					imdis.src = img_path + "but_check_"+state+".gif";
				} else {
					imdis.className = "qb_check_image_"+state;
				}
			}
		}
	}
}

function toggle_check(control_name) {
	var v = get_object(control_name);
	var im = get_object("img_"+control_name);
	var rex = new RegExp("but_check_o");
	var is_button = (rex.exec(im.src)=="but_check_o");
	rex = new RegExp("but_check_ods");
	var rdex = new RegExp("qb_check_image_ods");
	var is_disabled = (rex.exec(im.src)=="but_check_ods") || (rdex.exec(im.className)=="qb_check_image_ods");
	
	var a = control_name.split("_");
	var module_question_id = -(-a[a.length-1]);
	var force_noselect=false;
	var i=0;		
	var option_id = -(-a[a.length-2]);


	if(is_disabled)
		return false;
	
	force_noselect=is_disabled;
	
	// Handle option lockouts
	if(lockout_option_on[module_question_id] && force_noselect==false) {
		if(lockout_option_on[module_question_id]=='off') {
			lockout_option_on[module_question_id]='on';
			toggle_check_options(option_id, module_question_id, "ods");
			lockout_option_id[option_id] = true;
		} else if(lockout_option_on[module_question_id]=='on' && lockout_option_id[option_id]==true) {
			lockout_option_on[module_question_id]='off';
			toggle_check_options(option_id, module_question_id, "off");
			lockout_option_id[option_id] = false;
		}
	} else if(lockout_option_id[option_id]==true) {
		force_noselect=true;
	}
	
	
	
	// Handle limited rows
	if(limit_multi[module_question_id] && force_noselect==false) {
		if(!limit_count[module_question_id]) {
			limit_count[module_question_id]=0;
		}
		if(limit_count[module_question_id]<limit_multi[module_question_id] && v.value=="") {
			limit_count[module_question_id]++;
			if(limit_count[module_question_id]==limit_multi[module_question_id]) {
				toggle_check_row(module_question_id, "ods");
			}
			
		} else if(limit_count[module_question_id]>0 && v.value!="") {
			if(limit_count[module_question_id]==limit_multi[module_question_id]) {
				toggle_check_row(module_question_id, "off");
			}
			limit_count[module_question_id]--;
		} else {
			force_noselect=true;

		}
	}
	if(v.value=="") {
		if(force_noselect==false) {
			v.value="on";
			if(is_button==true) {
				im.src = img_path + "but_check_on.gif";
			} else {
				im.className = "qb_check_image_on";
			}
		}
	} else {
		v.value="";
		if(is_button==true) {
			im.src = img_path + "but_check_off.gif";
		} else {
			im.className = "qb_check_image_off";
		}
	}
}

function toggle_other(control_id) {
	v = document.getElementById("module_question_text_"+control_id);
	(v.disabled==true) ? v.disabled=false : v.disabled=true;
	clear_other(control_id);
}

function toggle_single_other(control_id, mode) {
	v = document.getElementById("module_question_text_"+control_id);
	if(v) {
		v.disabled=mode;
		clear_other(control_id);
	}
}

function clear_other(control_id) {
	v = document.getElementById("module_question_text_"+control_id);
	if(v) {
		v.value="";
		if(v.disabled==true) {
			v.className = "qb_text_disabled";
		} else {
			v.className = "qb_text";
		}
	}
}

function update_years(control_id) {
	y = document.getElementById("module_question_date_y_"+control_id);
	dd = new Date();
	
	if(y.value>dd.getFullYear()) y.value=dd.getFullYear();
	if(y.value<dd.getFullYear()-90 && y.value.length==4) y.value=dd.getFullYear()-90;		
	if(browser_type=="ie") {
		z = "0000";
		for(i=1;i<=3;i++) {
			if(y.value.length==i) {
				y.value = y.value+z.substr(0,z.length-i);
				y.select();
				x = document.selection.createRange();
				x.moveStart("character", i);
				x.select();			
				break;
			}
		}
	}
}


var ugly_selectorText_workaround_flag = true;
var allStyleRules;

function ugly_selectorText_workaround() {
	if((navigator.userAgent.indexOf("Gecko") == -1) ||
	   (ugly_selectorText_workaround_flag)) {
		return; // we've already been here or shouldn't be here
	}
	var styleElements = document.getElementsByTagName("style");
	
	for(var i = 0; i < styleElements.length; i++) {
		var styleText = styleElements[i].firstChild.data;
		// this should be using match(/\b[\w-.]+(?=\s*\{)/g but ?= causes an
		// error in IE5, so we include the open brace and then strip it
		allStyleRules = styleText.match(/\b[\w-.]+(\s*\{)/g);
	}

	for(var i = 0; i < allStyleRules.length; i++) {
		// probably insufficient for people who like random gobs of 
		// whitespace in their styles
		allStyleRules[i] = allStyleRules[i].substr(0, (allStyleRules[i].length - 2));
	}
	ugly_selectorText_workaround_flag = true;
}



function getStyleById(i, p) {

	var n = document.getElementById(i);
	var s = eval("n.style." + p);

	// try inline
	if((s != "") && (s != null)) {
		return s;
	}

	// try currentStyle
	if(n.currentStyle) {
		var s = eval("n.currentStyle." + p);
		if((s != "") && (s != null)) {
			return s;
		}
	}
	
	// try styleSheets
	var sheets = document.styleSheets;

	if(sheets.length > 0) {
		// loop over each sheet
		for(var x = 0; x < sheets.length; x++) {
			// grab stylesheet rules
			var rules = sheets[x].cssRules;
			
			if(rules.length > 0) {
				// check each rule
				for(var y = 0; y < rules.length; y++) {
					var z = rules[y].style;
					// selectorText broken in NS 6/Mozilla: see
					// http://bugzilla.mozilla.org/show_bug.cgi?id=51944
					ugly_selectorText_workaround();
					if(allStyleRules) {
						if(allStyleRules[y] == i) {
							return z[p];
						}			
					} else {
						// use the native selectorText and style stuff
						if(((z[p] != "") && (z[p] != null)) ||
						   (rules[y].selectorText == i)) {
							return z[p];
						}
					}
				}
			}
		}
	}
	return null;
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
	window.onload = func;
  } else {
	window.onload = function() {
	  if (oldonload) {
		oldonload();
	  }
	  func();
	}
  }
}

function elementHeightCopy(IEsourceElement, sourceElement, destElement, adjustment) {
	if (browser_type=='ie') {
		 var tdpaddingTop = getStyleById(IEsourceElement,'paddingTop').substr(0,getStyleById(IEsourceElement, 'paddingTop').length-2);
		 var tdpaddingBottom = getStyleById(IEsourceElement,'paddingBottom').substr(0,getStyleById(IEsourceElement, 'paddingBottom').length-2);
		 var tdbotborder = getStyleById(IEsourceElement,'borderBottomWidth').substr(0,getStyleById(IEsourceElement, 'borderBottomWidth').length-2);

		 var rowheight = get_object(IEsourceElement).clientHeight;
		 
		 var newheight = rowheight - tdpaddingTop - tdpaddingBottom + adjustment;
		 get_object(destElement).style.height=newheight + 'px';
	 
	} else {

		 get_object(destElement).style.height=get_object(sourceElement).clientHeight+'px';
	}
}

function sliderWidthCalc(IEsourceElement, sourceElement) {
	if (browser_type=='ie') {
		 var tdpaddingLeft = getStyleById(IEsourceElement,'paddingLeft').substr(0,getStyleById(IEsourceElement, 'paddingLeft').length-2);
		 var tdpaddingRight = getStyleById(IEsourceElement,'paddingRight').substr(0,getStyleById(IEsourceElement, 'paddingRight').length-2);
		 var tdleftborder = getStyleById(IEsourceElement,'borderLeftWidth').substr(0,getStyleById(IEsourceElement, 'borderLeftWidth').length-2);
		 var tdrightborder = getStyleById(IEsourceElement,'borderRightWidth').substr(0,getStyleById(IEsourceElement, 'borderRightWidth').length-2);

		 var tdwidth = document.getElementById(IEsourceElement).clientWidth;
		 
		 return (tdwidth - tdpaddingLeft - tdpaddingRight);
	} else {

		 return document.getElementById(sourceElement).clientWidth;
	}
}

// Toggle the free text when no answer is selected
function toggle_no_answer_text(module_question_id, none_text) {
	var hidden = "";
	var text = document.getElementById("module_question_text_"+module_question_id);
	if (text.type == "hidden") {
		hidden = text;
		text = document.getElementById("OLD_module_question_text_"+module_question_id);
	}
	if(text) {
		if (text.disabled == true){
			text.disabled = false;
			text.value = "";
			if (hidden != "") {
				text.removeChild(hidden);
				text.name = "module_question_text_"+module_question_id;
				text.id = "module_question_text_"+module_question_id;
			}
		} else {
			text.disabled = true;
			text.value = none_text;			
			var input = document.createElement("input");
			input.setAttribute("type", "hidden");
			input.setAttribute("name", "module_question_text_"+module_question_id);
			input.setAttribute("id", "module_question_text_"+module_question_id);
			input.setAttribute("value", none_text);
			text.appendChild(input);
			text.name = "OLD_module_question_text_"+module_question_id;
			text.id = "OLD_module_question_text_"+module_question_id;
		}
	}	
}


// Update the hidden variable for currency questions
function updateCurrency(hidden_field, whole_field, part_field) {
	hidden_field.value = whole_field.value+"."+part_field.value;
}


	
MM_preloadImages(img_path + "but_radio_off.gif", img_path + "but_radio_on.gif", img_path + "but_radio_ods.gif", img_path + "but_check_off.gif", img_path + "but_check_on.gif", img_path + "but_check_ods.gif");