function ajaxFunction()
{
	var xmlHttp;
	try
  	{
  		// Firefox, Opera 8.0+, Safari
  		xmlHttp=new XMLHttpRequest();
  	}
	catch (e)
  	{
  		// Internet Explorer
  		try
    	{
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	}
  		catch (e)
    	{
    		try
      		{
      			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      		}
    		catch (e)
      		{
      			alert("Your browser does not support AJAX!");
      			return false;
      		}
    	}
  	}
	return xmlHttp;
}




function onLyric(id,aid,search)
{
	var url = search + 'lyric.php?' + "&aid=" + aid + "&sid=" + id;
	var xmlHttp = ajaxFunction();
	xmlHttp.onreadystatechange=function()
    {
    	if(xmlHttp.readyState==4)
      	{
			if(xmlHttp.responseText == 'Fail')
			{
				//alert ("error.");	
			}else
			{
				text = xmlHttp.responseText;
				var array_text = text.split("---");
				document.getElementById('songName').innerHTML=array_text[0];
				document.getElementById('artistName').innerHTML=array_text[1];
				document.getElementById('albumName').innerHTML=array_text[2];
				document.getElementById('lyric').innerHTML=array_text[3];
			}
      	}
    }
  	
	xmlHttp.open("GET",url,true);
  	xmlHttp.send(null);
}


function showPic(file,sid,aid,search){
	var url = search + 'showPicture.php?'  + "&sid=" + sid + "&aid=" + aid;
	var xmlHttp = ajaxFunction();
	xmlHttp.onreadystatechange=function()
    {
    	if(xmlHttp.readyState==4)
      	{
			if(xmlHttp.responseText == 'Fail')
			{
				//alert ("error.");	
			}else
			{
				var text = xmlHttp.responseText;
					var array_text = text.split("---");
					if( file=="bedroom" ){
						document.getElementById('imgArtist').src = array_text[1];
						document.getElementById('artistProfile').innerHTML = array_text[2]+"<br>"+array_text[6];
						document.getElementById('songName').innerHTML = array_text[3];
						document.getElementById('sid').value = sid;
						document.getElementById('aid').value = aid;
						document.getElementById('arid').href = "index.php?option=com_artist_profile&arid="+array_text[8];
						document.getElementById('download').href = "index.php?option=com_music&task=payment&aid="+aid+"&sid="+sid;
						document.getElementById('artistName').innerHTML = "<a href='index.php?option=com_artist_profile&arid="+array_text[8]+"'>"+array_text[2]+"</a>";
						document.getElementById('albumName').innerHTML = array_text[4];
					}
					else if( file=="fatspecialize") {
						document.getElementById('imgAlbum').src = array_text[0];
						document.getElementById('artistName').innerHTML = "<a href='index.php?option=com_artist_profile&arid="+array_text[8]+"'>"+array_text[2]+"</a>";
						document.getElementById('albumName').innerHTML = array_text[4];
					}
					else if(file=="fat40"){ // file fat 40
						document.getElementById('imgAlbum').src = array_text[0];
						document.getElementById('songProfile').innerHTML = array_text[7];
						document.getElementById('songName').innerHTML = array_text[3];
						document.getElementById('arid').href = "index.php?option=com_artist_profile&arid="+array_text[8];
						document.getElementById('download').href = "index.php?option=com_music&task=payment&aid="+aid+"&sid="+sid;
						document.getElementById('artistName').innerHTML = "<a href='index.php?option=com_artist_profile&arid="+array_text[8]+"'>"+array_text[2]+"</a>";
						document.getElementById('albumName').innerHTML = array_text[4];
					}
					else if(file=="playlist") { // popup play list
						document.getElementById('imgAlbum').src = array_text[0];
						document.getElementById('songName').innerHTML = array_text[3];
						document.getElementById('artistName').innerHTML = array_text[2];
						document.getElementById('albumName').innerHTML = array_text[4];
					}
					//document.getElementById('albumName').innerHTML = array_text[2];
			}
      	}
    }
  	
	xmlHttp.open("GET",url,true);
  	xmlHttp.send(null);
}

function showSongPic(file,sid,search){

	var url = search + 'showSongPic.php?'  + "sid=" + sid;
	var xmlHttp = ajaxFunction();
	xmlHttp.onreadystatechange=function()
    {
    	if(xmlHttp.readyState==4)
      	{
			if(xmlHttp.responseText == 'Fail')
			{
				//alert ("error.");	
			}else
			{
				//alert (t.responseText);
				var text = xmlHttp.responseText;
				var array_text = text.split("---");
				document.getElementById('imgSong').src = array_text[0];
				document.getElementById('artistName').innerHTML = array_text[1];
			}
      	}
    }
  	
	xmlHttp.open("GET",url,true);
  	xmlHttp.send(null);
}

function play(indexPlay){
	if (indexPlay!=-1) { 
	var p = window.document.mp3player2;
	p.SetVariable("indexPlay", indexPlay);
	}
}
function play2(index){
	if (index!=-1) { 
	var p2 = window.document.mp3player;
	p2.SetVariable("indexPlay", index);
	}
}
/*
function getAlbumAndProfile(aid,search){
	
	var url = search + 'albumProfile.php?'  + "aid=" + aid;
	var xmlHttp = ajaxFunction();
	xmlHttp.onreadystatechange=function()
    {
    	if(xmlHttp.readyState==4)
      	{
			if(xmlHttp.responseText == 'Fail')
			{
				alert ("error.");	
			}else
			{
				//alert (t.responseText);
				var text = xmlHttp.responseText;
				var array_text = text.split("---");
				//document.getElementById('imgAlbum1').src = array_text[0];
				document.getElementById('profileAlbum').innerHTML =  array_text[1]+"<br>"+array_text[2];
			}
      	}
    }
  	
	xmlHttp.open("GET",url,true);
  	xmlHttp.send(null);
}
*//*
function getArtistProfile(aid,search){
	
	var url = search + 'artistProfile.php?'  + "aid=" + aid;
	var xmlHttp = ajaxFunction();
	xmlHttp.onreadystatechange=function()
    {
    	if(xmlHttp.readyState==4)
      	{
			if(xmlHttp.responseText == 'Fail')
			{
				alert ("error.");	
			}else
			{
				//alert (t.responseText);
				var text = xmlHttp.responseText;
				var array_text = text.split("---");
				document.getElementById('imgArtist').src = array_text[0];
				document.getElementById('artistName').innerHTML = array_text[1];
				document.getElementById('artistProfile').innerHTML = array_text[1]+"<br>"+array_text[2];
				document.getElementById('albumName').innerHTML = array_text[3];
			}
      	}
    }
  	
	xmlHttp.open("GET",url,true);
  	xmlHttp.send(null);
}
*/
function fat40Vote(sId,aId,url){
		
	var url = url + 'fat40Vote.php?'  + "aid=" + aId + "&sid=" + sId;
	var xmlHttp = ajaxFunction();
	xmlHttp.onreadystatechange=function()
    {
    	if(xmlHttp.readyState==4)
      	{
			if(xmlHttp.responseText == 'OK')
			{
				alert ("Your vote complete.");
			}else
			{
				alert ("You can't vote.");
				
			}
      	}
    }
  	
	xmlHttp.open("GET",url,true);
  	xmlHttp.send(null);
}

function addPlayList(uId,sId,aId,url){
	var url = url + 'addPlayList.php?' + 'uid='+ uId +'&sid=' + sId + "&aid=" + aId;
	var xmlHttp = ajaxFunction();
	xmlHttp.onreadystatechange=function()
    {
    	if(xmlHttp.readyState==4)
      	{
			if(xmlHttp.responseText == 'OK')
			{
				alert ("Your add list complete.");	
			}else
			{
				alert ("Your can't add list.");
			}
      	}
    }
  	
	xmlHttp.open("GET",url,true);
  	xmlHttp.send(null);
}

function popupMovie(mylink,file,id,page)
{
	mylink = mylink + '?id=' + id + '&file='+ file +'&i='+page;
	newwindow=window.open(mylink,"movie","height=350,width=360,scrollbars=yes");
}

function popup(mylink,uid,aid,sid,index,url)
{
	mylink = mylink + '?id=' + uid + '&aid='+ aid+ "&sid=" + sid+'&i='+index;
	newwindow=window.open(mylink,"name","height=550,width=450,scrollbars=yes");
}

function popupMoblie1(aid,sid)
{
		mylink = 'content_item/toMobilePayment01.php?id=' +  aid+ '_' + sid;
		newwindow=window.open(mylink,"popup","height=600,width=600,scrollbars=yes");
}

function popupMoblieCP(cid,aid,sid,t)
{
		mylink = 'content_item/toMobilePayment01.php?id=' +  aid+ '_' + sid + '&t='+ t + '&cid=' + cid;
		newwindow=window.open(mylink,"popup","height=600,width=600,scrollbars=yes");
}
function popupMobileCP2(mylink,cid){
		var checked = "";
		var stype = document.forms['mainform'].elements['stype']
		for(var i = 0; i < stype.length; i++) {
			if(stype[i].checked) {
				checked = stype[i].value;
			}
		}
		mylink = mylink+'?id=' + cid + '&stype=' + checked + '&t=cp';
		newwindow=window.open(mylink, "popup");
}

function popupMobile2(mylink,sid)
{		
		var checked = "";
		var stype = document.forms['mainform'].elements['stype']
		for(var i = 0; i < stype.length; i++) {
			if(stype[i].checked) {
				checked = stype[i].value;
			}
		}
		mylink = mylink+'?id=' + sid + '&stype=' + checked;
		newwindow=window.open(mylink, "popup");
}

function popupMoblie3(aid,sid)
{
		mylink = '../content_item/toMobilePayment01.php?id=' +  aid+ '_' + sid;
		newwindow=window.open(mylink,"popup","height=600,width=600,scrollbars=yes");
}



function popupEmail()
{
	mylink = 'components/com_music/emailBox.php';
	newwindow=window.open(mylink,"name","height=200,width=450,scrollbars=yes");
}

function fatTrackBox(total,page,itemPerPage)
{
	var mod = parseInt(total%itemPerPage);
	if( mod!=0 ) var numOfPage = parseInt(total/itemPerPage)+1;
	else var numOfPage = parseInt(total/itemPerPage);
	var startItem = (page-1)*itemPerPage;
	var endItem = (page*itemPerPage);

	var url = 'sidebar_box/fat_track_box2.php?' + 'startItem='+ startItem +'&endItem=' + endItem + "&pub=published";
	var xmlHttp = ajaxFunction();
	xmlHttp.onreadystatechange=function()
    {
    	if(xmlHttp.readyState==4)
      	{
			if(xmlHttp.responseText == 'OK')
			{
				//alert ("Error.");	
			}else
			{
				var text = xmlHttp.responseText;
				document.getElementById('fattrackbox').innerHTML = text;
			}
      	}
    }
  	
	xmlHttp.open("GET",url,true);
  	xmlHttp.send(null);
}

function searchFatTrackBox(keyword)
{
	  //var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	
	//if(keyCode == 13){
		//var keyword = document.getElementById('search_track').value;
		keyword= escape(keyword);
		var url = 'sidebar_box/fat_track_box2.php?' + 'keyword='+ keyword + "&pub=search";
		var xmlHttp = ajaxFunction();
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState==4)
			{
				if(xmlHttp.responseText == 'OK')
				{
					//alert ("Error.");	
				}else
				{
					var text = xmlHttp.responseText;
					document.getElementById('fattrackboxSearch').innerHTML = text;
				}
			}
		}
		
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	//}
}
function searchFriendsEnter(event,url,iduser)
{
	 var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if(keyCode == 13){
		searchFriends(url,iduser);
		//alert("search finished");
	}
}
function searchFriendsClick(url,iduser){
	searchFriends(url,iduser);
}
function searchFriends(url,iduser)
{
		var keyword = document.getElementById('search_text').value;
		keyword= escape(keyword);
		var url = url + 'searchFriend.php?' + 'keyword='+ keyword+"&iduser="+iduser;
		var xmlHttp = ajaxFunction();
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState==4)
			{
				if(xmlHttp.responseText == 'OK')
				{
					//alert ("Error.");	
				}else
				{
					var text = xmlHttp.responseText;
					document.getElementById('friendBox').innerHTML = text;
				}
			}
		}
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
}
function searchAvan(url,email,name,agemin,agemax,sex,fav_art,style,city,itemid)
{
		name= escape(name);
		city = escape(city);
		var url = url + 'advanceSearch.php?' + 'email='+ email+"&name="+name+"&ageMin="+agemin +"&ageMax="+agemax +"&sex="+sex +"&style="+style +"&city="+city +"&favArt="+fav_art+"&itemid"+itemid;
		var xmlHttp = ajaxFunction();
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState==4)
			{
				if(xmlHttp.responseText == 'OK')
				{
					//alert ("Error.");	
				}else
				{
					var text = xmlHttp.responseText;
					document.getElementById('search_zone').innerHTML = text;
					document.getElementById('search_zone').style.display = "block";
					document.getElementById('main1').style.display = "none";
				}
			}
		}
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
}
function editlink2(url,id)
{
		var url = url + 'EditLink.php?' + 'id='+ id + "&job=showData";
		var xmlHttp = ajaxFunction();
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState==4)
			{
				if(xmlHttp.responseText == 'OK')
				{
					//alert ("Error.");	
				}else
				{
					var text = xmlHttp.responseText;
					var array_text = text.split("---");
					document.getElementById('URL').value = array_text[0];
					document.getElementById('title').value = array_text[1];
					document.getElementById('linkid').value = id;
				}
			}
		}
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
}
function editsave(url){
	var link = document.getElementById('URL').value;
	var title = document.getElementById('title').value;
	var id = document.getElementById('linkid').value;
	title= escape(title);
	var url = url + 'EditLink.php?' + 'id='+ id + '&job=saveEditLink' + '&url=' + link + '&title=' + title;
		var xmlHttp = ajaxFunction();
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState==4)
			{
				if(xmlHttp.responseText!='') alert (xmlHttp.responseText);
				window.location.reload();
			}
		}
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
}

function addlinks(url,order,uid){
	var link = document.getElementById('URL').value;
	var title = document.getElementById('title').value;
	title= escape(title);
	var url = url + 'EditLink.php?' + 'job=addLink' + '&url=' + link + '&title=' + title + '&uid=' + uid + '&order=' + order;
		var xmlHttp = ajaxFunction();
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState==4)
			{
				if(xmlHttp.responseText!='') alert (xmlHttp.responseText);
				window.location.reload();
			}
		}
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
}
function deletelinks(url,id){
	var url = url + 'EditLink.php?' + 'id='+ id + '&job=delLink';
		var xmlHttp = ajaxFunction();
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState==4)
			{
				if(xmlHttp.responseText!='') alert (xmlHttp.responseText);
				window.location.reload();
			}
		}
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
}
function saveorderlinks(url,linkid){
	var url = url + 'EditLink.php?' + 'id='+ linkid + '&job=saveOrderLink';
	var xmlHttp = ajaxFunction();
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			if(xmlHttp.responseText!='') alert (xmlHttp.responseText);
			window.location.reload();
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function setEmbedURL() {
    var auto = document.getElementById('autoStartCheckbox');
	sid = auto.value;
	if (auto.checked) {
		changeEmbedAuto(sid);
	} else {	
		changeEmbed(sid);
	}
	
}

function changeEmbed(sid,embed){
	if(embed!='0'){
		document.getElementById('embedzone').style.display = "";
		var emurl = "http://fz.smart-sync.com/fat/administrator/components/com_music/search/";
		var auto = document.getElementById('autoStartCheckbox');
		var embed = document.getElementById('EmbeddablePlayer');
		auto.value = sid;
		var str = '<object name="mp3player" id="mp3player2" type="application/x-shockwave-flash" data="'+ emurl + 'player/mp3FlashPlayerWhite.swf" style="vertical-align: bottom;" width="170" height="11"> <param name="type" value="application/x-shockwave-flash"> <param name="codebase" value="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"> <param name="movie" value="'+ emurl+ 'player/mp3FlashPlayerWhite.swf"> <param name="FlashVars" value="pathXml=' + emurl + 'songList.php?id=song_' + sid + '"></object>';
		embed.value = str;
	}
	else{
		document.getElementById('embedzone').style.display = "none";
	}
} 

function changeEmbedAuto(sid){
	var emurl = "http://fz.smart-sync.com/fat/administrator/components/com_music/search/";
	var auto = document.getElementById('autoStartCheckbox');
	var embed = document.getElementById('EmbeddablePlayer');
	auto.value = sid;
	var str = '<object name="mp3player" id="mp3player2" type="application/x-shockwave-flash" data="'+ emurl + 'player/mp3FlashPlayerWhite.swf" style="vertical-align: bottom;" width="170" height="11"> <param name="type" value="application/x-shockwave-flash"> <param name="codebase" value="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"> <param name="movie" value="' + emurl + 'player/mp3FlashPlayerWhite.swf"> <param name="FlashVars" value="pathXml=' + emurl + 'songList.php?id=song_' + sid + '"></object>';
	return str;
}

function addtogroup(url,gid,uid){
	var url = url + 'addToGroup.php?' + 'uid='+ uid +'&gid=' + gid;
	var xmlHttp = ajaxFunction();
	xmlHttp.onreadystatechange=function()
    {
    	if(xmlHttp.readyState==4)
      	{
			if(xmlHttp.responseText == 'OK')
			{
				//alert ("Your add group complete.");	
				window.location.reload();
			}else
			{
				alert ("Your can't add group.");
			}
      	}
    }
  	
	xmlHttp.open("GET",url,true);
  	xmlHttp.send(null);
}

function sendRequest(url,song,artist,name){
	song = escape(song);
	artist = escape(artist);
	name = escape(name);
	var url = url + 'sendrequest.php?' + 'song='+ song+'&artist=' +artist+'&name='+name;
	var xmlHttp = ajaxFunction();
	xmlHttp.onreadystatechange=function()
    {
    	if(xmlHttp.readyState==4)
      	{
			if(xmlHttp.responseText == 'OK')
			{
				document.getElementById('fail').style.display = 'none';	
				document.getElementById('complete').style.display = 'block';
				document.getElementById('song').value = '';
				document.getElementById('artist').value = '';
				document.getElementById('name').value = '';
				showButton();
			}else
			{
				alert (xmlHttp.responseText);
				document.getElementById('complete').style.display = 'none';
				document.getElementById('fail').style.display = 'block';
			}
      	}
    }
  	
	xmlHttp.open("GET",url,true);
  	xmlHttp.send(null);
}