// 小葵花 JS - 基于Jquery
function Dialog_Ajax(url, _class, data, success){
	this.url = url;
	this._class = _class || 'modpop1';
	this.data = data || '';
	this.success = success;
	this.dialog = null;
	this.init();	
}
Dialog_Ajax.prototype = {
	init: function(url, _class, page, success){
		this.buildModel();
		this.buildDialog();
	},
	buildModel: function(){
		if(!$('.dialog-model').length){
			$(document.body).append('<div class="dialog-model" style="width:'+document.body.offsetWidth+'px; height:'+document.body.offsetHeight+'px;"></div>');
		}else{
			$('.dialog-model').removeClass('nodisplay');
		}
	},
	buildDialog: function(){
		var pd = this._class.match(/modpop(\d+)/)[1], _s = this;
		var dialog_str = '<div class="'+this._class+'">';
		dialog_str += '<div class="modpop'+pd+'-hd"><a href="#" class="close">关闭</a></div>';
		dialog_str += '<div class="modpop'+pd+'-bd"></div>';
		dialog_str += '<div class="modpop'+pd+'-ft"></div>';
        dialog_str += '</div>';
		$(document.body).append(dialog_str);
		this.dialog = $('.modpop'+pd);
		this.dialog.find('.modpop'+pd+'-bd').html('<div class="pop-ok">正在加载...</div>');
		this.dialog.find('.modpop'+pd+'-bd').load(this.url, this.data, function(){_s.dialog.css('marginTop', (document.documentElement.scrollTop-_s.dialog.height()/2)+'px');_s.bindEvent();});
	},
	bindEvent: function(){
		var _s = this;
		this.dialog.find('.close').click(function(e){
			e.preventDefault();
			_s.closeDialog();
		});
		this.success.call(this);
	},
	closeDialog: function(){
		$('.dialog-model').addClass('nodisplay');
		this.dialog.remove();
	}
};

//登陆框
function Pop_Login(){
	new Dialog_Ajax('snippet-login.php', 'modpop1 mod-login pop-login2', '', function(){
		var dialog = this.dialog, form = dialog.find('form'), _s = this;
		dialog.find('.logbtn').click(function(e){
			e.preventDefault();
			$.post('login.php', form.serialize(), function(o){
			
			if (o['code']==1)
			{
				form.html('<div class="pop-ok">'+o['msg']+'</div>');
				setTimeout(function(){
					location.reload();
					//_s.closeDialog();
				}, 2000);
			}else{
				alert(o['msg']);
				return;
			}
				
			},'json');
		});
	});
}

//注册框
function Pop_Regist(){
	new Dialog_Ajax('snippet-reg.php', 'modpop1 mod-personal pop-login', 'reg', function(){
		var dialog = this.dialog, form = dialog.find('form'), _s = this;
		
		dialog.find('.createaccount-btn').click(function(e){
			e.preventDefault();
			//alert( dialog.find('input[id="vcode"]')[0].src );
			if (!$('#agreeif').attr('checked'))
			{
				alert('请先同意服务条款');
				return;
			}
			$.post('register.php', form.serialize(), function(o){
				if (o['code']==1)
				{
					form.html('<div class="pop-ok">'+o['msg']+'</div>');
					setTimeout(function(){
						location.reload();
						//_s.closeDialog();
					}, 2000);
				}else if (o['code']==6)
				{
					//验证码错误
					alert(o['msg']);
					$('#vcodeimg').attr('src','vcode.php?r='+Math.random());
				}else{
					alert(o['msg']);
					return;
				}
			},'json');
		});
	});
}

//药Q测试
function Pop_Qtest(url, data){
	new Dialog_Ajax(url, 'modpop3 pop-qtest', data, function(){
		var dialog = this.dialog, form = dialog.find('form'), _s = this;
		var submitBtn = dialog.find('.confirm-btn'), qtestItems = dialog.find('.qtest-items li'), length = qtestItems.length, counter = 0, countStr = dialog.find('.qtest-footer span');
		submitBtn.click(function(e){
			e.preventDefault();
			var radios = $(qtestItems[counter]).find('input[type="radio"]'), radioLen = radios.length, checkI = 0;
			radios.each(function(){
				if(!$(this).attr('checked')){
					checkI++;
				}
			});
			if(checkI === radioLen){
				alert('请选择一个答案!');
				return;
			}
			if(counter<length-1){
				$(qtestItems[counter]).addClass('nodisplay');
				$(qtestItems[++counter]).removeClass('nodisplay');
				countStr.html('共'+length+'题，这是第'+(counter+1)+'题，还有'+(length-counter-1)+'题');
			}else{
				var data = form.serialize();
				_s.closeDialog();
				
				new Dialog_Ajax('snippet-yq.php', 'modpop3 pop-qtest', data, function(){
					var dialog2 = this.dialog, _s2 = this;
					dialog2.find('.login-link').click(function(e){
						e.preventDefault();
						_s2.closeDialog();
						Pop_Login();
					});
					dialog2.find('.reg-link').click(function(e){
						e.preventDefault();
						_s2.closeDialog();
						Pop_Regist();
					});
					dialog2.find('.close-btn').click(function(e){
						e.preventDefault();
						_s2.closeDialog();
						location.reload();
					});
				});
			}
		});
	});	
}

//我要提问
function Pop_Question(data){
	new Dialog_Ajax('snippet-ask.php', 'modpop2 pop-question', data, function(){
		var dialog = this.dialog, form = dialog.find('form'), _s = this;
		dialog.find('.question-btn').click(function(e){
			e.preventDefault();
			$.post('ask_action.php', form.serialize(), function(o){
			if (o['code']==1)
			{
				form.html('<div class="pop-ok">'+o['msg']+'</div>');
				setTimeout(function(){
					location.reload();
					//_s.closeDialog();
				}, 2000);
			}else{
				alert(o['msg']);
				return;
			}
			},'json');
		});
		dialog.find('.login-link').click(function(e){
			e.preventDefault();
			$.post('ask_action.php', form.serialize(), function(o){
			if (o['code']==1)
			{
				form.html('<div class="pop-ok">'+o['msg']+'</div>');
				setTimeout(function(){
					location.reload();
					//_s.closeDialog();
				}, 2000);
			}else{
				alert(o['msg']);
				return;
			}
			},'json');
		});
		/*
		dialog.find('.login-link').click(function(e){
			e.preventDefault();
			//_s.closeDialog();
			//Pop_Login();
			$.post('login.php', form.serialize(), function(o){
				alert( o );
				return;
				form.html('<div class="pop-ok">'+o+'</div>');
				setTimeout(function(){_s.closeDialog();}, 2000);
				//location.reload();
			},'json');
		});
		*/
		dialog.find('.reg-link').click(function(e){
			e.preventDefault();
			_s.closeDialog();
			Pop_Regist();
		});
	});	
}

function chkReg()
{
	if (!$('#agreeif').attr('checked'))
	{
		alert('请先同意服务条款');
		return false;
	}
	
	return true;
}

function asAnonymous(obj)
{	
	if (obj.checked){
		$('#loginPanel').css('display','none');
	}else{
		$('#loginPanel').css('display','');
	}
}





function CheckForm()
{
if(document.getElementById("username").value == '')
{
alert("用户名不能为空！");
document.getElementById("username").focus();
return false;
}

var C = /^[a-zA-Z0-9_]+$/;

if(!C.test(document.getElementById("username").value))
{
alert("用户名应由英文，数字，下划线组成！");
document.getElementById("username").focus();
return false;
}



if(document.getElementById("password").value == '' || document.getElementById("password").value.length < 6 || document.getElementById("password").value.length > 30)
{
alert("密码长度应在6-30位之间！");
document.getElementById("password").focus();
return false;
}

if(document.getElementById("confirmPassword").value != document.getElementById("password").value)
{
alert("两次密码应保持一致！");
document.getElementById("confirmPassword").focus();
return false;
}

if(document.getElementById("cmsname").value == '')
{
alert("真实姓名不能为空！");
document.getElementById("cmsname").focus();
return false;
}

if(document.getElementById("bname").value == '')
{
alert("宝宝姓名不能为空！");
document.getElementById("bname").focus();
return false;
}

if(document.getElementById("cmstel").value.length != 11 )
{
alert("手机号码应为11位数字！");
document.getElementById("cmstel").focus();
return false;
}

if(document.getElementById("cmsaddr").value == '' )
{
alert("详细地址不能为空！");
document.getElementById("cmsaddr").focus();
return false;
}


if(document.getElementById("email").value == '' )
{
alert("邮件地址不能为空！");
document.getElementById("email").focus();
return false;
}

if(!document.getElementById("agreeif").checked )
{
alert("请先阅读服务条款！");
return false;
}
}



var PicUtil = {
	calPic:function(ori_w,ori_h,w,h){

		var targetW = ori_w;
		var targetH = ori_h;
		var oriRate = ori_h/ori_w;
		if ( (w/ori_w) > (h/ori_h) )
		{
			if ( ori_h > h )
			{
				
				targetH = h;
				targetW = targetH/oriRate;			
			}
		}else{
			if ( ori_w > w )
			{
				
				targetW = w;
				targetH = targetW*oriRate;			
			}
			
		}
		
		return new Array(targetW,targetH);
    	
    },
    
    resizePic:function(obj,w,h){
    	var wh = this.calPic(obj.width,obj.height,w,h);
    	obj.width = wh[0];
    	obj.height = wh[1];
    }
 }

