// JavaScript Document

// set both state and city
function setStateCity(theForm,theCboCountryId,theCboStateId,theCboCityId)
{
	$.ajax
	({
		type: "POST",
		url: "ajaxStateCity.php",
		data: 'mode=State&countryid='+theCboCountryId,
		success: function(argState)
		{
			
			var main = new Array();
			main = argState.split(",");
			
			var stateId = new Array();
			stateId = main[1].split("|");
			
			var state = new Array();
			state = main[0].split("|");
			
			if(state != '')
			{
				theForm.cboCity.options.length = 1;
				for(i=1; i<=state.length; i++)
				{
					if(state[i-1] == '' ||  ( typeof state[i-1] == "undefined" ))
						continue;
					theForm.cboState.options[i]=new Option(i);
					theForm.cboState.options[i].text=state[i-1];
					theForm.cboState.options[i].value=stateId[i-1];
					if(stateId[i-1]==theCboStateId)
						theForm.cboState.options[i].selected = true;
				}
			}
			else
			{
				theForm.cboState.options.length = 1;
				theForm.cboCity.options.length = 1;
				theForm.cboCity.options.selectedIndex=0;					
				theForm.cboState.options.selectedIndex=0;	
			}
		}
	});
	
	$.ajax
		({
			type: "POST",
			url: "ajaxStateCity.php",		
			data: 'mode=City&StateId='+theCboStateId,
			success: function(argCity)
			{
				
				var main = new Array();
				main = argCity.split(",");
				
				var cityId = new Array();
				cityId = main[1].split("|");
				
				var city = new Array();
				city = main[0].split("|");
				
				//alert(theCboState.form.cboCity);
				//alert(city);
				
				for(i=0; i<city.length; i++)	
				{
					if(city[i] == '' ||  ( typeof city[i] == "undefined" ))
						continue;
					theForm.cboCity.options[i+1]=new Option(i);
					theForm.cboCity.options[i+1].text=city[i];
					theForm.cboCity.options[i+1].value=cityId[i]
					if(cityId[i] == theCboCityId)
						theForm.cboCity.options[i+1].selected = true;
					/*var anOption = document.createElement("OPTION");
					theCboState.form.cboState.options.add(anOption);
					anOption.innerText = city[i];
					anOption.Value = cityId[i];*/
				}
				if(city.length == 1 && ( typeof city[i] == "undefined" ))
				{	
					theForm.cboCity.options.length = 1;	
					theForm.cboCity.options.selectedIndex=0;
				}
			}
		});
}



function setState(theCboCountry,theCboState,theCboCity)
{
	
	if(theCboCountry.value == '')
	{
		theCboState.options.length = 1;
		theCboCity.options.length = 1;	
		theCboCity.options.selectedIndex=0;
		theCboState.options.selectedIndex=0;
	}
	else
	{
		$.ajax
		({
			type: "POST",
			url: "ajaxStateCity.php",
			data: 'mode=State&countryid='+theCboCountry.value,
			success: function(argState)
			{
				var main = new Array();
				main = argState.split(",");
				
				var stateId = new Array();
				stateId = main[1].split("|");
				
				var state = new Array();
				state = main[0].split("|");
				
				if(state != '')
				{
					for(i=1; i<=state.length; i++)
					{
						if(state[i-1] == '' ||  ( typeof state[i-1] == "undefined" ))
							continue;
						theCboState.options[i]=new Option(i);
						theCboState.options[i].text=state[i-1];
						theCboState.options[i].value=stateId[i-1];
						
					}
				}
				else
				{
					theCboState.options.length = 1;
					theCboCity.options.length = 1;
					theCboCity.options.selectedIndex=0;					
					theCboState.options.selectedIndex=0;	
				}
			}
		});
	}
}

function setCity(theCboState,theCboCity)
{	
	if(theCboState.value =='')
	{
		theCboCity.options.length = 1;	
		theCboCity.options.selectedIndex=0;
	}
	else
	{
		$.ajax
		({
			type: "POST",
			url: "ajaxStateCity.php",		
			data: 'mode=City&StateId='+theCboState.value,
			beforeSend: function()
			{
				$('#city,#cboCity',theCboState.form).before('<span style="color:#FF0000" id="spanSearchLoading">Loading...</span>');
				$('#city,#cboCity',theCboState.form).hide();
			},
			success: function(argCity)
			{
				
				var main = new Array();
				main = argCity.split(",");
				
				var cityId = new Array();
				cityId = main[1].split("|");
				
				var city = new Array();
				city = main[0].split("|");
				
				//alert(theCboState.form.cboCity);
				theCboCity.options.length = 1;
					
				for(i=0; i<city.length; i++)	
				{
					
					if(city[i] == '' ||  ( typeof city[i] == "undefined" ))
						continue;
					theCboCity.options[i+1]=new Option(i);
					theCboCity.options[i+1].text=city[i];
					theCboCity.options[i+1].value=cityId[i]
				
					/*var anOption = document.createElement("OPTION");
					theCboState.form.cboState.options.add(anOption);
					anOption.innerText = city[i];
					anOption.Value = cityId[i];*/
				}
				if(city.length == 0 && ( typeof city[i] == "undefined" ))
				{	
					theCboCity.options.length = 1;	
					theCboCity.options.selectedIndex=0;
				}
				$('span[id=spanSearchLoading]').hide();
				$('select[id=city],select[id=cboCity]').show();
			}
		});
	}
}
function setStateAdmin(theCboCountry,theCboState,theCboCity)
{
	
	if(theCboCountry.value == '')
	{
		theCboState.options.length = 1;
		theCboCity.options.length = 1;	
		theCboCity.options.selectedIndex=0;
		theCboState.options.selectedIndex=0;
	}
	else
	{
		$.ajax
		({
			type: "POST",
			url: "../ajaxStateCity.php",
			data: 'mode=State&countryid='+theCboCountry.value,
			success: function(argState)
			{
				var main = new Array();
				main = argState.split(",");
				
				var stateId = new Array();
				stateId = main[1].split("|");
				
				var state = new Array();
				state = main[0].split("|");
				
				if(state != '')
				{
					for(i=1; i<=state.length; i++)
					{
						if(state[i-1] == '' ||  ( typeof state[i-1] == "undefined" ))
							continue;
						theCboState.options[i]=new Option(i);
						theCboState.options[i].text=state[i-1];
						theCboState.options[i].value=stateId[i-1];
						
					}
				}
				else
				{
					theCboState.options.length = 1;
					theCboCity.options.length = 1;
					theCboCity.options.selectedIndex=0;					
					theCboState.options.selectedIndex=0;	
				}
			}
		});
	}
}

function setCityAdmin(theCboState,theCboCity)
{	
	if(theCboState.value =='')
	{
		theCboCity.options.length = 1;	
		theCboCity.options.selectedIndex=0;
	}
	else
	{
		$.ajax
		({
			type: "POST",
			url: "../ajaxStateCity.php",		
			data: 'mode=City&StateId='+theCboState.value,
			success: function(argCity)
			{
				
				var main = new Array();
				main = argCity.split(",");
				
				var cityId = new Array();
				cityId = main[1].split("|");
				
				var city = new Array();
				city = main[0].split("|");
				
				//alert(theCboState.form.cboCity);
				//alert(city);
				for(i=0; i<city.length; i++)	
				{
					if(city[i] == '' ||  ( typeof city[i] == "undefined" ))
						continue;
					theCboCity.options[i+1]=new Option(i);
					theCboCity.options[i+1].text=city[i];
					theCboCity.options[i+1].value=cityId[i]
				
					/*var anOption = document.createElement("OPTION");
					theCboState.form.cboState.options.add(anOption);
					anOption.innerText = city[i];
					anOption.Value = cityId[i];*/
				}
				if(city.length == 1 && ( typeof city[i] == "undefined" ))
				{	
					theCboCity.options.length = 1;	
					theCboCity.options.selectedIndex=0;
				}
			}
		});
	}
}




function setStateNew(theCboState,countryId,selectedValue)
{
	$.ajax
	({
		type: "POST",
		url: "ajaxStateCity.php",
		data: 'mode=State&countryid='+countryId,
		success: function(argState)
		{
			
			var main = new Array();
			main = argState.split(",");
			
			var stateId = new Array();
			stateId = main[1].split("|");
			
			var state = new Array();
			state = main[0].split("|");
			
				
			for(i=1; i<=state.length; i++)
			{
				
				if(state[i-1] == '' ||  ( typeof state[i-1] == "undefined" ))
					continue;
				theCboState.options[i]=new Option(i);
				theCboState.options[i].text=state[i-1];
				theCboState.options[i].value=stateId[i-1];	
				if(selectedValue == stateId[i])
						theCboState.selectedIndex =i-1;
			}	alert(selectedValue);
		}
	});
	
}

function setCityNew(theCboState,theCboCity,selectedValue)
{	
	if(theCboState.value =='')
	{
		theCboCity.options.length = 1;	
		theCboCity.options.selectedIndex=0;
	}
	else
	{
		$.ajax
		({
			type: "POST",
			url: "ajaxStateCity.php",		
			data: 'mode=City&StateId='+theCboState.value,
			success: function(argCity)
			{
				
				var main = new Array();
				main = argCity.split(",");
				
				var cityId = new Array();
				cityId = main[1].split("|");
				
				var city = new Array();
				city = main[0].split("|");
				
				//alert(theCboState.form.cboCity);
				//alert(city);
				for(i=0; i<city.length; i++)	
				{
					if(city[i] == '' ||  ( typeof city[i] == "undefined" ))
						continue;
					theCboCity.options[i+1]=new Option(i);
					theCboCity.options[i+1].text=city[i];
					theCboCity.options[i+1].value=cityId[i]
					if(selectedValue == cityId[i])
						theCboCity.SelectedIndex =i;
					/*var anOption = document.createElement("OPTION");
					theCboState.form.cboState.options.add(anOption);
					anOption.innerText = city[i];
					anOption.Value = cityId[i];*/
				}
				if(city.length == 1 && ( typeof city[i] == "undefined" ))
				{	
					theCboCity.options.length = 1;	
					theCboCity.options.selectedIndex=0;
				}
			}
		});
	}
}



function setStateArray(theCboCountryArray,theCboCountry)
{
	newCboState = theCboCountry.form.cboState;
	newCboCity = theCboCountry.form.cboCity;
	
	if(isArray(theCboCountryArray[0]))
	{
		for(i=0;i<theCboCountryArray.length;i++)
		{
			if(theCboCountryArray[i] == theCboCountry)
			{
				newCboState = theCboCountry.form.cboState[i];
				newCboCity = theCboCountry.form.cboCity[i];
			}
		}
	}
		
	if(theCboCountry.value == '')
	{
		newCboState.options.length = 1;
		newCboCity.options.length = 1;	
		newCboState.options.selectedIndex=0;
		newCboCity.options.selectedIndex=0;
	
	}
	else
	{
		$.ajax
		({
			type: "POST",
			url: "ajaxStateCity.php",
			data: 'mode=State&countryid='+theCboCountry.value,
			success: function(argState)
			{
				
				var main = new Array();
				main = argState.split(",");
				
				var stateId = new Array();
				stateId = main[1].split("|");
				
				var state = new Array();
				state = main[0].split("|");
				
							
				for(i=1; i<=state.length; i++)
				{
					//alert(state[i] == undefined);
					if(state[i-1] == '' ||  ( typeof state[i-1] == "undefined" ))
						continue;
					newCboState.options[i]=new Option(i);
					newCboState.options[i].text=state[i-1];
					newCboState.options[i].value=stateId[i-1];
					
					
				}
			}
		});
	}
}


function setCityArray(theCboStateArray,theCboState)
{	
	newCboCity = theCboState.form.cboCity;
	if(isArray(theCboStateArray[0]))
	{
		for(i=0;i<theCboStateArray.length;i++)
		{
			if(theCboStateArray[i] == theCboState)
				newCboCity = theCboState.form.cboCity[i];
		}
	}

	if(theCboState.value =='')
	{
		newCboCity.options.length = 1;	
		newCboCity.options.selectedIndex=0;
	}
	else
	{
		$.ajax
		({
			type: "POST",
			url: "ajaxStateCity.php",
			data: 'mode=City&StateId='+theCboState.value,
			success: function(argCity)
			{
				
				var main = new Array();
				main = argCity.split(",");
				
				var cityId = new Array();
				cityId = main[1].split("|");
				
				var city = new Array();
				city = main[0].split("|");
				
				//alert(theCboState.form.cboCity);
				//alert(city);
				for(i=0; i<city.length; i++)	
				{
					if(city[i] == '' ||  ( typeof city[i] == "undefined" ))
						continue;
					newCboCity.options[i+1]=new Option(i);
					newCboCity.options[i+1].text=city[i];
					newCboCity.options[i+1].value=cityId[i]
					/*var anOption = document.createElement("OPTION");
					theCboState.form.cboState.options.add(anOption);
					anOption.innerText = city[i];
					anOption.Value = cityId[i];*/
				}
				if(city.length == 1 && ( typeof city[i] == "undefined" ))
				{	
					newCboCity.options.length = 1;	
					newCboCity.options.selectedIndex=0;
				}
			}
		});
	}
}