var idgen = 0;
var ElementTextCache = {};
var tblParentCache = {};
var JumpEl;
var jumpcount = 0;

function searchpage(searchBoxEl) {
    var searchText = searchBoxEl.value.toLowerCase();
    var liEls = document.getElementsByTagName('li');
    var matchCount = 0;
    var allParentUls = {};
    var matchedParentUls = {};
    var matchEl;
    for(var i = 0;i < liEls.length;i++) {
	    var innerText;
  
        if (!ElementTextCache[liEls[i].id] || ElementTextCache[liEls[i].id] == "") {
            var AddedsearchText = liEls[i].getAttribute('title');
            innerText = liEls[i].innerHTML.replace(/\<[^\>]+\>/g,'').toLowerCase();
            if (AddedsearchText) { innerText += " " + AddedsearchText; }
            ElementTextCache[liEls[i].id] = innerText;
        } else {
            innerText = ElementTextCache[liEls[i].id];
        }


        var tblParent = getTblParent(liEls[i]);
        if (innerText.match(searchText)) {
            matchEl = liEls[i];
            matchCount++;
            

            liEls[i].className='searchshow';
            matchedParentUls[tblParent.id] = 1;
            allParentUls[tblParent.id] = tblParent;
        } else {
            allParentUls[tblParent.id] = tblParent;
            liEls[i].className='searchhide';
        }
    }

    for(var i in allParentUls) {
        if (matchedParentUls[i]) {
                allParentUls[i].className='searchshow';
        } else {
                allParentUls[i].className='searchhide';
        }
    }
}

function getTblParent(tagEl) {
    if (!tagEl.id) { 
        tagEl.id = 'idgen' + idgen++;
    }
    if (tblParentCache[tagEl.id]) { return tblParentCache[tagEl.id]; } 
    
    var thisEl = tagEl;
    while(thisEl.tagName != "TABLE" && thisEl.parentNode) {
        thisEl = thisEl.parentNode;
    }
    tblParentCache[tagEl.id] = thisEl;
    return thisEl;
}

function clearsearch() {
    var quickJumpEl = document.getElementById('quickjump');
    quickJumpEl.value='';
    searchpage(quickJumpEl);
    
}
function gosearch() {
    var lnkEls = jumpEl.getElementsByTagName('a');
    if (! lnkEls[0].name) {
        lnkEls[0].name = 'jump' + jumpcount++;
    }
    window.location.href='#' + lnkEls[0].name;
}

function print_like_button(url) {
	document.write('<table border="0"><tr><td><g:plusone size="medium"></g:plusone></td><td><div id="fb-root"></div><div class="fb-like" data-href="http://sieuthimayvanphong.com/'+url+'" data-send="false" data-layout="button_count" data-width="450" data-show-faces="false"></div></td></tr></table>'+"<script type='text/javascript'>(function() {var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true; po.src = 'https://apis.google.com/js/plusone.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s); })(); (function(d, s, id) { var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) {return;} js = d.createElement(s); js.id = id; js.src = '//connect.facebook.net/vi_VN/all.js#xfbml=1&appId=298382363526562'; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk'));</script>");
}
//-------------- Vote ------------
function vote(amnt) {
	for (i=1; i<6; i++) {
		obj_select=document.getElementById("stars"+i);
		obj_select.style.background="";
		obj_select.style.width="";
		obj_select.style.left="";
	}
	obj_select=document.getElementById("stars"+amnt);
	obj_select.style.background="url(template/alt_star.gif) left bottom";
	obj_select.style.width=(amnt*25)+"px";
	obj_select.style.left="0px";
	document.getElementById("review_vote").value=amnt;
}
function submitReview(formobj){
	product_id=formobj.product_id.value;
	review_name=formobj.review_name.value;
	review_capcha=formobj.review_capcha.value;
	review_content=formobj.review_content.value;
	review_vote=formobj.review_vote.value;
	$.ajax({
		type: "POST",
		url: "review.php",
		data: "product_id="+product_id+"&review_name="+review_name+"&review_capcha="+review_capcha+"&review_content="+review_content+"&review_vote="+review_vote,
		success: function(res){
			if (res.substr((res.length-1),1)!="\n") {
				formobj.review_name.disabled='disabled';
				formobj.review_capcha.disabled='disabled';
				formobj.review_content.value=review_sended;
				formobj.review_content.disabled='disabled';
				formobj.review_vote.value=0;
				formobj.review_submit.disabled="disabled";
			}
			alert(res);
		}
	});
	return false;
}
//-------------- Map ------------
	function showSelectLocation() {
		document.getElementById("selectLocation").style.display="block";
		setTimeout("hideSelectLocation()",60000);
	}
	function hideSelectLocation() {
		document.getElementById("selectLocation").style.display="none";
	}
	function selectLocation(l) {
		if (document.getElementById) {
			var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
		}
		if (x) {
			x.onreadystatechange = function() {
				if (x.readyState == 4 && x.status == 200) {
					window.location.reload();
				}
			}
		x.open("GET", "setLocation.php?local="+l, true);
		x.send(null);
		}
	}
