J(document).ready(
	function ()
	{
		if(J('#outside').attr('checked')) {
			J('#state-cell').hide();
			J('#zip-cell').hide();
		}
		
		J('#captcha').defaultvalue('Please enter text you see on left');
		
		bindProfileEvents();
	}
);

//EVENTS BINDING
function bindProfileEvents()
{
	J('#teamtalk').click(
		function () {
			if(J(this).attr('checked'))
				J('#username-cell').show();
			else
				J('#username-cell').hide();
		}
	);
	
	//hide states and zip input if outside usa selected
	J('#outside').click(
		function () {
			J('.countries').toggle().filter(':visible :selected').eq(0).attr('selected', 1);
			
			J('#state-cell').toggle();
			J('#zip-cell').toggle();
		}
	);
	
	//send profile form
	J('#profile-form').ajaxForm({
		success:  processResponse,
		beforeSubmit: function () {
			var date = new Date;
			
			J('#client-time').val(parseInt(date.getTime() / 1000));
			J('#wait-msg').show(); 
		},
		dataType: 'json'
	});
}

//IMPLODE PHONE FIELDS VALUES
function implodePhones()
{	
	for(phone in phones)
	{
		var numbers = new Array(J(phone+'1').attr('value'), J(phone+'2').attr('value'), J(phone+'3').attr('value'));
		
		J(phone).attr('value', numbers.join('-'));
	}
	
	return 1;
}

//PROCESS RESPONSE ADD DISPLAY ERRORS
function processResponse(response)
{
	J('#wait-msg').hide();
	if(response.success)
	{
		J('#form_your_profile').hide();
		J('#page-title').hide();
		
		J('#success').show();
		
		return true;
	}
	else
	{
		setErrors(response);
		return false;
	}
	
	return true;
}

