var modalWindow = {
	parent:"body",
	windowId:null,
	content:null,
	width:null,
	height:null,
	close:function()
	{
		$(".modal-window").remove();
		$(".modal-overlay").remove();
	},
	open:function()
	{
    var obj = this;

    $.get(this.source, function(data) {
      var modal = "";
      modal += "<div class=\"modal-overlay\"></div>";
		  modal += "<div id=\"" + obj.windowId + "\" class=\"modal-window\">";
      modal += data;
      modal += "</div>";
      $(obj.parent).append(modal);

      $(".cancel-button").click(function(){ modalWindow.close(); });
      $(".modal-overlay").click(function(){ modalWindow.close(); });

      var validator = $('form').validate({
        rules: {
          name: {
            required: true,
            minlength: 4
          },
          title: {
            required: true,
            minlength: 2
          },
          organization: {
            required: true,
            minlength: 5
          },
          email: {
            required: true,
            email: true
          },
          phone: {
            phoneUS: true
          },
          contact: {
            required: true
          },
          referral: {
            required: true
          }
        },
        messages: {
          name: "Enter your full name",
          title: "Enter your title",
          organization: {
            required: "Enter your organization name",
            minlength: "Doesn't meet min requirements"
          },
          email: {
            required: "Enter your e-mail address",
            email: "Enter a valid e-mail address"
          },
          phone: "Enter a valid phone number",
          contact: "Required",
          referral: "Required"
        },
        errorPlacement: function(error, element) {
    			if ( element.is(":radio") )
    				error.appendTo( element.parent().parent() );
    			else if ( element.is(":checkbox") )
    				error.appendTo ( element.next() );
    			else
    				error.appendTo( element.parent().next() );
    		},
        success: function(label) {
    			// set &nbsp; as text for IE
    			label.html("&nbsp;").addClass("checked");
    		},
        submitHandler: function() {

          $.post(obj.postURL, $('form').serialize(), function() {
            changePage(obj.followupURL);
            modalWindow.close();
          });
        }
      });
    });
	}
};

jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
    phone_number = phone_number.replace(/\s+/g, "");
	return this.optional(element) || phone_number.length > 9 &&
		phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
}, "Please specify a valid phone number");

var openMyModal = function(source, postURL, followupURL, id)
{
	modalWindow.windowId = id;
	modalWindow.width = 700;
	modalWindow.height = 540;
  modalWindow.source = source;
  modalWindow.postURL = postURL;
  modalWindow.followupURL = followupURL;
	modalWindow.open();
};

$(document).ready(function() {
  $('.schedule-demo').click(function() {
    openMyModal('/form-schedule-demo/', '/submit-form.php?id=schedule-demo', 'schedule-demo-info', 'schedule-demo-form');
    return false;
  });

  $('.toolkit').click(function() {
    openMyModal('/form-toolkit/', '/submit-form.php?id=toolkit', 'toolkit-info', 'toolkit-form');
    return false;
  });


});

