var root = '/';
var xmlhttp = true;
var xmlhttp_country;
var xmlhttp_network;
var xmlhttp_category;

function ajax_init(country) {
		  try {
					 xmlhttp_country = new XMLHttpRequest();
					 xmlhttp_network = new XMLHttpRequest();
					 xmlhttp_category = new XMLHttpRequest();
		  } catch (error) {
					 try {
								xmlhttp_country = new ActiveXObject("Msxml2.XMLHTTP");
								xmlhttp_network = new ActiveXObject("Msxml2.XMLHTTP");
								xmlhttp_category = new ActiveXObject("Msxml2.XMLHTTP");
					 } catch (error) {
								try {
										  xmlhttp_country = new ActiveXObject("Microsoft.XMLHTTP");
										  xmlhttp_network = new ActiveXObject("Microsoft.XMLHTTP");
										  xmlhttp_category = new ActiveXObject("Microsoft.XMLHTTP");
								} catch (error) {
										  xmlhttp = false;
								}
					 }
		  }
		  
		  if (xmlhttp) {
					 AJAXRequest(document.getElementById('ajax_country'), country);
		  }
}

function AJAXRequest(selObj, addon) {
		  if ("COUNTRY_ID" == selObj.name) {
					 document.getElementById('ajax_network').selectedIndex = 0;
					 document.getElementById('ajax_network').length = 1;
					 document.getElementById('ajax_network').options[1] = new Option('Please, wait...', '0');
					 
					 document.getElementById('ajax_category').selectedIndex = 0;
					 document.getElementById('ajax_category').length = 1;
					 
					 if (selObj.selectedIndex) {
								AJAXSendNetwork(document.getElementById('ajax_network'), root + "request.php?source=network&country=" + selObj.options[selObj.selectedIndex].value);
					 } else {
								if (1 == selObj.options.length) {
										  AJAXSendCountry(selObj, root + "request.php?source=country");
								}
								if ("" != addon && '0' != addon) {
										  AJAXSendNetwork(document.getElementById('ajax_network'), root + "request.php?source=network&country=" + addon);
								} else {
										  AJAXSendNetwork(document.getElementById('ajax_network'), root + "request.php?source=network");
								}
					 }
		  }
		  
		  if ("NETWORK_ID" == selObj.name) {
					 if (selObj.selectedIndex) {
								document.getElementById('ajax_category').selectedIndex = 0;
								document.getElementById('ajax_category').length = 1;
								document.getElementById('ajax_category').options[1] = new Option('Please, wait...', '0');
								
								if (document.getElementById('ajax_country').selectedIndex) {
										  url = root + "request.php?source=category&country=" + document.getElementById('ajax_country').options[document.getElementById('ajax_country').selectedIndex].value + "&network=" + selObj.options[selObj.selectedIndex].value;
								} else {
										  url = root + "request.php?source=category&network=" + selObj.options[selObj.selectedIndex].value;
								}
								AJAXSendCategory(document.getElementById('ajax_category'), url);
					 } else {
								document.getElementById('ajax_category').selectedIndex = 0;
								document.getElementById('ajax_category').length = 1;
					 }
		  }
}

function AJAXSendCountry(selObj, url) {
		  xmlhttp_country.open("GET", url, true);
		  xmlhttp_country.onreadystatechange = function() {
					 if (xmlhttp_country.readyState == 4) {
								eval("AJAXResponse(selObj, xmlhttp_country.responseText);");
					 }
		  }
		  xmlhttp_country.send(null);
}

function AJAXSendNetwork(selObj, url) {
		  xmlhttp_network.open("GET", url, true);
		  xmlhttp_network.onreadystatechange = function() {
					 if (xmlhttp_network.readyState == 4) {
								eval("AJAXResponse(selObj, xmlhttp_network.responseText);");
					 }
		  }
		  xmlhttp_network.send(null);
}

function AJAXSendCategory(selObj, url) {
		  xmlhttp_category.open("GET", url, true);
		  xmlhttp_category.onreadystatechange = function() {
					 if (xmlhttp_category.readyState == 4) {
								eval("AJAXResponse(selObj, xmlhttp_category.responseText);");
					 }
		  }
		  xmlhttp_category.send(null);
}

function AJAXResponse(selObj, value) {
		  if ("COUNTRY_ID" == selObj.name) {
					 addOptions(selObj, eval(value));
		  }
		  
		  if ("NETWORK_ID" == selObj.name) {
					 addOptions(selObj, eval(value));
		  }
		  
		  if ("CATEGORY_ID" == selObj.name) {
					 addOptions(selObj, eval(value));
		  }
}

function addOptions(selObj, array) {
		  selObj.selectedIndex = 0;
		  selObj.length = 1;
		  cnti = array.length;
		  for (i=0; i<cnti; i++) {
					 selObj.options[i+1] = new Option(array[i][1], array[i][0]);
					 if ("1" == array[i][2]) {
								selObj.options[i+1].id="bglightgray";
					 }
		  }
}

var _country;
var _network;
var _category;

function ajax_update(country, network, category) {
		  _country = country;
		  _network = network;
		  _category = category;
		  
		  if ("0" != country) {
					 window.setTimeout("update_country()", 100);
		  }
		  
		  if ("0" != network) {
					 window.setTimeout("update_network()", 100);
		  }
		  
		  if ("0" != category) {
					 window.setTimeout("update_category()", 100);
		  }
}

function update_country() {
		  if (1 == document.getElementById('ajax_country').options.length) {
					 window.setTimeout("update_country()", 100);
					 return;
		  }
		  
		  cntj = document.getElementById('ajax_country').options.length;
		  for (j=1; j<cntj; j++) {
					 if (document.getElementById('ajax_country').options[j].value == _country) {
								document.getElementById('ajax_country').options[j].selected = true;
								break;
					 }
		  }
}

function update_network() {
		  if (1 == document.getElementById('ajax_network').options.length) {
					 window.setTimeout("update_network()", 100);
					 return;
		  }
		  if ("Please, wait..." == document.getElementById('ajax_network').options[1].text) {
					 window.setTimeout("update_network()", 100);
					 return;
		  }
		  
		  cntj = document.getElementById('ajax_network').options.length;
		  for (j=1; j<cntj; j++) {
					 if (document.getElementById('ajax_network').options[j].value == _network) {
								document.getElementById('ajax_network').options[j].selected = true;
								AJAXSendCategory(document.getElementById('ajax_category'), root + "request.php?source=category&country=" + _country + "&network=" + _network);
								break;
					 }
		  }
}

function update_category() {
		  if (1 == document.getElementById('ajax_category').options.length) {
					 window.setTimeout("update_category()", 100);
					 return;
		  }
		  if ("Please, wait..." == document.getElementById('ajax_category').options[1].text) {
					 window.setTimeout("update_category()", 100);
					 return;
		  }
		  
		  cntj = document.getElementById('ajax_category').options.length;
		  for (j=1; j<cntj; j++) {
					 if (document.getElementById('ajax_category').options[j].value == _category) {
								document.getElementById('ajax_category').options[j].selected = true;
								break;
					 }
		  }
}
