var modal;

var newsletter_top = '<div id="newsletter_register" class="newsletter_box">'+
'<img src="'+base_url+'images/newsletter/h-subscribetogoalink.gif" width="160" height="16" align="left">'+
'<img id="newsletter_close" src="'+base_url+'images/newsletter/close.gif" width="49" height="16" align="right" onclick="close_click()">'+
'<p>goaLink is our e-newsletter. Enter your details below to subscribe and receive emails about what\'s happening with goa, as well as useful articles on Outdoor possibilities.</p>'+
'<hr />';
var newsletter_form = newsletter_top+
'<form action="'+base_url+'newsletter/subscribe" id="newsletter_form" method="post">'+
'<div class="newsletter_left">    '+
'<label for="name">Your Name *</label> <input type="text" name="name" id="name"  maxlength="100" /><br />'+
'<label for="l265987-265987">Email *</label> <input type="text" name="cm-265987-265987" id="l265987-265987" maxlength="100" /><br />'+
'</div>'+
'<div class="newsletter_right">'+
'<label for="cm-f-135626">Company</label> <input type="text" name="cm-f-135626" id="Company" maxlength="100" /><br />'+
'<label for="cm-f-135627">Job Title</label> <input type="text" name="cm-f-135627" id="JobTitle" maxlength="100" /><br />'+
'</div>'+
'<p><input type="image" src="'+base_url+'images/newsletter/b-subscribe.gif" class="newsletter_button" align="right" /><span style="color:#db0e21">*</span> You must fill out all the boxes with an asterisk beside them before you can submit your message</p>'+
'</form>'+
'</div>';

var newsletter_success = newsletter_top+
'<p style="padding-top:0px;">Hi,<br />'+
'Thanks so much for joining our list, your subscription was a success.</p>'+
'<p>We\'ll be including a removal link in every email we send you, so you can leave our list any time that suits you.</p>'+
'</div>';

function close_click (e)
{
//	e = new Event(e).stop();
	modal.modalHide();	
}

window.addEvent("domready", function() {  
  // create modal dialog  
  $('newsletter_show').addEvent('click', function(e) {
    e = new Event(e).stop();
    modal = new StickyWinModal({width: 430, height:300});
    modal.setContent(newsletter_form);
    
    $('newsletter_form').addEvent('submit', function(e) {
      e = new Event(e).stop();        
      var why = "";
      why += checkEmail(this['cm-265987-265987'].value);
      if (!this.name.value.length) {
        why += "Please enter your name";
      }
      why += checkLength(this['cm-f-135626'].value, 'Company', 0, 100);
      why += checkLength(this['cm-f-135627'].value, 'Job Title', 0, 100);        
      if (why != "") {
         alert(why);
         return false;
      }
      this.send({
        onSuccess: function(obj) {
          if (obj.length > 5) {
            alert(obj);
          }
          else {
            modal.setContent(newsletter_success);          
            setTimeout("modal.modalHide()", 6000);
          }
        },
				onFailure: function(obj) {
          alert('Unknown error occurred');
				}
      });
    });
    $('newsletter_close').addEvent('click', close_click);
  });  
});