$(document).ready(function () {
	// preload images
	var wrapobj = function(selector){
		return (selector.wrap('<div></div>').parent().html());
	}

	var validatorloginform = $("#loginForm").validate();
	$("#loginForm").ajaxForm({
        beforeSubmit: function(){        	
        	if ($('#loginForm').valid()){
        		return true;
        	}
        	return false;
        },
        success: function(responseText, statusText){
			var msg = responseText; 
            if ("error:" == msg.substring(0, 6)) { 
                var fp = "<font color='red'>" + msg.substring(6, msg.length) + '</font><br><a href="forgetPwd.html">Forget Password?</a>'; 
                $("#loginBox div.error").html(fp).slideDown(3000,function(){$("#loginBox div.error").html(fp).slideUp()}); 
            } else if ("url:" == msg.substring(0, 4)) { 
	            if (msg.substring(4).indexOf('error')>=0){
					$("#loginBox div.error").html("错误用户名和密码,请重试").show();
					setTimeout('$("#loginBox div.error").hide()',3000);
	            }else{
	            	window.location="/";
					$.modal.close();
	            }
            } else if ("message:" == msg.substring(0, 8)) { 
                $("#loginBox div.error").html(msg.substring(8, msg.length)).show(); 
            } 
        }
	})
	$("#alogin").click(function (e){
		e.preventDefault();
		$("#loginBox").modal({
				overlayId: 'contact-overlay',
				opacity:30,
				persist : true,
				containerId: 'modalContainer',
				closeClass: 'simplemodal-close',
				onOpen: contact.open,
				onClose: contact.close
		});
	});
	$(".askquestion").click(function (e){
		e.preventDefault();
		$("#askquestioncontainer").modal({
				overlayId: 'contact-overlay',
				opacity:30,
				persist : true,
				containerId: 'modalContainer',
				closeClass: 'simplemodal-close',
				onOpen: function(dialog){
					dialog.overlay.fadeIn(200, function () {
						$("#askquestionform")[0].reset();
						$("#askquestionform .msg").hide();
						dialog.container.fadeIn(10, function () {
							dialog.data.fadeIn(10);
						});
					});
					
				}
		});
	});
	$("#askquestionform").validate({
		errorPlacement: function(error, element) {
				error.appendTo( element.parent("td").next("td").find("span.error"));
		},
		rules: {
			author : {
				minlength: 2
			},
			content: {
				required: true,
				minlength: 40
			}
		},
		messages: {
			author : {
				minlength: "请输入至少2个字符"
			},
			content: {
				minlength: "请输入至少20个汉字，以便于专家为您提供准确答案"
			}
		}
	})
	$("#askquestionform").ajaxForm({
		beforeSubmit: function(){        	
        	if ($('#askquestionform').valid()){
        		return true;
        	}
        	return false;
        },
        success: function(responseText, statusText){
        	if(responseText.indexOf("success")>=0){
                $("#askquestioncontainer .msg").show();
                $("#askquestioncontainer table.asktable").hide();
                setTimeout("$.modal.close()",4000);
			}
	      }
      })
	
});

var contact = {
			message: null,
			open: function (dialog) {
				// dynamically determine height
				var h = 180;
				var title = $('#modalContainer .contact-title').html();
				$("#modalContainer form")[0].reset();
				dialog.overlay.fadeIn(200, function () {
					$("#loginBoxChild1").show(function(){$('#loginBox .logtitle span').text('会员登录')});
					$("#phonevalidatebox").hide();
					$("#pwdbox").hide();
					$("#retrievepwdbox").hide();

					dialog.container.fadeIn(200, function () {
						dialog.data.fadeIn(200, function () {
						});
					});
				});
			},
			close: function (dialog) {
				dialog.data.fadeOut(200, function () {
					dialog.container.fadeOut(200, function () {
						dialog.overlay.fadeOut(200, function () {
							$.modal.close();
						});
					});
				});
		
			},
			error: function (xhr) {
				alert(xhr.statusText);
			}
			
		};
function babyBirth(){
	$('#birthday').val($('#month').val() + "/" + $('#day').val() + "/" +$('#year').val());
	var baby_year = parseInt($("#year").val());
	var baby_month = parseInt($("#month").val());
	var baby_day = parseInt($("#day").val());
	var babyDate = new Date();
	babyDate.setFullYear(baby_year,baby_month-1,baby_day);
	var age1 = new Date(); age1.setFullYear(2008,8,1)
	var age2 = new Date(); age2.setFullYear(2007,8,1)
	var age3 = new Date(); age3.setFullYear(2006,8,1)
	var age4 = new Date(); age4.setFullYear(2005,8,1)
	var age5 = new Date(); age5.setFullYear(2004,8,1)
	if(babyDate<age5){
		$("#productentrylink").attr("href","/product5.jsp");
	}else if(babyDate<age4){
		$("#productentrylink").attr("href","/product4.jsp");
	}else if(babyDate<age3){
		$("#productentrylink").attr("href","/product3.jsp");
	}else if(babyDate<age2){
		$("#productentrylink").attr("href","/product2.jsp");
	}else{
		$("#productentrylink").attr("href","/product1.jsp");
	}
}
function playmp3(target){
	window.open("play.jsp?songname=" + target, "mp3page", "height=600, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no");
}
function showlyrics(target){
	window.open("lyrics.jsp?songname=" + target, "lyrics", "height=400, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no");
}
function birthchange1(){
	$('#questiontitle').val("孩子生日:" + $('#year1').val() + "-" + $('#month1').val() );
}	
