var inserted_trs=0;

function select_type(selected_type)
{
//alert(selected_type);
if ((selected_type==3)||(selected_type==4)) {show_type_input();insert_lnk_for_add_type_input();}
if ((selected_type==1)||(selected_type==2)) {hide_type_input();}
}

function show_type_input()
{
	inserted_trs++;
	var toInsert=document.getElementById("add_opinion_parameter");
	var cTR=document.createElement('tr'); 
	cTR.id="inserted_tr"+inserted_trs;
	toInsert.tBodies[0].appendChild(cTR);
	var cTD=document.createElement('td');
	var oText=document.createTextNode('Значение поля:');	
	cTD.appendChild(oText);
	cTR.appendChild(cTD);
	
	var cTD=document.createElement('td');
	oInput=document.createElement("input"); ///создание элемента INPUT   
	oInput.id="type_input"+inserted_trs;
	oInput.size='30';
	oInput.name='type_input'+inserted_trs;
	oInput.type='text';
	//oInput.className+="text";
	cTD.appendChild(oInput);
	cTR.appendChild(cTD);
	
	var inserted_trs_counter=document.getElementById("inserted_trs_counter");
	inserted_trs_counter.value=inserted_trs;
}

function insert_lnk_for_add_type_input()
{
	var toInsert=document.getElementById("insert_lnk_for_add_type_input");
	cA=document.createElement('a');
	cA.id="lnk_for_add_type_input";
	oText=document.createTextNode("Добавить поле");	
	cA.appendChild(oText); 
	newAHref=document.createAttribute("href");
	newAHref.value = "javascript:show_type_input()";	 
	cA.setAttributeNode(newAHref);
	//cA.className+="task_title_link"; ///!!!!
	toInsert.appendChild(cA);
}

function hide_type_input()
{
	cRemove=document.getElementById('lnk_for_add_type_input');
	cRemove.removeNode(true);
	if (inserted_trs>=1)
	{
		for(i=1;i<=inserted_trs;i++)
		{
			id="inserted_tr"+i;
			cRemove=document.getElementById(id);
			cRemove.removeNode(true);
		}
	}
}


////Функции выбора стран, регонов, городов, агенств
function get_region_private(country_id)
{
//alert(country_id);
if (document.getElementById("select_cities")){var toDelete=document.getElementById("select_cities");toDelete.removeNode(true);}
if (document.getElementById("select_regions")){var toDelete=document.getElementById("select_regions");toDelete.removeNode(true);}
if (document.getElementById("select_agencies")){var toDelete=document.getElementById("select_agencies");toDelete.removeNode(true);}
url="generator.php?ajax_action=get_region&country_id="+country_id;
sendRequestPrivate(url);
}

function get_city_private()
{
	if (document.getElementById("select_cities")){var toDelete=document.getElementById("select_cities");toDelete.removeNode(true);}
	if (document.getElementById("select_agencies")){var toDelete=document.getElementById("select_agencies");toDelete.removeNode(true);}
	region_id=cSelect.value;
	//alert(region_id);
	url="generator.php?ajax_action=get_city&region_id="+region_id;
	sendRequestPrivate(url);
}

function get_agency_private()
{
	if (document.getElementById("select_agencies")){var toDelete=document.getElementById("select_agencies");toDelete.removeNode(true);}
	city_id=cSelect2.value;
	//alert(city_id);
	url="generator.php?ajax_action=get_agency&city_id="+city_id;
	sendRequestPrivate(url);
}


function sendRequestPrivate(url)
{
		if(window.XMLHttpRequest)
		{
		    //alert('t1');
			req = new XMLHttpRequest();
		    req.open("GET", url, true);
			req.onreadystatechange=processReqChangePrivate;
		    req.send(null);	  
			
		 } 
		  else if(window.ActiveXObject)
		 {
		   
			req = new ActiveXObject("Microsoft.XMLHTTP");
		    if(req)
			{ 
			//alert('t2');
		      req.open("GET", url, true); 
     	      req.onreadystatechange=processReqChangePrivate;
		      req.send(); 
		  	}
		  }   
//alert(req.status); 
}

function processReqChangePrivate() 
{
	if ((req.readyState==4) && (req.status==200)) 
  
	{  	
		var response;
		if (req.responseXML.documentElement) 
		{
		//alert('1');
	 	response = req.responseXML.documentElement;
	 	} 
		else 
		{
		//alert('2');
	  	response=req.responseXML.loadXML(req.responseText);
	 	}	
		//if (read_action==1) {showTaskDetailp(response4);}
		PrivateResponse(response);
	}	//else alert(req4.readyState); 
	//else alert("Идет загрузка");
}

function PrivateResponse(response)
{
	var receivedTag = req.responseXML; 
	var action = receivedTag.getElementsByTagName("data").item(0).getAttribute("action");
	//alert(action);
	if (action=="get_regions")
	{
		var itemList = receivedTag.getElementsByTagName("data");
		var nodeList = itemList.item(0).childNodes;
		var cDiv,oData,oText,oID,oExecutor,cIns;
		
		if (nodeList.length>=1)
		{
			
			cIns=document.getElementById('region');
			//создание элемента SELECT
			cSelect=document.createElement('select');
			cSelect.id='select_regions';
			cSelect.name='region_id';
			cSelect.onchange=get_city_private;
			cIns.appendChild(cSelect);
			
			///////// OPTION ////////////////
			option=document.createElement('option'); ///создание элемента OPTION	
			//option.document.createAttribute("value");	///добавление атрибута элемента option	
			//option.setAttribute("value",0); ///присвоение атрибуту значения
			//option.setAttribute("selected","yes"); 
			option.value=0;
			cSelect.appendChild(option);
			var oText=document.createTextNode("регион");	
			option.appendChild(oText);
			//alert(oText);
			//alert("tt");
		}
		
		for (i=0;i<nodeList.length;i++)
		{
			var node = nodeList.item(i); 
			if (node.nodeName == "region") 
			{
				
				gData = node.firstChild.nodeValue;
				var oID = node.getAttribute("id");
				///////// OPTION ////////////////
				option=document.createElement('option'); ///создание элемента OPTION	
				//option.document.createAttribute("value");	///добавление атрибута элемента option	
				//option.setAttribute("value",oID); ///присвоение атрибуту значения
				option.value=oID;
				//option.setAttribute("selected","yes");
				cSelect.appendChild(option);
				var oText=document.createTextNode(gData);	
				option.appendChild(oText); 
				//option.id="every_days";
				
			}
		}
	}
	////CITIES
	if (action=="get_cities")
	{
		var itemList = receivedTag.getElementsByTagName("data");
		var nodeList = itemList.item(0).childNodes;
		var cDiv,oData,oText,oID,oExecutor,cIns;
		
		if (nodeList.length>=1)
		{
			cIns=document.getElementById('city');
			//создание элемента SELECT
			cSelect2=document.createElement('select');
			cSelect2.id='select_cities';
			cSelect2.name='city_id';
			cSelect2.onchange=get_agency_private;
			cIns.appendChild(cSelect2);
			
			///////// OPTION ////////////////
			option=document.createElement('option'); ///создание элемента OPTION	
			//option.document.createAttribute("value");	///добавление атрибута элемента option	
			//option.setAttribute("value",0); ///присвоение атрибуту значения
			//option.setAttribute("selected","yes");
			option.value=0;
			cSelect2.appendChild(option);
			var oText=document.createTextNode("город");	
			option.appendChild(oText);
		}
		
		for (i=0;i<nodeList.length;i++)
		{
			var node = nodeList.item(i); 
			if (node.nodeName == "city") 
			{
				
				gData = node.firstChild.nodeValue;
				var oID = node.getAttribute("id");
				//alert (gData);
				
				///////// OPTION ////////////////
				option=document.createElement('option'); ///создание элемента OPTION	
				//option.document.createAttribute("value");	///добавление атрибута элемента option	
				//option.setAttribute("value",oID); ///присвоение атрибуту значения
				option.value=oID;
				//option.setAttribute("selected","yes");
				cSelect2.appendChild(option);
				var oText=document.createTextNode(gData);	
				option.appendChild(oText); 
				//option.id="every_days";
				
			}
		}
	}
	//AGENCIES
	if (action=="get_agencies")
	{
		var itemList = receivedTag.getElementsByTagName("data");
		var nodeList = itemList.item(0).childNodes;
		var cDiv,oData,oText,oID,oExecutor,cIns;
		
		if (nodeList.length>=1)
		{
			cIns=document.getElementById('agency');
			//создание элемента SELECT
			cSelect3=document.createElement('select');
			cSelect3.id='select_agencies';
			cSelect3.name='agency_id';
			cIns.appendChild(cSelect3);
			cSelect3.onchange=activate_next_page;
			///////// OPTION ////////////////
			option=document.createElement('option'); ///создание элемента OPTION	
			//option.document.createAttribute("value");	///добавление атрибута элемента option	
			//option.setAttribute("value",0); ///присвоение атрибуту значения
			//option.setAttribute("selected","yes");
			option.value=0;
			cSelect3.appendChild(option);
			var oText=document.createTextNode("агентство");	
			option.appendChild(oText);
		}
		
		for (i=0;i<nodeList.length;i++)
		{
			var node = nodeList.item(i); 
			if (node.nodeName == "agency") 
			{
				
				gData = node.firstChild.nodeValue;
				var oID = node.getAttribute("id");
				//alert (gData);
				
				///////// OPTION ////////////////
				option=document.createElement('option'); ///создание элемента OPTION	
				//option.document.createAttribute("value");	///добавление атрибута элемента option	
				//option.setAttribute("value",oID); ///присвоение атрибуту значения	
				option.value=oID;
				//option.setAttribute("selected","yes");
				cSelect3.appendChild(option);
				var oText=document.createTextNode(gData);	
				option.appendChild(oText); 
				//option.id="every_days";
				
			}
		}
		
	}
	
	function activate_next_page()
	{
		if (document.getElementById('add_opinion_next')) 
		{
			nextPage=document.getElementById('add_opinion_next');
			nextPage.style.visibility='visible';
			dir_id=cSelect2.value;
			agency_id=cSelect3.value;
			nextPage.href="index.php?action=add_opinion&dir_id="+dir_id+"&agency_id="+agency_id;
		}
	}
} 

