var activation_code_sent = false;

$(function() {

	$('.show_verify_cell').click(show_verify_cell);
	$('.verify_cell').click(verify_cell);
	$('.resend_activation_code').click(resend_activation_code);
	
});

function show_verify_cell() 
{
	$('#verify').html('');
	$('#verify_cell, #phone_number_ex, #resend').toggle();
	
	if(!activation_code_sent) 
	{
		$.ajax({
			type: 'post',
			url: base_url + 'home/send_verification_code',
			data: {
				ajax: true,
				provider_id: $('#provicer_id').val(),
				mobile_number: $('#mobile_number').val()
			},
			success: function(response) {
				activation_code_sent = true;
			}
		});
	}
}

function verify_cell() 
{
	$.ajax({
		type: 'post',
		url: base_url + 'home/verify_mobile',
		data: {
			ajax: true,
			verification_code: $('#activation_code').val()
		},
		success: function(response) {
			if(response == "1") {
				$('#verify').html('Verified');
				$('#verify').css('color', 'green');
				$('#verify_cell, #phone_number_ex, #resend').toggle();
			} else {
				$('#activation_code').val('');
				// TODO $('#activation_code').effect('shake');
			}
		}
	});
}

function resend_activation_code() 
{
	var el = this;
	
	$.ajax({
		type: 'post',
		url: base_url + 'home/send_verification_code/1',
		data: {
			ajax: true,
			provider_id: $('#provicer_id').val(),
			mobile_number: $('#mobile_number').val()
		},
		success: function(response) {
			$(el).html('Activation code resent');
			$(el).removeClass('resend_activation_code link');
		}
	});
}
