﻿// JScript File
function NewsLetterBasic(baseAppPath_,txtEmailID_,DefaultEmail_,flowType_,btnSignUpID_,lblMessageID_)
{
    var _baseAppPath = baseAppPath_;
    var _flowType = flowType_;
    var _txtEmail = jQuery('#'+txtEmailID_);
    var _txtbtnSignUp = jQuery('#'+btnSignUpID_);
    var _lblMessage = jQuery('#'+lblMessageID_);
    var _defaultEmail = DefaultEmail_;
   
    this.GetBaseAppPath=function(){return _baseAppPath;};
    this.GetDefaultEmailVal=function(){return _defaultEmail;};
    this.GetEmailTextBox=function(){return _txtEmail;};
    this.GetMessageLabel=function(){return _lblMessage;};
    this.GetSignUpButton=function(){return _txtbtnSignUp;};
    this.GetFlowType=function(){return _flowType;};
    this.EmailRegex = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
    
    _txtEmail.val(DefaultEmail_);
}
NewsLetterBasic.prototype.SignUp=function()
{
    var txtEmail=this.GetEmailTextBox();
    var currentObj=this;
    if(this.IsEmailValid(txtEmail.val()))
    {
        jQuery.ajax({
               type: "POST",
               async:false,
               contentType: "application/json; charset=utf-8",  
               dataType: "json",
               url: this.GetBaseAppPath()+"/DesktopModules/Misc/NewsLetterService.asmx/BasicSignUp",
               data: "{'email_':'" +txtEmail.val()+ "','flowType_':'"+this.GetFlowType()+"'}",              
               success: function(response)
               {
                    var jSonResponse = eval('(' + response.d + ')');       
                    if(jSonResponse.IsSucceed==false && jSonResponse.ErrorInfo!='')
                    {
                       currentObj.GetMessageLabel().html(jSonResponse.ErrorInfo);                       
                    }
                    else if(jSonResponse.IsSucceed==true)
                    {
                        currentObj.GetSignUpButton().hide();
                        txtEmail.hide();                      
                        currentObj.GetMessageLabel().html('Thank you for signing up');                   
                    }                  
               }   
            });
     }
}
NewsLetterBasic.prototype.IsEmailValid=function(email_)
{  
    if(email_=='undefined' || email_==null || email_=='' || email_==this.GetDefaultEmailVal())
    {
       this.GetMessageLabel().html('Email address is missing');
       return false;
    }
    else if(this.EmailRegex.test(email_)==false)
    {
       this.GetMessageLabel().html('Email address is incorrect');
       return false;
    }
    return true; 
}
NewsLetterBasic.prototype.CleanDefault=function()
{
    if (this.GetEmailTextBox().val()  == this.GetDefaultEmailVal())
        this.GetEmailTextBox().val('');
}
NewsLetterBasic.prototype.SignUpOnEnter=function(keyEvent)
{
    var keyPressedCode = (keyEvent != null ? keyEvent.keyCode : event.keyCode);
    if (keyPressedCode == 13)
    {
        if(blockSrch!=undefined && blockSrch!=null)
            blockSrch = true;//This is added for AutoSuggest on Flight/Car/Hotel to block search in enter pressed on Email textbox
        this.SignUp();
    }
}
function MM_openBrWindow(theURL,winName,features) 
{ //v2.0
  window.open(theURL,winName,features);
}

