/* Jquery popup */
(function($){ 		  
	$.fn.popupWindow = function(instanceSettings){
		
		return this.each(function(){
		
		$(this).click(function(){
		
		$.fn.popupWindow.defaultSettings = {
			centerBrowser:0, // center window over browser window? {1 (YES) or 0 (NO)}. overrides top and left
			centerScreen:0, // center window over entire screen? {1 (YES) or 0 (NO)}. overrides top and left
			height:500, // sets the height in pixels of the window.
			left:0, // left position when the window appears.
			location:0, // determines whether the address bar is displayed {1 (YES) or 0 (NO)}.
			menubar:0, // determines whether the menu bar is displayed {1 (YES) or 0 (NO)}.
			resizable:0, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
			scrollbars:0, // determines whether scrollbars appear on the window {1 (YES) or 0 (NO)}.
			status:0, // whether a status line appears at the bottom of the window {1 (YES) or 0 (NO)}.
			width:500, // sets the width in pixels of the window.
			windowName:null, // name of window set from the name attribute of the element that invokes the click
			windowURL:null, // url used for the popup
			top:0, // top position when the window appears.
			toolbar:0 // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
		};
		
		settings = $.extend({}, $.fn.popupWindow.defaultSettings, instanceSettings || {});
		
		var windowFeatures =    'height=' + settings.height +
								',width=' + settings.width +
								',toolbar=' + settings.toolbar +
								',scrollbars=' + settings.scrollbars +
								',status=' + settings.status + 
								',resizable=' + settings.resizable +
								',location=' + settings.location +
								',menuBar=' + settings.menubar;

				settings.windowName = this.name || settings.windowName;
				settings.windowURL = this.href || settings.windowURL;
				var centeredY,centeredX;
			
				if(settings.centerBrowser){
						
					if ($.browser.msie) {//hacked together for IE browsers
						centeredY = (window.screenTop - 120) + ((((document.documentElement.clientHeight + 120)/2) - (settings.height/2)));
						centeredX = window.screenLeft + ((((document.body.offsetWidth + 20)/2) - (settings.width/2)));
					}else{
						centeredY = window.screenY + (((window.outerHeight/2) - (settings.height/2)));
						centeredX = window.screenX + (((window.outerWidth/2) - (settings.width/2)));
					}
					window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + centeredX +',top=' + centeredY).focus();
				}else if(settings.centerScreen){
					centeredY = (screen.height - settings.height)/2;
					centeredX = (screen.width - settings.width)/2;
					window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + centeredX +',top=' + centeredY).focus();
				}else{
					window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + settings.left +',top=' + settings.top).focus();	
				}
				return false;
			});
			
		});	
	};
})(jQuery);

function expand_category()
{
	document.getElementById("li_1").reset();
}

/*Load model list*/
function model_reload(object)
{
	// Clear options
	var popt = document.getElementById("model_id");
	for ( var i = popt.length-1; i>= 0; i-- )
	{
		popt.remove(i);
	}
	// Add option
	var data = object.options[object.selectedIndex].getAttribute("title");
	
	data = data.split("||");
	for ( var j = 0; j<  data.length; j++ )
	{
		if ( data[j] )
		{
			var opt = document.createElement("option");
			var item = data[j].split("|");
			opt.text = item[1];
			opt.value = item[0];

			if ( popt.getAttribute("defaultvalue") == item[0] )
			{
				opt.selected = "selected";
			}
			
			popt.options.add(opt);
		}
	}
}

function comment_reload(object)
{
	// Clear options
	var popt = document.getElementById("model");
	for ( var i = popt.length-1; i>= 0; i-- )
	{
		popt.remove(i);
	}
	// Add option
	var data = object.options[object.selectedIndex].getAttribute("title");
	data = data.split("||");

	for ( var j = 0; j<  data.length; j++ )
	{
		if ( data[j] )
		{
			var opt = document.createElement("option");
			var item = data[j].split("|");
			opt.text = item[1];
			opt.value = item[0];
			popt.options.add(opt);
		}
	}
}

function vsearch(theForm)
{
		if (theForm.keyword.value=='' || theForm.keyword.value==theForm.keyword.defaultValue)
		{
			alert("Để tìm kiếm vui lòng nhập vào Nội Dung.");
			theForm.keyword.focus();
			return false;
		 }

		if (theForm.keyword.value.length <=2)
		{
			alert("Từ khóa quá ngắn. Vui lòng nhập lại với từ khóa khác trên 2 kí tự.");
			theForm.keyword.focus();
			return false;
		}
}

function vsearch_advance(theForm)
{
		if (theForm.keyword.value!='' && theForm.keyword.value.length <=2)
		{
			alert("Từ khóa quá ngắn. Vui lòng nhập lại với từ khóa khác trên 2 kí tự.");
			theForm.keyword.focus();
			return false;
		}
}

function vcontact(theForm) 
{
	if (theForm.name.value=='' || theForm.name.value==theForm.name.defaultValue) 
	{
		alert("Bạn chưa nhập Họ Tên.");
		theForm.name.focus();
		return false;
	}
	if (theForm.email.value=='' || theForm.email.value==theForm.email.defaultValue)
	{
		alert("Vui lòng điền Email.");
		theForm.email.focus();
		return false;
	}
	
	if (theForm.email.value.indexOf('@', 0) == -1 || theForm.email.value.indexOf('.') == -1)
	{
		alert("Vui lòng điền đúng Email.");
		theForm.email.focus();
		return false;
	}
	
	if (theForm.title.value=='' || theForm.title.value==theForm.title.defaultValue) 
	{
		alert("Bạn chưa nhập Tiêu Đề.");
		theForm.title.focus();
		return false;
	}
	
	if (theForm.message.value=='' || theForm.message.value==theForm.message.defaultValue) 
	{
		alert("Bạn chưa nhập Nội Dung.");
		theForm.message.focus();
		return false;
	}
	
	return true;
}



function vcart(theForm) 
{
	if (theForm.cart_name.value=='' || theForm.cart_name.value==theForm.cart_name.defaultValue) 
	{
		alert("Bạn chưa nhập Họ Tên.");
		theForm.cart_name.focus();
		return false;
	}
	if (theForm.cart_add.value=='' || theForm.cart_add.value==theForm.cart_add.defaultValue) 
	{
		alert("Bạn chưa nhập địa chỉ giao hàng.");
		theForm.cart_add.focus();
		return false;
	}
	if (theForm.cart_add.value.length <=8) 
	{
		alert("Vui lòng \"nhập đúng\" địa chỉ giao hàng!");
		theForm.cart_add.focus();
		return false;
	}
	if (theForm.cart_phone.value=='' || theForm.cart_phone.value==theForm.cart_phone.defaultValue) 
	{
		alert("Bạn chưa nhập số điện thoại.");
		theForm.cart_phone.focus();
		return false;
	}
	if (isNaN(theForm.cart_phone.value) )
	{
		alert("Vui lòng nhập đúng số điện thoại!");
		theForm.cart_phone.focus();
		return false;
	}
	if (theForm.cart_add.value.length <=8) 
	{
		alert("Vui lòng nhập đúng số điện thoại!");
		theForm.cart_phone.focus();
		return false;
	}
	if (theForm.cart_email.value=='' || theForm.cart_email.value==theForm.cart_email.defaultValue )
	{
		alert("Vui lòng điền Email.");
		theForm.cart_email.focus();
		return false;
	}
	if (theForm.cart_email.value.indexOf('@', 0) == -1 || theForm.cart_email.value.indexOf('.') == -1)
	{
		alert("Vui lòng điền đúng Email!");
		theForm.cart_email.focus();
		return false;
	}
	
	return true;
}

// CART UPDATE
function chk_cart_upt(product_id)
{
	var the_form = document.getElementById("cart_step2_1");

	for ( var i = 0; i < the_form.length; i++ )
	{
		// Period
		if ( the_form[i].name == "count_"+product_id )
		{
			if (isNaN( the_form[i].value))
			{
				//UPDATE BTN
				alert("Vui lòng nhập số!");
				document.getElementById("cart_update").setAttribute("disabled","");		
				document.getElementById("cart_update").setAttribute("style","opacity: 0.4; font-weight: normal; color: rgb(0, 0, 0); cursor: default;");
				
				// NEXT
				document.getElementById("sub_mit").setAttribute("href",site_root_domain+"/gio-hang-cua-toi.html");
				document.getElementById("sub_mit").setAttribute("disabled","");		
				document.getElementById("sub_mit").setAttribute("style","opacity: 0.4; font-weight: normal; color: rgb(0, 0, 0); cursor: default;");
				
				return false;
			}
			if (document.getElementById("sp_"+product_id).value <=0)
			{
				
				alert("Số lượng phải lớn hơn 0");
				
				//UPDATE BTN
				document.getElementById("cart_update").setAttribute("disabled","disabled");		
				document.getElementById("cart_update").setAttribute("style","opacity: 0.4; font-weight: normal; color: rgb(0, 0, 0); cursor: default;");
				
				// NEXT
				document.getElementById("sub_mit").setAttribute("disabled","disabled");		
				document.getElementById("sub_mit").setAttribute("style","opacity: 0.4; font-weight: normal; color: rgb(0, 0, 0); cursor: default;");
				
				return false;
			}
			else
			{
				//UPDATE BTN
				document.getElementById("cart_update").removeAttribute("disabled");		
				document.getElementById("cart_update").removeAttribute("style");
				
				// NEXT
				document.getElementById("cart_steps2").setAttribute("href",site_root_domain+"/gio-hang-cua-toi/nhap-thong-tin.html");
				document.getElementById("cart_steps2").removeAttribute("disabled","");		
				document.getElementById("cart_steps2").removeAttribute("style");
			
			}
			
		}
	}
	return true;
	
}

function turnon_button()
{
	var the_form = document.getElementById("cart_step2_1");
	for ( var i = 0; i < the_form.length; i++ )
	{

				document.getElementById("cart_update").removeAttribute("disabled");		
				document.getElementById("cart_update").setAttribute("style","cursor: pointer;");
				
	}
	
	var the_form = document.getElementById("cart_step2_2");
	for ( var i = 0; i < the_form.length; i++ )
	{
				document.getElementById("sub_mit").removeAttribute("disabled");
				document.getElementById("sub_mit").setAttribute("style","cursor: pointer;");
	}
}

// Alert when add a product
function cart_messenger_add()
{
	var r=confirm("Đã thêm 1 sản phẩm vào giỏ hàng");
	
	if (r==true)
  	{
  		alert("Vào giỏ hàng");
  	}
	else
	{
  		alert("Tiếp tục mua hàng");
  	}
	
}

	//############################################
	// ADD CART
	//############################################
	function add_cart_once(product_id,product_name)
	{
		AjaxRequest.get(
		{
			'url': site_root_domain+'/gio-hang-cua-toi/them/'+product_id+'.html'
	
			,'onLoading':function(req)
			{

			}
	
			,'onSuccess':function(req)
			{
				// Popup choice
				$.confirm({
				'title'		: 'Bạn đã thêm sản phẩm vào giỏ hàng',
				'message'	: '<b>' + product_name +'</b>',
				'buttons'	: {
					'Vào giỏ hàng'	: {
						'class'	: 'gray',
						'action': function(){
							$(window.location).attr('href', site_root_domain+'/gio-hang-cua-toi.html');
						}
					},
					'Tiếp tục mua'	: {
						'class'	: 'blue',
						'action': function(){
							//$(window.location).attr('href', site_referer+'#topcart');
						}
					}
				}
				});

				// Insert cart HTML
				document.getElementById("topcart_content").innerHTML = req.responseText;
				
				open_cart();
			}
		}
		);
	}
	
	function check_cart(cartc)
	{
		if ( cartc > 0 )
		{
			document.getElementById("topcart").style.display = "block";	
		}
	}
	
	function open_cart()
	{
		document.getElementById("topcart").style.display = "block";	
	}


