var featured_countries = [];

/**
* initialization on DOM of page loaded
*/
$(document).ready( function () {
	load_countries();

//	$("#telephone").hide();
//	$("#telephone_label").hide();
//	$("#telephone_prompt_text").hide();

	//$("#country_id").val('0');
	//$("#timezone_id").val('0');
});

/**
* registering functionality for user
*/
function register_back() {
	$.post("ajax/register1.php", {back: "1"},  on_register_back_done, "json");
}

function on_register_back_done(data) {
  location.href = "register.php";
}

/**
* WSA 2009-10-14 
* remove extra phone formating to insure uniform formating and validation
* lets user know imediately how to format numbers
*/
function strip_non_numeric (obj) 
{
	obj.value = obj.value.replace(/[^0-9]+/g, "");
}

/**
* registering functionality for user
*/
function register_user()
{
  $("#register_ai").show();
	$.post("ajax/register1.php", $("#register_form").serialize(),  validate_actions, "json");
}

function register_user2()
{
  $("#register_ai").show();
	$.post("ajax/register2.php", $("#register_form").serialize(),  validate_actions2, "json");
}

/**
* actions after form validation performed
*/
function validate_actions(data)
{
  $("#register_ai").hide();
	if (data.result == 'ok')
	{
		location.href = 'register.php';
	}
	else if ( (data.result == 'errors') && (data.content.length > 0) )
	{
		$.each(data.content, function (key, error) {
		  $.jGrowl(error, {life: 10000});
		});
	}
}

function validate_actions2(data)
{
  $("#register_ai").hide();
	if (data.result == 'ok')
	{
	  if (data.upgradehash == undefined || data.upgradehash == null) {
		  location.href = "user_area.php";
	  }
	  else {
		  $('#userhash').val(data.upgradehash);
		  document.getElementById("pro_account_upgrade_form").submit();
	  }
	  
	}
	else if ( (data.result == 'errors') && (data.content.length > 0) )
	{
		$.each(data.content, function (key, error) {
		  $.jGrowl(error, {life: 10000});
		});
	}
}


/**
* load featured countries
*/
function load_countries()
{
	$.post("ajax/countries_attribs.php", {}, on_countries_loaded, "json");
}

/**
* fill loaded countries codes array
*/
function on_countries_loaded(data)
{
	//alert(data);
	if (data.result == 'ok')
	{
		var countries_arr = data.countries;

		for (i = 0; i < countries_arr.length; i++)
		{
			featured_countries.push(countries_arr[i][0]);
		}
	}
}

/**
* on country change actions
*/
function render_fields()
{
	var country_id = $("#country_id").val();

	if (isNaN(country_id))
	{
		country_id = 0;
	}

	var code_pos = jQuery.inArray(country_id, featured_countries);

	if (code_pos > -1)
	{
		$("#telephone").show();
		$("#telephone_label").show();
		$("#pho_dial_prefix").show();
		$("#telephone_prompt_text").show();
	}
	else
	{
		$("#telephone").val('');
		$("#telephone").hide();
		$("#telephone_label").hide();
		$("#pho_dial_prefix").hide();
		$("#telephone_prompt_text").hide();
	}

	$.post("ajax/country_dialcode.php", {'country_id' : country_id}, on_dialcode_loaded, "json");
}

/**
* show country dial code
*/
function on_dialcode_loaded(data)
{
	if (data.result == 'ok')
	{
		$("#pho_dial_prefix").text(data.dialing_code);
		$("#mob_dial_prefix").text(data.dialing_code);
	}
	else
	{
		$("#pho_dial_prefix").text('');
		$("#mob_dial_prefix").text('');
	}
}

function edit_reminder(id)
{
	alert("Test")
}