var child_global;
var current_div;

function getDataFromCountry(baby, child, parent_n, ele, type, cols)
{
	clearAllDivContent();
	var div_ele = document.getElementById(child + '_div');
	current_div = div_ele;
	getData(baby, child, parent_n, ele.value, type, cols);
	//getDataFromCountry(child,parent,ele.value);
}

function getDataFromProvince(baby, child, parent_n, ele, type, cols)
{
	clearAllDivContent();
	var div_ele = document.getElementById(child + '_div');
	current_div = div_ele;
	getData(baby, child, parent_n, ele.value, type, cols);
}

function getDataFromRegion(baby, child, parent_n, ele, type, cols)
{
	document.getElementById('city_div').innerHTML = '';
	var div_ele = document.getElementById(child + '_div');
	current_div = div_ele;
	getData(baby, child, parent_n, ele.value, type, cols);
}

function getData(baby, child, parent_n, ID, type, cols) {
	child_global = child;
    if (ID != '') {
        loadXMLDoc('ajax2.php?baby=' + baby + '&child=' + child + '&parent=' + parent_n + '&ID=' + ID + '&type=' + type + '&cols=' + cols);
    }
}

function selectDefaultItem(id, data, type)
{
	var selectEle = data + '[' + id + ']';
	ele = document.getElementById(selectEle);
	if(type == 's')
		ele.selected = true;
	else
		ele.checked = true;
}

function setCheckboxGroup (checkbox, baby, child, parent_n, type, cols) {
  var checkboxGroup = checkbox.form[checkbox.name];
  var div_ele = document.getElementById(child + '_div');
  div_ele.innerHTML = "";
  current_div = div_ele;
  var IDs = '';
  for (var c = 0; c < checkboxGroup.length; c++)
  {
		if (checkboxGroup[c].checked)
		{
			if(IDs != '')
				IDs += ';';
			IDs += checkboxGroup[c].value;
		}
  }
  getData(baby, child, parent_n, IDs, type, cols);
}

function clearAllDivContent()
{
	document.getElementById('region_div').innerHTML = '';
	document.getElementById('city_div').innerHTML = '';
}

var xmlhttp;
function loadXMLDoc(url) {
    // Native
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = processStateChange;
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
    // ActiveX
    } else if (window.ActiveXObject) {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        if (xmlhttp) {
            xmlhttp.onreadystatechange = processStateChange;
            xmlhttp.open("GET", url, true);
            xmlhttp.send();
        }
    }
}

function processStateChange() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			current_div.innerHTML = xmlhttp.responseText;
    }
}

// Service Update
// Made by Olivier Patry
var xmlhttp2;
function serviceSubcategoryUpdate(category) {
	// Native
    if (window.XMLHttpRequest) {
        xmlhttp2 = new XMLHttpRequest();
        xmlhttp2.onreadystatechange = updateSubcategorySelect;
        xmlhttp2.open("GET", "ajax_service.php?do=getsubsforcat&category=" + category, true);
		xmlhttp2.overrideMimeType("text/html; charset=ISO-8859-1");
        xmlhttp2.send(null);
    // ActiveX
    } else if (window.ActiveXObject) {
        xmlhttp2 = new ActiveXObject("Microsoft.XMLHTTP");
        if (xmlhttp2) {
            xmlhttp2.onreadystatechange = updateSubcategorySelect;
            xmlhttp2.open("GET", "ajax_service.php?do=getsubsforcat&category=" + category, true);
            xmlhttp2.send();
        }
    }
}

function updateSubcategorySelect() {
	if (xmlhttp2.readyState == 4 && xmlhttp2.status == 200) {
		
		// Set vars
		var response = xmlhttp2.responseText;
		var SubCatSelect = document.getElementById("subcategory");
		
		// Split response
		var options = response.split("|");
		
		// Clear subcategory select
		SubCatSelect.options.length = 0;
		
		// Split & append each option
		for (var i = 0; i <= options.length; i++) {
			var options2 = options[i].split(",");
			var subcategoryvalue = options2[0];
			var subcategorytext = options2[1];
			SubCatSelect.options[SubCatSelect.length] = new Option(subcategorytext, subcategoryvalue);
		}
    }
}
