// JavaScript Document
function onChangeCityValue(slEl) {
	var sl_id_default = 'city_list';
	var city_id;
	var country_value;
	var suffix;
	if (slEl.id.search('city_list') == -1) {
		return false;
	}
	if (slEl.id == sl_id_default) {
		city_id = 'city_name';
		country_id = 'country_id';
		suffix = "";
	}
	else {
		suffix = slEl.id.substring(sl_id_default.length);
		
		city_id = 'city_name' + suffix;
		country_id = 'country_id' + suffix;
	}
	if (document.getElementById(country_id))
	{
		country_value = document.getElementById(country_id).value;
	}
	//alert(suffix);
	if (country_value != '46') {
		return false;
	}
	var $j = jQuery.noConflict();
	var data = 'misc_js.php?task=auto_suggest_city&state=' + slEl.value + '&country=' + country_value;
	oldAjax=1;
	$j("#" + city_id).unautocomplete();
	$j("#" + city_id).autocomplete(data, {selectFirst: false});
	$j("#" + city_id).flushCache();
}

function getCitiesByCountry(country_id,obj){
	if(country_id>0 && obj)
	{
		var parent = obj.parentNode;
		var loading_id = obj.id + "_loading";
		var loading_ele = document.getElementById(loading_id);
		if(!loading_ele)
		{
			loading_ele = document.createElement("SPAN");
			loading_ele.id = loading_id;
			loading_ele.innerHTML = "<img src='./images/icons/loading2.gif'/>";
			loading_ele.style.display = "block";
			parent.appendChild(loading_ele);
		}
		else
		{
			loading_ele.style.display = "block";
		}
		obj.style.display = "none";
		
		var makeRequest = new Request.JSON({
			url: "misc_js.php",
			'method':'post',
			'data' : {
				'task' : 'get_city_list',
				'country_id' :country_id						
			  },
			onComplete: function (response){
				obj.style.display = "block";
				obj.options.length = 0;
				var cities_list = response;  
				for(var i = 0; i < cities_list.length; i ++)
				{
					 var optn = document.createElement("OPTION");
					 optn.text = cities_list[i]['city_name'];
					 optn.value = cities_list[i]['city_id'];
					 obj.options.add(optn);
				}
				obj.onchange = function() {
					onChangeCityValue(this);
				}
				loading_ele.style.display = "none";
			  }
		    });
		makeRequest.send();
	}
}



//GET LOCATION FORM

function getLocationForm(country_id,city_id,obj,province_name,city_name,street_name,zipcode_name,is_array,city_value,street_value,zipcode_value,streetlength, returnFunc)
{
	if (country_id == 0 || country_id =='')
		{
			country_id = 46;
		}
	
	if (typeof returnFunc != "undefined" && typeof returnFunc == "function") {
		returnFunc = returnFunc;
	}
	else {
		returnFunc = function(){};
	}
	var makeRequest = new Request(
				{
					url: "misc_js.php",
					'method':'post',
					'data' : {
						'task' : 'get_location_form',
						'country_id' :country_id,	
						'city_id' :city_id,
						'city_name' :city_name,
						'province_name' :province_name,
						'street_name' :street_name,
						'zipcode_name' :zipcode_name,
						'is_array' :is_array,
						'city_value' :city_value,
						'street_value' :street_value,
						'zipcode_value' :zipcode_value,
						'streetlength' :streetlength	
					  },
					onComplete: function (respone){
						obj.set('html',respone);
						
						// var state_obj = obj.getElementsByTagName('select')[1];
						// if (state_obj) {
							// onChangeCityValue(state_obj);
						//};
						var $j = jQuery.noConflict();
						$j(document).ready(function(){
							var inputs = obj.getElementsByTagName('input');
							if (inputs) {
								var input_country = inputs[0];
						        var input_state = inputs[1];
						        var input_city = inputs[2];
						        // alert(input_country.id + '|' + input_state.id + '|' + input_city.id);
						        if (input_country && input_state && input_city) {
						        	var data_country = 'misc_js.php?task=autosuggest_country';
							        var data_state = 'misc_js.php?task=autosuggest_state';
							        var data_city = 'misc_js.php?task=autosuggest_city';
							        
							        $j("#" + input_country.id).autocomplete(data_country,{selectFirst: true});
							        $j("#" + input_state.id).autocomplete(data_state,
							        		{
							        			selectFirst: true, 
							        			extraParams: {
							        				country: function() {							        					
							        					// alert(input_country.id);
							        					return document.getElementById(input_country.id).value;
							        				}
							        			},
							        			cacheLength: 0
							        		}
							        );
							        $j("#" + input_city.id).autocomplete(data_city,
							        		{
							        			selectFirst: true,
							        			extraParams: {
							        				country: function() {
							        					return document.getElementById(input_country.id).value;
							        				},
							        				state: function() {
							        					return document.getElementById(input_state.id).value;
							        				}
							        			},
							        			cacheLength: 0
							        	
							        		}
							        
							        );
						        }
							}
					    });
						returnFunc();
					}
				}
		)
		makeRequest.send();
}

function getMultiLocationForm(country_id,city_id,obj,province_name,city_name,street_name,zipcode_name,is_array,city_value,street_value,zipcode_value,streetlength, returnFunc)
{
	if (country_id == 0 || country_id =='')
	{
		country_id = 46;
	}
	if (typeof returnFunc != "undefined" && typeof returnFunc == "function") {
		returnFunc = returnFunc;
	}
	else {
		returnFunc = function(){};
	}
	var makeRequest = new Request(
				{
					url: "misc_js.php",
					'method':'post',
					'data' : {
						'task' : 'get_multi_location_form',
						'country_id' :country_id,	
						'city_id' :city_id,
						'city_name' :city_name,
						'province_name' :province_name,
						'street_name' :street_name,
						'zipcode_name' :zipcode_name,
						'is_array' :is_array,
						'city_value' :city_value,
						'street_value' :street_value,
						'zipcode_value' :zipcode_value,
						'streetlength' :streetlength	
					  },
					onComplete: function (response){ 
						obj.innerHTML = response;
						/*
						var state_obj = obj.getElementsByTagName('select')[1];
						if (state_obj) {
							onChangeCityValue(state_obj);
						};*/

						var $j = jQuery.noConflict();
						$j(document).ready(function(){
							var inputs = obj.getElementsByTagName('input');
							if (inputs) {
								var input_country = inputs[0];
						        var input_state = inputs[1];
						        var input_city = inputs[2];
						        // alert(input_country.id + '|' + input_state.id + '|' + input_city.id);
						        if (input_country && input_state && input_city) {
						        	var data_country = 'misc_js.php?task=autosuggest_country';
							        var data_state = 'misc_js.php?task=autosuggest_state';
							        var data_city = 'misc_js.php?task=autosuggest_city';
							        
							        $j("#" + input_country.id).autocomplete(data_country,{selectFirst: true});
							        $j("#" + input_state.id).autocomplete(data_state,
							        		{
							        			selectFirst: true, 
							        			extraParams: {
							        				country: function() {							        					
							        					// alert(input_country.id);
							        					return document.getElementById(input_country.id).value;
							        				}
							        			},
							        			cacheLength: 0
							        		}
							        );
							        $j("#" + input_city.id).autocomplete(data_city,
							        		{
							        			selectFirst: true,
							        			extraParams: {
							        				country: function() {
							        					return document.getElementById(input_country.id).value;
							        				},
							        				state: function() {
							        					return document.getElementById(input_state.id).value;
							        				}
							        			},
							        			cacheLength: 0
							        	
							        		}
							        
							        );
						        }
							}
					    });
						returnFunc();
					}
				}
		)
		makeRequest.send();
}

//GET KeyWord

function getKeyWord(keyword,returnFunc)
{	
	if (typeof returnFunc != "undefined" && typeof returnFunc == "function") {
		returnFunc = returnFunc;
	}
	else {
		returnFunc = function(){};
	}
	var makeRequest = new Request(
				{
					url: "misc_js.php",
					'method':'post',
					'data' : {
						'task' : 'get_key_word',
						'keyword' : keyword	
					  },
					onComplete: function (respone){
						//obj.set('html',respone);
						returnFunc(respone);
					}
				}
		)
		makeRequest.send();
}

//GET NAME

function getName(name,returnFunc)
{	
	if (typeof returnFunc != "undefined" && typeof returnFunc == "function") {
		returnFunc = returnFunc;
	}
	else {
		returnFunc = function(){};
	}
	var makeRequest = new Request(
				{
					url: "misc_js.php",
					'method':'post',
					'data' : {
						'task' : 'get_name',
						'name' : name	
					  },
					onComplete: function (respone){
						//obj.set('html',respone);
						returnFunc(respone);
					}
				}
		)
		makeRequest.send();
}

