/* - - - - - - - - - - - - - - - - - - - - - - -
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 send_post(strurl, parameters,objId) {      
  objectId = objId;
  //lert (parameters);
  //alert (parameters);
  http.open('POST', strurl, true);
  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http.setRequestHeader("Content-length", parameters.length);
  http.setRequestHeader("Connection", "close");
  http.onreadystatechange = handleResponse;
  http.send(parameters);
}

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 {
		getobj(objectId).innerHTML = "<br><div align=center><img src='images/loading.gif' 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";
  	}
}

// khong the phong to cua so
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(); 
   } 
}

// co the phong to cua so
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 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;
}

function findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function log_out(theURL){
		ht = document.getElementsByTagName("body");
		ht[0].style.filter = "progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)";
		if (confirm('Do you want logout ? '))
		{
			
			ht[0].style.filter = "";
			window.location.href=theURL;
			return true;
		}
		else
		{
			ht[0].style.filter = "";
			return false;
		}
}

function do_mailist(f) {
	furl = 'act:mail_list|f_email:'+f.f_email.value;
	sendReq('index.php', 'vnTRUST', furl,'ext_maillist');
	return false;
}

function do_sendmail(f) {
	furl = 'act:tellfriend|f_email:'+f.f_email.value;
	sendReq('index.php', 'vnTRUST', furl,'ext_tellfriend');
	return false;
}

function setFavorites(id,mem_id,action,lang){
	key='id,mem_id,action,lang';
	value = id+','+mem_id+','+action+','+lang;
	url = 'ajax/_favourite.php';
	sendReq(url,key ,value,'ext_favorite');
}

function select_vote(num) {
	var uservote=num;
	for (i=1;i<=5;i++) {
		objname='vote_'+i;
		var imgshow=(i<=num) ? 'soc2.gif':'soc0.gif';
		getobj(objname).src="images/"+imgshow;
	}
	
	getobj('hvote').value=num;
}

function do_vote(id,vote){
	key='id,vote';
	value = id+','+vote;
	url = 'ajax/_do_vote.php';
	sendReq(url,key ,value,'ext_vote');
	getobj('div_vote').innerHTML = "<span class='font_err'>C&#225;m &#417;n b&#7841;n &#272;&#227; b&#236;nh ch&#7885;n</span>";
}

function load_vote(id){
	key='id,lang';
	value = id+',vn';
	url = 'ajax/_load_vote.php';
	sendReq(url,key ,value,'ext_vote');
}


function do_report(id,mem_id,lang) {
	key = 'id,mem_id,lang' ;
	value = id+','+mem_id+','+lang ;
	url = 'ajax/_report.php';
	sendReq(url, key, value,'ext_report');
}

function show_discuss(id,mem_id,p,lang) {
		
	key='id,mem_id,p,lang';
	value = id+','+mem_id+','+p+','+lang;
	url = 'ajax/_show_discuss.php';
	sendReq(url,key ,value,'ext_discuss');
}

function form_discuss(id,mem_id,lang) {
	key='id,mem_id,lang';
	value = id+','+mem_id+','+lang;
	url ='ajax/_do_discuss.php';
	sendReq(url,key ,value,'ext_discuss');
}

function do_discuss(f,id,mem_id,lang) {

	var vote =f.hvote.value;
		if (f.hvote.value ==0) {
			alert('Vui long cho diem danh gia ');
			return false;
		}
	var content =f.content.value;
		if (f.content.value == '') {
			alert('Vui long nhap noi dung ');
			f.content.focus();
			return false;
		}
		
	content = encodeURIComponent(content);
	
/*	key='id,mem_id,lang,vote,content,do_submit';
	value = id+','+mem_id+','+lang+','+vote+','+content+',1';
	
	url = 'ajax/_do_discuss.php';
	sendReq(url,key ,value,'ext_discuss');
*/	
	var strpara = 'vote='+vote+'&content='+content;
	furl = 'ajax/_do_discuss.php?id='+id+'&mem_id='+mem_id+'&lang='+lang+'&do_submit=1';
	send_post(furl,strpara,'ext_discuss');
	
	getobj('ext_vote').innerHTML = '<a href=\'javascript:load_vote('+id+')\'>Xem k&#7871;t qu&#7843; m&#7899;i</a>';
	
	return false;
}

function show_contact(p_id,mem_id,lang) {
	key='id,mem_id,lang';
	value = p_id+','+mem_id+','+lang;
	url ='ajax/_do_contact.php';
	sendReq(url,key ,value,'ext_contact');
}

function do_contact(f,p_id,mem_id,lang) {
	

	var re =/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,5})$/gi;
	
	var username =f.username.value;
	if (f.username.value == '') 	{
		alert('Vui long nhap ten cua ban');
		f.username.focus();
		return false;
	}
	
	var your_email =f.your_email.value;
	if (your_email == '') 	{
		alert('Vui long nhap email cua ban');
		f.your_email.focus();
		return false;
	}
		
	if (f.your_email.value != '' && f.your_email.value.match(re)==null) {
		alert('Email cua ban ko hop le ');
		f.your_email.focus();
		return false;
	}
	
	var subject =f.subject.value;
	if (f.subject.value == '') {
		alert('Vui long nhap tieu de ');
		f.subject.focus();
		return false;
	}

	var content =f.content.value;
	if (f.content.value == '') {
		alert('Vui long nhap noi dung ');
		f.content.focus();
		return false;
	}
	
	username = encodeURIComponent(username);
	your_email = encodeURIComponent(your_email);
	subject = encodeURIComponent(subject);
	content = encodeURIComponent(content);
	
/*	key='id,mem_id,lang,username,your_email,subject,content,do_submit';
	value = p_id+','+mem_id+','+lang+','+f.username.value+','+f.your_email.value+','+subject+','+content+',1';
	url = 'ajax/_do_contact.php';
	sendReq(url,key ,value,'ext_contact');
*/	
	var strpara = 'username='+username+'&your_email='+your_email+'&subject='+subject+'&content='+content;
	
	furl = 'ajax/_do_contact.php?id='+p_id+'&mem_id='+mem_id+'&lang='+lang+'&do_submit=1';
	send_post(furl,strpara,'ext_contact');
	
	return false;
}

/*	JS for shopping */	


function rowOverEffect(object) {
  if (object.className == 'moduleRow') object.className = 'moduleRowOver';
}

function rowOutEffect(object) {
  if (object.className == 'moduleRowOver') object.className = 'moduleRow';
}
