;(function($) {

  $.includeScript('/javascripts/jquery.custom.effects.js');
  $.includeScript('/javascripts/jquery.color.js');

  $.fn.sipDomainValidator = function(method) {
    var runner = {
      insertForm: function($el) {
        var form;
        form = "<div>" +
          "<h4>Is your domain ready?</h4>" +
          "Check to see if your domain is ready for SIP Hosting:" +
          "<form  style='width: 100%; text-align: center; margin: 1ex;' onsubmit=\"return false;\">" +
          "<input type='text' name='domain' size='30' style='font-size: 1.1em; padding: 0.4ex; border: 2px solid #004a8f; margin-right: 1ex;'></input>" +
          "<input id='check-domain-submit' type='submit' value='Check Domain' " +
          "style='font-size: 1.1em; padding: 0.4ex; background-color: #004a8f; color: white; border: 2px solid #6AA0C4;' " +
          "onclick='$(\"#domain-check-container form\").sipDomainValidator(\"checkDomain\");'></input>" +
          "</form>" +
          "<div id=\"sip-hosting-result\"></div>" +
          "</div>";
//Enter a domain above and click "Check Domain"

        $el.append(form);
      },

      checkDomain: function(domain) {
        var value, msg, bgColor, borderColor, errors, errorMsg,
          loadMsg = "<span>Please wait while we look up your domain...</span>";
        
        $('#domain-check-container').bind('ajaxSend',
                                          function() {
                                            $('#sip-hosting-result').css({
                                              textAlign: 'center',
                                              borderColor: 'transparent',
                                              backgroundColor: 'transparent',
                                              padding: 0
                                            });
                                            $('#sip-hosting-result').html(loadMsg);
                                            $('#sip-hosting-result span').repeat(50,
                                                                                 function() {
                                                                                   $(this).animate({
                                                                                     backgroundColor: '#ffc'
                                                                                   }, 'slow');
                                                                                   $(this).animate({
                                                                                     backgroundColor: '#fff'
                                                                                   }, 'slow');
                                                                                 });
                                          });

        $.getJSON("/check_domain.php",
                  {Domain: domain},
                  function(json) {
                    value = json.Response.Context.Action.IsCompleted;

                    if(value == 'true') {
                      borderColor = "#9f9";
                      bgColor = "#cfc";
                      msg = "Congratulations! Your domain '" + domain + "' is properly configured for Junction Networks SIP domain hosting."
                    }else{
                      borderColor = "#600";
                      bgColor = "#f99";
                      msg = "We're sorry, but your domain is not properly configured at this time."
                      errors = json.Response.Context.Action.Errors || json.Response.Context.Request.Errors
                      errorMsg = "<ul style='text-align: left;'>";

                      $.each(errors, function(k, v) {
                        errorMsg += "<li>" + v.Code + " - " + v.Message + "</li>";
                      });
                      errorMsg += "</ul>";
                      
                      msg += errorMsg;
                    }
                    $('#sip-hosting-result')
                    .html("<div>" + msg + "</div>")
                    .css({
                      borderColor: borderColor, 
                      borderSize: "2px",
                      borderStyle: 'solid',
                      marginTop: "1em",
                      paddingTop: "1ex",
                      paddingBottom: "1ex",
                      textAlign: 'center'
                    })
                    .animate({
                      backgroundColor: bgColor
                    }, 'slow');
                  });
      }
    }

    if(String(method) == 'insertForm') {
      return runner.insertForm($(this));
    }else if(String(method) == 'checkDomain') {
      return runner.checkDomain($(this).find("input[name='domain']").attr('value'));
    }
    
  }

})(jQuery);
