// JavaScript Document
// JavaScript Document
// toc.js
/*  it's used to draw statgraphics

*/
	
function FillTables(total,table_x,table_y,thickness,table_width,all_width,all_height,title,type)
{
		var strdiv="";
		strdiv="graphics"+type.toString();
		var winstr="<div id="+strdiv+">";
		var nameArray=new Array(8);
		nameArray[0]="Major Urban";
		nameArray[1]="Large Urban";
		nameArray[2]="Other Urban";
		nameArray[3]="Larger Market Town";
		nameArray[4]="Rural Town";
		nameArray[5]="Village";
		nameArray[6]="Dispersed";
		nameArray[7]="Total";
		
		switch(type)
		{
			case 0:
			case 1:
			case 2:
			case 3:
	
			var tmdColor1 = new Array();
			
			tmdColor1[0] = "#FC6565";
			tmdColor1[1] = "#FFCC66";
			tmdColor1[2] = "#FAFFB2";
			tmdColor1[3] = "#FFDCCC";
			tmdColor1[4] = "#F2FAD4";
			tmdColor1[5] = "#B8E55C";
			tmdColor1[6] = "#63A642";
		
			var tmdColor2 = new Array();
			
			tmdColor2[0] = "#FE0000";
			tmdColor2[1] = "#FFAA01";
			tmdColor2[2] = "#F5FF78";
			tmdColor2[3] = "#FFA77F";
			tmdColor2[4] = "#E5FA9B";
			tmdColor2[5] = "#98E500";
			tmdColor2[6] = "#38A700";
		
			var tb_color = new Array(tmdColor1,tmdColor2);
		
			var line_color = "#69f";
			var table_degree=5;
			var left_width = 70;
		    var length = thickness/2;
			var classrect=-1;
			//if(title.toString().toLowerCase()=="england")
				//classrect=0;
			//else classrect=3;
			//if(type==1)
			//	classrect=0;
			//else
			//	classrect=3;
			classrect=type;
				
			var total_no = total[classrect].length;
			var temp1 = 0;
			var temp2,temp3,temp4;
	
			for(var i = 2;i<total_no-2;i=i+2)
			{
				if(temp1<total[classrect][i])
				{
					temp1 = total[classrect][i];
				}
			}
		
		
			temp1 = parseInt(temp1.toString());
		
			if(temp1>9)
			{ 
				temp2 = temp1.toString().substr(1,1)
				if(temp2>4)
				{
					temp3 = (parseInt((temp1/(Math.pow(10,(temp1.toString().length-1)))).toString())+1)*Math.pow(10,(temp1.toString().length-1));
				}
				else
				{
					temp3 = (parseInt((temp1/(Math.pow(10,(temp1.toString().length-1)))).toString())+0.5)*Math.pow(10,(temp1.toString().length-1));
				//	alert(temp3);
				} 
			} 
			else
			{
				if(temp1>4)
					temp3 = 10; 
				else 
					temp3 = 5;
			}
				
			//******************************************************
			//figure out the table_degree
		
			var scale=0;
			if(temp1/10<5)
			{
				table_degree=parseInt(temp1/5)+1;	
				
				scale=5;
			}
			else
			{
				table_degree=parseInt(temp1/10)+1;
				if(table_degree>=10)
					table_degree=10;
				scale=10;
				
				temp3=scale*table_degree;
				//temp3 = (parseInt((temp1/(Math.pow(10,(temp1.toString().length-1)))).toString())+1)*Math.pow(10,(temp1.toString().length-1));
				
			}
			//******************************************************
			temp4=temp3
			//alert(temp4);
			//fill box color
			winstr+="<!--[if gte vml 1]><v:rect id='_x0000_s1027' alt='' style='position:absolute;left:" + (table_x+left_width) + "px;top:" + (table_y-45) + "px;width:" + all_width + "px;height:" + (all_height+45) + "px;z-index:-1' fillcolor='#C2D1F0' stroked='f'><v:fill rotate='t' color2='#C2D1F0' angle='-45' focus='100%' type='gradient'/></v:rect><![endif]-->";
			//draw x line
			winstr+="<!--[if gte vml 1]><v:line id='_x0000_s1027' alt='' style='position:absolute;left:0;text-align:left;top:0;flip:y;z-index:-1' from='" + (table_x+left_width) + "px," + (table_y+all_height) + "px' to='" + (table_x+all_width+left_width) + "px," + (table_y+all_height) + "px'/><![endif]-->";
			//draw y line
			winstr+="<!--[if gte vml 1]><v:line id='_x0000_s1027' alt='' style='position:absolute;left:0;text-align:left;top:0;flip:y;z-index:-1' from='" + (table_x+left_width) + "px," + (table_y-40) + "px' to='" + (table_x+left_width) + "px," + (table_y+all_height) + "px'/><![endif]-->";
			//set title text
			winstr+="<!--[if gte vml 1]>";
			winstr+="<v:shape id='_x0000_s1027' type='#_x0000_t202' alt='' style='position:absolute;left:" + (table_x) + "px;top:" + (table_y-45) + "px;width:" + all_width + "px;height:18px;z-index:1'>";
			winstr+="<v:textbox inset='0px,0px,0px,0px'><table cellspacing='3' cellpadding='0' width='100%' height='100%'><tr><td align='center'>" + title + "</td></tr></table></v:textbox></v:shape><![endif]-->";
				
			//figure out the space between the table and talbe
			var table_space = (all_width-table_width*7)/7;
			//draw y line 's right line,it's order to see 3d;
			winstr+="<!--[if gte vml 1]><v:line id='_x0000_s1027' alt='' style='position:absolute;left:0;text-align:left;top:0;flip:y;z-index:-1' from='" + (table_x+left_width+length) + "px,"+ (table_y-45) + "px' to='" + (table_x+left_width+length) + "px," + (table_y+all_height-length) + "px' strokecolor='" + line_color + "'/><![endif]-->";
			//set origin text
			winstr+="<!--[if gte vml 1]>";
			winstr+="<v:shape id='_x0000_s1025' type='#_x0000_t202' alt='' style='position:absolute;left:" + table_x + "px;top:" + (table_y+all_height) + "px;width:" + left_width + "px;height:18px;z-index:1'>";
			winstr+="<v:textbox inset='0px,0px,0px,0px'><table cellspacing='3' cellpadding='0' width='100%' height='100%'><tr><td align='right'>" + 0.00 + "</td></tr></table></v:textbox></v:shape><![endif]-->";
			//draw xie line
			winstr+="<!--[if gte vml 1]><v:line id='_x0000_s1027' alt='' style='position:absolute;left:0;text-align:left;top:0;flip:y;z-index:-1' from='" + (table_x+left_width)+ "px," + (table_y-length) + "px' to='" + (table_x+left_width+length) + "px," + table_y +"px' strokecolor='" + line_color + "'/><![endif]-->";
			//draw line top of  the x line
			winstr+="<!--[if gte vml 1]><v:line id='_x0000_s1027' alt='' style='position:absolute;left:0;text-align:left;top:0;flip:y;z-index:-1' from='" + (table_x+left_width+length) + "px," + (table_y-length) + "px' to='" + (table_x+all_width+left_width) + "px," + (table_y-length) + "px' strokecolor='" + line_color + "'/><![endif]-->";
				
				//temp4 = temp4-scale;
			for(var i=0;i<=all_height-1;i+= all_height/table_degree)
			{
				//alert(all_height+","+i+","+table_degree);
				//draw xie line
				winstr+="<!--[if gte vml 1]><v:line id='_x0000_s1027' alt='' style='position:absolute;left:0;text-align:left;top:0;flip:y;z-index:-1' from='" + (table_x+left_width)+ "px," + (table_y+all_height-length-i) + "px' to='" + (table_x+left_width+length) + "px," + (table_y+all_height-i) +"px' strokecolor='" + line_color + "'/><![endif]-->";
				//draw line top of  the x line
				winstr+="<!--[if gte vml 1]><v:line id='_x0000_s1027' alt='' style='position:absolute;left:0;text-align:left;top:0;flip:y;z-index:-1' from='" + (table_x+left_width+length) + "px," + (table_y+all_height-length-i) + "px' to='" + (table_x+all_width+left_width) + "px," + (table_y+all_height-length-i) + "px' strokecolor='" + line_color + "'/><![endif]-->";
				//draw horizontal left of y line
				winstr+="<!--[if gte vml 1]><v:line id='_x0000_s1027' alt='' style='position:absolute;left:0;text-align:left;top:0;flip:y;z-index:-1' from='" + (table_x+(left_width-15)) + "px," + (table_y+i) + "px' to='" + (table_x+left_width) + "px," + (table_y+i) + "px'/><![endif]-->";
				winstr+="<!--[if gte vml 1]>";
				//draw textbox and set text
				winstr+="<v:shape id='_x0000_s1025' type='#_x0000_t202' alt='' style='position:absolute;left:" + table_x + "px;top:" + (table_y+i) + "px;width:" + left_width + "px;height:18px;z-index:1'>";
				winstr+="<v:textbox inset='0px,0px,0px,0px'><table cellspacing='3' cellpadding='0' width='100%' height='100%'><tr><td align='right'>" + temp4 + "</td></tr></table></v:textbox></v:shape><![endif]-->";
				temp4 = temp4-scale;
			}
			for(var i=2;i<total_no-2;i=i+2)
			{
			//figure out the point.x of the every table
				var temp_space = table_x + left_width + table_space / 2 + table_space * (i-2)/2 + table_width * (i-2)/2; 
				//alert(temp_space+"tablespace"+table_space);
				winstr+="<v:rect id='_x0000_s1025' alt='' style='position:absolute;left:";
				winstr+=temp_space;
				winstr+="px;top:";
				winstr+=table_y + all_height * (1 - (total[classrect][i] / temp3));
				winstr+="px;width:" + table_width + "px;height:" + all_height * (total[classrect][i] / temp3) + "px;z-index:1' fillcolor='" + tb_color[1][(i-2)/2] + "'>";
				winstr+="<v:fill color2='" + tb_color[0][(i-2)/2] + "' rotate='t' type='gradient'/>";
				winstr+="<o:extrusion v:ext='view' backdepth='" + thickness + "pt' color='" + tb_color[1][(i-2)/2] + "' on='t'/>";
				winstr+="</v:rect>";
				
				winstr+="<v:shape id='_x0000_s1025' type='#_x0000_t202' alt='' style='position:absolute;left:" + temp_space + "px;top:" + (table_y+all_height*(1-(total[classrect][i]/temp3))-table_width) + "px;width:" + (table_space+15) + "px;height:18px;z-index:1'>";
				winstr+="<v:textbox inset='0px,0px,0px,0px'><table cellspacing='3' cellpadding='0' width='100%' height='100%'><tr><td align='center'>" + total[classrect][i] + "</td></tr></table></v:textbox></v:shape>";
				//alert(nameArray[(i-2)/2]);
				//draw classcifiction
				winstr+="<v:shape id='_x0000_s1025' type='#_x0000_t202' alt='' style='position:absolute;left:" + (temp_space-table_space/2) + "px;top:" + (table_y+all_height+1) + "px;width:" + (table_space+table_width) + "px;height:48px;z-index:1'>";
				winstr+="<v:textbox inset='0px,0px,0px,0px'><table cellspacing='3' cellpadding='0' width='100%' height='100%'><tr><td align='center'>" + nameArray[(i-2)/2] + "</td></tr></table></v:textbox></v:shape>";
			}
		break;
	case 4:
		//alert(total);
		winstr+="<v:roundrect   style='position:absolute;left:"+(table_x-20)+"; top:"+(table_y-40)+"; width:"+(all_width+40)+"; height:400'>";
		//winstr+="<v:background fillcolor='white'></v:background>";
		//winstr+="<v:fill angle=50 type='gradient' />";
		winstr+="<v:fill rotate='t' angle='-45' focus='100%' type='solid' color='#C2D1F0' color2='#C2D1F0'/>";
		winstr+="</v:roundrect >";
		

		winstr+="<!--[if gte vml 1]>";
		winstr+="<v:shape id='_x0000_s1027' type='#_x0000_t202' alt='' style='position:absolute;left:" + (table_x) + "px;top:" + (table_y-30) + "px;width:" + all_width + "px;height:18px;z-index:3'>";
		winstr+="<v:textbox inset='0px,0px,0px,0px'><table cellspacing='3' cellpadding='0' width='100%' height='100%'><tr><td align='center'><B>Settlement Population Comparisons: National, Regional, County, Local Authority</B></td></tr></table></v:textbox></v:shape><![endif]-->";
		


		var tablewidth=all_width/10;
		var y=0;
		//alert(tablewidth);
		//var tableheight=total.
		//draw frist rectline
		var rectArray=new Array();
		rectArray[0]='';
		rectArray[1]="England";
		rectArray[2]="Region";
		rectArray[3]="County";
		rectArray[4]="District";
		for(var i=0;i<5;i++)
		{
			winstr+="<!--[if gte vml 1]>";
			winstr+="<v:rect id='_x0000_s1027' type='#_x0000_t202' alt='' style='position:absolute;left:" + (table_x+i*(tablewidth*2)) + "px;top:" + (table_y+y) + "px;width:" +(tablewidth*2) + "px;height:18px;z-index:1' strokecolor=black strokeweight=1pt>";
			winstr+="<v:textbox inset='0px,0px,0px,0px'><table cellspacing='3' cellpadding='0' width='tablewidth*2' height='100%'><tr><td align='left'><strong>" +rectArray[i]+ "</strong></td></tr></table></v:textbox></v:rect><![endif]-->";
		}
		y+=18;
			winstr+="<!--[if gte vml 1]>";
			winstr+="<v:rect id='_x0000_s1027' type='#_x0000_t202' alt='' style='position:absolute;left:" + (table_x) + "px;top:" + (table_y+y) + "px;width:" +(tablewidth*2) + "px;height:18px;z-index:1' strokecolor=black strokeweight=1pt>";
			winstr+="<v:textbox inset='0px,0px,0px,0px'><table cellspacing='3' cellpadding='0' width='tablewidth*2' height='100%'><tr><td align='left'></td></tr></table></v:textbox></v:rect><![endif]-->";
		
		for(var i=0;i<4;i++)
		{
			winstr+="<!--[if gte vml 1]>";
			winstr+="<v:rect id='_x0000_s1027' type='#_x0000_t202' alt='' style='position:absolute;left:" + (table_x+(i+1)*(tablewidth*2)) + "px;top:" + (table_y+y) + "px;width:" +(tablewidth*2) + "px;height:18px;z-index:1' strokecolor=black strokeweight=1pt>";
			winstr+="<v:textbox inset='0px,0px,0px,0px'><table cellspacing='3' cellpadding='0' width='tablewidth*2' height='100%'><tr><td align='left'><strong>" + total[i][0] + "</strong></td></tr></table></v:textbox></v:rect><![endif]-->";
		}
		
		y+=18;
			winstr+="<!--[if gte vml 1]>";
			winstr+="<v:rect id='_x0000_s1027' type='#_x0000_t202' alt='' style='position:absolute;left:" + (table_x) + "px;top:" + (table_y+y) + "px;width:" +(tablewidth*2) + "px;height:18px;z-index:1' strokecolor=black strokeweight=1pt>";
			winstr+="<v:textbox inset='0px,0px,0px,0px'><table cellspacing='3' cellpadding='0' width='tablewidth*2' height='100%'><tr><td align='left'><strong>Settlement Type</strong></td></tr></table></v:textbox></v:rect><![endif]-->";
		for(var i=2;i<10;i++)
		{
		 	winstr+="<!--[if gte vml 1]>";
			winstr+="<v:rect id='_x0000_s1027' type='#_x0000_t202' alt='' style='position:absolute;left:" + (table_x+i*tablewidth) + "px;top:" + (table_y+y) + "px;width:" +tablewidth + "px;height:18px;z-index:1' strokecolor=black strokeweight=1pt>";
			if(i%2==0)
				winstr+="<v:textbox inset='0px,0px,0px,0px'><table cellspacing='3' cellpadding='0' width='tablewidth*2' height='100%'><tr><td align='left'><strong>Number</strong></td></tr></table></v:textbox></v:rect><![endif]-->";
			else
				winstr+="<v:textbox inset='0px,0px,0px,0px'><table cellspacing='3' cellpadding='0' width='tablewidth*2' height='100%'><tr><td align='left'><strong>Percent</strong></td></tr></table></v:textbox></v:rect><![endif]-->";
			
		
		}
		
		//urbanArray{1]="";
		y=y+18;
		
		var y2=y;
		/*
		var urbanArray=new Array();
		urbanArray[0]="Major Urban";
		urbanArray[1]="Large Urban";
		urbanArray[2]="Other Urban";
		urbanArray[3]="Larger Market Town";
		urbanArray[4]="Rural Town";
		urbanArray[5]="Village";
		urbanArray[6]="Dispersed";
		urbanArray[7]="Total";
		*/
		
		for(var i=0;i<16;i++)
		{
		 //y2+=36;
			winstr+="<!--[if gte vml 1]>";
			winstr+="<v:rect id='_x0000_s1027' type='#_x0000_t202' alt='' style='position:absolute;left:" + (table_x) + "px;top:" + (table_y+y2) + "px;width:" +(tablewidth*2) + "px;height:18px;z-index:1' strokecolor=black strokeweight=1pt>";
			
			if(i%2==0)
			{
				winstr+="<v:textbox inset='0px,0px,0px,0px'><table cellspacing='3' cellpadding='0' width='tablewidth*2' height='100%'><tr><td align='left'></td></tr></table></v:textbox></v:rect><![endif]-->";
			
			}
			else
			{
				winstr+="<v:textbox inset='0px,0px,0px,0px'><table cellspacing='3' cellpadding='0' width='tablewidth*2' height='100%'><tr><td align='left'><strong>"+nameArray[(i-1)/2]+"</strong></td></tr></table></v:textbox></v:rect><![endif]-->";
				
			}
			y2=y2+18;
		}
		
		
		for(var i=0;i<16;i++)
		{
		 //y2+=36;
				 for(var j=0;j<8;j++)
				{
				
				//alert(i);
				
					winstr+="<!--[if gte vml 1]>";
					winstr+="<v:rect id='_x0000_s1027' type='#_x0000_t202' alt='' style='position:absolute;font-weight:bolder;left:" + (table_x+(j+2)*tablewidth) + "px;top:" + (table_y+y) + "px;width:" +(tablewidth) + "px;height:18px;z-index:1' strokecolor=black strokeweight=1pt>";
					
					if(i%2==0)
					{
						winstr+="<v:textbox inset='0px,0px,0px,0px'><table cellspacing='3' cellpadding='0' width='tablewidth*2' height='100%'><tr><td align='left'></td></tr></table></v:textbox></v:rect><![endif]-->";
					
					}
					else
					{
						winstr+="<v:textbox inset='0px,0px,0px,0px'><table cellspacing='3' cellpadding='0' width='tablewidth*2' height='100%'><tr><td align='left'>"+ total[parseInt(j/2)][i+j%2]+"</td></tr></table></v:textbox></v:rect><![endif]-->";
						
					}
				}
				y=y+18;
		}
		/**/
	
	  break;
	}
	winstr+='</div>';
	//alert(winstr);
	var strdiv='statgraphics'+type.toString();
	document.getElementById(strdiv).innerHTML = winstr;
}

