$(document).ready(function(){
	$("#send_to_friend_mail, #send_to_friend_sms").colorbox({overlayClose: false, width: 500, onComplete: function() {
			//create captcha after the content has loaded
			createCaptcha();
			TofslaFormsStart();
		},
		onClosed: function() {
			//if there is another recaptcha in the main window, reload it, because its challenge field is already invalid
			if ($('#recaptcha_widget').length > 0) {
				Recaptcha.reload();
			}
		}
	});
});

function createCaptcha() {
	Recaptcha.create($('#recaptcha_public_key').val(), "recaptcha_ajax_widget", {
	    "theme": "custom",
	    "lang": "en",
	    "callback": function() { $.colorbox.resize(); }
	});
}

function sendMailForm() {
	var link = encodeURI(window.location);
	$.post('/public/sendtofriendmail',
		  {sender_name: $('#sender_name').val(), 
		      sender_email: $('#sender_email').val(),
		      friend_email: $('#friend_email').val(),
		      subject: $('#subject').val(),
		      'link': link,
		      'recaptcha_response_field': $('#cboxContent input[name=recaptcha_response_field]').val(),
		      'recaptcha_challenge_field': $('#cboxContent input[name=recaptcha_challenge_field]').val()
		  },
		  function(response) {
              if(response.errorCode == 0) {
                  $('#cboxLoadedContent').html('Съобщението бе изпратено успешно.');
              } else {
                  $('#cboxLoadedContent').html(response.response);
                  createCaptcha();
                  TofslaFormsStart();
              }
          },
          'json');
    return false;
}

function sendSmsForm() {
	var link = encodeURI(window.location);
	$.post('/public/sendtofriendsms',
		  {friend_msisdn: $('#friend_msisdn').val(), 
		      note: $('#note').val(),
		      link: link,
		      'recaptcha_response_field': $('#cboxContent input[name=recaptcha_response_field]').val(),
		      'recaptcha_challenge_field': $('#cboxContent input[name=recaptcha_challenge_field]').val()
		  },
		  function(response) {
              if(response.errorCode == 0) {
                  $('#cboxLoadedContent').html('Съобщението бе изпратено успешно.');
              } else {
                  $('#cboxLoadedContent').html(response.response);
                  createCaptcha();
                  TofslaFormsStart();
              }
          },
          'json');
    return false;
}
