// Popup
$(document).ready(function() { 
 
    //select all the a tag with name equal to modal
    $('a[name=modal]').click(function(e) {
		if($(this).attr("onclick") != "undefined")
		{
			eval($(this).attr("onclick"));
		}
		
        //Cancel the link behavior
        e.preventDefault();
        //Get the A tag
        var id = $(this).attr('href');
     
        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();
     
        //Set height and width to mask to fill up the whole screen
        $('#mask').css({'width':maskWidth,'height':maskHeight});
         
        //transition effect    
        $('#mask').fadeIn(100);   
        $('#mask').fadeTo("slow",0.8); 
     
        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();
               
        //Set the popup window to center
        $(id).css('top',  winH/2-$(id).height()/2);
        $(id).css('left', winW/2-$(id).width()/2);
     
        //transition effect
        $(id).fadeIn(500);
     
    });
     
    //if close button is clicked
    $('.window .close').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();
        $('#mask, .window').hide();
    });    
     
    //if mask is clicked
    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
    });        
     
});

function autoload()
{
	$("#auto_popup").click();
}

$(function(){
	$("a[rel='menu']").hover(
		function(){
			$('ul[id^="subs_"]').hide();
			$("#subs_" + $(this).attr("id")).fadeIn(300);
		},
		function(){
			//$('ul[id^="subs_"]').hide();
		}
	);
});

function gotoSlide(slideno, ele)
{
	$(".button").removeClass("active");
	$(ele).addClass("active");
	$('#banner_center_box').cycle(slideno);
}
var FIRST_OFF = true;
$('#banner_center_box').cycle({ 
    fx:    'fade',
    speed:  800,
	delay: 2000,
	startingSlide: 0,
	before: function(curSlide, nextSlide, options){
		if(FIRST_OFF)
		{
			FIRST_OFF = false;
			return;
		}
		var index = options.nextSlide;
		$(".button").removeClass("active");
		$("#button" + index).addClass("active");
	}
 });

function product_search(){
	if($("#search_box").val()=="Search Products.."){$("#search_box").val("")}
	window.location = "/keyword-" + $("#search_box").val() + ".html";
}

function enter(event)
{
	if(event.code)
	{
		if(event.code==13){product_search()}
	}
	if(event.which)
	{
		if(event.which==13){product_search()}
	}
}

function enter2(event)
{
	if(event.code)
	{
		if(event.code==13){good_search('',1)}
	}
	if(event.which)
	{
		if(event.which==13){good_search('',1)}
	}
}

function enter3(event)
{
	if(event.code)
	{
		if(event.code==13){online_search(1)}
	}
	if(event.which)
	{
		if(event.which==13){online_search(1)}
	}
}

function show_maker(ele, page)
{
	var sort_maker = $("#sort").val();
	var category = $("#category").val();
	var makerproductid = $("#makerproductid").val();
	var status = $("#status").val();
	var status2 = "";
	switch(ele)
	{
		case "hot":
		status2 = "hot";
		break;
		case "new":
		status2 = "new";
		break;
	}
	window.location =  "/trang-thai-hang-dien-thoai-" + category + "-" + makerproductid + "-sap-xep-gia-" + sort_maker + "-" + status + "-" + status2 + "-" + page + ".html";
}

function good_search(ele, page)
{
	var action = $("#action").val();
	var category = $("#category").val();
	var toprice = $("#toprice").val();
	var fromprice = $("#fromprice").val();
	var makerproductid = $("#makerproductid").val();
	var productname = $("#productname").val();
	var sort_index = $("#sort").val();
	var status = $("#status").val();
	var status2 = "";
	switch(ele)
	{
		case "hot":
		status2 = "hot";
		break;
		case "new":
		status2 = "new";
		break;
	}
	window.location = "/tim-kiem-" + category + "-gia-" + fromprice + "-" + toprice + "-hang-" + makerproductid + "-ten-" + productname + "-sap-xep-gia-" + sort_index + "-" + status + "-" + status2 + "-" + page + ".html";
}

function online_search(page)
{
	var action = $("#action").val();
	var category = $("#category").val();
	var toprice = $("#toprice").val();
	var fromprice = $("#fromprice").val();
	var makerproductid = $("#makerproductid").val();
	var productname = $("#productname").val();
	window.location = "/online-tim-kiem-" + category + "-gia-" + fromprice + "-" + toprice + "-hang-" + makerproductid + "-ten-" + productname + "-" + page + ".html";
}

function print_()
{
	//document.getElementById("print2").innerHTML=document.getElementById("print").innerHTML;
	window.print();
}

function kiemtra3()
{
	var regex = RegExp("^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$");
	var loga = "";
	if($("#email").val()=="")
		loga = "Chưa nhập Email người nhận !";
	else if(!regex.test($("#email").val()))
		loga = "Định dạng email không hợp lệ !";
	
	if(loga == "")
		return true;
		
	alert(loga);
	return false;
}

function kiemtra_login()
{
	if($("#username").val()=="" || $("#password").val()=="") return false;
	return true;
}

function kiemtra_pass()
{
	if($("#password").val()=="" || $("#password1").val()=="" || $("#password2").val()=="")
	{
		return false;
	}
	else if($("#password1").val().length < 6)
	{
		alert("Mật khẩu phải trên 5 kí tự !");
		return false;
	}
	else if($("#password1").val() != $("#password2").val())
	{
		alert("Nhập lại mật khẩu mới chưa chính xác !");
		return false;
	}
	
	return true;
}

function kiemtra_emailw()
{
	var regex = RegExp("^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$");
	if($("#emailw").val()=="")
	{
		return false;
	}
	else if(!regex.test($("#emailw").val()))
	{
		alert("Định dạng email không hợp lệ !");
		return false;
	}
	
	return true;
}

function rand_code()
{
	$.get("/ajax/random.php",
		 function(response)
		 {
			 $("#showcode").html(response);
			 $("#code").val(response);
		 });
}
