var Wizard = jQuery("#wizard");

var wiz_type_PSTN = jQuery("#sPSTN");
var wiz_type_VOIP = jQuery("#sVOIP");
var wiz_type_SKYPE = jQuery("#sSKYPE");
var wiz_type_GTALK = jQuery("#sGTALK");
var wiz_type_MSN = jQuery("#sMSN");

var wiz_type_VOIP_Protocol = jQuery("#voip_protocol");

var wiz_box_DEFAULT = jQuery("#box_none");
var wiz_box_PSTN = jQuery("#box_pstn");
var wiz_box_VOIP = jQuery("#box_voip");
var wiz_box_SKYPE = jQuery("#box_skype");
var wiz_box_GTALK = jQuery("#box_gtalk");
var wiz_box_MSN = jQuery("#box_msn");

var wiz_var_Type = 'NONE';
var wiz_var_Country = jQuery("#wiz_country"); 
var wiz_var_City = jQuery("#wiz_city");
var wiz_var_Dest = '';
var wiz_var_DestCountry = jQuery("#pstn_dest_country");

var country = '';
var city = '';
var submit = '';
var sql_dest_proto = '';
var sgl_dest_country = '';
var sgl_show_dest = '';
var sgl_dest = '';
var sgl_dest_device = '';
var sgl_dest_prefix = '';
var querystring = '';
var _a = "wizard";
var _do = "step1";
var _m = "core";

var _prev_country = '';
var _prev_city = '';
var wiz_box_CURRENT = wiz_box_DEFAULT;

jQuery(document).ready(function(){

/***********************************************************************/
// EVENTS

	wiz_var_DestCountry.change(function(){
		SaveWizardData();
	});
	
	wiz_var_City.change(function(){
		if( wiz_var_City.val() != _prev_city )
		{
			SaveWizardData();
			_prev_city = wiz_var_City.val();
		}
	});
	
	wiz_var_Country.change(function(){
		if( wiz_var_Country.val() != _prev_country )
		{
			reloadCityList();
			_prev_country = wiz_var_Country.val();
		}
	});
	
	wiz_type_PSTN.click(function(){
		if( wiz_var_Type != 'PSTN' )
		{
			wiz_var_Type = 'PSTN';
			wiz_box_CURRENT.hide();
			wiz_box_CURRENT = wiz_box_PSTN;
			wiz_box_CURRENT.fadeIn();
			SaveWizardData();
		}
	});
	
	wiz_type_VOIP.click(function(){
		if( wiz_var_Type != 'VOIP' )
		{
			wiz_var_Type = 'VOIP';
			wiz_box_CURRENT.hide();
			wiz_box_CURRENT = wiz_box_VOIP;
			wiz_box_CURRENT.fadeIn();
			SaveWizardData();
		}
	});
	
	wiz_type_SKYPE.click(function(){
		if( wiz_var_Type != 'SKYPE' )
		{
			wiz_var_Type = 'SKYPE';
			wiz_box_CURRENT.hide();
			wiz_box_CURRENT = wiz_box_SKYPE;
			wiz_box_CURRENT.fadeIn();
			SaveWizardData();
		}
	});
	
	wiz_type_GTALK.click(function(){
		if( wiz_var_Type != 'GTALK' )
		{
			wiz_var_Type = 'GTALK';
			wiz_box_CURRENT.hide();
			wiz_box_CURRENT = wiz_box_GTALK;
			wiz_box_CURRENT.fadeIn();
			SaveWizardData();
		}
	});
	
	wiz_type_MSN.click(function(){
		if( wiz_var_Type != 'MSN' )
		{
			wiz_var_Type = 'MSN';
			wiz_box_CURRENT.hide();
			wiz_box_CURRENT = wiz_box_MSN;
			wiz_box_CURRENT.fadeIn();
			SaveWizardData();
		}		
	});

	Wizard.submit(function(){
		SaveWizardData();
	
		var params = '<input type="hidden" name="country" value="'+ country +'">';
		params += '<input type="hidden" name="city" value="'+ city +'">';
		params += '<input type="hidden" name="sql_dest_proto" value="'+ sql_dest_proto +'">';
		params += '<input type="hidden" name="sgl_dest" value="'+ sgl_dest +'">';
		params += '<input type="hidden" name="sgl_show_dest" value="'+ sgl_show_dest +'">';
		params += '<input type="hidden" name="sgl_dest_device" value="'+ sgl_dest_device +'">';
		if(wiz_var_Type == 'PSTN')
			params += '<input type="hidden" name="sgl_dest_country" value="'+ sgl_dest_country +'">';
		
		
		Wizard.append( params );
		
	});
	
	jQuery("input[type=text][id*=text]").blur(function(){
		SaveWizardData();
	});
	
/***********************************************************************/
// FUNCTIONS
	
	function reloadCityList()
	{
		wiz_var_City.load(
				"/index.php?_m=core&_a=ajax&sid="+Math.random(),
				{
				'country': wiz_var_Country.val() ,
				'city':  cC
				}
			);
		_prev_country = wiz_var_Country.val();
	}

	function wiz_prepare_data()
	{
		wiz_var_Dest = jQuery("#"+wiz_var_Type.toLowerCase()+"text");
		
		country = wiz_var_Country.val();
		city = wiz_var_City.val();
		sgl_dest_device = wiz_var_Type;
		switch( wiz_var_Type )
		{
			case 'NONE':
				sgl_dest_device = '';
				break;
				
			case 'PSTN':
				sgl_dest_country = wiz_var_DestCountry.val();
				sgl_show_dest = wiz_var_Dest.val();
				sgl_dest = wiz_var_DestCountry.val() + sgl_show_dest;		
				break;
				
			case 'VOIP':
				sql_dest_proto = wiz_type_VOIP_Protocol.val();
			
			default: 
				sgl_dest = wiz_var_Dest.val();
				break;
		}
	}

	function SaveWizardData(){
		wiz_prepare_data();	
		jQuery.post(
			"/index.php?_m=core&_a=ajax",
			{
				'country': country,
				'city': city,
				'sql_dest_proto': sql_dest_proto,
				'sgl_dest': sgl_dest,
				'sgl_dest_country': sgl_dest_country,
				'sgl_dest_prefix': sgl_dest_prefix,
				'sgl_show_dest': sgl_show_dest,
				'sgl_dest_device': sgl_dest_device,
				'sgl_src':	"wizard"				
			});			
	} 

/***********************************************************************/
// MISC
	

	//reloadCityList();
	if( cA != '' )
		jQuery("#s"+cA).click();

});