/* - - - - - - - - - - - - - - - - - - - - - - -
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 timershow=false;
var curx=-200;
var cury=200;
var win_w=window.innerWidth ? window.innerWidth : document.body.offsetWidth;
var mid_w=win_w/2;

var http=createRequestObject();
var objectId = '';
var loadok = 0;
var interval = '';

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;
      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=0;}
else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}

settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=yes,directories=no,status=no,menubar=yes,toolbar=no,resizable=yes';
win=window.open(mypage,myname,settings);}

//============================================================

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 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 submitDoc(formName) { 
  var obj;
 
	if (obj=findObj(formName)!=null) 
	{
		findObj(formName).submit(); 
	}
	else 
	{
		alert('The form you are attempting to submit called \'' + formName + '\' couldn\'t be found. Please make sure the submitDoc function has the correct id and name.');
	}

}

function do_sendmail(f) {
	var re =/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,5})$/gi;
	email = f.f_email.value;
	if (email != '' && email.match(re)==null) {
		alert("Email bạn nhập không hợp lệ");
		f.f_email.focus();
		return false;
	}
		
	furl = 'act:tellfriend|f_email:'+email;
	sendReq('index.php', 'vnTRUST', furl,'ext_tellfriend');
	return false;
}

function do_maillist(f) {
	var re =/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,5})$/gi;
	email = f.f_emaillist.value;
	if (email != '' && email.match(re)==null) {
		alert("Email bạn nhập không hợp lệ");
		f.f_emaillist.focus();
		return false;
	}
	
	furl = 'act:tellfriend|sub:maillist|f_email:'+email;
	sendReq('index.php', 'vnTRUST', furl,'ext_maillist');
	return false;
}


function check_search (f){
	if (f.keyword.value.length <2 ) {
		alert(note_search);
		f.keyword.focus();
		return false;
	}
		return true;	
}

function show_faqs(f_id) {
	hideobj('list_faqs');
	furl = 'act:get_faqs|f_id:'+f_id;
	sendReq('index.php', 'vnTRUST', furl,'ext_display');
}

function send_faqs() {
	furl = 'act:do_sendfaqs';
	sendReq('index.php', 'vnTRUST', furl,'ext_display');
}
function do_sendfaqs(f) {
		var title = f.title.value;
		if (title == '') {
			alert('Vui long nhap tieu de ');
			f.title.focus();
			return false;
		}

		if (f.h_code.value != f.code.value) {
			alert('Ma bao ve khong dung ');
			f.code.focus();
			return false;
		}

	f_title = encodeURIComponent(f.title.value);
	furl = 'act:do_sendfaqs|ok:1|title:'+f_title+'|h_code:'+f.h_code.value+'|code:'+f.code.value;
	sendReq('index.php', 'vnTRUST', furl,'ext_display');
	return false;
}

//#################### NEWS ########################
function getNewsCat(cat_id,p) {
	key = 'cat_id,p';
	value = cat_id+','+p;
	url = 'ajax/_cat_news.php';	
	sendReq(url, key, value,'ext_cat_news_'+cat_id);
	//alert ("arr_cat_news = "+arr_cat_news);
}

function getNewsDetail(newsid) {
	key = 'newsid,';
	value = newsid+',';
	url = 'ajax/_news.php';
	sendReq(url, key, value,'ext_list_main');
}

//#################### PRODUCT ########################
function getInfoProduct(id) {
	key = 'iid,';
	value = id+',';
	url = 'ajax/_product.php';
	sendReq(url, key, value,'ct-main_page');
}

function showProduct (p) {
	furl = 'p:'+p;
	sendReq('ajax/_product.php', 'vnTRUST', furl,'extListPro');
}

//#################### PREVIEW SONG ########################
function play_radio(id) {
/*	key = 'id,type';
	value = id+',radio';
	url = 'ajax/_preview.php';
	sendReq(url, key, value,'ext_radio');*/
	furl = 'act:preview|iid:'+id+'|type:radio';	
	sendReq('index.php', 'vnTRUST', furl,'ext_preview');
}

function play_song(id) {
/*	key = 'id,';
	value = id+',';
	url = 'ajax/_preview.php';
	sendReq(url, key, value,'extPlay');*/
	furl = 'act:preview|iid:'+id;	
	sendReq('index.php', 'vnTRUST', furl,'ext_preview');
}

function list_top_song_gal(keyword,extra,p) {
	furl = 'act:preview|keyword:'+keyword+'|type:list_top|p:'+p+'|extra:'+extra;
	sendReq('index.php', 'vnTRUST', furl,'ext_list');
}

function play_song_gal(id) {
/*key = 'id,type';
	value = id+',gal';
	url = 'ajax/_preview.php';
	sendReq(url, key, value,'ext_preview');*/
	if ( getobj('box_stats_gallery') ) {
		hideobj('box_stats_gallery');	
	}	
	furl = 'act:preview|iid:'+id+'|type:gal';	
	sendReq('index.php', 'vnTRUST', furl,'ext_preview');
}

function play_clip(code) {
	furl = 'act:preview|code:'+code+'|type:clip';	
	sendReq('index.php', 'vnTRUST', furl,'ext_preview');	
}

function checkParent(id) {
	if(document.getElementById('cat'+id)) {
		document.getElementById('cat'+id).setAttribute('expanded',1);
	}
}

function getGallery(id) {
	furl = 'act:gallery|view:ajax|pic_id:'+id;	
	sendReq('index.php', 'vnTRUST', furl,'extGal');	
}

function getAllGallery(arrPic,dir_view,username){
	var image_set = new Array();
	tmpPic = arrPic.split(',');
	for(i=0;i<tmpPic.length;i++) {
		var temp = new Array();
		temp['caption'] = username;
		pic = dir_view+tmpPic[i];
		temp['url'] = pic;
		image_set.push(temp);
	}
	GB_showImageSet(image_set, 1);
}

function select_vote(num) {
	uservote=num;
	for (i=1;i<=5;i++) {
		objname='vote_'+i;
		var imgshow=(i<=num) ? 'star2.gif':'star0.gif';
		getobj(objname).src="images/"+imgshow;
	}
}

function setValueVote(vote) {
	getobj('num_vote').value = vote;
}

function do_vote(id){
	var vote = getobj('num_vote').value;
	key = 'iid,vote' ;
	value = id+','+vote;
	url = 'ajax/_do_vote.php';
	sendReq(url, key, value,'ext_vote_song');
}

function do_vote_song(id){
	var vote = getobj('num_vote').value;
	key = 'iid,vote,type' ;
	value = id+','+vote+',song';
	url = 'ajax/_do_vote.php';
	sendReq(url, key, value,'ext_vote_song');
}

function controlSearch () {
	type = document.getElementById('typeSearch').value;	
	if(type=="forum") {
		document.getElementById('f_search').action = "forum/search.php";
		keyword = document.getElementById('keyword').value;	
		document.getElementById('query').value = keyword;	
	}else if (type=="news") {
		document.getElementById('f_search').action = "?vnTRUST=act:news";
	}else {
		document.getElementById('f_search').action = "?vnTRUST=act:product";
	}
//	alert (document.getElementById('f_search').action);	
}

function doComment(page,id) {
	url = 'act:comment|page:'+page+'|iid:'+id;
	sendReq('index.php','vnTRUST', url, 'ext_comment');	
}

function doSendComment (f) {
//	alert('ssad');
	var re =/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,5})$/gi;
	var email = f.emailComment.value;
	var page = f.pageComment.value;
	var id = f.idComment.value;
	var content = f.contentComment.value;
	if (email != '' && email.match(re)==null) {
		alert("Please enter your email");
		f.emailComment.focus();
		return false;
	}
	
	var strpara = 'email='+email+'&page='+page+'&iid='+id+'&content='+encodeURIComponent(content);
	var url = 'index.php?vnTRUST=act:comment|sub:add';
	send_post(url, strpara,'ext_comment');
	return false;
}

function getBooking(f,date) {
	var check_date = date.split('/');	
	var c_year = parseInt(check_date[2]);
	var c_month = parseInt(check_date[1]);
	var c_day = parseInt(check_date[0]);
	
	var year = parseInt(getobj('selYear').value);
	var month = parseInt(getobj('selMonth').value);
	var day = parseInt(getobj('selDay').value);	
	if( year<c_year ) {
		alert ('Bạn không thể chọn năm nhỏ hơn năm hiện tại.');
		return false;
	}
	
	if(month < c_month && year<=c_year) {
		alert ('Bạn không thể chọn tháng nhỏ hơn tháng hiện tại.');
		return false;
	}
	
	if(day<c_day &&	month <= c_month && year<=c_year ) {
		alert ('Bạn không thể chọn ngày nhỏ hơn ngày hiện tại.');
		return false;
	}
	
/*	alert ('year = '+year);
	alert ('month = '+month);
	alert ('day = '+day);*/
	
	key = 'year,month,day' ;
	value = year+','+month+','+day;
	url = 'ajax/_get_perform.php';
	sendReq(url, key, value,'extShowPerform');
}

function showFeel (id){
/*	key = 'id,' ;
	value = id+',';
	url = 'ajax/_feel.php';
	sendReq(url, key, value,'ext_feel');*/
	furl = 'act:feel|iid:'+id;
	sendReq('index.php','vnTRUST', furl, 'ext_feel');
}

function getFeel (id){
/*	key = 'id,type' ;
	value = id+',view';
	url = 'ajax/_feel.php';
	sendReq(url, key, value,'ext_feel');*/
	furl = 'act:feel|iid:'+id+'|type:view';
	sendReq('index.php','vnTRUST', furl, 'ext_feel');
}

function addFeel(f, lang) {
	var fullname = f.fullname.value;
	var content = f.content.value;
	var id = f.song_id.value;
	if (fullname=='' || fullname=='Tên của bạn' || fullname=='Your name') {
		if(lang=='en') {
			alert("Please enter your name.");	
		}else {
			alert("Vui lòng nhập tên của bạn.");	
		}		
		f.fullname.focus();
		return false;
	}
	
	if (content=='') {
		if(lang=='en') {
			alert("Please enter your content.");	
		}else {
			alert("Vui lòng nhập nội dung bạn cần gửi.");
		}		
		f.content.focus();
		return false;
	}
	
	var strpara = 'iid='+id+'&fullname='+fullname+'&content='+encodeURIComponent(content);
	var url = 'index.php?vnTRUST=act:feel|do_submit:1';
	send_post(url, strpara,'ext_feel');
	return false;
}

function displayStats(type) {
	var divSong = "list_stats_song";
	var divSinger = "list_stats_singer";
	if(type=='song') {
		getobj(divSong).style.display="";
		getobj(divSinger).style.display="none";
	}else {
		getobj(divSong).style.display="none";
		getobj(divSinger).style.display="";
	}
}