function getRandNumber(){
	return (new Date().getTime());
}

function Reload_ca (elemName) {
		try
		{
			var randnum = getRandNumber();
			document.getElementById(elemName).innerHTML = '<img src="/progs/getca.php?randnum' + randnum + '" border="0" width="120" height="60">';
		}
		catch(err)
		{
			return false;
		}
	};

	
function sortselectlist(elemName) {
		try
		{
			var SelectElem = document.getElementById(elemName);
			
			if (SelectElem != null) 
			{
				var arrTexts = new Array();
				var text_val = new Array();
				var i;
	

				for(i=0; i<SelectElem.length; i++)
				{
					arrTexts[i] = SelectElem.options[i].text+"          "+SelectElem.options[i].value+"          "+(SelectElem.options[i].selected?1:0);
				}
	
				arrTexts.sort();
	
				for(i=0; i<SelectElem.length; i++)
				{
					text_val = arrTexts[i].split("          ");
					SelectElem.options[i].text = text_val[0];
					SelectElem.options[i].value = text_val[1];
					SelectElem.options[i].selected = (text_val[2]=="1"?true:false);							
				}
			}
		}
		catch(err)
		{
			return false;
		}
	}

	function clear_radio(InputRadio) {
		try
		{
			if(!InputRadio) return "";
			var InputRadioLen = InputRadio.length;
			if(InputRadioLen == undefined) return "";
			for(var i = 0; i < InputRadioLen; i++)
				InputRadio[i].checked = false;
			return "";	
		}
		catch (err)
		{
			return false;
		}
	};	
	

function Stats_Elems_merge(thisform,stats_elems,copyid)
{
	var i = 0;
	var j = 0;	
	var str = "";
	var num_selected = 0;
	var num_of_first_selected = -1;

	var elems_num_new = [];
	var elems_proc_new = [];
	var elems_ans_new = [];
	

	for (i=0;i<stats_elems.proc.length;i++)
	{
		if (thisform["bar"+copyid+"_"+i].checked == true)
		{
			num_selected++;
			if (num_of_first_selected == -1)
				num_of_first_selected = i;
		}
	}
	
	if (num_selected > 1)
	{
		j = 0;
		elems_ans_new[num_of_first_selected] = "";
		elems_proc_new[num_of_first_selected] = 0;
		elems_num_new[num_of_first_selected] = 0;
		for (i=0;i<stats_elems.proc.length;i++)
		{
			
			if (i<num_of_first_selected)
			{
				elems_num_new[j] = stats_elems.num[i];				
				elems_proc_new[j] = stats_elems.proc[i];
				elems_ans_new[j] = stats_elems.ans[i];				
				j++;
			}
			else
			{
				if (thisform["bar"+copyid+"_"+i].checked == true)
				{
					elems_num_new[num_of_first_selected] = Math.round((elems_num_new[num_of_first_selected] + stats_elems.num[i])*100)/100;
					elems_proc_new[num_of_first_selected] = Math.round((elems_proc_new[num_of_first_selected] + stats_elems.proc[i])*100)/100;
					elems_ans_new[num_of_first_selected] = elems_ans_new[num_of_first_selected].concat(stats_elems.ans[i] , "; ");
					if (i == num_of_first_selected)
						j++;
				}
				else
				{
					elems_num_new[j] = stats_elems.num[i];					
					elems_proc_new[j] = stats_elems.proc[i];
					elems_ans_new[j] = stats_elems.ans[i];				
					j++;
				}
				
			}
		}

		stats_elems.num = elems_num_new;
		stats_elems.proc = elems_proc_new;
		stats_elems.ans = elems_ans_new;
	}
	
}	

	function Stats_manage_num_selected(elemObj, num) {

		if (elemObj.checked == true)
			num++;
		else{
			if (num>0)
			num--;
		}
		return num;
	}


function Stats_RePrint_hor(stats_elems_to_print, copyid)
{
	var i;
	var str = "";
	var cur_pic ="";
	
str = '                        <table width="50%" border="0" cellspacing="0" cellpadding="0" align="center">' +
                          '<tr>' +
                            '<td>'+
                              '<table width="100%" border="0" cellspacing="0" cellpadding="0" style="height: ' + stats_elems_to_print.t_height +'; font-size: ' + stats_elems_to_print.font_size + ';">' +
                                '<tr valign="bottom" style="vertical-align: bottom;">';
	
	
	
	for (i=0;i<stats_elems_to_print.proc.length;i++)
	{
		if (stats_elems_to_print.num[i] == 0)
			cur_pic = "<img src=\"/pics/1/00.gif\" width = \"" + stats_elems_to_print.base_width + "\" height=\"4\" alt=\"\">";
		else
			cur_pic = "<img src=\"/pics/1/5.gif\" width = \"" + stats_elems_to_print.base_width + "\" height=\""+(Math.round(stats_elems_to_print.one_percent_float_height * stats_elems_to_print.proc[i])+3) +"\" alt=\"\">";

		str  = str.concat('<td class="st_h1">'+ stats_elems_to_print.proc[i] +'% ('+stats_elems_to_print.num[i]+')<br> '+ cur_pic +'<\/td>');

	}

	str  = str.concat(' <\/tr>\
			<tr valign="bottom" style="vertical-align: top;">');  
	
	for (i=0;i<stats_elems_to_print.proc.length;i++)
		str  = str.concat('<td class="st_h2"><hr><\/td>');
		
	str  = str.concat('<\/tr>\
			<tr valign="bottom" style="text-align: center; vertical-align: top;">');  
			
	for (i=0;i<stats_elems_to_print.proc.length;i++)
		str  = str.concat('<td class="st_h3"><input type="checkbox" name="bar'+ copyid + '_' + i +'" id="bar'+ copyid + '_' + i +'" value="1" onclick="st_e(this,stats_elems' + copyid + ',' + copyid + ')"><\/td>');

	str  = str.concat('<\/tr>\
			<tr style="text-align: center;">');  

	for (i=0;i<stats_elems_to_print.proc.length;i++)
		str  = str.concat('<td class="st_h4">&nbsp;'+stats_elems_to_print.ans[i]+'&nbsp;<\/td>');


	str  = str.concat('<\/tr>\
                              <\/table>\
                            <\/td>\
                          <\/tr>');

if (stats_elems_to_print.scale_str.length > 0)
	str  =  	str.concat('                          <tr>\
                            <td style="text-align: center; font-size: 90%;">\
                              '+ stats_elems_to_print.scale_str + '&nbsp;\
                            <\/td>\
                          <\/tr>');


	str  = str.concat('                        <\/table>\
				<div id="manage_form_buttons'+ copyid + '" style="position: absolute; top:5px; right:40px; width: auto; visibility: hidden;">\
					<input type="button" name="Merge" value="Merge" onclick="Stats_Elems_merge(document.getElementById(\'manage_graph'+ copyid + '\'), stats_elems'+ copyid + ', '+ copyid + ');  document.getElementById(\'stat_graph'+ copyid + '\').innerHTML = Stats_RePrint_hor(stats_elems'+ copyid + ', '+ copyid + '); stats_elems'+ copyid + '.selected=0;">\
					<br> <br>\
					<input type="button" name="Reset" value="Reset" onclick="stats_elems'+ copyid + '.num = stats_elems'+ copyid + '.init_num; stats_elems'+ copyid + '.proc = stats_elems'+ copyid + '.init_proc; stats_elems'+ copyid + '.ans = stats_elems'+ copyid + '.init_ans; document.getElementById(\'stat_graph'+ copyid + '\').innerHTML = Stats_RePrint_hor(stats_elems'+ copyid + ', '+ copyid + '); stats_elems'+ copyid + '.selected=0;">\
				<\/div>	');  
		
	return str;
}



function Stats_RePrint_ver(stats_elems_to_print,copyid)
{
	var str = "";
	var cur_pic ="";
	
	str = '                        <table width="100%" border="0" cellspacing="0" cellpadding="0"> \
                          <tr>\
                            <td>\
                              <table width="100%" border="0" cellspacing="0" cellpadding="0" style="height: ' + stats_elems_to_print.t_height + '; font-size: ' + stats_elems_to_print.font_size + ';">\
	';
	
	
	for (i=0;i<stats_elems_to_print.proc.length;i++)
	{
		if (stats_elems_to_print.num[i] == 0)
			cur_pic = "<img src=\"/pics/1/00.gif\" width = \"4\" height=\"" + stats_elems_to_print.base_width + "\" alt=\"\">";
		else
			cur_pic = "<img src=\"/pics/1/5.gif\" width = \""+(Math.round(stats_elems_to_print.one_percent_float_width  * stats_elems_to_print.proc[i])+3) +"\" height=\"" + stats_elems_to_print.base_width + "\" alt=\"\">";
			
			
				
			
	
		str  = str.concat('                                <tr>\
                                  <td class="st_v1">'+stats_elems_to_print.ans[i]+'<\/td> \
                                  <td class="st_v2"><input type="checkbox" name="bar'+ copyid + '_' + i +'" id="bar'+ copyid + '_' + i +'" value="1" onclick="st_e(this,stats_elems' + copyid + ',' + copyid + ')"><\/td> \
                                  <td class="st_v3">'+cur_pic+' '+ stats_elems_to_print.proc[i] + '% ('+ stats_elems_to_print.num[i] +')<\/td> \
                                <\/tr> \
	');
	}	

	str  = 	str.concat('                              <\/table>\
                            <\/td>\
                          <\/tr>');
if (stats_elems_to_print.scale_str.length > 0)
	str  =  	str.concat('                          <tr>\
                            <td style="text-align: center; font-size: 90%;">\
                              '+ stats_elems_to_print.scale_str + '&nbsp;\
                            <\/td>\
                          <\/tr>');
                          
	str  =  	str.concat('                        <\/table>\
				<div id="manage_form_buttons' + copyid + '" style="position: absolute; top:5px; right:40px; width: auto; visibility:hidden;">\
					<input type="button" name="Merge" value="Merge" onclick="Stats_Elems_merge(document.getElementById(\'manage_graph' + copyid + '\'), stats_elems' + copyid + ','+ copyid + ');  document.getElementById(\'stat_graph' + copyid + '\').innerHTML = Stats_RePrint_ver(stats_elems' + copyid + ',' + copyid + '); stats_elems' + copyid + '.selected=0;">\
					<br> <br>\
					<input type="button" name="Reset" value="Reset" onclick="stats_elems' + copyid + '.num = stats_elems' + copyid + '.init_num; stats_elems' + copyid + '.proc = stats_elems' + copyid + '.init_proc; stats_elems' + copyid + '.ans = stats_elems' + copyid + '.init_ans;  document.getElementById(\'stat_graph' + copyid + '\').innerHTML = Stats_RePrint_ver(stats_elems' + copyid + ',' + copyid + '); stats_elems' + copyid + '.selected=0;">\
				<\/div>\
	');
	
	return str;
	
}


function Show_add_comment_form(elemName, copyid, iscaptcha) {
var str;
	try
	{

	str = '	<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">\
		<tr>\
			<td>\
				<form id="form1_'+ copyid +'" name="form1_'+ copyid +'" method="post" action="" style="font-size: 75%;">\
					Comment: <br />\
					<textarea name="comment" id="comment" cols="45" rows="5" style="border: 1px solid #000000; width: 50%;"><\/textarea><br />\
					Name: <br />\
					<input type="text" name="author" maxlength="125" style="border: 1px solid #000000;" />	<br />';
	if (iscaptcha)
		str = str.concat('<br /><span id="c1_'+ copyid +'"><img src="/progs/getca.php"><\/span>	<br><a onclick="Reload_ca(\'c1_'+ copyid +'\'); return false;" href="#">Reload Control Code<\/a> <br /><br />	Please input Control Code:	<input type="text" name="ccode" value="" maxlength="4" style="border: 1px solid #000000;" /> ');

	str = str.concat('			<input type="hidden" name="oid" value="'+ copyid +'">\
					<input type="submit" name="ok" value="ok" style="border: 1px solid #000000;" />\
				<\/form>\
			<\/td>\
		<\/tr>\
	<\/table>');


		document.getElementById(elemName).innerHTML = str;
	}
	catch(err)
	{
		return false;
	}
}



function st_e(formObj,stObj,copyid)
{
	stObj.selected = Stats_manage_num_selected(formObj,stObj.selected); 
	if (stObj.selected>0) 
		document.getElementById('manage_form_buttons'+copyid).style.visibility='visible'; 
	else 
		document.getElementById('manage_form_buttons'+copyid).style.visibility='hidden';
}


function show_fast_splits_menu(id_val)
{
	return '\
<div style="position: absolute; top: 5px; right: 5px; width: auto; ">\
<table cellpadding="0" cellspacing="0">\
<tr style="cursor:pointer;" onClick="$(\'#elemid_qd_' + id_val + '\').load(\'/statistics/statistics.htm?q=' + id_val + '&partrenew=1&statmore=1&t=4&opt_comments=1&rand'+ getRandNumber() +'\');">\
 <td style="width: 11px;"><img src="/pics/buttons/ygl.gif"></td>\
 <td style="background:url(/pics/buttons/fg_e.gif); background-repeat: repeat-x; padding: 0 9 0 9; text-align: center;  height: 20px; font-size: 16px;">\
  Total\
 </td>\
 <td style="width: 11px;"><img src="/pics/buttons/ygr.gif"></td>\
</tr>\
<tr> <td colspan="3">&nbsp;</td></tr>\
<tr style="cursor:pointer;" onClick="$(\'#elemid_qd_' + id_val + '\').load(\'/statistics/statistics.htm?q=' + id_val + '&partrenew=1&statmore=2&t=4&opt_comments=1&rand'+ getRandNumber() +'\');">\
 <td><img src="/pics/buttons/ybl.gif"></td>\
 <td style="background:url(/pics/buttons/fb_e.gif); background-repeat: repeat-x; padding: 0 9 0 9; text-align: center;  height: 20px; font-size: 16px;">\
 Male/female\
 </td>\
 <td><img src="/pics/buttons/ybr.gif"></td>\
</tr>\
<tr> <td colspan="3">&nbsp;</td></tr>\
<tr style="cursor:pointer;" onClick="$(\'#elemid_qd_' + id_val + '\').load(\'/statistics/statistics.htm?q=' + id_val + '&partrenew=1&statmore=3&t=4&opt_comments=1&rand'+ getRandNumber() +'\');">\
 <td><img src="/pics/buttons/yyl.gif"></td>\
 <td style="background:url(/pics/buttons/fy_e.gif); background-repeat: repeat-x; padding: 0 9 0 9; text-align: center;  height: 20px; font-size: 16px;">\
  By age\
 </td>\
 <td><img src="/pics/buttons/yyr.gif"></td>\
</tr>\
<tr> <td colspan="3">&nbsp;</td></tr>\
<tr style="cursor:pointer;" onClick="$(\'#elemid_qd_' + id_val + '\').load(\'/statistics/statistics.htm?q=' + id_val + '&partrenew=1&statmore=4&t=4&opt_comments=1&rand'+ getRandNumber() +'\');">\
 <td><img src="/pics/buttons/yrl.gif"></td>\
 <td style="background:url(/pics/buttons/fr_e.gif); background-repeat: repeat-x; padding: 0 9 0 9; text-align: center;  height: 20px; font-size: 16px;">\
  By continent\
 </td>\
 <td><img src="/pics/buttons/yrr.gif"></td>\
</tr>\
</table>\
</div>';
}