function loadLyteBoxLinkParams(link, widthparam, heightparam) {
window.$.fancybox({
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic',
				'titlePosition'	: 'inside',
				'padding' : 40,
				'hideOnContentClick' : 'true',
				'overlayColor' : '#fff',
				'overlayOpacity' : '0.3',
				'easingIn' : 'swing',
				'easingOut' : 'swing',
				'width' : widthparam,
				'height' : heightparam,
				'cyclic' : 'true',
				'type' : 'iframe',
				'content' : link
});
}

function checkEmail(email) { 
    var pattern = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    var emailVal = $("#" + email).val();
    return pattern.test(emailVal);
}

$(function() {
    $('#OMsubscribeForm').submit(function(e) {
      e.preventDefault();
      var formAction = this.action;
      var emailId = 'OMemail';      
      if (!checkEmail(emailId)) {
        alert("Introduceti o adresa de email valida");
        return;
      }
      
      // Serialize form values to be submitted with POST
      var str = $(this).serialize();
      
      // Add form action to end of serialized data
      // CDATA is used to avoid validation errors
      //<![CDATA[
      var serialized = str + "&action=";
      // ]]>
	  //+ formAction
      alert(serialized);
      // Submit the form via ajax
      $.ajax({
        url: "http://www.viacanada.ro/modules/newsletter/proxy.php",
        type: "POST",
        data: serialized,
        dataType: 'xml',
        success: function(data){
          // Server-side validation		
		  alert('here')	;
		  var status = $(data).find('status').text();
          var message = $(data).find('message').text();
		  var subscriber_id = $(data).find('subscriber_id').text();
		  var checkupdate = $(data).find('checkupdate').text();
		  alert('Status from response:'+status);
		  //alert('Message from response:'+message);
		  //alert('subscriber_id:'+subscriber_id);
		  //alert('checkupdate:'+checkupdate);
		  //alert('Output:'+$(output).unserialize());
		  /*if(status == 1) {
		    OMlistid=document.getElementById('OMlistid').value;
			OMemail=document.getElementById('OMemail').value;
			loadLyteBoxLinkParams('http://www.freshy.ro/hotelatlantic/modules/newsletter/step2.php?subscriber_id='+subscriber_id+'&OMlistid='+OMlistid+'&OMemail='+OMemail,'506','490');	
			document.getElementById('OMemail').value='';	
		  }
          else {*/
		    //document.getElementById('messagenewsleter').innerHTML=message;
			//here we need to check if the user is not already added to do an edit
			var serialized = str + "&action=" + formAction + "&checkupdate=1";
			 $.ajax({
				url: "http://www.viacanada.ro/modules/newsletter/proxy.php",
				type: "POST",
				data: serialized,
				dataType: 'xml',
				success: function(data){
					var status = $(data).find('status').text();
					var message = $(data).find('message').text();
					var subscriber_id = $(data).find('subscriber_id').text();
					var checkupdate = $(data).find('checkupdate').text();
					//alert('Status2 from response:'+status);
					//alert('Message2 from response:'+message);
					//alert('subscriber_id2:'+subscriber_id);					
					//alert('checkupdate2:'+checkupdate);
					//
					if(status == 1) {
						OMlistid=document.getElementById('OMlistid').value;
						OMemail=document.getElementById('OMemail').value;
						
						var first_name = $(data).find('first_name').text();
						var last_name = $(data).find('last_name').text();
						var dataid = $(data).find('dataid').text();
						var dataval = $(data).find('dataval').text();
						
						loadLyteBoxLinkParams('http://www.viacanada.ro/modules/newsletter/step2.php?subscriber_id='+subscriber_id+'&OMlistid='+OMlistid+'&OMemail='+OMemail+'&first_name='+first_name+'&last_name='+last_name+'&dataid='+dataid+'&dataval='+dataval,506,490);	
						document.getElementById('OMemail').value='';	
					}
					else {
						document.getElementById('messagenewsleter').innerHTML=message;
					}
					//
				}
			});
        }
      });
    });
  });
  
