// JavaScript Document

var isIE		= (navigator.userAgent.toLowerCase().indexOf("msie") == -1 ? false : true);
var isIE6	= (navigator.userAgent.toLowerCase().indexOf("msie 6") == -1 ? false : true);
var isIE7	= (navigator.userAgent.toLowerCase().indexOf("msie 7") == -1 ? false : true);

function addCommas(nStr){
	nStr += ''; x = nStr.split('.');	x1 = x[0]; x2 = ""; x2 = x.length > 1 ? '.' + x[1] : ''; var rgx = /(\d+)(\d{3})/; while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + ',' + '$2'); }	return x1 + x2;
}

function addToCartButton(record_id, estore_id, estore_name, vatgia_verified, map_friend, direct_add_to_cart_link, baokim_payment, redirect){
	strReturn	= '<div class="add_to_cart_button">';
	if(vatgia_verified == 1) strReturn	= strReturn + '<div class="vatgia_verified">Hãy đặt hàng tại đây<br />để được đảm bảo an toàn <img class="estore_verified" src="/images/question.gif" style="cursor:pointer" /></div>';
	if(direct_add_to_cart_link != ""){
		strReturn	= strReturn + '<div><a href="' + direct_add_to_cart_link + '"><img src="/images/btn_order.gif" /></a></div>';
		strReturn	= strReturn + '<div class="end"><a href="/home/addfavorites.php?addto=product&record_id=' + record_id + '&redirect=' + redirect + '" rel="nofollow"><img src="/images/btn_order_favorites.gif" /></a></div>';
	}
	else{
		strReturn	= strReturn + '<div><a href="/home/addtocart.php?iPro=' + record_id + '&estore_id=' + estore_id + '&estore_name=' + estore_name + '&return=' + redirect + '"><img src="/images/btn_order.gif" /></a></div>';
		strReturn	= strReturn + '<div class="or">Hoặc</div>';
		strReturn	= strReturn + '<div>';
			if(map_friend !== null){
				strReturn	= strReturn + '<select id="friend_id"><option value="0">- Chọn người nhận hàng -</option>';
				$.each(map_friend, function(key, value){
					strReturn	= strReturn + '<option value="' + key + '">' + value + '</option>';
				});
				strReturn	= strReturn + '</select>';
			}
			strReturn	= strReturn + '<a href="javascript:;" onClick="window.location.href=\'/home/buynow.php?iPro=' + record_id + '&iUse=' + estore_id + '&iUf=\' + parseInt($(&quot;#friend_id&quot;).val())"><img src="/images/btn_order_1_click.gif" /></a>';
		strReturn	= strReturn + '</div>';
		strReturn	= strReturn + '<div' + (baokim_payment == 0 ? ' class="end"' : "") + '><a href="/home/addfavorites.php?addto=product&record_id=' + record_id + '&redirect=' + redirect + '" rel="nofollow"><img src="/images/btn_order_favorites.gif" /></a></div>';
		if(baokim_payment == 1) strReturn	= strReturn + '<div class="baokim_pay_now end">An toàn hơn qua Baokim.vn<br /><a href="/home/baokim_pay_now.php?iPro=' + record_id + '&iUse=' + estore_id + '" target="_blank"><img src="/images/btn_order_baokim.gif" /></a></div>';
	}
	strReturn	= strReturn + '</div>';
	return strReturn;
}

function baokimPayment(){
	return '<a title="Thanh toán với Baokim.vn" class="colorbox_iframe_baokim" href="/ajax_v2/load_news.php?iData=7964" target="_blank" rel="nofollow"><img align="absmiddle" src="/images/baokim_icon.gif" /></a>';
}

function change_tab(id_tab, num_data, id_show, id_data, dom){
	for(i=0; i<num_data; i++){
		$("#tab_" + i).attr("class", "normal");
	}
	$("#tab_" + id_tab).attr("class", "current");
	if(id_show != "" && id_data != ""){
		data = $("#" + id_data).html();
		$("#" + id_show).html(data);
	}
	if(dom == 1){
		$('.product_picture_thumbnail').tooltip({
			bodyHandler: function(){
				return this.rel;
			},
			track: true,
			showURL: false,
			extraClass: ''
		});
	}
}

function check_form_login(){
	frm	= document.form_login;
	if(frm.loginname.value == ""){alert("Bạn chưa nhập tên đăng nhập."); frm.loginname.focus(); return false;}
	if(frm.password.value == ""){alert("Bạn chưa nhập mật khẩu."); frm.password.focus(); return false;}
	frm.submit();
}

function close_teaser(id_text, id_nut_open, id_nut_close){
	document.getElementById(id_text).className			= "product_teaser product_teaser_close";
	document.getElementById(id_nut_close).style.display= "none";
	document.getElementById(id_nut_open).style.display	= "inline";
}

function deleteExclusivePicture(iPro, iGal, authen_code){
	if(confirm("Bạn có muốn xóa ảnh này không?")){
		$("#picture_" + iPro + "_" + iGal).load("/ajax_v2/delete_exclusive_picture.php?iPro=" + iPro + "&iGal=" + iGal + "&authen_code=" + authen_code);
	}
}

function formatCurrency(div_id, str_number){
	document.getElementById(div_id).innerHTML = addCommas(str_number);
}

function generateVote(numberstar){
	numberstar	= parseFloat(numberstar)
	var intStar	= parseInt(numberstar);
	//Kiểm tra sai số
	if (intStar < 0) { intStar = 0; numberstar = 0; }
	if (intStar > 10) { intStar = 10; numberstar = 10; }
	//Ghi star xịn ra
	for (i=1; i<=intStar; i++) document.write('<img src="' + theme_imagepath + 'star_1.gif" />');
	//Neu intStar!=numberstar thi them 0,5 vao va cong intStar them 1
	if (intStar!=numberstar){ document.write('<img src="' + theme_imagepath + 'star_2.gif" />'); intStar++;}
	//ghi ra so sao = 0 con lai 
	for (i=intStar+1; i<=10; i++) document.write('<img src="' + theme_imagepath + 'star_0.gif" />');
}

function getCookie(c_name){
	if(document.cookie.length>0){
		c_start=document.cookie.indexOf(c_name + "=");
		if(c_start!=-1){ 
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

function hoidap_reply_thankyou_point(point){
	rate_max = 5;
	for(i=1; i<=rate_max; i++){
		star_image = (i <= point) ? "/images/star_green.gif" : "/images/star_none.gif";
		document.write('<img align="absmiddle" src="' + star_image + '" />');
	}
}

function initColorBox(){
	$(".colorbox").colorbox({ maxWidth: "80%", maxHeight: "80%", current: "ảnh {current} / {total}" });
	$(".colorbox_iframe").colorbox({ iframe: true, width: "650px", height: "500px", overlayClose: false });
	$(".colorbox_iframe_baokim").colorbox({ iframe: true, width: "735px", height: "500px", overlayClose: false });
	$(".colorbox_iframe_upload").colorbox({ iframe: true, width: "800px", height: "545px" });
	$(".myflash").colorbox({inline:true, href:"#inline_flash"});
}

function notice_alert(type, url){
	str = '';
	switch(type){
		case "price":
			str = '<a class="notice_price" title="Báo sai giá sản phẩm của gian hàng này" href="#notice_price" rel="nofollow" lang="vi" onClick="if(confirm(\'Xin bạn vui lòng đăng nhập để sử dụng tính năng này !\\nBạn hãy click vào OK để đến trang đăng nhập.\')) window.location.href=\'/home/login.php?redirect=' + url + '\'">Báo sai giá</a>';
			break;
		case "stock":
			str = '<a class="notice_stock" title="Báo hết hàng tại cửa hàng này" href="#notice_stock" rel="nofollow" lang="vi" onClick="if(confirm(\'Xin bạn vui lòng đăng nhập để sử dụng tính năng này !\\nBạn hãy click vào OK để đến trang đăng nhập.\')) window.location.href=\'/home/login.php?redirect=' + url + '\'">Báo hết hàng</a>';
			break;
	}
	document.write(str);
}

function notice_product(type, pro_id, u_id, u_name){
	str = '';
	switch(type){
		case "price":
			str = '<a class="notice_price" title="Báo sai giá sản phẩm của gian hàng này" href="#notice_price" rel="nofollow" lang="vi" onclick="if (confirm(\'Bạn đã kiểm tra và chắc chắn giá tại cửa hàng ' + u_name + ' là sai?\')){load_data(\'/ajax/notice_price.php?iUse=' + u_id + '&iPro=' + pro_id + '\', \'div_notice_' + u_id + '\'); }">Báo sai giá</a>';
			break;
		case "stock":
			str = '<a class="notice_stock" title="Báo hết hàng tại cửa hàng này" href="#notice_stock" rel="nofollow" lang="vi" onclick="if (confirm(\'Bạn đã kiểm tra và chắc chắn sản phẩm cửa hàng ' + u_name + ' đã hết hàng?\')){load_data(\'/ajax/notice_stock.php?iUse=' + u_id + '&iPro=' + pro_id + '\', \'div_notice_' + u_id + '\'); }">Báo hết hàng</a>';
			break;
	}
	document.write(str);
}

function open_teaser(id_text, id_nut_open, id_nut_close){
	document.getElementById(id_text).className			= "product_teaser product_teaser_open";
	document.getElementById(id_nut_open).style.display	= "none";
	document.getElementById(id_nut_close).style.display= "inline";
}

function product_season(pro_id, type){
	document.write('<a title="Sản phẩm theo mùa" href="#season" rel="nofollow" style="cursor:pointer" onClick="window.open(\'/ajax/user_product_season.php?pro_id=' + pro_id + '\',\'\',\'width=600,height=500,menubar=0,resizable=1,scrollbars=1,statusbar=0,titlebar=0,toolbar=0\')"><img align="absmiddle" src="/images/icon_season_' + type + '_v2.gif" /> Theo mùa</a>');
}

function quick_search_center(user_path){
	frm		= document.center_search_form;
	keyword	= frm.keyword.value;
	price		= frm.price.value;
	price_to	= frm.price_to.value;
	
	url		= user_path + "&module=quicksearch";
	if(keyword != ""){
		while(keyword.indexOf('/') >= 0)	keyword = keyword.replace('/','');
		while(keyword.indexOf('\\') >= 0)keyword = keyword.replace('\\','');
		url	= url + "&keyword=" + encodeURI(keyword);
	}
	if(parseFloat(price) > 0) 	url	= url + "&price=" + price;
	if(parseFloat(price_to) > 0) url	= url + "&price_to=" + price_to;
	
	window.location.href = url;
}

function save_cookie(cookie_name, cookie_value){
	document.cookie = cookie_name + "=" + cookie_value;
}

function show_hide_content(id1, id2, height){
	ob1	= document.getElementById(id1);
	ob2	= document.getElementById(id2);
	if(ob1.style.height	== height){
		ob1.style.height	= "auto";
		ob2.innerHTML		= "Thu nhỏ";
		ob2.className		= "collapse";
	}
	else{
		ob1.style.height	= height;
		ob2.innerHTML		= "Mở rộng";
		ob2.className		= "expand";
	}
}

function showYM(nick, icontype, text, max_length, url){
	name	= (text.length <= max_length || max_length == 0) ? text : text.substr(0, max_length) + "...";
	document.write('<a title="' + text + '" class="text_link_v2" href="ymsgr:sendim?' + nick + '&m=Xin chao, toi muon hoi ve san pham ' + url + ' !" rel="nofollow"><img align="absmiddle" border="0" src="http://opi.yahoo.com/online?u=' + nick + '&m=g&t=' + icontype + '&l=us" alt="Y!M" />' + name + '</a>');
}

function showSkype(nick, text, max_length){
	name	= (text.length <= max_length || max_length == 0) ? text : text.substr(0, max_length) + "...";
	document.write('<a title="' + text + '" class="text_link_v2" href="skype:' + nick + '?call" rel="nofollow"><img align="absmiddle" border="0" src="/images/skype.gif" alt="Skype" />' + name + '</a>');
}

function tooltipBaokimPayNow(){
	$(".baokim_pay_now").tooltip({
		bodyHandler: function(){
			$("#tooltip").css("width", "300px");
			return '<div style="margin-bottom:5px">Bạn cần liên hệ với người bán trước khi thanh toán để chắc chắn về những thông tin sau:</div>' +
					 '- Giá chính xác của sản phẩm.<br />' +
					 '- Tình trạng hàng (Còn hàng/ Hết hàng).<br />' +
					 '- Phí vận chuyển đối với sản phẩm.<br />' +
					 '- Chi phí khác (nếu có).';
		},
		track: true,
		showURL: false,
		extraClass: ''
	});
}
