var $fields;
var new_call_id;
var last_call_id;
var first_callback = true;

var output_msg = "";

function start_check_for_free_call()
{
    cfc = function()
    {
        $.getJSON("/api/guid/calls/from/0/for/1/desc/?guid=" + guid + "&rnd=" + Math.random(),
        function (data)
        {
            if (data.calls[0] != null)
            {
                new_call_id = data.calls[0].callid;
                if (new_call_id != last_call_id)
                {
                    if (first_callback)
                    {
                        first_callback = false;
                    } else {
                        $("#sharemessages").html("<center><a href=\"/account/call/" + new_call_id + "/?affiliate=prankdial\" target=\"_blank\"><img src=\"/media/img/continue_img.gif\" border=\"0\"></a></center>"); 
                    }
                }
                last_call_id = new_call_id;
            } else {
                first_callback = false;
            }
            setTimeout(cfc, 5000);
        });
    }
    cfc();
}

function load_form() {
  $fields.each(function(){
    var love = $.cookie('last_call_' + $(this).attr('name'));
    if (love)
      $(this).val(love);
  });
}

function save_form() {
  $fields.each(function(){
    $.cookie('last_call_' + $(this).attr('name'), $(this).val());
  });
}

$(function(){
  $fields = $("div.fields :input");
  load_form();
  $("#id_my_phoneno").focus();

  /* only show "email recording" checkbox if recording is enabled */
  $("#id_record").change(function() {
    if ($(this).val() == "no-announce" || $(this).val() == "announce")
      $("#row_email_recording").show();
    else
      $("#row_email_recording").hide();
  }).change();
  
  $("#webcall").submit(function(){
    
	$fields.css("background", "")
           .css("color", "")
           .css("font-weight", "");
		   
			   
    var data = this.elements;
    $.ajax({
      type:     "GET",
      url:      api_url,
      dataType: "json",
      data:     $(this).serialize(),
      success:
        function(json){
			$("#success span.dialinnumber").html("");
		 	centerPopup();
			loadPopup();
		  	document.getElementById('success').style.display = "none";
		  	document.getElementById('error').style.display = "none";		  
          	if (json.is_error) {
            	$("#error p").html(json.message);
				document.getElementById('error').style.display = "";
            	$("#id_" + json.field).css("background", "#CD321D")
                                  .css("color", "white")
                                  .css("font-weight", "bold")
                                  .show().focus();
          	} else {
            	$("#success span.dialinnumber").html(json.access_number_pretty);
				document.getElementById('success').style.display = "";
            	save_form();
          	}
        }
    });
    return false;
  });
});

function clean_phone(pn_field)
{
    matcher = new RegExp(pn_field.value, /\(?([0-9]{3})\)?-?([0-9]{3})-?([0-9]{4})/);
    matched = matcher.exec();
    new_num = matched[0] + "-" + matched[1] + "-" + matched[2];
    pn_field.value = new_num;
}
