var selected_country = 0;
var selected_region = 0;
var selected_state = 0;


function addOptionsFromJson(element, json) {
	try {
		var jscode = eval(json);
		var obj = null;
		
		if (typeof(element)=='object'){
			obj = element;
		} else {
			obj = getEl(element);
		}
		//obj.options[0] = new Option("-chose "+element.id+"-",0);
		
		obj.options[0] = new Option(" -----",0);
		for(var i=0; i < jscode.length; i++) {
			obj.options[i+1] = new Option(jscode[i].name, jscode[i].value)
			obj.options[i].selected = false;
		}
	}catch(e) {
		if(window.error_reporting) {
			alert('Error: ('+e+') in locations.js line 28');
		}
	}
	
}

/*
function onCountrySelect(id) {
	var canSelectFirstIndex = true;
	 if (typeof(id)!='number'){
	 	selected_country = this.options[this.selectedIndex].value;
	 } else {
	 	selected_country = id;
		canSelectFirstIndex = false;
	 }
	 
	 $.get("inc/locations.php",
	   { id_country: selected_country, action:"region", lang:selected_lang},
	   function(data){
			//$('#region').html(data);
			$('#region').empty()
			addOptionsFromJson($('#region').get(0), data);
			$('#region').change(onRegionSelect);
			if(canSelectFirstIndex) {
				getEl('region').selectedIndex = 0;
			}
	   }
	 );
	$('#state').empty();
	$('#city').empty();
}

function onRegionSelect(id) {
	var canSelectFirstIndex = true;
	 if (typeof(id)!='number'){
	 	selected_region = this.options[this.selectedIndex].value;
	 } else {
	 	selected_region = id;
		canSelectFirstIndex = false;
	 }
	 
	 $.get("inc/locations.php",
	   { id_country: selected_country, id_region:selected_region , action:"state", lang:selected_lang},
	   function(data){
			//$('#state').html(data);
			$('#state').empty();
			addOptionsFromJson($('#state').get(0), data);
			$('#state').change(onStateSelect);
			if(canSelectFirstIndex) {
				getEl('state').selectedIndex = 0;			
			}
	   }
	 );
	$('#city').empty();
}
*/

function onCountrySelect(id) {
	try {
		var canSelectFirstIndex = true;
		 if (typeof(id)!='number'){
		 	selected_country = this.options[this.selectedIndex].value;
		 } else {
		 	selected_country = id;
			canSelectFirstIndex = false;
		 }
		 
		 $.get("inc/locations.php",
		   //{ id_country: selected_country, id_region:selected_region , action:"state", lang:selected_lang},
		   { id_country: selected_country , action:"state", lang:selected_lang},
		   function(data){
				//$('#state').html(data);
				$('#state').empty();
				addOptionsFromJson($('#state').get(0), data);
				$('#state').change(onStateSelect);
				if(canSelectFirstIndex) {
					getEl('state').selectedIndex = 0;			
				}
		   }
		 );
		$('#city').empty();
	}catch(e) {
		if(window.error_reporting) {
			alert('Error: ('+e+') in locations.js line 111');
		}
	}
}


function onStateSelect(id) {
	try {
		 var canSelectFirstIndex = true;
		 if (typeof(id)!='number'){
		 	selected_state = this.options[this.selectedIndex].value;
		 } else {
		 	selected_state = id;
			canSelectFirstIndex = false;
		 }
		 
		 $.get("inc/locations.php",
		   { id_country:selected_country, id_region:selected_region,id_state:selected_state, action:"city", lang:selected_lang},
		   function(data){
				//$('#city').html(data);
				addOptionsFromJson($('#city').get(0), data);
				if(canSelectFirstIndex) {
					getEl('city').selectedIndex = 0;
				}
		   }
		 );
	 }catch(e) {
		if(window.error_reporting) {
			alert('Error: ('+e+') in locations.js line 139');
		}
	}
}


function initPost(){
	try {
		if (id_country){
			onCountrySelect(id_country);
			selectIndex('country', id_country);
		}
		/*
		if (id_country && id_region){
			onRegionSelect(id_region);
			selectIndex('region', id_region);
		}
		*/
		//if (id_country && id_region && id_state){
		if (id_country && id_state){
			onStateSelect(id_state);
			selectIndex('state', id_state);
		}
		//if (id_country && id_region && id_state && id_city){
		if (id_country && id_state && id_city){
			selectIndex('city', id_city);
		}
	}catch(e) {
		if(window.error_reporting) {
			alert('Error: ('+e+') in locations.js line 168');
		}
	}
}

function getEl(id){
	return document.getElementById(id);
}


function selectIndex(element, value){
	try {
		var obj = null;
		
		if (typeof(element)=='object'){
			obj = element;
		} else {
			obj = getEl(element);
		}
		
		if (obj.nodeName.toLowerCase()!='select'){
			return;
		}
		
		for (var i=0;i<obj.options.length;i++){
			if(obj.options[i].value==value){
				obj.selectedIndex = i;
				//alert("select["+element+"] index " + i + " & val = " + obj.options[i].value);
			}
		}
	}catch(e) {
		if(window.error_reporting) {
			alert('Error: ('+e+') in locations.js line 200');
		}
	}
}


///client site part 

//extragem continutul selectat
function getSelElValue(idel) {
	try {
		var element = $('#'+idel).get(0);
		
		if (!element) return "";
		
		var index = element.selectedIndex;
		
		if(!$(element).find('option').get(0)) {
			return -1;
		}
		return element.options[index].value;
	}catch(e) {
		if(window.error_reporting) {
			alert('Error: ('+e+') in locations.js line 223');
		}
	}
}




if(typeof(window.onload)=='function') {
	var oldEvent2 = window.onload;
	window.onload = function() {
		oldEvent2();
		initLocations();
	}
} else {
	window.onload = function() {
		initLocations();
	}
}



function initLocations() {
	$('#country').change(onCountrySelect);
	//if (formSubmited){
		initPost();
	//}
}