﻿jQuery(document).ready(function() {
	
  if (($.cookie('emailSignup')) != 'showed') { //if cookie is set then not showing popup
  	timeDelay();
  }
  function timeDelay() {
    setTimeout(emailSignupPopup, 100);
  }
  function setCookie() {
    $.cookie('emailSignup', 'showed', { expires: 180 });
  }
  function removePOPup() {
    $('#emailPOP').fadeOut(1000, function() {
      $(this).remove();
    });
  }
  function closePOPup() {
	  $('#emailPOP').fadeOut(1000, function() {
	    $(this).remove();
	  });
  }
  function emailSignupPopup() {

	$('body').append(getFormHTML());
    docWidth = $('body').width();
    docHeight = $('body').height();
    //$('#emailPOP').css({ left: docWidth / 2 - 75 , top: docHeight / 2 +75});
    $('#emailPOP').css({ left: docWidth / 2 - 75 , top: 75});
    //alert( $('#emailPOP').css ("left"));
    $('#emailPOP a#close').css ( { align: 'center'} );
    $('#emailPOP a#close').click(function() { //show popup again tmr if closing by cross
      $.cookie('emailSignup', 'showed', { expires: 1 });
      closePOPup();
      return false;
    });
    
    $('#nothanks').click(function() {
      setCookie();
      closePOPup();
      return false;
    });
    $('#mc-embedded-nothanks').click(function() {
      setCookie();
      closePOPup();
      return false;
    });
	var options = { 
        target:        '#error',   // target element(s) to be updated with server response 
        beforeSubmit:  showRequest,
        success:       showResponse  // post-submit callback 
    }; 
    
    $('#emailFORM').ajaxForm(options); 

    /*    
    $('#subscribe').click(function() { //do supscribe
      isFormGood = validate();
      if (isFormGood == true) {
		// bind form using 'ajaxForm' 
        $('#subscribe').unbind('click');
        var data = $("#emailFORM :checkbox").fieldValue();
        setCookie();
        subscriberName = $('#emma_member_name_first').val();
        subscriberSurname = $('input#emma_member_name_last').val();
        subscriberEmail = $('input#emma_member_name_email').val();
        //newSubscriber(subscriberName, subscriberSurname, subscriberEmail);
      }
    });
    */
    
	function showRequest(formData, jqForm, options) { 
	    // formData is an array; here we use $.param to convert it to a string to display it 
	    // but the form plugin does this for you automatically when it submits the data 
	    var queryString = $.param(formData); 
	 
	    // jqForm is a jQuery object encapsulating the form element.  To access the 
	    // DOM element for the form do this: 
	    //var formElement = jqForm[0]; 
	 
	    var isFormGood = validate();
	    //alert('About to submit: \n\n' + queryString); 
	 
	    // here we could return false to prevent the form from being submitted; 
	    // returning anything other than false will allow the form submit to continue 
	    // validate the form here.
	    return isFormGood ; 
	} 
   
  // post-submit callback 
	function showResponse(responseText, statusText)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
    $('#emailPOP').html(responseText);
	setCookie();
    setTimeout(removePOPup, 3000);
    
    //alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + '\n\nThe output div should have already been updated with the responseText.'); 
	} 
  
    function validate() {//check on emtpy fields
      i = 0; //count for errors if 0 then all fields are field and check for email
      var err = 0;
      $('#emailPOP input').each(function() {
        if (($(this).val()) == '') {
          thisID = $(this).parent();
          ++i; //count errors
          showErrorMsg(thisID);
          err = 1; //if error then 1
        } else {
          $(this).parent().css({ color: '#535252' });
          if ($(this).attr('id') == 'email') {
            fieldValue = $(this).val();
            if (!fieldValue.match(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i)) {
              $('p#error').text('Email address is invalid!').css({ color: '#CF1925' });
              $('input#email').parent().css({ color: '#CF1925' }); //email title make red
              err = 1;
            } else {
              err = 0; //if email is ok then err empty (0)
            }
          }
        }
      });
      
      if ((err == 0) && (i == 0)) { //if no empty and email is correct then return true
        return true;
      }
      return false;
    }
    function showErrorMsg(ID) {
      $('p#error').text('Some information is missing or invalid!');
      $('p#error').css({ color: '#CF1925' });
      $(ID).css({ color: '#CF1925' });
    }
    
    $('#emailPOP').fadeIn(500); //show popup
 
  }
 } );

 
function getIFrameHTML()
{
	var html = 
	'<div id="emailPOP" style="border:3px solid black; background-color:#9999ff; padding:5px; font-size:150%; '
		+ 'text-align:center; position:absolute; z-index:9000; ">'
                    + '<a id="close" href="#" title="close" ><img border="0" src="/Images/closeIE6.gif" /></a>'
					+ '<P><iframe src="jtest.aspx" style="width:330px;height:460px;" />'
					+ '<P>'
                    + '<a href="#" id="nothanks" style="font-size:50%"><span>No thanks</span></a>'
					+ '</div>';
	return html;
	
}

function getFormHTML()
{
	var html = 
	
	'<div id="emailPOP" style="border:3px solid black; background-color:#9999ff; padding:5px; '
	+ ' font-size:100%;  position:absolute; z-index:9000; text-align:left; ">'
    + '<a id="close" href="#" title="close" align="right" ><img border="0" src="/Images/closeIE6.gif" /></a>'
	+ '<div id="mc_embed_signup" style="width: 300px;">' 
	+ '<form action="subscribeNewsLetter.aspx" method="post" '
	+ '	id="emailFORM" name="emailFORM" class="validate" style="font: normal 100% Arial;font-size: 12px;"> '
	+'<fieldset style="-moz-border-radius: 4px;border-radius: 4px;-webkit-border-radius: 4px;border: 1px solid #000000;padding-top: 1.5em;margin: .5em 0;background-color: #FFFFFF;color: #333333;"> '
    + '<p id="error">&nbsp;</p>'

	+'<div class="mc-field-group" style="margin: 1.3em 5%;">'
	+'<label for="mce-FNAME" style="display: block;margin: .3em 0;line-height: 1em;font-weight: bold;">First Name:</label>'
	+'<input type="text" value="" name="FNAME" class="" id="FNAME" style="margin-right: 1.5em;padding: .2em .3em;width: 95%;position: relative;z-index: 999;">'
	+'</div>'
	
	
	+'<div class="mc-field-group" style="margin: 1.3em 5%;"> '
	+'<label for="mce-LNAME" style="display: block;margin: .3em 0;line-height: 1em;font-weight: bold;">Last Name:</label> '
	+'<input type="text" value="" name="LNAME" class="" id="LNAME" style="margin-right: 1.5em;padding: .2em .3em;width: 95%;position: relative;z-index: 999;"> '
	+'</div> '
	+'<div class="mc-field-group" style="margin: 1.3em 5%;"> '
	+'<label for="mce-EMAIL" style="display: block;margin: .3em 0;line-height: 1em;font-weight: bold;">Email Address:</label>'
	+'<input type="text" value="" name="EMAIL" class="required email" id="EMAIL" style="margin-right: 1.5em;padding: .2em .3em;width: 95%;position: relative;z-index: 999;">'
	+'</div>'
	+'<div class="mc-field-group" style="margin: 1.3em 5%;">'
	+'    <label class="input-group-label" style="display: block;margin: .3em 0;line-height: 1em;font-weight: bold;">I am interested in::</label>'
	+'    <div class="input-group" style="padding: .7em;font-size: .9em;margin: 0 0 1em 4%; ">'
	+'    <ul style="margin: 0;padding: 0; align:left; border:1px solid blue;"><li style="list-style: none;overflow: hidden;padding: .2em 0;clear: right; "><input type="checkbox" value="1" name="group[1]" id="mce-group-0" style="margin-right: 4%;padding: .2em .3em;width: auto;position: relative;z-index: 999;float: left;"><label for="mce-group-0" style="display: block;margin: .4em 0 0 0;line-height: 1em;font-weight: bold;width: auto;text-align: left;">Small Ship Adventure Cruises</label></li><br/>'
	+'<li style="list-style: none;overflow: hidden;padding: .2em 0;clear: right;"><input type="checkbox" value="2" name="group[2]" id="mce-group-1" style="margin-right: 4%;padding: .2em .3em;width: auto;position: relative;z-index: 999;float: left;"><label for="mce-group-1" style="display: block;margin: .4em 0 0 0;line-height: 1em;font-weight: bold;width: auto;text-align: left;">Small Ship River Cruises</label></li><br/>'
	+'<li style="list-style: none;overflow: hidden;padding: .2em 0;clear: right;"><input type="checkbox" value="4" name="group[4]" id="mce-group-2" style="margin-right: 4%;padding: .2em .3em;width: auto;position: relative;z-index: 999;float: left;"><label for="mce-group-2" style="display: block;margin: .4em 0 0 0;line-height: 1em;font-weight: bold;width: auto;text-align: left;">Small Ship Expedition Cruises</label></li><br/>'
	+'<li style="list-style: none;overflow: hidden;padding: .2em 0;clear: right;"><input type="checkbox" value="8" name="group[8]" id="mce-group-3" style="margin-right: 4%;padding: .2em .3em;width: auto;position: relative;z-index: 999;float: left;"><label for="mce-group-3" style="display: block;margin: .4em 0 0 0;line-height: 1em;font-weight: bold;width: auto;text-align: left;">Small Group Cultural Tours</label></li><br/>'
	+'<li style="list-style: none;overflow: hidden;padding: .2em 0;clear: right;"><input type="checkbox" value="16" name="group[16]" id="mce-group-4" style="margin-right: 4%;padding: .2em .3em;width: auto;position: relative;z-index: 999;float: left;"><label for="mce-group-4" style="display: block;margin: .4em 0 0 0;line-height: 1em;font-weight: bold;width: auto;text-align: left;">Private Journeys</label></li><br/>'
	+'</ul>'
	+'    </div>'
	+'</div>'
	+'<div id="mce-responses" style="position: relative;top: -1.4em;padding: 0em .5em 0em .5em;overflow: hidden;">'
	+'	<div class="response" id="mce-error-response" style="display: none;margin: 1em 0;padding: 1em .5em .5em 0;font-weight: bold;position: relative;top: -1.5em;z-index: 1;width: 80%;background: #FFEEEE;color: #FF0000;"></div>'
	+'	<div class="response" id="mce-success-response" style="display: none;margin: 1em 0;padding: 1em .5em .5em 0;font-weight: bold;position: relative;top: -1.5em;z-index: 1;width: 80%;background: #;color: #529214;"></div>'
	+'</div>'
	+'<div>'
	+'<span>'
	+'<input type="submit" value="Subscribe" name="subscribe" id="subscribe" class="btn" style="width: auto;margin: .1em 1em;display: block; float:left;">'
	+'<input type="button" value="No Thanks" name="nothanks" id="nothanks" class="btn" style="width: auto;margin: .1em 1em;display: block;">'
	+'</span>'
	+'</div>'
	
	
	+'</fieldset>	'
	+'</form> '
	+'</div>'
	+'</div>';
	
	return html;
}
 