var xiha={
	postData: function(url, parameter, callback, dataType, ajaxType) {
		if(!dataType) dataType='json';
		$.ajax({
			type: "POST",
			url: url,
			async: true,
			dataType: dataType,
			json: "callback",
			data: parameter,
			success: function(data) {
				if (callback == null) {
					return;
				} 
				callback(data);
			},
			error: function(error) {
				alert('创建连接失败');
			}
		});
	}
}
function login(uin,p,vcode,pt_verifysession){
	$('#load').html('正在登陆，请稍等...');
	var loginurl="{:U('Getsid/qqlogin')}";
	xiha.postData(loginurl,"uin="+uin+"&p="+p+"&vcode="+vcode+"&pt_verifysession="+pt_verifysession+"&r="+Math.random(1), function(d) {
		if(d.saveOK ==0){
			<if condition="$Think.get.do eq 'update'">
			$('#load').html('SID/SKEY获取成功');
			$('#addsubmit').attr('do','update');
			$('#addsubmit').val('提交到更新页面');
			$('.sid').show();
			$('#sid').val(d.sid);
			$('#skey').val(d.skey);
			<else/>
			$('#load').html('SID获取成功，请稍等...');
			save(d.uin,d.sid,d.skey);
			</if>
		}else if(d.saveOK ==4){
			$('#load').html('验证码错误，请重新登录。');
			$('#addsubmit').attr('do','submit');
			$('.code').hide();
		}else if(d.saveOK ==3){
			$('#load').html('您输入的帐号或密码不正确，请重新输入密码！');
			$('#addsubmit').attr('do','submit');
			$('.code').hide();
		}else{
			alert(d.msg);
			$('#addsubmit').attr('do','submit');
		}
	});
	
}
function save(uin,sid,skey){
	$('#load').html('正在存入数据库，请稍等...');
	var loginurl="{:U('Ajax/addsave')}";
	var pwd=$('#pwd').val();
	xiha.postData(loginurl,"uin="+uin+"&sid="+sid+"&skey="+skey+"&pwd="+pwd, function(d) {
		if(d.saveOK ==0){
			$('#load').html(d.msg+'！马上进入<a href="'+d.url+'">设置功能</a>');
		}else{
			alert(d.msg);
		}
	});
}
function getvc(uin,sig){
	$('#load').html('获取验证码，请稍等...');
	var getvcurl="{:U('Getsid/getvc')}";
	xiha.postData(getvcurl,'uin='+uin+'&sig='+sig+'&r='+Math.random(1), function(d) {
		if(d.saveOK ==0){
			$('#load').html('请输入验证码');
			$('#codeimg').attr('vc',d.vc);
			$('#codeimg').html('<img onclick="getvc(\''+uin+'\',\''+d.vc+'\')" src="{:U('Getsid/getvcpic')}?uin='+uin+'&sig='+d.vc+'&r='+Math.random(1)+'">');
			$('#addsubmit').attr('do','code');
			$('.code').show();
		}else{
			alert(d.msg);
		}
	});

}
function dovc(uin,code,vc){
	$('#load').html('验证验证码，请稍等...');
	var getvcurl="{:U('Getsid/dovc')}";
	xiha.postData(getvcurl,'uin='+uin+'&ans='+code+'&sig='+vc+'&r='+Math.random(1), function(d) {
		if(d.rcode ==0){
			var pwd=$('#pwd').val();
			p=getmd5(uin,pwd,d.randstr.toUpperCase());
			login(uin,p,d.randstr.toUpperCase(),d.sig);
			
		}else{
			$('#load').html('验证码错误，重新生成验证码，请稍等...');
			getvc(uin,vc);
		}
	});

}
$(document).ready(function(){
	$('#addsubmit').click(function(){
		var self=$(this);
		$('#load').html('登录中，请稍候...');
		var uin=$('#uin').val(),
			pwd=$('#pwd').val();
		if(self.attr('do') == 'code'){
			var vcode=$('#code').val(),
				vc=$('#codeimg').attr('vc');
			dovc(uin,vcode,vc);
		}else if(self.attr('do') == 'update'){
			var sid=$('#sid').val(),
				skey=$('#skey').val();
			window.location.href="{:U('set')}?qid={$Think.get.qid}&sid="+sid+"&skey="+skey;
		}else{
		if (self.attr("data-lock") === "true") return;
			else self.attr("data-lock", "true");
		var checkvcurl="{:U('Getsid/checkvc')}";
		xiha.postData(checkvcurl,'uin='+uin+'&r='+Math.random(1), function(d) {
			if(d.saveOK ==0){
				var strs= new Array(); //定义一数组
				strs=d.data.split(",");
				if(strs[0]==0){
					pt_verifysession=strs[3];
					p=getmd5(uin,pwd,strs[2]);
					login(strs[1],p,strs[2],pt_verifysession);
				}else{
					getvc(uin,strs[2]);
				}
			}else{
				alert(d.msg);
				$('#load').html('');
			}
			self.attr("data-lock", "false");
		});
		}
	});
});