var bgContainer, popupContainer;

function popupForm(id, images_dir, popup_text)
{
	var firstChild, ch, cw;

	cw= window.innerWidth || document.documentElement.clientWidth;
	ch= (window.innerHeight || document.documentElement.clientHeight) + 200;

        scroll(0,0);
	if (!bgContainer)
	{
		bgContainer=document.createElement("div");
		bgContainer.style.position="absolute";
		bgContainer.style.top="0px";
		bgContainer.style.left="0px";
		//bgContainer.style.border="1px red solid";
		bgContainer.style.width=cw+"px";
		bgContainer.style.height=ch+"px";
		bgContainer.style.backgroundImage="url('" + images_dir + "/page_disabled.png')"
		document.body.insertBefore(bgContainer, document.body.firstChild);
		bgContainer.style.zIndex=1000;
	}
	bgContainer.style.display="block";
	popupContainer=document.getElementById(id);

        jQuery(popupContainer).remove();
        var first_child = document.body.firstChild;
        var doc_body = document.body;
	doc_body.insertBefore(popupContainer, first_child);          

        if (id == "email_form") {
          addEmailSubmitListener();
        }
        else if (id == "subscribe_form") {
          addSubscribeSubmitListener();
        }
        else if (id == "complimentary_form") {
          addComplimentarySubmitListener();
        }
        else if (id == "tell_us_form") {
          if (popup_text) {
            jQuery('#tell_us_intro').text(popup_text);
          }
          addTellUsSubmitListener();
        }

	popupContainer.style.left=parseInt((cw-600)/2)+"px";
	popupContainer.style.top="100px"
	popupContainer.style.display="block";
	popupContainer.style.zIndex=1001;
}

function closePopupForm(previous_url)
{
	popupContainer.style.display="none";
	bgContainer.style.display="none";
        if (previous_url) {
          window.location = previous_url;
        }
}

function addSubscribeSubmitListener() {
      jQuery(document).ready(function() {
        var post_url = jQuery('#subscribe_form_element').attr("action");
        jQuery('#subscribe_form_element').submit(function() {
          jQuery.post(post_url,
            {name: jQuery('#subscribe_name_field').attr("value"),
             email_address: jQuery('#subscribe_email_field').attr("value"),             
             cmd_subscribe_x: "cmd_subscribe_x"},
            function(data) {
              var result = eval('(' + data + ')');
              if (result.success) {
                closePopupForm();
                jQuery("#confirmation_msg").text(result.msg);
                jQuery("#confirmation_title").text(result.msgTitle);
                popupForm('confirmation_form');
              }
              else {
                jQuery("#subscribe_errors").empty();
                jQuery("#subscribe_errors").append(result.msg);
                jQuery("#subscribe_errors").css("display", "inline");
              }
            }
          );
          return false;
        });
      });
}

function addComplimentarySubmitListener() {
      jQuery(document).ready(function() {
        var post_url = jQuery('#complimentary_form_element').attr("action");
        jQuery('#complimentary_form_element').submit(function() {
          jQuery.post(post_url,
            {name: jQuery('#complimentary_name_field').attr("value"),
             company_name: jQuery('#complimentary_company_name_field').attr("value"),
             email_address: jQuery('#complimentary_email_field').attr("value"),
             street: jQuery('#complimentary_street_field').attr("value"),
             city: jQuery('#complimentary_city_field').attr("value"),
             state: jQuery('#complimentary_state_field').attr("value"),
             zip: jQuery('#complimentary_zip_field').attr("value"),
             cmd_complimentary_x: "cmd_complimentary_x"},
            function(data) {
              var result = eval('(' + data + ')');
              if (result.success) {
                closePopupForm();
                jQuery("#confirmation_msg").text(result.msg);
                jQuery("#confirmation_title").text(result.msgTitle);
                popupForm('confirmation_form');
              }
              else {
                jQuery("#complimentary_errors").empty();
                jQuery("#complimentary_errors").append(result.msg);
                jQuery("#complimentary_errors").css("display", "inline");
              }
            }
          );
          return false;
        });
      });
}

function addTellUsSubmitListener() {
      jQuery(document).ready(function() {
        var post_url = jQuery('#tell_us_form_element').attr("action");
        jQuery('#tell_us_form_element').submit(function() {
          jQuery.post(post_url,
            {name: jQuery('#tell_us_name_field').attr("value"),
             email_address: jQuery('#tell_us_email_field').attr("value"),
             comments: jQuery('#tell_us_comments_field').attr("value"),
             cmd_tell_us_x: "cmd_tell_us_x"},
            function(data) {
              var result = eval('(' + data + ')');
              if (result.success) {
                closePopupForm();
                jQuery("#confirmation_msg").text(result.msg);
                jQuery("#confirmation_title").text(result.msgTitle);
                popupForm('confirmation_form');
              }
              else {
                jQuery("#tell_us_errors").empty();
                jQuery("#tell_us_errors").append(result.msg);
                jQuery("#tell_us_errors").css("display", "inline");
              }
            }
          );
          return false;
        });
      });
}

function addEmailSubmitListener() {
        var post_url = jQuery('#email_form_element').attr("action");
        jQuery('#email_form_element').submit(function() {
          jQuery.post(post_url,
            {name: jQuery('#email_name_field').attr("value"),
             email_address: jQuery('#email_from_field').attr("value"),
             friend_email: jQuery('#friend_email_field').attr("value"),
             personal_message: jQuery('#personal_message_field').attr("value"),
             email_article_link: jQuery('#email_article_link_field').attr("value"),
             email_article_title: jQuery('#email_article_title_field').attr("value"),
             email_article_type: jQuery('#email_article_type_field').attr("value"),
             cmd_email_x: "cmd_email_x"},
            function(data) {
              var result = eval('(' + data + ')');
              if (result.success) {
                closePopupForm();
                jQuery("#confirmation_msg").text(result.msg);
                jQuery("#confirmation_title").text(result.msgTitle);
                popupForm('confirmation_form');
              }
              else {
                jQuery("#email_errors").empty();
                jQuery("#email_errors").append(result.msg);
                jQuery("#email_errors").css("display", "inline");
              }
            }
          );
          return false;
        });
}
