/************************************************************************************************************
Ajax chained select
Copyright (C) 2006  DTHMLGoodies.com, Alf Magne Kalleland

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

Dhtmlgoodies.com., hereby disclaims all copyright interest in this script
written by Alf Magne Kalleland.

Alf Magne Kalleland, 2006
Owner of DHTMLgoodies.com


************************************************************************************************************/	
var ajax = new Array();

function getDistrictList(sel)
{
	var countryCode = sel.options[sel.selectedIndex].value;
	document.getElementById('ad_district').options.length = 0;	// Empty city select box
	document.getElementById('ad_city').options.length = 0;	// Empty city select box
	document.getElementById('ad_city_district').options.length = 0;	// Empty city select box

	AddItem('ad_city', 'Välj stad/kommun');
	AddItem('ad_city_district', 'Välj stadsdel');  
	
	if(document.getElementById('ad_ad_loop')){
	  document.getElementById('ad_ad_loop').options.length = 0;	// Empty city select box
		AddItem('ad_ad_loop', 'Välj slinga', '');
	} 	

	if(countryCode.length>0){
		var index = ajax.length;
		ajax[index] = new sack();
		
		ajax[index].requestFile = 'getDistrict.php?countryCode='+countryCode;	// Specifying which file to get
		ajax[index].onCompletion = function(){ createDistricties(index) };	// Specify function that will be executed after file has been found
		ajax[index].runAJAX();		// Execute AJAX function
	}
}

function getDistrictList_nc(sel, n)
{
	var countryCode = sel.options[sel.selectedIndex].value;
	document.getElementById('ad_district_' + n).options.length = 0;	// Empty city select box
	
	if(countryCode.length>0){
		var index = ajax.length;
		ajax[index] = new sack();
		
		ajax[index].requestFile = 'getDistrict.php?countryCode='+countryCode;	// Specifying which file to get
		ajax[index].onCompletion = function(){ createDistricties_nc(index, n) };	// Specify function that will be executed after file has been found
		ajax[index].runAJAX();		// Execute AJAX function
	}
}

function getCityList(sel, sel2)
{
	var districtCode = sel.options[sel.selectedIndex].value; 
	var countryCode = sel2.options[sel2.selectedIndex].value;
	
	document.getElementById('ad_city').options.length = 0;	// Empty city select box
	AddItem('ad_city', 'Välj stad/kommun', ''); 	
	document.getElementById('ad_city_district').options.length = 0;	// Empty city select box
	AddItem('ad_city_district', 'Välj stadsdel', ''); 	
	
	if(document.getElementById('ad_ad_loop')){
  	document.getElementById('ad_ad_loop').options.length = 0;	// Empty city select box
	  AddItem('ad_ad_loop', 'Välj slinga', '');
	} 	
	
	if(districtCode.length>0){
		var index = ajax.length;
		ajax[index] = new sack();
		
		ajax[index].requestFile = 'getCity.php?countryCode='+countryCode+'&districtCode='+districtCode;	// Specifying which file to get
		ajax[index].onCompletion = function(){ createCities(index) };	// Specify function that will be executed after file has been found
		ajax[index].runAJAX();		// Execute AJAX function
	}
}

function getCityList_nc(sel, sel2, n)
{
	var districtCode = sel.options[sel.selectedIndex].value; 
	var countryCode = sel2.options[sel2.selectedIndex].value;
	document.getElementById('ad_city_' + n).options.length = 0;	// Empty city select box
	
	document.getElementById('ad_city_district_' + n).options.length = 0;
	AddItem('ad_city_district_' + n, 'Välj stadsdel', ''); 	

	if(districtCode.length>0){
		var index = ajax.length;
		ajax[index] = new sack();
		
		ajax[index].requestFile = 'getCity.php?show=1&countryCode='+countryCode+'&districtCode='+districtCode;	// Specifying which file to get
		ajax[index].onCompletion = function(){ createCities_nc(index, n) };	// Specify function that will be executed after file has been found
		ajax[index].runAJAX();		// Execute AJAX function
	}
}

function getCityDistrictList(sel, sel2, sel3)
{
	var cityCode = sel.options[sel.selectedIndex].value;
	var districtCode = sel3.options[sel3.selectedIndex].value; 
	var countryCode = sel2.options[sel2.selectedIndex].value;
	
	document.getElementById('ad_city_district').options.length = 0;	// Empty city select box
	AddItem('ad_city_district', 'Välj stadsdel', ''); 	

	if(document.getElementById('ad_ad_loop')){
	  document.getElementById('ad_ad_loop').options.length = 0;	// Empty city select box
		AddItem('ad_ad_loop', 'Välj slinga', '');
	} 
	
	if(cityCode.length>0){
		var index = ajax.length;
		ajax[index] = new sack();
		
		ajax[index].requestFile = 'getCityDistrict.php?countryCode='+countryCode+'&districtCode='+districtCode+'&cityCode='+cityCode;	// Specifying which file to get
		ajax[index].onCompletion = function(){ createCityDistricties(index) };	// Specify function that will be executed after file has been found
		ajax[index].runAJAX();		// Execute AJAX function
	}
}

function getCityDistrictList_nc(sel, sel2, sel3, n)
{
  var cityCode = sel.options[sel.selectedIndex].value;
	var districtCode = sel3.options[sel3.selectedIndex].value; 
	var countryCode = sel2.options[sel2.selectedIndex].value;
	document.getElementById('ad_city_district_' + n).options.length = 0;	// Empty city select box
	if(cityCode.length>0){
		var index = ajax.length;
		ajax[index] = new sack();
		
		ajax[index].requestFile = 'getCityDistrict.php?show=1&countryCode='+countryCode+'&districtCode='+districtCode+'&cityCode='+cityCode;	// Specifying which file to get
		ajax[index].onCompletion = function(){ createCityDistricties_nc(index, n) };	// Specify function that will be executed after file has been found
		ajax[index].runAJAX();		// Execute AJAX function
	}
}

function getAdLoopList(sel1, sel2, sel3, sel4)
{
	var countryCode = sel2.options[sel2.selectedIndex].value;  	
	var districtCode = sel3.options[sel3.selectedIndex].value; 
  	var cityCode = sel4.options[sel4.selectedIndex].value;
	var cityDistrictCode = sel1.options[sel1.selectedIndex].value;
	
	document.getElementById('ad_ad_loop').options.length = 0;	// Empty city select box
	
	if(countryCode.length>0 && districtCode.length>0 && cityCode.length>0 && cityDistrictCode.length>0){
		var index = ajax.length;
		ajax[index] = new sack();
		
		ajax[index].requestFile = 'getAdLoop.php?countryCode='+countryCode+'&districtCode='+districtCode+'&cityCode='+cityCode+'&cityDistrictCode='+cityDistrictCode;	// Specifying which file to get
		ajax[index].onCompletion = function(){ createAdLoops(index) };	// Specify function that will be executed after file has been found
		ajax[index].runAJAX();		// Execute AJAX function
	}
}

function AddItem(id, Text,Value)
    {
        // Create an Option object        
        var opt = document.createElement("option");

        // Add an Option object to Drop Down/List Box
        document.getElementById(id).options.add(opt);

        // Assign text and value to Option object
        opt.text = Text;
        opt.value = Value;

    }

function createAdLoops(index)
{
	var obj = document.getElementById('ad_ad_loop');
	eval(ajax[index].response);	// Executing the response from Ajax as Javascript code	
}

function createCities(index)
{
	var obj = document.getElementById('ad_city');
	eval(ajax[index].response);	// Executing the response from Ajax as Javascript code	
}

function createCities_nc(index, n)
{
	var obj = document.getElementById('ad_city_' + n);
	eval(ajax[index].response);	// Executing the response from Ajax as Javascript code	
}

function createDistricties(index)
{
	var obj = document.getElementById('ad_district');
	eval(ajax[index].response);	// Executing the response from Ajax as Javascript code	
}

function createDistricties_nc(index, n)
{
	var obj = document.getElementById('ad_district_' + n);
	eval(ajax[index].response);	// Executing the response from Ajax as Javascript code	
}

function createCityDistricties(index)
{
	var obj = document.getElementById('ad_city_district');
	eval(ajax[index].response);	// Executing the response from Ajax as Javascript code	
}

function createCityDistricties_nc(index, n)
{
	var obj = document.getElementById('ad_city_district_' + n);
	eval(ajax[index].response);	// Executing the response from Ajax as Javascript code	
}

function getSubCategoryList(sel)
{
	var category = sel.options[sel.selectedIndex].value;
	document.getElementById('dhtmlgoodies_subcategory').options.length = 0;	// Empty city select box
	if(category.length>0){
		var index = ajax.length;
		ajax[index] = new sack();
		
		ajax[index].requestFile = 'getSubCategories.php?category='+category;	// Specifying which file to get
		ajax[index].onCompletion = function(){ createSubCategories(index) };	// Specify function that will be executed after file has been found
		ajax[index].runAJAX();		// Execute AJAX function
	}
}
function createSubCategories(index)
{
	var obj = document.getElementById('dhtmlgoodies_subcategory');
	eval(ajax[index].response);	// Executing the response from Ajax as Javascript code	
}		
