var basic={form:'Erlo',err:{ok:'确认',cl:'取消',tit:'系统提示',reset:'重新请求',sx:'刷新页面',suc:'操作成功',fail:'服务器响应失败',maxtime:'【处理数据过大，请等待】'},load:'erloLoad',email:'lj_f123@126.com',www:'//www.deyuad.com/',loadimg:'style/img/load.gif',path:'/',version:'2.4'},ljTimes,nophone=['passwd','old','set','sq'],fdata=[];
var ue;

var ydalt=getCookie('ydalt'),cy=true;
ydalt=ydalt?ydalt:0;
if($('.lh-consult').length>0){
    setTimeout(function(){
	   $('.lh-consult,.lh-consult-box').show();
    },12000)
    $('.lh-consult-close,.close').click(function(){
	    $('body').addClass('closeConsult');	
	    //setCookie('ydalt',parseInt(ydalt)+1);
	    if(cy){
		  cy=false;
		   setTimeout(function(){
			 $('body').removeClass('closeConsult');	
		   },30000)
	    }
    });
}
if(ydalt>1){
    //$('.lh-consult').hide();
}

$(function(){
    /*****************************************************/
    /*****************************************************/
    $(document).on('click','.ljImg',function(){
        var html='<img src="'+$(this).attr('src')+'">';
        alertLj(html,{img:true});
    })
    function zview(obj,data){
	   data=getFormDataa(obj,data);
        if(!data)return false;
        ajaxForm(obj,data,function(msg){
            if(msg && msg.error=='0'){
			 obj.removeClass(basic.load);
                if(msg.data){
				if(msg.data.del){
				    obj.removeClass('on')
				}else{
				    obj.addClass('on')
				}
				if(obj.find('span').size()>0)
				    obj.find('span').text(0);
				else
				    obj.append('(<span>0</span>)');
				var i=msg.data.zview-3;if(i<0)i=0;
				var is=setInterval(function(){
				    i++;
				    obj.find('span').text(i);
				    if(i>msg.data.zview){
					   clearInterval(is);
				    }
				},10)
                }
            }else{
			 if(obj.find('.ljSub').attr('txt'))obj.find('.ljSub').text(obj.find('.ljSub').attr('txt'));
                alertLj(msg?(msg.html?msg.html:basic.err.fail):basic.err.fail);
                obj.removeClass(basic.load);
                if(obj.find('.codeClick').size()>0)obj.find('.codeClick').click();
            }
        },function(){
		  if(obj.find('.ljSub').attr('txt'))obj.find('.ljSub').text('提交中');
        },function(){
		  if(obj.find('.ljSub').attr('txt'))obj.find('.ljSub').text(obj.find('.ljSub').attr('txt'));
            alertLj(basic.err.fail)
        })
    }
    var zviews;
    $('.ljZview').click(function(){
	   var obj=$(this),data={action:'zview'};
	   if(zviews)clearTimeout(zviews);
	   zviews=setTimeout(function(){
		  zview(obj,data);
	   },300);
        return false;
    })
    if($('.ljZview').size()>0){
	   var obj=$('.ljZview'),data={action:'gzview'};
	   zviews=setTimeout(function(){
		  zview(obj,data);
	   },300);
    }
    $('.ljSub').click(function(){
        $(this).parents('form').submit();
    })
    $('.ljForm').submit(function(){
        var obj=$(this),data={action:obj.attr('action')};
        if(obj.attr('fun')){
            return false;
        }
        data=getFormDataa(obj,data);
        if(!data)return false;
        ajaxForm(obj,data,function(msg){
            if(msg && msg.error=='0'){
                if(msg.url){
                    if(msg.url=='/vip' && obj.attr('url')){
                        window.location.href=obj.attr('url');
                    }else{
                        window.location.href=msg.url;
                    }
                }else{
                    if(obj.attr('ourl'))
                    alertLj(msg.html?msg.html:basic.err.suc,{noClose:true,click:'window.location.href=\''+obj.attr('ourl')+'\';'});
                    else
                    alertLj(msg.html?msg.html:basic.err.suc,{noClose:true,click:'window.location.reload();'});
                }
            }else{
			 if(obj.find('.ljSub').attr('txt'))obj.find('.ljSub').text(obj.find('.ljSub').attr('txt'));
                alertLj(msg?(msg.html?msg.html:basic.err.fail):basic.err.fail);
                obj.removeClass(basic.load);
                if(obj.find('.codeClick').size()>0)obj.find('.codeClick').click();
            }
        },function(){
		  if(obj.find('.ljSub').attr('txt'))obj.find('.ljSub').text('提交中');
        },function(){
		  if(obj.find('.ljSub').attr('txt'))obj.find('.ljSub').text(obj.find('.ljSub').attr('txt'));
            alertLj(basic.err.fail)
        })
        return false;
    });
    var srcs=[],wx=$('meta[name="wx"]').size()>0?$('meta[name="wx"]').attr('content'):'';
    $('.ljimgshow img').each(function(){
        srcs.push(wx+$(this).attr('src'))
    })
    $('.ljimgshow img').click(function(){
        var src=$(this).attr('src'),img='<img src="'+src+'" style="max-width:100%;">';
        wx=false;
        if(wx){
              var n=$(this).index();
              WeixinJSBridge.invoke('imagePreview', {
                'current' : n,
                'urls' : srcs
            });
        }else{
            var box='<div class="boxlj" style="z-index:2;position:fixed;top:0;left:0;background:rgba(0,0,0,.5);width:100vw;height:100vh;text-align:center;padding:20px 0;"><div style="/*top:30%;transform: translateY(-50%);position: relative;*/display: flex;align-items: center;justify-content: center;height: 100%;">'+img+'</div><a href="javascript:" style="display:block;text-align:center;line-height:35px;color:#fff;position:fixed;bottom:0;left:0;width:100%">关闭</a></div>';
            $('body').append(box);
        }

    })
})
function ajaxForm(obj,data,suc,bef,err){//异步请求
    if($('meta[name="web"]').size()>0)data['ljweb']=true;
    if(obj.hasClass(basic.load))return false;
    $.ajax({
        type:'post',data:data,dataType:'json',beforeSend:function(){
            if(bef)bef();
            obj.addClass(basic.load);
        },error:function(){
            if(err)err();
            obj.removeClass(basic.load);
        },success:function(msg){
            if(suc)suc(msg)
        }
    })
}

function getFormDataa(obj,data){
    var e;
    data['sign']=$('meta[name="sign"]').attr('content');
    obj.find('.formValt').each(function(){
        if(data[$(this).attr('name')]){
            data[$(this).attr('name')]+=' - '+$(this).val();
        }else{
            data[$(this).attr('name')]=$(this).val();
        }
    })
    obj.find('.formVal').each(function(){
        if(!e && $(this).attr('nonull') && !$(this).val()){
            if(isNaN($(this).attr('nonull'))){
                e=$(this).attr('nonull');
            }else{
                e=$(this).attr('data-placeholder')?$(this).attr('data-placeholder'):$(this).attr('placeholder');
            }
        }
        if($(this).val()!=$(this).attr('data-placeholder')){
            data[$(this).attr('name')]=$(this).val();
        }
    })
    obj.find('.formVals:checked').each(function(){
        if(data[$(this).attr('name')]){
            data[$(this).attr('name')]+=$(this).val()+',';
        }else{
            data[$(this).attr('name')]=','+$(this).val()+',';
        }
    })
    obj.find('.formValz.on').each(function(){
        if(data[$(this).attr('name')]){
            data[$(this).attr('name')]+=','+$(this).attr('val');
        }else{
            data[$(this).attr('name')]=$(this).attr('val');
        }
    })
    obj.find('.formValLi').each(function(){
        if($(this).find('li.on').size()>0){
            data[$(this).attr('name')]=$.trim($(this).find('li.on').text());
        }
    })
    obj.find('.formValc:checked').each(function(){
        data[$(this).attr('name')]=$(this).val();
    })
    if(ue)data['cont']=ue.getContent();
    if(e){
        alertLj(e);
        //alertLj(e+'不能为空');
        return false;
    }
    return data;
}
function alertLj(str,obj,ppobj){
    if(ljTimes)clearInterval(ljTimes);
    if($('.alertLjBox').size())$('.alertLjBox').remove();
    if(!obj)obj={};
    if(typeof obj=='string')obj=o(obj);
    if(!obj.title)obj.title=basic.err.tit;
    if(!obj.ok)obj.ok=basic.err.ok;
    if(!obj.c)obj.c=basic.err.cl;
    var pobj=ppobj?ppobj:$('body'),pwobj=ppobj?(ppobj.attr('class')?ppobj:$(window)):$(window);
    var load='<img src="'+basic.path+'style/img/load.gif">',html,close='<a class="alertClose" onclick="alertLjClose()">关闭</a>',title='<div class="alertTitle">'+obj.title+'</div>',sure='<a class="alertSure">'+obj.ok+'</a>',cancel='<a class="alertCancel" onclick="alertLjClose()">'+obj.c+'</a>';
    var handle='<div class="alertHandle">'+sure+cancel+'</div>';
    html='<div class="alertBox">'+str+'</div>';
    html='<div class="alertLj">'+close+title+html+handle+'</div>';
    html='<div class="alertLjBox '+(obj.img?' ljImgBox" onclick="alertLjClose();':'')+'">'+html+'</div>';
    var cssLeft='-'+pwobj.width()+'px',cssTop=0;
    pobj.append(html).find('.alertLj').css({left:cssLeft,top:cssTop});
    if(obj.click){
	   $('.alertLj .alertSure').attr('onclick',obj.click);
    }else{
	   $('.alertLj .alertSure').attr('onclick','alertLjClose()');
    }
    if(obj.noClose)$('.alertLj .alertClose').remove();
    if(!obj.cancel)$('.alertLj .alertCancel').remove();
    if(obj.cancel && obj.cancelClick)$('.alertLj .alertCancel').attr('onclick',obj.cancelClick);
    if(str=='load'){
	   $('.alertLj').addClass('load').find('.alertBox').html(load).append('<span class="ljTimes"> </span>');
	   $('.alertLj .alertClose').remove();
	   ljLoadTimes();
    }
    cssLeft=(pwobj.width()-$(".alertLj").width())/2;
    cssTop=(pwobj.height()-$(".alertLj").height())/2.5+pwobj.scrollTop();
    $('.alertLj').css({left:cssLeft,top:cssTop},300).fadeIn(300);
    $('.alertLjBox').height($(document).height());
}
function ljLoadTimes(res){
    var ljI=1;
    ljTimes=setInterval(ljTimesGo,1000);
    function ljTimesGo(){
        if($('.ljTimes').size()>0){
            if(ljI>10){
                $('.ljTimes').html((ljI<10?("0"+ljI):ljI)+'s'+(ljI>10?('<br>'+basic.err.maxtime):''));
            }
            ljI++;
        }else{
            clearInterval(ljTimes);
        }
    }
}
function setCookie(cname, cvalue) {
    var exdays=1;//1天有效期
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}
//获取cookie
function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
	   var c = ca[i];
	   while (c.charAt(0)==' ') c = c.substring(1);
	   if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
    }
    return "";
}
//清除cookie 
function clearCookie(name) { 
    setCookie(name, "", -1); 
} 
function alertLjClose(fun,now){
    if(now) {
        $('.alertLjBox').hide();fun;
    }else{
        $('.alertLjBox').fadeOut(300,function(){
           $(this).remove();fun
        });
    }
}
function errHandle(error){
    if(error.status==810){
	   alertLj(error.responseText,{ok:basic.err.reset,click:'window.location.reload();',cancel:false,noClose:true});
    }else if(error.status==818){
	   alertLj(error.responseText,{ok:basic.err.sx,click:'window.location.reload();',cancel:false,noClose:true});
    }else{
	   alertLj(basic.err.fail)
    }
}