(function(){
    String.prototype.trim=function(){return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');};
    var window=this;
    var _SH =window._SH = {};
    _SH.DivMsg= ["Failed to send the email, please resubmit your email address.","Please provide a valid email address","The report already been sent to this email address, please try to resend it to an alternative email address.","Sending ..."];
    _SH.Msg= "#wg_Email_Msg div:last";
    _SH.Input= "#wg_Email_Msg input";
    _SH.Timer;
    _SH.Url="/general/EmailNewsLetterHandler.ashx";
    _SH.Email;
    _SH.checkEmail=function(){
        var regExp=new RegExp("^(([^<>()[\\]\\\\.,;:\\s@\\\"]+(\\.[^<>()[\\]\\\\.,;:\\s@\\\"]+)*)|(\\\".+\\\"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$");
        if (_SH.Timer!==undefined && _SH.Timer!==null){
            clearTimeout(_SH.Timer);
        }

        var content=$(_SH.Input).val().trim().toLowerCase();
        
        if (content===_SH.Email){
            $(_SH.Msg).html(_SH.DivMsg[2]).show();
            _SH.Timer=setTimeout("_SH.hide(1000)",1000);
            return;
        }
        
        if (content.match(regExp)){
            $(_SH.Msg).html(_SH.DivMsg[3]).show();
            $.ajax({
                type: "POST",
                url: _SH.Url,
                async: true,
                data: "email="+content,
                contentType: "application/x-www-form-urlencoded",
                dataType: "json",
                success: function(msg) {
                    $(_SH.Msg).html(msg.body).show();
                    if (msg.body.search(/failed/i)===-1){
                        _SH.Email=content;
                    }
                    _SH.Timer=setTimeout("_SH.hide(2000)",1000);
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    $(_SH.Msg).html(_SH.DivMsg[0]).show();
                    _SH.Timer=setTimeout("_SH.hide(1500)",1000);
                }
            });
        }else{
            $(_SH.Msg).html(_SH.DivMsg[1]).show();
            _SH.Timer=setTimeout("_SH.hide(1000)",1000);
        }
    };

    _SH.hide=function(count){
        $(_SH.Msg).fadeOut(count);
        clearTimeout(_SH.Timer);
        _SH.Timer=null;
    };
    
    _SH.revealModal=function(divID){
        window.onscroll=function(){document.getElementById(divID).style.top = document.body.scrollTop;};
        document.getElementById("ifEmail").src="/general/TickTraxFreeNewsLetter.aspx";
        var tmp=document.getElementById(divID);
        document.body.insertBefore(tmp,document.body.firstChild);
        tmp.style.top=document.body.scrollTop;
        tmp.style.display="block";
    };

    _SH.hideModal=function(divID)
    {
        document.getElementById(divID).style.display="none";
        document.getElementById("ifEmail").src="";
    };
    
     _SH.hideParentModal=function(divID)
    {
        parent.document.getElementById(divID).style.display="none";
        parent.document.getElementById("ifEmail").src="";
    };
})();
