// class.emailSignup.js

(function( $ ){ //Start jQuery Wrapper

//Start Method Array
var methods = {
	init	: 	function(){
					that = this;
					$(this).click(function(){
						methods.doAjax(this);
					});
					$(this).siblings("input[name='logonId']").keypress(function(e){
						if(e.which == 13) methods.doAjax(that);
					});
					$("input[name='logonId']").focus(function () {
         				if($(this).val()=="Saisir adresse courriel") $(this).val("");
    				});
    				$("input[name='logonId']").blur(function () {
         				if($(this).val()==""||$(this).val()==null) $(this).val("Saisir adresse courriel");
    				});
					$("#email_message").hide();
				},
	doAjax	:	function(that){
					map = "";
					email = "";
					var agree = true;
					$(that).siblings("input").each(function(index) {
						if(index !=0) map = map + "&";	
						if($(this).attr("name")=="logonId") email = $(this).val();
						else if($(this).attr("name")=="email_agree") agree =  $(this).attr('checked');			
						map = map + $(this).attr("name") + "=" + $(this).val();
					});
					if(!$.validate.email(email)) {
						$("#email_error_message").children("small").html("Veuillez entrer une adresse \351lectronique valide.");
						$("#email_message").hide();
						$("#email_error_message").show();
						return false;
					} else if(!agree) {
						$("#email_error_message").children("small").html("Veuillez confirmer que vous avez au moins 18 ans.");
						$("#email_message").hide();
						$("#email_error_message").show();
						return false;
					}
					$.post("http://french.lenscrafters.ca/webapp/wcs/stores/servlet/UserRegistrationAdd",
   						map,
    					function(data){
    							//var starcomTag = "";
    							var axel = Math.random() + "";
								var a = axel * 10000000000000;
    							if(document.URL.toString().indexOf("lenscrafters.com") != -1){
	    							$("#starcom").html("<div style=\"display:none;\"><iframe src=\"http://fls.doubleclick.net/activityi;src=2558490;type=lensc478;cat=lc_em148;ord=" + a +   "?\" width=\"1\" height=\"1\" frameborder=\"0\"></iframe></div>");
								} else if(document.URL.toString().indexOf("lenscrafters.ca") != -1){
									$("#starcom").html("<div style=\"display:none;\"><iframe src=\"http://fls.doubleclick.net/activityi;src=2558644;type=lensc790;cat=lc_em971;ord=" + a + "?\" width=\"1\" height=\"1\" frameborder=\"0\"></iframe></div>");
								}
																
								if(data.indexOf('fail') == -1) {
	    							$("#email_error_message").hide();
	    							$("#email_message").children("small").html("Thanks for registering at LensCrafters.com!");
	    							$("#email_message").show();
	    							
	    							//omniture call for email signup
	    							s.events='event9'; 
	    							s.t();    							
    							}
    							else {
									$("#email_error_message").children("small").html("Cette adresse courriel correspond d\351j\340 \340 une inscription.");
									$("#email_message").hide();
									$("#email_error_message").show();
    							
    							}
    							
  						});
  						
  						
				}// End Methods
};// End Methed Array

// Start Method Fetch
$.fn.emailSignup = function( method ) {
    if ( methods[method] ) {
      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist in catalogNavigation' );
    }    
};//End Method Fetch


})(jQuery);//End jQuery Wrapper
