﻿var popupStatus = 0;

//http://www.west-wind.com/weblog/posts/472329.aspx
jQuery.fn.serializeNoViewState = function() {
  return this.find("input,textarea,select,hidden")
               .not("[type=hidden][name^=__]")
               .serialize();
}

function RequestMoreInfo() {
  jQuery(this).hide();
  jQuery("#RequestMoreInfoImageDown").show();
  setTimeout(function() {
    jQuery("#RequestMoreInfoImageDown").hide();
    jQuery("#RequestMoreInfoImage").show();
  }, 300);

  centerPopup("#ContactPopup");
  loadPopup();
  return false; // cancel nav
}

jQuery(document).ready(function() {

  //jQuery("#DSIWebForm").validate();

  jQuery("#RequestMoreInfoLink").click(RequestMoreInfo);
  jQuery("#RequestMoreInfoImage").click(RequestMoreInfo);


  jQuery("#CancelRequest").click(function() { disablePopup(); });
  jQuery("#CloseButton").click(function() { disablePopup(); });


  //  jQuery("#RequestMoreInfoImage").mouseover(function() {
  //    jQuery(this).attr("src", "/images/request_dn.gif");

  //  });

  //  jQuery("#RequestMoreInfoImage").mouseout(function() {
  //    jQuery(this).attr("src", "/images/request.gif");

  //  });

  jQuery("#backgroundPopup").click(function() {
    disablePopup();
  });

  jQuery(document).keypress(function(e) {
    if (e.keyCode == 27 && popupStatus == 1) {
      disablePopup();
    }
  });

  jQuery("#SendRequest").click(function() {

    if (!jQuery("#DSIWebForm").validate().form())
      return false;


    var FirstName = new String(jQuery("#FirstName").val());
    var LastName = new String(jQuery("#LastName").val());
    var Title = new String(jQuery("#Title").val());
    var Company = new String(jQuery("#Company").val());
    var Address = new String(jQuery("#Address").val());
    var Address2 = new String(jQuery("#Address2").val());
    var City = new String(jQuery("#City").val());
    var State = new String(jQuery("#State").val());
    var Zip = new String(jQuery("#Zip").val());
    var Email = new String(jQuery("#Email").val());
    var Phone = new String(jQuery("#Phone").val());
    var PhoneAlt = new String(jQuery("#PhoneAlt").val());
    var AdditionalInformation = new String(jQuery("#AdditionalInformation").val());
    var SendOptionals = "";

    if (jQuery("#SendDSIInfo:checked").val())
      SendOptionals += jQuery("#SendDSIInfo").attr("title") + "<br />";

    if (jQuery("#ContactMe:checked").val())
      SendOptionals += jQuery("#ContactMe").attr("title") + "<br />";

    //Email, string Phone, string Subject, string AdditionalInformation

    //var post = jQuery().serializeNoViewState();

    //$.post("/contact.aspx/SendMessage", post, postResult, "json");
    jQuery.ajax({
      type: "POST",
      url: "/contact.aspx/SendMessage",
      data: '{ "FirstName" : "' + escape(FirstName) + '", ' +
								'"LastName" : "' + escape(LastName) + '", ' +
								'"Title" : "' + escape(Title) + '", ' +
								'"Company" : "' + escape(Company) + '", ' +
								'"Address" : "' + escape(Address) + '", ' +
								'"Address2" : "' + escape(Address2) + '", ' +
								'"City" : "' + escape(City) + '", ' +
								'"State" : "' + escape(State) + '", ' +
								'"Zip" : "' + escape(Zip) + '", ' +
								'"Email" : "' + escape(Email) + '", ' +
								'"Phone" : "' + escape(Phone) + '", ' +
								'"PhoneAlt" : "' + escape(PhoneAlt) + '", ' +
								'"AdditionalInformation" : "' + escape(AdditionalInformation) + '", ' +
								'"SendOptionals" : "' + escape(SendOptionals) + '"}',
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(msg) {
        swapPopup(msg.d);
      },
      error: function(msg) {
        swapPopup(msg.d);
      }
    });

  });

});

function postResult() {
  swapPopup();
}

function loadPopup() {
  if (popupStatus == 0) {
    jQuery("#backgroundPopup").css({
      "opacity": "0.7"
    });
    jQuery("#backgroundPopup").fadeIn("slow");
    jQuery("#ContactPopup").fadeIn("slow");
    jQuery("#ContactResult").show();
    jQuery("#ContactResultError").hide();
    popupStatus = 1;
  }
}

function swapPopup(msg) {
  if (popupStatus == 1) {
    jQuery("#ContactPopup").fadeOut("slow");
    centerPopup("#ContactConfirm");
    if (msg.length > 0) {
      jQuery("#ContactResult").hide();
      jQuery("#ContactResultError").show();
      jQuery("#ContactResultError").html(msg);
    }
    jQuery("#ContactConfirm").fadeIn("slow");
  }
}


function disablePopup() {
  if (popupStatus == 1) {
    jQuery("#backgroundPopup").fadeOut("slow");
    jQuery("#ContactPopup").fadeOut("slow");
    jQuery("#ContactConfirm").fadeOut("slow");
    popupStatus = 0;
  }
}

//centering popup
function centerPopup(id) {
  //request data for centering
  var windowWidth = document.documentElement.clientWidth;
  var windowHeight = document.documentElement.clientHeight;
  var popupHeight = jQuery(id).height();
  var popupWidth = jQuery(id).width();
  //centering

  var _top = windowHeight / 2 - popupHeight / 2;
  var _left = windowWidth / 2 - popupWidth / 2;

  if (_top < 0) _top = 0;
  if (_left < 0) _left = 0;

  jQuery(id).css({
    "position": "absolute",
    "top": _top,
    "left": _left
  });
  //only need force for IE6

  jQuery("#backgroundPopup").css({
    "height": windowHeight
  });

}
		
