// JavaScript Document
var split_word = '|||';
function call_ajax_region_list(url)
	{
		var obj = document.getElementById('cityList');
		obj.style.display = 'block';

		 var obj = document.getElementById('ajax_region_id');
		 obj.innerHTML = '';


		country = document.form_create_user.country;
		country_code = country.options[country.selectedIndex].value;
		url = siteURL + url + '?country_code=' + country_code;
		new AG_ajax(url,'call_ajax_region_show');
		return false;
	}

function call_ajax_region_show(data)
	{
        data = unescape(data);
		var obj = document.getElementById('ajax_region_id');
        if( data.substr(data.indexOf('option'))!=0)
            {
        		if(obj!=null)
        			{
        				obj.innerHTML = data;
        			}
        	}
	}
function call_ajax_city_list(url)
	{
		var obj = document.getElementById('ajax_city_id');
		obj.innerHTML = '';
		country = document.form_create_user.country;
		country_code = country.options[country.selectedIndex].value;
		region = document.form_create_user.region_list;
		region_code = region.options[region.selectedIndex].value;
		url = siteURL + url + '?region_code=' + region_code +'&country_code=' + country_code;
		new AG_ajax(url,'call_ajax_city_show');
		return false;
	}
function call_ajax_city_show(data)
	{
		data = unescape(data);
		var obj = document.getElementById('ajax_city_id');
		if(obj!=null)
			{
				obj.innerHTML = data;
			}
	}

function call_ajax_country_content(url, citiescnturl, val)
	{
//		alert(citytotalcnt);
		citiesObj = document.getElementById('selCities');
		citiesObj.style.display = 'none';
		clearlist('location');
		stateObj = document.getElementById('selstate');
		stateObj.style.display = 'none';
		clearlist('state');
		if (val == 'US')
			{
				url = siteURL + url;
				//alert('wait');
				new AG_ajax(url,'call_ajax_profile_state_show');
			}
		else
			{
				call_ajax_profile_cities_url(citiescnturl, 'nostate');
			}
		return false;
	}

function call_ajax_profile_state_show(data)
	{
		data = unescape(data);
		data = data.split(split_word);
		if (data[0].indexOf('success') >=1)
			{
				var content = data[1];
				var states = content.split(',');
				var stateCnt = states.length;
				clearlist('state');
				stateObj = document.getElementById('selstate');
				stateObj.style.display = 'block';
				citiesObj = document.getElementById('selCities');
				citiesObj.style.display = 'none';
				source = document.getElementById('state');
				source.options[source.options.length] = new Option( choose_word, '');
				stateCnt = stateCnt -1;
				for(i=0; i<stateCnt;i++)
					{
						var itemVal = states[i].split(':');
						source.options[source.options.length] = new Option(itemVal[1],itemVal[0]);
					}
			}
		else
			{
				location.replace(replace_url);return;
			}
	}
function call_ajax_profile_cities_url(url, val)
	{
		frmobj = document.forms['form_profile'];
		countryValue  =	frmobj.getElementById('country').value ;alert(countryValue);
		querystring = '';
		url = siteURL + url +'?state='+val+'&country='+countryValue;
		querystring = '?state='+val+'&country='+countryValue;
	//	alert('please wait');
		loadingObj = document.getElementById('selLoading');
		loadingObj.style.display = 'block';

		new AG_ajax(url,'call_ajax_profile_cities_show');
		return false;
	}

function call_ajax_profile_cities_show(data)
	{
		data = unescape(data);
		data = data.split(split_word);
		if (data[0].indexOf('success') >=1)
			{
				if(data[1].indexOf(',') >=1)
					{
						var content = data[1];
						var citycnt = content.split(',');
						citytotalcnt = citycnt[0];
						clearlist('location');
						citycurrentcnt = 0;
						make_page_list_ajax(0); //starting as Zero
					}
			}
		else
			{
				location.replace(replace_url);return;
			}
	}
function make_page_list_ajax(startcnt)
	{
		url = siteURL + cityURL + querystring+'&start='+startcnt;
//		alert('start count'+startcnt);
//		alert(url);
		new AG_ajax(url,'call_ajax_profile_cities_page_list');
	}
function call_ajax_profile_cities_page_list(data)
	{
		data = unescape(data);
		data = data.split(split_word);
//		alert(data);
//		alert(querystring);
		if (data[0].indexOf('success') >=1)
			{
				if(data[1].indexOf(',') >=1)
					{
						var content = data[1];
						var cities = content.split(',');
						var citiesCnt = cities.length;
						source = document.getElementById('location');
						citiesCnt = citiesCnt -1;
						for(i=0; i<citiesCnt;i++)
							{
								var itemVal = cities[i].split(':');
								source.options[source.options.length] = new Option(itemVal[1],itemVal[0]);
							}
					}
				citycurrentcnt = citycurrentcnt+ 100; //legth to retrieve per processing
				if (citycurrentcnt < citytotalcnt)
					{
						make_page_list_ajax(citycurrentcnt);
					}
				else
					{
						stateObj = document.getElementById('selCities');
						stateObj.style.display = 'block';
						loadingObj = document.getElementById('selLoading');
						loadingObj.style.display = 'none';
						//alert('finish');
					}
			}
		else
			{
				location.replace(replace_url);return;
			}
	}

function clearlist(sourceID)
	{
		source = document.getElementById(sourceID);
		maxItems = source.options.length;
		for (i=0;i<maxItems;i++)
			{
				 source.options[i].text='';
				 source.options[i].value="";
				 source.options[i] = null;
			}
	}
