﻿
		function MultiRegion(controlId){
			var inits=document.getElementById(controlId+'_initValue').value.split(';');
			//var inits=initValue[0].split(';');
			PopulateCountry(controlId,inits[0]);
			PopulateRegiontype(controlId,inits[1]);
			if(inits[1] && inits[1]!="-1"){
				PopulateTargetRegions(controlId,inits[0],inits[1],inits[2]);
			}

		}
		function PopulateTargetRegions(controlId,cids,rt,rvs){
			var table=document.getElementById(controlId+'_table');
			var url=GetUrl_ListTargetRegions(cids,rt);
			AddTargetRegions(controlId,url,rvs);
		}
		function PopulateCountry(controlId,cids){
								
			var select=document.getElementById(controlId+'_country');			
			var par,index,text,value;
			var cc=cids.split(',');
			
			for(var i=0;i<countries.length;i++){
				par=countries[i];
				index=par.indexOf(',');
				text=par.substring(0,index);
				value=par.substring(index+1);
				var defaultSelected=false;
				for(var j=0;j<cc.length;j++){
					if(value==cc[j]){
						defaultSelected=true;
						break;
					}
				}
				select.options[i]=new Option(text,value);
				if(defaultSelected){
					select.options[i].selected=true;
				}
				
			}
			if(select.multiple && cc.length<2){
				document.getElementById(controlId+'_errChooseMore').style.display='block';
			}
			if(select.multiple){
				document.getElementById(controlId+'_ctrlTip').style.display='block';
			}
			
			var checkbox=document.getElementById(controlId+'_checkbox');
			checkbox.defaultChecked=select.multiple;
		
		}
		function CreateStrongSpan(msg){
			var b=document.createElement('b');
			b.appendChild(document.createTextNode(msg));
			var span=document.createElement('span');
			span.appendChild(b);
			return span;
		}
		function PopulateRegiontype(controlId,rId){
			var theTable=document.getElementById(controlId+'_table');
			var theRow=theTable.insertRow(1);
			var td=theRow.insertCell(0);
			if(regiontypeNumber){
				td.appendChild(CreateStrongSpan(regiontypeNumber[0]));
			}
			td=theRow.insertCell(1);
			td.appendChild(document.createTextNode(msgText[5]));
			var td=theRow.insertCell(2);
			var select=document.createElement('select');
			select.id=controlId+'_regiontype';
			select.name=controlId+'_regiontype';
			if(select.addEventListener){
				select.addEventListener('change',function(){
					RegiontypeChanged(controlId);
				},false);
			}
			else if(select.attachEvent){
				select.attachEvent('onchange',function(){
					RegiontypeChanged(controlId);
				});
			}
			td.appendChild(select);
			AppendErrDiv(GetRtErrPrefix()+'_regiontype',rId,"-1",td);
			
			var country=document.getElementById(controlId+'_country');
			var url=GetUrl_ListRegionType(country.value,country.multiple);

			ProcessXmlHttpSelect(url,select,rId);
		}
		
		function RegiontypeChanged(controlId){
			var country=document.getElementById(controlId+'_country');
			var regiontype=document.getElementById(controlId+'_regiontype');
			var rtRow=regiontype.parentNode.parentNode;
			var table=document.getElementById(controlId+'_table');
			ClearRow(table,rtRow.rowIndex+1,table.rows.length);
			if(regiontype.value=="-1")
				return false;
			var err=document.getElementById(GetRtErrPrefix()+'_regiontype');
			if(err){
				err.style.display='none';
			}

			var cids=GetMultipleSelectedValue(country);
			
			if(country.multiple){
				if(cids.split(',').length<2){
					regiontype.selectedIndex=0;
					document.getElementById(controlId+'_errChooseMore').style.display='block';
					return false;
				}
			}
			var url=GetUrl_ListTargetRegions(cids,regiontype.value);
			AddTargetRegions(controlId,url);
			
			
		}
		
		function GetMultipleSelectedValue(selElement){
			var ret;
			ret="";
			for(var ii=0;ii<selElement.length;ii++){
				if(selElement.options[ii].selected){
					if(ret!="")
						ret+=","
					ret+=selElement.options[ii].value;
				}
			}
			return ret;
		}

		function ClearRow(table,indexFrom,indexTo){
			var index=indexFrom;
			var ii;
			for(ii=indexFrom;ii<indexTo;ii++){
				//when row deleted, index will be recounted.
				//that's why we need to keep to delete the row with index of (index)
				table.deleteRow(index);
			}
		}

		function checkbox_Clicked(controlId){
			var country=document.getElementById(controlId+'_country');
			var checkbox=document.getElementById(controlId+'_checkbox');
			var table=document.getElementById(controlId+'_table');
			ClearRow(table,2,table.rows.length);
			country.multiple=checkbox.checked;
			var url;
			if(country.multiple){
				country.selectedIndex=-1;
				url=GetUrl_ListRegionType(country.value,true);
				//url="/snabbasvar/regions.aspx?action=4";
				document.getElementById(controlId+'_ctrlTip').style.display='block';
			}
			else{
				country.selectedIndex=-1;
				country.options[0].selected=true;
				//var cid=document.getElementById(controlId+'_country').value;
				url=GetUrl_ListRegionType(country.value);
				//url="/snabbasvar/regions.aspx?action=2&cid="+cid;
				document.getElementById(controlId+'_ctrlTip').style.display='none';
			}
			var select=document.getElementById(controlId+'_regiontype');
			ProcessXmlHttpSelect(url,select);
			document.getElementById(controlId+'_errChooseMore').style.display='none';

		}
		function country_Changed(controlId){
			var table=document.getElementById(controlId+'_table');
			ClearRow(table,2,table.rows.length);
			var regiontype=document.getElementById(controlId+'_regiontype');
			regiontype.selectedIndex=0;
			var country=document.getElementById(controlId+'_country');
			var cc=GetMultipleSelectedValue(country).split(',');
			var errChooseMore=document.getElementById(controlId+'_errChooseMore');
			if(country.multiple){
				if(cc.length<2){
					errChooseMore.style.display='block';
				}
				else{
					errChooseMore.style.display='none';
				}
				
			}
			
		}
		function GetUrl_ListRegionType(cid,multiple){
			if(multiple)
				return "../regions.aspx?action=4";
			else
				return "../regions.aspx?action=2&cid="+cid;
		}
		function GetUrl_ListTargetRegions(cid,regiontype){
			return "../regions.aspx?action=3&cid="+cid+"&rt="+regiontype;
		}
		function ProcessXmlHttpSelect(url,select,rId){
			var xmlhttp=createXMLHttpRequest();
			xmlhttp.open("GET",url,true);
			xmlhttp.onreadystatechange=function(){
				if(xmlhttp.readyState==4){
					var options=xmlhttp.responseText.split(';');
					var par,index,text,value;
					for(var i=0;i<options.length;i++) {
						par=options[i];
						index=par.indexOf(',');
						text=par.substring(0,index);
						value=par.substring(index+1);
						select.options[i]=new Option(text,value);
						if(rId && rId==value)
							select.options[i].selected=true;
					}
				}
			}
			xmlhttp.send(null);
		}
		function AddTargetRegions(controlId,url,rvs){
			var xmlhttp=createXMLHttpRequest();
			xmlhttp.open("GET",url,true);
			xmlhttp.onreadystatechange=function(){
				if(xmlhttp.readyState==4){
					TargetRegionsHandler(controlId,xmlhttp.responseText,rvs);
				}
			}
			xmlhttp.send(null);
		} 
		
		function TargetRegionsHandler(controlId,response,rvs){
			var tagIndex=response.indexOf(',');
			var htmlTag=response.substring(0,tagIndex);
			var response=response.substring(tagIndex+1);
			
			var table=document.getElementById(controlId+'_table');
			var rtRow=document.getElementById(controlId+'_regiontype').parentNode.parentNode;
			var country=document.getElementById(controlId+'_country');
			var cid=GetMultipleSelectedValue(country);
			
			switch(htmlTag){
				case "none":
					break;
				case "select":
					var selRow=table.insertRow(rtRow.rowIndex+1);
					selRow.insertCell(0);
					selRow.insertCell(1);
					var selCell=selRow.insertCell(2);
					if(rvs){
						var vv=rvs.split('_')[2];
						//alert(vv);
					}
					selCell.appendChild(CreateSelect(GetRvPrefix()+cid,response,vv));
					if(vv){
						AppendErrDiv(GetRvErrPrefix()+cid,vv,"-1",selCell);
					}
					break;
				case "text":
					var txtRow=table.insertRow(rtRow.rowIndex+1);
					txtRow.insertCell(0);
					txtRow.insertCell(1);
					var txtCell=txtRow.insertCell(2);
					txtCell.appendChild(CreateTextDiv(response));
					break;
				case "selects":
					var theRow,theCell,theId,theNextId;
					var cids=cid.split(',');
					var j=0;
					for(var i=0;i<cids.length;i++){
						theRow=table.insertRow(rtRow.rowIndex+j+1);
						theRow.insertCell(0);
						theCell=theRow.insertCell(1);
						theCell.appendChild(document.createTextNode(
							GetCountryName(cids[i])));
						theCell=theRow.insertCell(2);
						
						theId=GetRtPrefix()+cids[i];
						if(i!=cids.length-1)
							theNextId=GetRtPrefix()+cids[i+1];
						else
							theNextId="";
						if(rvs){
							var rv=rvs.split(',')[i];
						}
							
						theCell.appendChild(CreateSubRegiontype(cids[i],theId,theNextId,rv));
						if(rv){
							AppendErrDiv(GetRtErrPrefix()+cids[i],rv.split('_')[1],"-1",theCell);
						}
						if(rv){
							j+=AddRegionVariable(controlId,GetRtPrefix()+cids[i],rv);
						}
						j++;
						
					}
					break;
		
			}
			
		}
		function AddRegionVariable(controlId,rtId,rv){
			var newRow=0;
			var table=document.getElementById(controlId+'_table');
			var rt=document.getElementById(rtId);
			var rtRow=rt.parentNode.parentNode;
			var tripplet=rv.split('_');
			var cid=tripplet[0];
			var url=GetUrl_ListTargetRegions(cid,tripplet[1]);

			var xmlhttp=createXMLHttpRequest();
			xmlhttp.open("GET",url,true);
			xmlhttp.onreadystatechange=function(){
				if(xmlhttp.readyState==4){
					var response=xmlhttp.responseText;		
					var tagIndex=response.indexOf(',');
					var htmlTag=response.substring(0,tagIndex);
					var response=response.substring(tagIndex+1);					

					switch(htmlTag){
						case "none":
							break;
						case "select":
							var selRow=table.insertRow(rtRow.rowIndex+1);
							selRow.insertCell(0);
							selRow.insertCell(1);
							var selCell=selRow.insertCell(2);
							selCell.appendChild(CreateSelect(GetRvPrefix()+cid,response,tripplet[2]));
							AppendErrDiv(GetRvErrPrefix()+cid,tripplet[2],"-1",selCell);
							newRow=1;
							break;
						case "text":
							var txtRow=table.insertRow(rtRow.rowIndex+1);
							txtRow.insertCell(0);
							txtRow.insertCell(1);
							var txtCell=txtRow.insertCell(2);
							txtCell.appendChild(CreateTextDiv(response));
							newRow=1
							break;
					}
				}
			}
			xmlhttp.send(null);
			return newRow;
			
		}
		function SubRegiontypeChanged(cid,id,nextId){
			var subRt=document.getElementById(id);
			var theRow=subRt.parentNode.parentNode;
			var table=theRow.parentNode;
			
			var nextIndex;
			if(nextId=="")
				nextIndex=table.rows.length;
			else
				nextIndex=document.getElementById(nextId).parentNode.parentNode.rowIndex;
			ClearRow(table,theRow.rowIndex+1,nextIndex);
			var url=GetUrl_ListTargetRegions(cid,subRt.value);
			
			var xmlhttp=createXMLHttpRequest();
			xmlhttp.open("GET",url,true);
			xmlhttp.onreadystatechange=function(){
				if(xmlhttp.readyState==4){
					var response=xmlhttp.responseText;
					var tagIndex=response.indexOf(',');
					var htmlTag=response.substring(0,tagIndex);
					var response=response.substring(tagIndex+1);
					switch(htmlTag){
						case "none":
							break;
						case "select":
							theRow=table.insertRow(theRow.rowIndex+1);
							theRow.insertCell(0);
							theRow.insertCell(1);
							theCell=theRow.insertCell(2);
							theCell.appendChild(CreateSelect(GetRvPrefix()+cid,response));
							break;
						case "text":
							theRow=table.insertRow(theRow.rowIndex+1);
							theRow.insertCell(0);
							theRow.insertCell(1);
							theCell=theRow.insertCell(2);
							theCell.appendChild(CreateTextDiv(response));
							break;
					}
				}
			}
			xmlhttp.send(null);
		}
		function CreateSubRegiontype(cid,id,nextId,rv){
			var select=document.createElement('select');
			select.id=GetRtPrefix()+cid;
			select.name=GetRtPrefix()+cid;

			if(select.addEventListener){
				select.addEventListener('change',
					function(){
						SubRegiontypeChanged(cid,id,nextId);
					},false);
			}else if(select.attachEvent){
				select.attachEvent('onchange',function(){
					SubRegiontypeChanged(cid,id,nextId);
				});
			}
			if(rv){
				var tripplet=rv.split('_');
				var t_cid=tripplet[0];
				var t_rt=tripplet[1];
				var t_rv=tripplet[2];
			}
						
			var url=GetUrl_ListRegionType(cid);
			var xmlhttp=createXMLHttpRequest();
			xmlhttp.open("GET",url,true);
			xmlhttp.onreadystatechange=function(){
				if(xmlhttp.readyState==4){
					var options=xmlhttp.responseText.split(';');
					var par,index,text,value;
					for(var i=0;i<options.length;i++) {
						par=options[i];
						index=par.indexOf(',');
						text=par.substring(0,index);
						value=par.substring(index+1);
						select.options[i]=new Option(text,value);
						if(t_rt && t_rt==value)
							select.options[i].selected=true;
						
					}
				}
			}
			xmlhttp.send(null);
			return select;
			
		}
		
		function CreateTextDiv(selVariables){
			var div=document.createElement("DIV");
			
			var variables=selVariables.split(";");
			var ii,par,index;
			for(ii=0;ii<variables.length;ii++){
				par=variables[ii];
				index=par.indexOf(',');
				if(ii>0)
					div.appendChild(document.createElement("br"));
				div.appendChild(document.createTextNode(par.substring(0,index)));
			}
			return div;	
		}
		
		function CreateSelect(selId,selVariables,initvalue){
			var sel=document.createElement("select");
			sel.id=selId;
			sel.name=selId;
			var variables=selVariables.split(";");
			var ii,par,index,text,value;
			for(ii=0;ii<variables.length;ii++){
				par=variables[ii];
				index=par.indexOf(',');
				text=par.substring(0,index);
				value=par.substring(index+1);
				sel.options[ii]=new Option(text,value);
				if(value==initvalue)
					sel.options[ii].selected=true;
			}
			return sel;
		}
		function GetRtPrefix(){
			return "rt_";
		}
		function GetRvPrefix(){
			return "rv_";
		}
		function GetRtErrPrefix(){
			return "rtErr_";
		}
		function GetRvErrPrefix(){
			return "rvErr_";
		}

		function AppendErrDiv(id,checkValue,trapValue,select){
			if(typeof MyMultiRegion!='undefined'){
				if(checkValue==trapValue){
					var theDiv=document.createElement("span");
					theDiv.id=id;
					theDiv.setAttribute("style","display:inline;");
					theDiv.className="errmsg";
					theDiv.appendChild(document.createTextNode(" Var god välj"));
					select.appendChild(theDiv);
				}
			}
		}
		
		function GetCountryName(cid){
			var index,country,name;
			for(var i=0;i<countries.length;i++){
				country=countries[i];
				index=country.indexOf(',');
				if(country.substring(index+1)==cid)
					return country.substring(0,index);
			}
		}


		function createXMLHttpRequest() {
			try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
			try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
			try { return new XMLHttpRequest(); } catch(e) {}
			return null;
		}
