$(function(){
	var currentIndex = 0;
    var DEMO; //函数对象
    var currentID = 0; //取得鼠标下方的对象ID
    var pictureID = 0; //索引ID
    $("#ifocus_piclist li").eq(0).show(); //默认
    autoScroll();
    $("#ifocus_btn li").hover(function() {
        StopScrolll();
        $("#ifocus_btn li").removeClass("current")//所有的li去掉当前的样式加上正常的样式
        $(this).addClass("current"); //而本身则加上当前的样式去掉正常的样式
        currentID = $(this).attr("id"); //取当前元素的ID
        pictureID = currentID.substring(currentID.length - 1); //取最后一个字符
        $("#ifocus_piclist li").eq(pictureID).fadeIn("slow"); //本身显示
        $("#ifocus_piclist li").not($("#ifocus_piclist li")[pictureID]).hide(); //除了自身别的全部隐藏
        $("#ifocus_tx li").hide();
        $("#ifocus_tx li").eq(pictureID).show();

    }, function() {
        //当鼠标离开对象的时候获得当前的对象的ID以便能在启动自动时与其同步
        currentID = $(this).attr("id"); //取当前元素的ID
        pictureID = currentID.substring(currentID.length - 1); //取最后一个字符
        currentIndex = pictureID;
        autoScroll();
    });
    //自动滚动
    function autoScroll() {
        $("#ifocus_btn li:last").removeClass("current");
        $("#ifocus_tx li:last").hide();
        $("#ifocus_btn li").eq(currentIndex).addClass("current");
        $("#ifocus_btn li").eq(currentIndex - 1).removeClass("current");
        $("#ifocus_tx li").eq(currentIndex).show();
        $("#ifocus_tx li").eq(currentIndex - 1).hide();
        $("#ifocus_piclist li").eq(currentIndex).fadeIn("slow");
        $("#ifocus_piclist li").eq(currentIndex - 1).hide();
        currentIndex++; currentIndex = currentIndex >= 4 ? 0 : currentIndex;
        DEMO = setTimeout(autoScroll, 2000);
    }
    function StopScrolll()//当鼠标移动到对象上面的时候停止自动滚动
    {
        clearTimeout(DEMO);
    }

	$("#login_email").focus();
	
	$("#reg_email").focus();
	
	$("#header_nav ul .normal_li").hover(function(){
		$(this).css('cursor','pointer');
		$(this).css('background','#4682B4');
	},function(){
		$(this).css('cursor','normal');
		$(this).css('background','#79A0BD');
	});
	
	$(":input").focus(function(){
		$(this).addClass('focus');
	}).blur(function(){
		$(this).removeClass('focus');
	});
	
	$("#checkout_remarkinfo_content textarea").focus(function(){
		$(this).css('background','#FFF');
	});
	
	
	$("#content_left ul li").hover(function(){
		$(this).css('cursor','pointer');
		$(this).css('background','#79A0BD');
	},function(){
		$(this).css('cursor','normal');
		$(this).css('background','#E7E7E7');
	});
	
	$("#btn_search,#bt_signin,#bt_email_reminder,#btn_reg,#bt_checkout_signin,#btn_chk_guest,#btn_addtocart,#img_confirm_order,#contactus_submit").hover(function(){
		$(this).css('cursor','pointer');
	},function(){
		$(this).css('cursor','normal');
	});
	
	$("#img_confirm_order").click(function(){
		$("#confirm_order_form").submit();
	});
	
	$("#btn_search").click(function(){
		$("#search_form_form").submit();
	});
	
	$("#login_email").bind('blur',function(){
		var search_str = /^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/;
	    var email_val = $("#login_email").val();
		if(!search_str.test(email_val)){
			$(".login_email_tip").show();
			$(".login_email_tip").html('<font color=red>Your E-mail Address is not formatted correctly.</font>');
		}else{
			$(".login_email_tip").hide();
			$(".login_email_tip").html('');
		}
	});
	
	$("#login_email").bind('focus',function(){
		$(".login_email_tip").hide();
	});
	
	$("#login_password").bind('blur',function(){
		if($(this).val().length < 5){
			$(".login_password_tip").show();
			$(".login_password_tip").html('<font color=red>Password must contain a minimum of 5 characters.</font>');
		}else{
			$(".login_password_tip").hide();
			$(".login_password_tip").html('');
		}
	});
	
	$("#login_password").bind('focus',function(){
		$(".login_password_tip").hide();
	});
	
	
	$("#checkout_login_email").bind('blur',function(){
		var search_str = /^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/;
	    var email_val = $("#checkout_login_email").val();
		if(!search_str.test(email_val)){
			$(".login_email_tip").show();
			$(".login_email_tip").html('<font color=red>Your E-mail Address is not formatted correctly.</font>');
		}else{
			$(".login_email_tip").hide();
			$(".login_email_tip").html('');
		}
	});
	
	$("#checkout_login_email").bind('focus',function(){
		$(".login_email_tip").hide();
	});
	
	$("#checkout_login_password").bind('blur',function(){
		if($(this).val().length < 5){
			$(".login_password_tip").show();
			$(".login_password_tip").html('<font color=red>Password must contain a minimum of 5 characters.</font>');
		}else{
			$(".login_password_tip").hide();
			$(".login_password_tip").html('');
		}
	});
	
	$("#checkout_login_password").bind('focus',function(){
		$(".login_password_tip").hide();
	});
	
	
	
	$("#retrieve_email").bind('blur',function(){
		var search_str = /^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/;
	    var email_val = $("#retrieve_email").val();
		if(!search_str.test(email_val)){
			$(".retrieve_email_tip").show();
			$(".retrieve_email_tip").html('<font color=red>Your E-mail Address is not formatted correctly.</font>');
		}else{
			$(".retrieve_email_tip").hide();
			$(".retrieve_email_tip").html('');
		}
	});
	
	$("#retrieve_email").bind('focus',function(){
		$(".retrieve_email_tip").hide();
	});
	
	$("#bt_email_reminder").click(function(){
		result = true;
		var search_str = /^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/;
	    var email_val = $("#retrieve_email").val();
		if(!search_str.test(email_val)){
			result = false;
			$(".retrieve_email_tip").show();
			$(".retrieve_email_tip").html('<font color=red>Your E-mail Address is not formatted correctly.</font>');
		}else{
			$(".retrieve_email_tip").hide();
			$(".retrieve_email_tip").html('');
		}
		
		if(result){
			email = $("#retrieve_email").val();
			$.post('retrieve_pwd.php',{ email:email },function(data){
				if(data == "success"){
					$(".retrieve_email_tip").show();
					$(".retrieve_email_tip").html('<font color=#7CCD7C>Your password has been sent to you  email, please check.</font>');
				}else if(data == "failure"){
					$(".retrieve_email_tip").show();
					$(".retrieve_email_tip").html('<font color=red>Error: We can not find this account.</font>');
				}
			});
		}
	});
	
	$("#bt_signin").click(function(){
		result = true;
		
		email = $("#reg_email").val();
	    $.get('check_register.php',{ email:email },function(data){
	    	if(data != "yes"){
	    		result = false;
	    		$("#reg_email_tip").show();
				$("#reg_email_tip").html('<font color=red>This E-mail Address can not used now,Please choose another one.</font>');
	    	}else{
	    		$("#reg_email_tip").hide();
				$("#reg_email_tip").html('');
	    	}
	    });
		
		var search_str = /^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/;
	    var email_val = $("#login_email").val();
	    if(!search_str.test(email_val)){
	    	result = false;
			$(".login_email_tip").show();
			$(".login_email_tip").html('<font color=red>Your E-mail Address is not formatted correctly.</font>');
		}else{
			$(".login_email_tip").hide();
			$(".login_email_tip").html('');
		}
	    
	    if($("#login_password").val().length < 5){
	    	result = false;
	    	$(".login_password_tip").show();
			$(".login_password_tip").html('<font color=red>Password must contain a minimum of 5 characters.</font>');
	    }else{
	    	$(".login_password_tip").hide();
			$(".login_password_tip").html('');
	    }
	    
	    if(result == true)
	    {
	    	email = email_val;
	    	password = $("#login_password").val();
	    	$.post("check_login.php",{ email:email,password:password },function(data){
	    		if(data == "error")
	    		{
	    			$(".login_password_tip").show();
	    			$(".login_password_tip").html('<font color=red>Your E-mail Address or Password is incorrect.</font>');
	    			$("#login_email").focus();
	    		}else{
	    			window.location.href = "myaccount.html";
	    		}
	    	});
	    }
	});
	
	
	$("#bt_checkout_signin").click(function(){
		result = true;	
		var search_str = /^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/;
	    var email_val = $("#checkout_login_email").val();
	    if(!search_str.test(email_val)){
	    	result = false;
			$(".login_email_tip").show();
			$(".login_email_tip").html('<font color=red>Your E-mail Address is not formatted correctly.</font>');
		}else{
			$(".login_email_tip").hide();
			$(".login_email_tip").html('');
		}
	    
	    if($("#checkout_login_password").val().length < 5){
	    	result = false;
	    	$(".login_password_tip").show();
			$(".login_password_tip").html('<font color=red>Password must contain a minimum of 5 characters.</font>');
	    }else{
	    	$(".login_password_tip").hide();
			$(".login_password_tip").html('');
	    }
	    
	    if(result == true)
	    {
	    	email = email_val;
	    	password = $("#checkout_login_password").val();
	    	$.post("check_login.php",{ email:email,password:password },function(data){
	    		if(data == "error")
	    		{
	    			$(".login_password_tip").show();
	    			$(".login_password_tip").html('<font color=red>Your E-mail Address or Password is incorrect.</font>');
	    			$("#checkout_login_email").focus();
	    		}else{
	    			window.location.href = "checkoutguest.html?email="+email;
	    		}
	    	});
	    }
	});

	

	$("#reg_email").bind('blur',function(){
		email = $("#reg_email").val();
	    $.get('check_register.php',{ email:email },function(data){
	    	if(data != "yes"){
	    		result = false;
	    		$("#reg_email_tip").show();
				$("#reg_email_tip").html('<font color=red>This E-mail Address can not used now,Please choose another one.</font>');
	    	}else{
	    		$("#reg_email_tip").hide();
				$("#reg_email_tip").html('');
	    	}
	    });
	});
	
	$("#btn_reg").click(function(){
		result = true;
		
		var search_str = /^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/;
	    var email_val = $("#reg_email").val();
	    if(!search_str.test(email_val)){
	    	result = false;
			$("#reg_email_tip").show();
			$("#reg_email_tip").html('<font color=red>Your E-mail Address is not formatted correctly.</font>');
		}else{
			$("#reg_email_tip").hide();
			$("#reg_email_tip").html('');
		}
	    
	    if($("#reg_password").val().length < 5){
	    	result = false;
	    	$("#reg_password_tip").show();
			$("#reg_password_tip").html('<font color=red>Password must contain a minimum of 5 characters.</font>');
	    }else{
	    	$("#reg_password_tip").hide();
			$("#reg_password_tip").html('');
	    }
	    
	    if($("#reg_conpassword").val() != $("#reg_password").val())
		{
			$("#reg_conpassword_tip").html("<font color=red>The Password Confirmation must match your Password.</font>");
			result = false;
		}else if($("#reg_conpassword").val().length < 5){
			$("#reg_conpassword_tip").html("<font color=red>Password must contain a minimum of 5 characters.</font>");
			result = false;
		}else if($("#reg_conpassword").val().length >= 5)
		{
			$("#reg_conpassword_tip").html("");
		}
	    
	    if($("#reg_fname").val().length < 2){
	    	result = false;
	    	$("#reg_fname_tip").show();
			$("#reg_fname_tip").html('<font color=red>First Name must contain a minimum of 2 characters.</font>');
	    }else{
	    	$("#reg_fname_tip").hide();
			$("#reg_fname_tip").html('');
	    }
	    
	    if($("#reg_lname").val().length < 2){
	    	result = false;
	    	$("#reg_lname_tip").show();
			$("#reg_lname_tip").html('<font color=red>Last Name must contain a minimum of 2 characters.</font>');
	    }else{
	    	$("#reg_lname_tip").hide();
			$("#reg_lname_tip").html('');
	    }
	    
	    if($("#reg_telephone").val().length < 2){
	    	result = false;
	    	$("#reg_telephone_tip").show();
			$("#reg_telephone_tip").html('<font color=red>Telephone must contain a minimum of 2 characters.</font>');
	    }else{
	    	$("#reg_telephone_tip").hide();
			$("#reg_telephone_tip").html('');
	    }
	    
	    if($("#reg_address").val().length < 5){
	    	result = false;
	    	$("#reg_address_tip").show();
			$("#reg_address_tip").html('<font color=red>Your Address must contain a minimum of 5 characters.</font>');
	    }else{
	    	$("#reg_address_tip").hide();
			$("#reg_address_tip").html('');
	    }
	    
	    if($("#reg_postcode").val().length < 2){
	    	result = false;
	    	$("#reg_postcode_tip").show();
			$("#reg_postcode_tip").html('<font color=red>Your Postcode must contain a minimum of 2 characters.</font>');
	    }else{
	    	$("#reg_postcode_tip").hide();
			$("#reg_postcode_tip").html('');
	    }
	    
	    if($("#reg_country").val() == ""){
	    	result = false;
	    	$("#reg_country_tip").show();
			$("#reg_country_tip").html('<font color=red>Your must choose a country.</font>');
	    }else{
	    	$("#reg_country_tip").hide();
			$("#reg_country_tip").html('');
	    }
	    
	    $.get('check_register.php',{ email:email_val },function(data){
	    	if(data != "yes"){
	    		result = false;
	    		$("#reg_email_tip").show();
				$("#reg_email_tip").html('<font color=red>This E-mail Address can not used now,Please choose another one.</font>');
	    	}else{
	    		$("#reg_email_tip").hide();
				$("#reg_email_tip").html('');
	    	}
	    });
	    
	    if(result)
	    {
	    	$("#register_form").submit();
	    }
	});
	
	
	
	$("#chk_email").bind('blur',function(){
		var search_str = /^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/;
	    var email_val = $("#chk_email").val();
		if(!search_str.test(email_val)){
			$("#chk_email_tip").show();
			$("#chk_email_tip").html('<font color=red>Your E-mail Address is not formatted correctly.</font>');
		}else{
			$("#chk_email_tip").hide();
			$("#chk_email_tip").html('');
		}
	});
	
	$("#chk_email").bind('focus',function(){
		$("#chk_email_tip").hide();
	});
	
	$("#chk_fname").bind('blur',function(){
		if($(this).val().length < 2){
			$("#chk_fname_tip").show();
			$("#chk_fname_tip").html('<font color=red>First Name must contain a minimum of 2 characters.</font>');
		}else{
			$("#chk_fname_tip").hide();
			$("#chk_fname_tip").html('');
		}
	});
	
	$("#chk_fname").bind('focus',function(){
		$("#chk_fname_tip").hide();
	});
	
	$("#chk_lname").bind('blur',function(){
		if($(this).val().length < 2){
			$("#chk_lname_tip").show();
			$("#chk_lname_tip").html('<font color=red>Last Name must contain a minimum of 2 characters.</font>');
		}else{
			$("#chk_lname_tip").hide();
			$("#chk_lname_tip").html('');
		}
	});
	
	$("#chk_lname").bind('focus',function(){
		$("#chk_lname_tip").hide();
	});
	
	
	$("#chk_telephone").bind('blur',function(){
		if($(this).val().length < 2){
			$("#chk_telephone_tip").show();
			$("#chk_telephone_tip").html('<font color=red>Telephone must contain a minimum of 2 characters.</font>');
		}else{
			$("#chk_telephone_tip").hide();
			$("#chk_telephone_tip").html('');
		}
	});
	
	$("#chk_telephone").bind('focus',function(){
		$("#chk_telephone_tip").hide();
	});
	
	
	$("#chk_address").bind('blur',function(){
		if($(this).val().length < 5){
			$("#chk_address_tip").show();
			$("#chk_address_tip").html('<font color=red>Your Address must contain a minimum of 5 characters.</font>');
		}else{
			$("#chk_address_tip").hide();
			$("#chk_address_tip").html('');
		}
	});
	
	$("#chk_address").bind('focus',function(){
		$("#chk_address_tip").hide();
	});
	
	
	$("#chk_postcode").bind('blur',function(){
		if($(this).val().length < 2){
			$("#chk_postcode_tip").show();
			$("#chk_postcode_tip").html('<font color=red>Your Postcode must contain a minimum of 2 characters.</font>');
		}else{
			$("#chk_postcode_tip").hide();
			$("#chk_postcode_tip").html('');
		}
	});
	
	$("#chk_postcode").bind('focus',function(){
		$("#chk_postcode_tip").hide();
	});
	
	$("#chk_country").bind('blur',function(){
		if($(this).val()== ""){
			$("#chk_country_tip").show();
			$("#chk_country_tip").html('<font color=red>Your must choose a country.</font>');
		}else{
			$("#chk_country_tip").hide();
			$("#chk_country_tip").html('');
		}
	});
	
	$("#chk_country").bind('focus',function(){
		$("#chk_country_tip").hide();
	});
	
	

	$("#btn_chk_guest").click(function(){
		result = true;
		
		var search_str = /^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/;
	    var email_val = $("#chk_email").val();
	    if(!search_str.test(email_val)){
	    	result = false;
			$("#chk_email_tip").show();
			$("#chk_email_tip").html('<font color=red>Your E-mail Address is not formatted correctly.</font>');
		}else{
			$("#chk_email_tip").hide();
			$("#chk_email_tip").html('');
		}
	    
	    if($("#chk_fname").val().length < 2){
	    	result = false;
	    	$("#chk_fname_tip").show();
			$("#chk_fname_tip").html('<font color=red>First Name must contain a minimum of 2 characters.</font>');
	    }else{
	    	$("#chk_fname_tip").hide();
			$("#chk_fname_tip").html('');
	    }
	    
	    if($("#chk_lname").val().length < 2){
	    	result = false;
	    	$("#chk_lname_tip").show();
			$("#chk_lname_tip").html('<font color=red>Last Name must contain a minimum of 2 characters.</font>');
	    }else{
	    	$("#chk_lname_tip").hide();
			$("#chk_lname_tip").html('');
	    }
	    
	    if($("#chk_telephone").val().length < 2){
	    	result = false;
	    	$("#chk_telephone_tip").show();
			$("#chk_telephone_tip").html('<font color=red>Telephone must contain a minimum of 2 characters.</font>');
	    }else{
	    	$("#chk_telephone_tip").hide();
			$("#chk_telephone_tip").html('');
	    }
	    
	    if($("#chk_address").val().length < 5){
	    	result = false;
	    	$("#chk_address_tip").show();
			$("#chk_address_tip").html('<font color=red>Your Address must contain a minimum of 5 characters.</font>');
	    }else{
	    	$("#chk_address_tip").hide();
			$("#chk_address_tip").html('');
	    }
	    
	    if($("#chk_postcode").val().length < 2){
	    	result = false;
	    	$("#chk_postcode_tip").show();
			$("#chk_postcode_tip").html('<font color=red>Your Postcode must contain a minimum of 2 characters.</font>');
	    }else{
	    	$("#chk_postcode_tip").hide();
			$("#chk_postcode_tip").html('');
	    }
	    
	    if($("#chk_country").val() == ""){
	    	result = false;
	    	$("#chk_country_tip").show();
			$("#chk_country_tip").html('<font color=red>Your must choose a country.</font>');
	    }else{
	    	$("#chk_country_tip").hide();
			$("#chk_country_tip").html('');
	    }
	    
	    if($(".rdo_payment:checked").val() == "Credit Card"){
	    	cardNO = $("#cardNO").val();
	    	cvv2 = $("#cvv2").val();
	    	cardExpireMonth = $("#cardExpireMonth").val();
	    	cardExpireYear = $("#cardExpireYear").val();
	    	issuingBank = $("#issuingBank").val();
	    	
	    	if(cardNO == "" || cvv2 == "" || cardExpireMonth == "" || cardExpireYear == "" || issuingBank == ""){
	    		result = false;
	    		$("#credit_tip").show();
	    	}else{
	    		$("#credit_tip").hide();
	    	}
	    	
	    }
	    
	    if(result)
	    {
	    	$("#checkout_guest_form").submit();
	    }
	});
	
	$("#btn_addtocart").click(function(){
		$("#form_addtocart").submit();
	});
	
	$(".rdo_payment").click(function(){
		if($(this).val() == "Credit Card"){
			$('.input_credit_card_info').show();
		}else{
			$('.input_credit_card_info').hide();
		}
	});
	

	$("#contactus_submit").click(function(){
		result = true;
		var search_str = /^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/;
	    var email_val = $("#c_email").val();
		if(!search_str.test(email_val))
		{
			$("#c_email_tip").html("<font color=red>Your E-mail Address is not formatted correctly.</font>");
			result = false;
		}else
		{
			$("#c_email_tip").html("");
		}
		
		if($("#fullname").val().length < 2)
		{
			$("#fullname_tip").html("<font color=red>FullName must contain a minimum of 2 characters.</font>");
			result = false;
		}else if($("#fullname").val().length >= 2)
		{
			$("#fullname_tip").html("");
		}
		
		if($("#subject").val().length < 2)
		{
			$("#subject_tip").html("<font color=red>Subject must contain a minimum of 2 characters.</font>");
			result = false;
		}else if($("#subject").val().length >= 2)
		{
			$("#subject_tip").html("");
		}
		
		if($("#question").val().length < 10)
		{
			$("#question_tip").html("<font color=red>Question must contain a minimum of 10 characters.</font>");
			result = false;
		}else if($("#question").val().length >= 10)
		{
			$("#question_tip").html("");
		}
		
		if($("#c_captcha").val().length != 4)
		{
			$("#c_captcha_tip").html("<font color=red>Captcha must contain 4 characters.</font>");
			result = false;
		}else
		{
			$("#c_captcha_tip").html("");
		}
		
		if(result == true)
		{
			$("#contactus_form").submit();
		}
	});
	
	$("#shipping_option_content_left :radio").click(function(){
		if($("#shipping_ems").attr('checked') == true){
			$("#shipping_cost_total").html("Free Shipping");
		}
		
		if($("#shipping_dhl").attr('checked') == true){
			$("#shipping_cost_total").html("$&nbsp;40");
		}
		
	});
	
	$("#cart_country").bind('change',function(){
		country = $(this).val();
		$.post('check_country.php',{country:country},function(data){
			if(data == "yes"){
				$("#payment_tip").hide();
				$("#shipping_ems_radio").show();
				$("#shipping_ems").attr('checked',true);
				$("#shipping_cost_total").html("Free Shipping");
			}else if(data == "no"){
				$("#payment_tip").show();
				$("#shipping_dhl").attr('checked',true);
				$("#shipping_ems_radio").hide();
				$("#shipping_cost_total").html("$&nbsp;40");
			}
		});
	});
	
	$("#cart_submit_img").hover(function(){
		$(this).css('cursor','pointer');
	},function(){
		$(this).css('cursor','normal');
	});
	
	$("#cart_submit_img").click(function(){
		country = $("#cart_country").val();
		method = $("#shipping_option_content_left input:checked").val();
		location.href="checkoutlogin.html?country="+country+"&method="+method;
	});
	
});
