/* - - - - - - - - - - - - - - - - - - - - - - -
Ajax Javascript
 - - - - - - - - - - - - - - - - - - - - - - - */
var ie45,ns6,ns4,dom;
if (navigator.appName=="Microsoft Internet Explorer") ie45=parseInt(navigator.appVersion)>=4;
else if (navigator.appName=="Netscape"){  ns6=parseInt(navigator.appVersion)>=5;  ns4=parseInt(navigator.appVersion)<5;}
dom=ie45 || ns6;

var http=createRequestObject();
var objectId = '';
var loadok = 0;

function createRequestObject(htmlObjectId){
    var obj;
    var browser = navigator.appName;
    
    if(browser == "Microsoft Internet Explorer"){
        obj = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else{
        obj = new XMLHttpRequest();
    }
    return obj;    
}

function sendReq(serverFileName, variableNames, variableValues,objId) {
	var paramString = '';
	
	objectId = objId;
	
	variableNames = variableNames.split(',');
	variableValues = variableValues.split(',');
	
	for(i=0; i<variableNames.length; i++) {
		paramString += variableNames[i]+'='+variableValues[i]+'&';
	}
	paramString = paramString.substring(0, (paramString.length-1));
			
	if (paramString.length == 0) {
	   	http.open('get', serverFileName);
	}
	else {
		http.open('get', serverFileName+'?'+paramString);
	}
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function handleResponse() {
	
	if (http.readyState == 4) {
		responseText = http.responseText;
		getobj(objectId).innerHTML = responseText;
    } else {
		image_loading =ROOT+'/images/loading.gif';	
		getobj(objectId).innerHTML = "<br><div align=center><img src='"+image_loading+"' border=0><br>&#272;ang l&#7845;y d&#7919; li&#7879;u ...</div>";
	}
		
}

function change_icon(imgDocID,url) {
document.images[imgDocID].src = url;
}  

function showhide(id) {
el = document.all ? document.all[id] :   dom ? document.getElementById(id) :   document.layers[id];
els = dom ? el.style : el;
  if (dom){
    if (els.display == "none") {
        els.display = "";
      } else {
        els.display = "none";
      }
    }
  else if (ns4){
    if (els.display == "show") {
        els.display = "hide";
      } else { 
      els.display = "show";
         }
  }
}

function getobj(id) {
el = document.all ? document.all[id] :   dom ? document.getElementById(id) :   document.layers[id];
return el;
}

function showobj(id) {
obj=getobj(id);
els = dom ? obj.style : obj;
 	if (dom){
	    els.display = "";
    } else if (ns4){
        els.display = "show";
  	}
}

function hideobj(id) {
obj=getobj(id);
els = dom ? obj.style : obj;
 	if (dom){
	    els.display = "none";
    } else if (ns4){
        els.display = "hide";
  	}
}

var win=null;
function NewWindow(mypage,myname,w,h,scroll,pos){
if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes';
win=window.open(mypage,myname,settings);}

function openPopUp(url, windowName, w, h, scrollbar) {
   var winl = (screen.width - w) / 2;
   var wint = (screen.height - h) / 2;
   winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scrollbar ;
   win = window.open(url, windowName, winprops);
   if (parseInt(navigator.appVersion) >= 4) { 
       	win.window.focus(); 
   } 
}

function is_num(event,f)
{
if (event.srcElement) {kc =  event.keyCode;} else {kc =  event.which;}
if ((kc < 47 || kc > 57) && kc != 8 && kc != 0) return false;
return true;
}


var horizontal_offset="9px" //horizontal offset of hint box from anchor link
/////No further editting needed
var vertical_offset="0" //horizontal offset of hint box from anchor link. No need to change.
var ie=document.all
var ns6=document.getElementById&&!document.all

function getposOffset(what, offsettype){
var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
var parentEl=what.offsetParent;
while (parentEl!=null){
totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}

function iecompattest(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function clearbrowseredge(obj, whichedge){
var edgeoffset=(whichedge=="rightedge")? parseInt(horizontal_offset)*-1 : parseInt(vertical_offset)*-1
if (whichedge=="rightedge"){
var windowedge=ie && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-30 : window.pageXOffset+window.innerWidth-40
dropmenuobj.contentmeasure=dropmenuobj.offsetWidth
if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
edgeoffset=dropmenuobj.contentmeasure+obj.offsetWidth+parseInt(horizontal_offset)
}
else{
var windowedge=ie && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
dropmenuobj.contentmeasure=dropmenuobj.offsetHeight
if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure)
edgeoffset=dropmenuobj.contentmeasure-obj.offsetHeight
}
return edgeoffset
}

function showhint(menucontents, obj, e, tipwidth){
if ((ie||ns6) && document.getElementById("hintbox")){
dropmenuobj=document.getElementById("hintbox")
dropmenuobj.innerHTML=menucontents
dropmenuobj.style.left=dropmenuobj.style.top=-500
if (tipwidth!=""){
dropmenuobj.widthobj=dropmenuobj.style
dropmenuobj.widthobj.width=tipwidth
}
dropmenuobj.x=getposOffset(obj, "left")
dropmenuobj.y=getposOffset(obj, "top")
dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+obj.offsetWidth+"px"
dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+"px"
dropmenuobj.style.visibility="visible"
obj.onmouseout=hidetip
}
}

function hidetip(e){
dropmenuobj.style.visibility="hidden"
dropmenuobj.style.left="-500px"
}

function createhintbox(){
var divblock=document.createElement("div")
divblock.setAttribute("id", "hintbox")
document.body.appendChild(divblock)
}

if (window.addEventListener)
window.addEventListener("load", createhintbox, false)
else if (window.attachEvent)
window.attachEvent("onload", createhintbox)
else if (document.getElementById)
window.onload=createhintbox


//Highlight form element- © Dynamic Drive (www.dynamicdrive.com)
//For full source code, 100's more DHTML scripts, and TOS,
//visit http://www.dynamicdrive.com

var highlightcolor= "FFFCE8";

var ns6=document.getElementById&&!document.all
var previous=''
var eventobj

//Regular expression to highlight only form elements
var intended=/INPUT|TEXTAREA|SELECT|OPTION/

//Function to check whether element clicked is form element
function checkel(which){
if (which.style&&intended.test(which.tagName)&&which.name != "Đong y" &&which.name != "Huy"){
if (ns6&&eventobj.nodeType==3)
eventobj=eventobj.parentNode.parentNode
return true
}
else
return false
}

//Function to highlight form element
function highlight(e){
eventobj=ns6? e.target : event.srcElement
if (previous!=''){
if (checkel(previous))
previous.style.backgroundColor=''
previous=eventobj
if (checkel(eventobj))
eventobj.style.backgroundColor=highlightcolor
}
else{
if (checkel(eventobj))

eventobj.style.backgroundColor=highlightcolor
previous=eventobj
}
}


function checkemail(email) {
	furl = 'act:check_email|email:'+email;
	sendReq('index.php', 'vnTRUST', furl,'msg-email_address');
}


/**
* Resize function for CP textareas
*
* @param	integer	If positive, size up, otherwise size down
* @param	string	ID of the textarea
*
* @return	boolean	false
*/
function resize_textarea(to, id)
{
	if (to < 0)
	{
		var rows = -5;
		var cols = -0;
	}
	else
	{
		var rows = 5;
		var cols = 0;
	}

	var textarea = getobj(id);
	if (typeof textarea.orig_rows == 'undefined')
	{
		textarea.orig_rows = textarea.rows;
		textarea.orig_cols = textarea.cols;
	}

	var newrows = textarea.rows + rows;
	var newcols = textarea.cols + cols;

	if (newrows >= textarea.orig_rows && newcols >= textarea.orig_cols)
	{
		textarea.rows = newrows;
		textarea.cols = newcols;
	}

	return false;
}

/****** Cokie ****/
function set_cookie(cookieName,cookieValue,nDays) 
{
 var today = new Date();
 var expire = new Date();
 expire.setTime(today.getTime() + 3600000*24*nDays);
 var curCookie = cookieName + "=" + escape(cookieValue) +
 				"; expires=" + expire.toGMTString()  
			
 document.cookie = curCookie;
}

function get_cookie(name) 
{
	var dc = document.cookie;
	var index = dc.indexOf(name + "=");
	if (index == -1) return '-1';
	index = dc.indexOf("=", index) + 1;
	var endstr = dc.indexOf(";", index);
	if (endstr == -1) endstr = dc.length;
	return unescape(dc.substring(index, endstr));
}


/*============ Load Ajax ===============*/
function show_favorites() {
//	key="c_favorites";
	value="act:ajax_favorites";
	url = ROOT+'/index.php';
	sendReq(url,"vnTRUST",value,'ext_favorites');
}

function setFavorites (fr){
	var name_count = fr.length;
	var num_check = 0;
	var aList = new Array;
	var msg="";
	for (i=0;i<name_count;i++){
		if (fr.elements[i].checked){
			p_id = fr.elements[i].value;
			num_check++;
			aList.push(p_id);
			msg+="\n";
			msg+=" San pham ID: "+p_id+" da them vao favorites";
		}
	}
		
	if (num_check==0)
		alert('Vui long chon san pham');
	else{	
		alert (msg) ;
	}
	
//	value = aList.join('_');
	str_favorites = aList.join('_');
	value="act:ajax_favorites|add:ok|c_favorites:"+str_favorites;
	url = ROOT+'/index.php';
	sendReq(url,"vnTRUST",value,'ext_favorites');
}


function DelFavorites (id){
	
	value="act:ajax_favorites|del:"+id;
	url = ROOT+'/index.php';
	sendReq(url,"vnTRUST",value,'ext_favorites'); 
}


function DelAllFavorites (){
	value="act:ajax_favorites|delall:ok";
	url = ROOT+'/index.php';
	sendReq(url,"vnTRUST",value,'ext_favorites'); 
}
	
function do_report(id) {
	key = 'p_id' ;
	value = id ;
	url = ROOT+'/ajax/_report.php';
	sendReq(url, key, value,'ext_report');
}

function show_discuss(p_id,p) {
	key='p_id,p';
	value = p_id+','+p;
	url = ROOT+'/ajax/_show_discuss.php';
	sendReq(url,key ,value,'ext_discuss');
}

function form_discuss(p_id) {
	key='p_id';
	value = p_id;
	url = ROOT+'/ajax/_do_discuss.php';
	sendReq(url,key ,value,'ext_discuss');
}

function do_discuss(f,p_id) {
	var re =/^[a-z][a-z0-9]+[@][a-z0-9]+([.][a-z]+)+$/gi;
	var email = f.email.value;
		if (email == '' ) {
			alert('Vui long nhap email ');
			f.email.focus();
			return false;
		}
		
		if (email != '' && email.match(re)==null) {
			alert('Email khong hop le ');
			f.email.focus();
			return false;
		}
		
	var uu_diem =f.uu_diem.value;
		if (f.uu_diem.value == '') {
			alert('Vui long nhap uu diem ');
			f.uu_diem.focus();
			return false;
		}

	var khuyet_diem =f.khuyet_diem.value;
		if (f.khuyet_diem.value == '') {
			alert('Vui long nhap khuyet diem ');
			f.khuyet_diem.focus();
			return false;
		}
	var votes =f.votes.value;
		if (f.votes.value == '') {
			alert('Vui long nhap diem ');
			f.votes.focus();
			return false;
		}


	uu_diem = encodeURIComponent(uu_diem);
	khuyet_diem = encodeURIComponent(khuyet_diem);
	
	key='p_id,email,uu_diem,khuyet_diem,votes,do_submit';
	value = p_id+','+email+','+uu_diem+','+khuyet_diem+','+votes+',1';
	url = ROOT+'/ajax/_do_discuss.php';
	sendReq(url,key ,value,'ext_discuss');
	return false;
}

function show_category (mem_id) {
	key='mem_id';
	value = mem_id;
	url = ROOT+'/ajax/_show_category.php';
	sendReq(url,key ,value,'ext_category');
	//ndkshow('ndkani');
}


function show_cart (session,store_id) {
	key='session,store_id';
	value = session+','+store_id;
	url = ROOT+'/ajax/_show_cart.php';
	sendReq(url,key ,value,'ext_cart');
	//ndkshow('ndkani');
}

function show_dang_trienlam () {
	key='';
	value ='' ;
	url = ROOT+'/ajax/show_dang_trienlam.php';
	sendReq(url,key ,value,'ext_showroom');
	getobj('tab_dang_trienlam').className = "tab_action";
	getobj('tab_sap_trienlam').className = "tab_normal";
	getobj('tab_da_trienlam').className = "tab_normal";
}

function show_sap_trienlam () {
	key='';
	value ='';
	url = ROOT+'/ajax/show_sap_trienlam.php';
	sendReq(url,key ,value,'ext_showroom');
	getobj('tab_dang_trienlam').className = "tab_normal";
	getobj('tab_sap_trienlam').className = "tab_action";
	getobj('tab_da_trienlam').className = "tab_normal";
}

function show_da_trienlam (mem_id) {
	key='';
	value = '';
	url = ROOT+'/ajax/show_da_trienlam.php';
	sendReq(url,key ,value,'ext_showroom');
	getobj('tab_dang_trienlam').className = "tab_normal";
	getobj('tab_sap_trienlam').className = "tab_normal";
	getobj('tab_da_trienlam').className = "tab_action";
}

function show_new_product () {
	key='';
	value ='' ;
	url = ROOT+'/ajax/_show_new_product.php';
	sendReq(url,key ,value,'ext_product');
	getobj('tab_new_product').className = "tab_action";
	getobj('tab_promote_pro').className = "tab_normal";
	getobj('tab_best_view_pro').className = "tab_normal";
}

function show_promote_pro () {
	key='';
	value ='';
	url = ROOT+'/ajax/_show_promote_pro.php';
	sendReq(url,key ,value,'ext_product');
	getobj('tab_new_product').className = "tab_normal";
	getobj('tab_promote_pro').className = "tab_action";
	getobj('tab_best_view_pro').className = "tab_normal";
}

function show_best_view (mem_id) {
	key='';
	value = '';
	url = ROOT+'/ajax/_show_best_view.php';
	sendReq(url,key ,value,'ext_product');
	getobj('tab_new_product').className = "tab_normal";
	getobj('tab_promote_pro').className = "tab_normal";
	getobj('tab_best_view_pro').className = "tab_action";
}
