var _globle=this;

//验证登录部分
$(document).ready(function () {
	_globle.LoginPath=$('#loginZone').attr('action');
	_globle.ForgetPath=$('#formFG').attr('action');
	_globle.contactPath=$('#CUForm').attr('action');
	_globle.regPath=$('#RegForm').attr('action');
	_globle.ppPath=$('#PPForm').attr('action');
	_globle.cpPath=$('#formCp').attr('action');	
	_globle.gotoTime=3000;

	jQuery.validator.addMethod("stringCheck", function(value, element) {       
		return this.optional(element) || /^[\u0391-\uFFE5\w|\s]+$/.test(value);       
	}, "Only Chinese charactors, English words, numbers and underline allowed."); 

	//右侧登陆
	$('#loginZone').validate({
		rules: {
			username:"required",
			password:"required"
		},
		messages: {
			username: "Please enter your username.",
			password: "Please enter your password."
		},
		submitHandler:function () {
			var dataString='';
			$('#loginZone').find('input:text').each(function (i,n) {
				dataString=dataString+$(n).attr('name')+'='+$(n).val()+'&';
			})
			$('#loginZone').find('input:password').each(function (i,n) {
				dataString=dataString+$(n).attr('name')+'='+$(n).val()+'&';
			})
			$.ajax({
			  type: "post",
			  data: dataString,
			  url: _globle.LoginPath,
			  success:function (msg) {
				if (msg=='ok') {
				    $("#succInfo p").text("You have successful logged in.");
					tb_show('Congratulations!','#TB_inline?height=150&width=400&inlineId=succInfo&width=290&height=70',false);
					_globle.blur();
					setTimeout(function () {
						window.location.reload()
					},_globle.gotoTime);
				} else {
				    $("#errInfo p").text("Your username or password is not correct.");
					tb_show('Failure!','#TB_inline?height=150&width=400&inlineId=errInfo&width=290&height=70',false);
					_globle.blur();
				}
			  },
			  error:function () {
			        $("#errInfo p").text("An unknown error occurred!");
					tb_show('Error!','#TB_inline?height=150&width=400&inlineId=errInfo&width=290&height=70',false);
					_globle.blur();
			  }
			});
		}
	});

	//登陆页面验证
	$('#loginZone2').validate({
		rules: {
			username2:"required",
			password2:"required"
		},
		messages: {
			username: "Please enter your username.",
			password: "Please enter your password."
		},
		submitHandler:function () {
			var dataString='';
			$('#loginZone2').find('input:text').each(function (i,n) {
				dataString=dataString+$(n).attr('name')+'='+$(n).val()+'&';
			})
			$('#loginZone2').find('input:password').each(function (i,n) {
				dataString=dataString+$(n).attr('name')+'='+$(n).val()+'&';
			})
			$.ajax({
			  type: "post",
			  data: dataString,
			  url: _globle.LoginPath,
			  success:function (msg) {
				if (msg=='ok') {
				    $("#succInfo p").text("You have successful logged in.");
					tb_show('Congratulations!','#TB_inline?inlineId=succInfo&width=290&height=70',false);
					_globle.blur();
					setTimeout(function () {
						window.location.reload();
					},_globle.gotoTime);
				} else {
				    $("#errInfo p").text("Your username or password is not correct.");
					tb_show('Failure!','#TB_inline?inlineId=errInfo&width=290&height=70',false);
					_globle.blur();
				}
			  },
			  error:function () {
			        $("#errInfo p").text("An unknown error occurred!");
					tb_show('Error!','#TB_inline?inlineId=errInfo&width=290&height=70',false);
					_globle.blur();
			  }
			});
		}
	});

	$('#loginZone2 #btnLogin').click(function () {
		$('#loginZone2').submit();
	})

	//忘记密码模块验证
	$('#formFG').validate({
		submitHandler:function () {
			$.ajax({
			  type: "post",
			  data: "emailFg="+$('#emailFg').val(),
			  url: _globle.ForgetPath,
			  success:function (msg) {
				if (msg=='ok') {
					$('#TB_ajaxWindowTitle').html('Success!');
					$('#TB_ajaxContent').html("Information submitted successful, please login your mailbox to find your new password.");
					setTimeout(function () {
						window.location.reload()
					},_globle.gotoTime);
				} else {
					$('#TB_ajaxWindowTitle').html('Failure!');
					$('#TB_ajaxContent').html(msg);
					setTimeout(function () {
						window.location.reload()
					},_globle.gotoTime);
				}
			  },
			  error:function () {
				$('#TB_ajaxWindowTitle').html('Error!');
				$('#TB_ajaxContent').html("An unknown error occurred!");
				setTimeout(function () {
					window.location.reload()
				},_globle.gotoTime);
			  }
			});
		}
	});

	$('#btnFGSubmit').click(function () {
		$('#formFG').submit();
	})

	//修改密码模块验证
	$('#formCp').validate({
		submitHandler:function () {
			$.ajax({
			  type: "post",
			  data: "oldPassword="+$('#oldPassword').val()+"&newPassword="+$('#newPassword').val()+"&verifyPassword="+$('#verifyPassword').val(),
			  url: _globle.cpPath,
			  success:function (msg) {
				if (msg=='ok') {
					tb_show('Success!','#TB_inline?inlineId=none&width=290&height=70',false);
					$('#changePassword').css({'display':'none'});
					$('#succInfoCp').css({'display':'block'});
					$('#errInfoCp').css({'display':'none'});
					$('#succInfoCp p').html("The new password is effect. Please login...");
					_globle.blur();
					
				    setTimeout(function () {
					    window.location.reload()
				    },_globle.gotoTime);
				    
				} else {
					tb_show('Failure!','#TB_inline?inlineId=none&width=290&height=70',false);
					$('#changePassword').css({'display':'none'});
					$('#succInfoCp').css({'display':'none'});
					$('#errInfoCp').css({'display':'block'});
					$('#errInfoCp p').html(msg);
					_globle.blur();
				}
			  },
			  error:function () {
				tb_show('Failure!','#TB_inline?inlineId=none&width=290&height=70',false);
				$('#changePassword').css({'display':'none'});
				$('#succInfoCp').css({'display':'none'});
				$('#errInfoCp').css({'display':'block'});
				$('#errInfoCp p').html("An unknown error occurred!");
				_globle.blur();
			  }
			});
		}
	});
	$('.changePassBtn').click(function () {
		$('#changePassword').css({'display':'block'});
		$('#succInfoCp').css({'display':'none'});
		$('#errInfoCp').css({'display':'none'});
	})
	$('#btnCpSubmit').click(function () {
		$('#formCp').submit();
	})


	//验证联系我们
	$('#CUForm').validate({
		submitHandler:function () {
			var dataString='';
			$('#CUForm').find('input:text').each(function (i,n) {
				dataString=dataString+$(n).attr('name')+'='+$(n).val()+'&';
			})
			$('#CUForm').find('input:checked').each(function (i,n) {
				dataString=dataString+$(n).attr('name')+'='+$(n).val()+'&';
			})
			$('#CUForm').find('textarea').each(function (i,n) {
				dataString=dataString+$(n).attr('name')+'='+$(n).val()+'&';
			})
			$.ajax({
			  type: "post",
			  data: dataString,
			  url: _globle.contactPath,
			  success:function (msg) {
				if (msg=='ok') {
				    $("#succInfoCU p").text("Information submitted successful. Thanks to contact us. We will contact you soon.");
					tb_show('Success!','#TB_inline?inlineId=succInfoCU&width=290&height=70',false);
					_globle.blur();
				} else {
				    $("#errInfoCU p").text("Failure to submit your information. Please submit again.");
					tb_show('Failure!','#TB_inline?inlineId=errInfoCU&width=290&height=70',false);
					_globle.blur();
				}
			  },
			  error:function () {
			    $("#errInfoCU p").text("An unknown error occurred!");
				tb_show('Error!','#TB_inline?inlineId=errInfoCU&width=290&height=70',false);
				_globle.blur();
			  }
			});
		}
	});
	jQuery.validator.addClassRules("y-tel", {
	  required: true,
	  number: true
	});
	$('#btnCUForm').click(function () {
		$('#CUForm').submit();
	})

	//验证注册页面
	$('#RegForm').validate({
		submitHandler:function () {
			var dataString='';
			$('#RegForm').find('input:text').each(function (i,n) {
				dataString=dataString+$(n).attr('name')+'='+$(n).val()+'&';
			})
			$('#RegForm').find('input:checked').each(function (i,n) {
				dataString=dataString+$(n).attr('name')+'='+$(n).val()+'&';
			})
			$('#RegForm').find('input:password').each(function (i,n) {
				dataString=dataString+$(n).attr('name')+'='+$(n).val()+'&';
			})
			$.ajax({
			  type: "post",
			  data: dataString,
			  url: _globle.regPath,
			  success:function (msg) {
				if (msg=='ok') {
				    $("#succInfoReg p").text("Registration success! Thanks for your registration.");
					tb_show('Congratulations!','#TB_inline?inlineId=succInfoReg&width=290&height=70',false);
					_globle.blur();
					
				    setTimeout(function () {
					    window.location.reload()
				    },_globle.gotoTime);
				    
				} else {
				    $("#errInfoReg p").text(msg);
					tb_show('Failure!','#TB_inline?inlineId=errInfoReg&width=290&height=70',false);
					_globle.blur();
				}
			  },
			  error:function () {
			    $("#errInfoReg p").text("An unknown error occurred!");
				tb_show('Error!','#TB_inline?inlineId=errInfoReg&width=290&height=70',false);
				_globle.blur();
			  }
			});
		}
	});
	jQuery.validator.addClassRules("reg-tel", {
	  required: true,
	  number: true
	});
	jQuery.validator.addClassRules("reg-fax", {
	  required: true,
	  number: true
	});
	jQuery.validator.addClassRules("reg-zip", {
	  required: true,
	  number: true
	});
	  
	$('#btnRegForm').click(function () {
		$('#RegForm').submit();
		if ($('#agreement-1').next().attr('for')=='agreement-1')	{
			$('#agreement-1-info').parent().append($('#agreement-1').next());
		}
		if ($('#agreement-2').next().attr('for')=='agreement-2')	{
			$('#agreement-2-info').parent().append($('#agreement-2').next());
		}
	})

	//个人中心
		$('#PPForm').validate({
		submitHandler:function () {
			var dataString='';
			$('#PPForm').find('input:text').each(function (i,n) {
				dataString=dataString+$(n).attr('name')+'='+$(n).val()+'&';
			})
			$('#PPForm').find('input:password').each(function (i,n) {
				dataString=dataString+$(n).attr('name')+'='+$(n).val()+'&';
			})
			$.ajax({
			  type: "post",
			  data: dataString,
			  url: _globle.ppPath,
			  success:function (msg) {
				if (msg=='ok') {
				    $("#succInfoPP p").text("Information submitted successful.");
					tb_show('Success!','#TB_inline?inlineId=succInfoPP&width=290&height=70',false);
					_globle.blur();
				} else {
				    $("#errInfoPP p").text(msg);
					tb_show('Failure!','#TB_inline?inlineId=errInfoPP&width=290&height=70',false);
					_globle.blur();
				}
			  },
			  error:function () {
			    $("#errInfoPP p").text("An unknown error occurred!");
				tb_show('Error!','#TB_inline?inlineId=errInfoPP&width=290&height=70',false);
				_globle.blur();
			  }
			});
		}
	});

	  
	$('#btnPPForm').click(function () {
		$('#PPForm').submit();
	})
})


//tools 
$(document).ready(function () {

	//收藏
	$('.favorites').click(function () {
		if (window.sidebar)	{
			window.sidebar.addPanel($('title').html(),window.location.href,"");
		} else if (document.all) { 
			window.external.AddFavorite(window.location.href,$('title').html());
		} else {
			alert('You can Ctrl + D to Add to Favorites');
		} 
	})

	//打印
	$('.print').click(function () {
		$('.print-mod').html($('.print-source').html());
		if ($('.print-mod table').attr('border')==0) {
			$('.print-mod table').attr('border','1');
		}
		window.print();
	})

	//邮件
	$('.mail').attr('href','mailto:?subject='+'title'+'&body='+window.location.href);
})

//events
$(document).ready(function () {
	$('#btnShowPhoto').click(function () {
		$('.left-mod .container').css({'height':'auto'});
		var o=$(this);
		$('#showLayer').animate({height:$('#showLayer table').height()+'px'},1000,function () {
			o.css({'display':'none'});
		});
		return false;
	})
})

//初始news高度
$(document).ready(function () {
	if ($('.left-mod').height()<$('.right-mod').height()) {
		$('.left-mod .container').height($('.right-mod').height()-35);
	}
})

