﻿var http = createRequestObject();
var field = '';
		   var loadingText = "<center><img src='"+site+"/images/loading.gif'><br><b>Đang tải dữ liệu ...</b></center>";
           var RATE_OBJECT_IMG = ""+site+"/images/rate/full.gif";
           var RATE_OBJECT_IMG_HOVER = ""+site+"/images/rate/full.gif";
           var RATE_OBJECT_IMG_HALF = ""+site+"/images/rate/half.gif";
           var RATE_OBJECT_IMG_BG = ""+site+"/images/rate/none.gif";
function createRequestObject() {
	var xmlhttp;
	try { xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); }
	catch(e) {
    try { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
	catch(f) { xmlhttp=null; }
  }
  if(!xmlhttp&&typeof XMLHttpRequest!="undefined") {
	xmlhttp=new XMLHttpRequest();
  }
	return  xmlhttp;
}
function trim(a) {
	return a.replace(/^s*(S*(s+S+)*)s*$/, "$1");
}
function handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			response = http.responseText;
			field.innerHTML = response;
			field.scrollIntoView();
			if(!response) window.location.href = url;
		}
  	}
	catch(e){}
	finally{}
}
function str_replace(search, replace, str){
	var ra = replace instanceof Array, sa = str instanceof Array, l = (search = [].concat(search)).length, replace = [].concat(replace), i = (str = [].concat(str)).length;
	while(j = 0, i--)
		while(str[i] = str[i].split(search[j]).join(ra ? replace[j] || "" : replace[0]), ++j < l);
	return sa ? str : str[0];
}
//Tìm kiếm sản phẩm
function do_search() {
	kw = document.getElementById("keyword").value;
	kw = str_replace(' ', '-', kw);
	kw = str_replace('%20', '-', kw);
	if (!kw || kw=='Tên Sản Phẩm Cần Tìm...') alert('Bạn chưa nhập từ khóa');
	else {
		kw = encodeURIComponent(kw);
		window.location.href = site+'/?act=search/'+kw;	
	}
  return false;
}
//Tìm kiếm nâng cao
function quick_search() {
		kw = document.getElementById("keyword2").value;
		slCompany = document.getElementById("slCompany").value;
		slPrice = document.getElementById("slPrice").value;
		slToPrice = document.getElementById("slToPrice").value;
	    var intMinPrice = parseInt(slPrice);
	    var intMaxPrice = parseInt(slToPrice);
	    if (intMinPrice == -1) {
	        intMinPrice = 0;
		}
	    if (intMaxPrice == -1) {
	        intMaxPrice = 500000000;
		}
	    if (intMinPrice > intMaxPrice) {
	        alert("Vui lòng chọn giá từ nhỏ hơn giá đến!");
	        return;
	    }
	    try
	    {
	        if (slCompany == "-1" && slPrice == "-1" &&  slToPrice == "-1")
	        {
	            alert("Vui lòng chọn thông tin tìm kiếm!");
	            return;
	        }
	    }
	    catch (e)
	    {
	        return;
	    }
window.location.href = site+'/?act=advance_search/'+slCompany+'/'+slPrice+'-'+slToPrice;
}
//Thêm biểu tượng cảm xúc
function addText(elname, wrap1, wrap2) {
	if (document.selection) { // for IE 
		var str = document.selection.createRange().text;
		document.forms['add'].elements[elname].focus();
		var sel = document.selection.createRange();
		sel.text = wrap1 + str + wrap2;
		return;
	} else if ((typeof document.forms['add'].elements[elname].selectionStart) != 'undefined') { // for Mozilla
		var txtarea = document.forms['add'].elements[elname];
		var selLength = txtarea.textLength;
		var selStart = txtarea.selectionStart;
		var selEnd = txtarea.selectionEnd;
		var oldScrollTop = txtarea.scrollTop;
		var s1 = (txtarea.value).substring(0,selStart);
		var s2 = (txtarea.value).substring(selStart, selEnd)
		var s3 = (txtarea.value).substring(selEnd, selLength);
		txtarea.value = s1 + wrap1 + s2 + wrap2 + s3;
		txtarea.selectionStart = s1.length;
		txtarea.selectionEnd = s1.length + s2.length + wrap1.length + wrap2.length;
		txtarea.scrollTop = oldScrollTop;
		txtarea.focus();
		return;
	} else {
		insertText(elname, wrap1 + wrap2);
	}
}
//Đếm số ký tự trong phần đánh giá sản phẩm
var submitcount=0;
   function checkSubmit() {

      if (submitcount == 0)
      {
      submitcount++;
      document.Surv.submit();
      }
   }
function wordCounter(field, countfield, maxlimit) {
wordcounter=0;
for (x=0;x<field.value.length;x++) {
      if (field.value.charAt(x) == " " && field.value.charAt(x-1) != " ")  {wordcounter++}
      if (wordcounter > 250) {field.value = field.value.substring(0, x);}
      else {countfield.value = maxlimit - wordcounter;}
      }
   }
function textCounter(field, countfield, maxlimit) {
  if (field.value.length > maxlimit)
      {field.value = field.value.substring(0, maxlimit);}
      else
      {countfield.value = maxlimit - field.value.length;}
  }  
//Hiện danh sách đánh giá
function showComment(num,media_id,page) { 
	field = document.getElementById("comment_field");
	field.innerHTML = loadingText;
	http.open('POST',  site+'/index.php');
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = handleResponse;
    http.send('showcomment=1&num='+num+'&media_id='+media_id+'&page='+page); 
  return false; 
}
function post_comment() {
	product_id = encodeURIComponent(document.getElementById("product_id").value);
	num = encodeURIComponent(document.getElementById("num").value);
	sec_num = encodeURIComponent(document.getElementById("sec_num").value);
	comment_poster = encodeURIComponent(document.getElementById("comment_poster").value);
	comment_content = encodeURIComponent(document.getElementById("comment_content").value);
	try {
	    document.getElementById("comment_loading").innerHTML = loadingText;
		document.getElementById("comment_loading").style.display = "block";
		http.open('POST',  site+'/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
		http.onreadystatechange = function () {
										try {
											if((http.readyState == 4)&&(http.status == 200)){
												var response = http.responseText;
												if (response == 'OK') {
													product_id = encodeURIComponent(document.getElementById("product_id").value);
													num = encodeURIComponent(document.getElementById("num").value);
													showComment(num,product_id,1);
												}
												else document.getElementById("comment_loading").innerHTML = response;

											}
										}
										catch(e){}
										finally{}
									};
		http.send('comment=1&product_id='+product_id+'&num='+num+'&comment_poster='+comment_poster+'&sec_num='+sec_num+'&comment_content='+comment_content);
		document.getElementById("submit").disabled=true;document.getElementById("submit").value="Wait...";
	}
	catch(e){}
	finally{}
  return false;
}
//Bình chọn sản phẩm
function rating(product_id,star) {
	try {
		document.getElementById("rate_s").innerHTML = loadingText;
		document.getElementById("rate_s").style.display = "block";
		hide_rating_process();
		http.open('POST', site+'/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
				document.getElementById("rating_field").innerHTML = http.responseText;
			}
		}
		http.send('rating=1&product_id='+product_id+'&star='+star);
	}
	catch(e){}
	finally{}
	return false;
}


	// pre-fetch image
	(new Image()).src = RATE_OBJECT_IMG;
	(new Image()).src = RATE_OBJECT_IMG_HALF;
	(new Image()).src = RATE_OBJECT_IMG_BG;

	function show_star(starNum,rate_text) {
		remove_star();
		document.getElementById("rate_text_d").innerHTML = rate_text;
		full_star(starNum);
	}
	
	function full_star(starNum) {
		for (var i=0; i < starNum; i++)
			document.getElementById('star'+ (i+1)).src = RATE_OBJECT_IMG;
	}
	function remove_star() {
		for (var i=0; i < 5; i++)
			document.getElementById('star' + (i+1)).src = RATE_OBJECT_IMG_BG; // RATE_OBJECT_IMG_REMOVED;
	}
	function remove_all_star() {
		for (var i=0; i < 5; i++) document.getElementById('star' + (i+1)).src = RATE_OBJECT_IMG_BG; // RATE_OBJECT_IMG_REMOVED;
		document.getElementById("rate_text_d").innerHTML = 'Bình Chọn';
	}
	function show_rating_process() {
		if(document.getElementById("rating_process")) document.getElementById("rating_process").style.display = "block";
		if(document.getElementById("rate_s")) document.getElementById("rate_s").style.display = "none";
	}
	function hide_rating_process() {
		document.getElementById("rating_process").style.display = "none";
		if(document.getElementById("rate_s")) document.getElementById("rate_s").style.display = "block";
	}
//Hiện danh sách sản phẩm bằng ajax
function showProduct(num,page,number,apr,cat_id) { 
    field = document.getElementById("ajax_"+num);
	field.innerHTML = loadingText;
	http.open('POST', site+'/index.php');
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = handleResponse;
    http.send('showProduct=1&num='+num+'&page='+page+'&number='+number+'&apr='+apr+'&cat_id='+cat_id); 
  return false; 
}
function imposeMaxLength(Object, MaxLen)
{
  return (Object.value.length <= MaxLen);
}
//Open popup
function openWindow(filename, winname, width, height, feature) {
    var features, top, left;
    var reOpera = /opera/i;
    var winnameRequired = ((navigator.appName == "Netscape" && parseInt(navigator.appVersion) == 4) || reOpera.test(navigator.userAgent));

    left = (window.screen.width - width) / 2;
    top = (window.screen.height - height) / 2;
    if (feature == '')
        features = "width=" + width + ",height=" + height + ",top=" + top + ",left=" + left + ",status=0,location=0";
    else
        features = "width=" + width + ",height=" + height + ",top=" + top + ",left=" + left + "," + feature;
    //	if(!winnameRequired)	winname = "";
    newwindow = window.open(filename, winname, features);
    newwindow.focus();
}
//Thêm, xóa giỏ hàng
function Cart(add_id,remove_id,count) {
	try{
		document.getElementById("cart_field").innerHTML = loadingText;
		http.open('POST',  site+'/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function () {
										try {
											if((http.readyState == 4)&&(http.status == 200)){
												var response = http.responseText;
												document.getElementById("cart_field").innerHTML = response;
											}
										}
										catch(e){}
										finally{}
									}
		http.send('add2Cart=1&add_id='+add_id+'&remove_id='+remove_id+'&count='+count);
		alert("Đã thực hiện");
	}
	catch(e){}
	finally{}
}
function add2Cart(product_id,count) {
if(count=== undefined) count = 1;
else count = document.getElementById("quantity_field").value;
	Cart(product_id,0,count);
}
function removeFromCart(product_id,type) {
if(type=== undefined) type = 0;
	Cart(0,product_id,'');
if(type==1) window.location.reload( true );	
}
// Đăng nhập
function quick_login() {
	name = encodeURIComponent(document.getElementById("frmlogin").name.value);
	pwd = encodeURIComponent(document.getElementById("frmlogin").pwd.value);
	type = encodeURIComponent(document.getElementById("frmlogin").type.value);
	if(	name == "" ||	pwd == "")
		alert("Bạn chưa nhập đầy đủ thông tin");
	else {
		try{
			document.getElementById("login_loading").innerHTML = loadingText;
			document.getElementById("login_loading").style.display = "block";
			http.open('POST',  site+'/index.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			http.onreadystatechange = function() {
												try {
													if((http.readyState == 4)&&(http.status == 200)){
														document.getElementById("login_loading").style.display = "none";
														var response = http.responseText;
														if (response) {
															document.getElementById("login_loading").innerHTML = response;
															document.getElementById("login_loading").style.display = "block";
														}
														else {
														if(type==1)
														window.location.reload( true );
														else if(type==0)
														window.location.href = site;
														}
													}
												}
												catch(e){}
												finally{}
			}
			http.send('login=1&name='+name+'&pwd='+pwd);
			
		}
		catch(e){}
		finally{}
	}
	return false;
}
function logout() {
		try{
			http.open('POST',  site+'/index.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			http.onreadystatechange = function() { window.location.href = site;}
			http.send('logout=1');
		}
		catch(e){}
		finally{}
	return false;
}
function language(lang) {
		try{
			http.open('POST',  site+'/index.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			http.onreadystatechange = window.location.reload(true);
			http.send('language='+lang);
		}
		catch(e){}
		finally{}
	return false;
}
//Quên mật khẩu
function forgot() {
	email = encodeURIComponent(document.getElementById("frmforgot").email.value);
	name = encodeURIComponent(document.getElementById("frmforgot").username.value);
	if (email=="" && name=="")
	alert("Bạn cần nhập tên đăng nhập hoặc email đã đăng ký mới có thể lấy lại mật khẩu");
	else {
		try{
			document.getElementById("forgot_loading").innerHTML = loadingText;
			document.getElementById("forgot_loading").style.display = "block";
			http.open('POST',  site+'/index.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			http.onreadystatechange = function () {
											try {
												if((http.readyState == 4)&&(http.status == 200)){
												document.getElementById("forgot_loading").style.display = "none";
												var response = http.responseText;
													if (response) {
														document.getElementById("forgot_loading").innerHTML = response;
														document.getElementById("forgot_loading").style.display = "block";
													}
													else window.location.reload( false );
												}
											}
											catch(e){}
											finally{}
										}
			http.send('forgot=1&email='+email+'&username='+name);
		}
		catch(e){}
		finally{}
	}
	return false;
}
function register() {
	ok = false;
	name = encodeURIComponent(document.getElementById("username").value);
	fullname = encodeURIComponent(document.getElementById("firstname").value);
	pwd = encodeURIComponent(document.getElementById("password").value);
	email = encodeURIComponent(document.getElementById("email").value);
	sec_num = encodeURIComponent(document.getElementById("sec_num").value); 
	address = encodeURIComponent(document.getElementById("lastname").value);
	city = document.getElementById("city").value;
	s = document.getElementsByName("sex");
	if (s[0].checked) sex = s[0].value;
	if (s[1].checked) sex = s[1].value;
	
	try{
		document.getElementById("reg_loading").innerHTML = loadingText;
		document.getElementById("reg_loading").style.display = "block";
		http.open('POST',  site+'/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
											try {
												if((http.readyState == 4)&&(http.status == 200)){
													document.getElementById("reg_loading").style.display = "none";
													var response = http.responseText;
													if (response) {
														document.getElementById("reg_loading").innerHTML = response;
														document.getElementById("reg_loading").style.display = "block";
													}
													else {
														alert('Chúc mừng bạn đã đăng ký thành công');
														alert('Bạn có thể đăng nhập ngay bay giờ');
														window.location.href = site;
													}
													}
												}
												catch(e){}
												finally{}
											}
		http.send('reg=1&name='+name+'&fullname='+fullname+'&pwd='+pwd+'&city='+city+'&email='+email+'&sex='+sex+'&address='+address+'&sec_num='+sec_num);
				
		}
	catch(e){}
	finally{}
	return ok;
}
//Thay đổi thông tin
function edit_info() {
	fullname = encodeURIComponent(document.getElementById("u_fullname").value);
	email = encodeURIComponent(document.getElementById("u_email").value);
	oldpwd = encodeURIComponent(document.getElementById("u_oldpwd").value);
	newpwd_1 = encodeURIComponent(document.getElementById("u_newpwd_1").value);
	newpwd_2 = encodeURIComponent(document.getElementById("u_newpwd_2").value);
	address = encodeURIComponent(document.getElementById("address").value);
	city = document.getElementById("city").value;
	phone = encodeURIComponent(document.getElementById("phone").value); 
	s = document.getElementsByName("sex");
	if (s[0].checked) sex = s[0].value;
	if (s[1].checked) sex = s[1].value;
	if(	trim(email) == "" )
		alert("Bạn chưa nhập đầy đủ thông tin");
	else if (newpwd_1 != newpwd_2)
		alert("Xác nhận mật khẩu không đúng");
	else {
		try{
			document.getElementById("change_info_loading").innerHTML = loadingText;
			document.getElementById("change_info_loading").style.display = "block";
			http.open('POST',  site+'/index.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			http.onreadystatechange = function () {
											try {
												if((http.readyState == 4)&&(http.status == 200)){
													document.getElementById("change_info_loading").style.display = "none";
													var response = http.responseText;
													if (response) {
														document.getElementById("change_info_loading").innerHTML = response;
														document.getElementById("change_info_loading").style.display = "block";
													}
													else window.location.reload( false );
												}
											}
											catch(e){}
											finally{}
										}
			http.send('edit_info=1&fullname='+fullname+'&email='+email+'&phone='+phone+'&address='+address+'&city='+city+'&oldpwd='+oldpwd+'&newpwd='+newpwd_1+'&sex='+sex);
		}
		catch(e){}
		finally{}
	}
	return false;
}
function checkout() {
	ok = false;
	try{
		document.getElementById("checkout_loading").innerHTML = loadingText;
		document.getElementById("checkout_loading").style.display = "block";
		http.open('POST',  site+'/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
											try {
												if((http.readyState == 4)&&(http.status == 200)){
													document.getElementById("checkout_loading").style.display = "none";
													var response = http.responseText;
													if (response) {
														document.getElementById("checkout_loading").innerHTML = response;
														document.getElementById("checkout_loading").style.display = "block";
													}
													else {
														alert('Cảm ơn bạn đã mua hàng tại website');
														alert('Chúng tôi sẽ duyệt đơn đặt hàng của bạn trong thời gian sớm nhất');
														window.location.href = site;
													}
													}
												}
												catch(e){}
												finally{}
											}
		http.send('checkout=1');
				
		}
	catch(e){}
	finally{}
	return ok;
}
function contact() {
	ok = false;
	fullname = encodeURIComponent(document.getElementById("firstname").value);
	email = encodeURIComponent(document.getElementById("email").value);
	sec_num = encodeURIComponent(document.getElementById("sec_num").value); 
	address = encodeURIComponent(document.getElementById("lastname").value);
	phone = encodeURIComponent(document.getElementById("phone").value);
	msg = encodeURIComponent(document.getElementById("msg").value);
	try{
		document.getElementById("contact_loading").innerHTML = loadingText;
		document.getElementById("contact_loading").style.display = "block";
		http.open('POST',  site+'/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
											try {
												if((http.readyState == 4)&&(http.status == 200)){
													document.getElementById("contact_loading").style.display = "none";
													var response = http.responseText;
													if (response) {
														document.getElementById("contact_loading").innerHTML = response;
														document.getElementById("contact_loading").style.display = "block";
													}
													else {
														alert('Cảm ơn bạn đã gửi liên hệ tới chúng tôi');
														alert('Chúng tôi sẽ liên hệ lại với bạn trong thời gian sớm nhất');
														window.location.href = site;
													}
													}
												}
												catch(e){}
												finally{}
											}
		http.send('contact=1&name='+fullname+'&phone='+phone+'&address='+address+'&email='+email+'&msg='+msg+'&sec_num='+sec_num);
				
		}
	catch(e){}
	finally{}
	return ok;
}
//Hiện danh sách sản phẩm theo thể loại
function Pro_By_Cat(num,com_id,page) {
	field = document.getElementById("company_ajax_"+com_id);
	field.innerHTML = loadingText;
	http.open('POST',  site+'/index.php');
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = handleResponse;
    http.send('ProByCat=1&num='+num+'&com_id='+com_id+'&page='+page); 
  return false; 
}
