﻿var imgServer = '';

String.prototype.trim = function() { //Trim ambas direcciones
   return this.replace(/^[ ]+|[ ]+$/g,"");
}

String.prototype.tripSpace = function() { //Trim ambas direcciones
   return this.replace(/(\s\s+)/g, " ");
	 //replace(/^[ ]+|[ ]+$/g,"");
}

//Kiem tra ki tu dac biet
function existsSpecialChars(str)
{
		//var re = /^[0-9a-zA-Z ]*$/;
		/*
		var re = /([!@#$%^&*]$/;
		str = str.trim();
		var pos = str.search(re);
		if(pos == -1)
			return false;
		else
			return true;
		*/
}	

//Kiem tra URL
function isURL(str) {
	var pattern = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.0123456789/~:";
	if (str.length > 0) {
		if (str.length < 5 ) {
			return false;
		} else {
			if (str.lastIndexOf(".") == -1) { 											// khong tim thay dau cham
				return false;
			} else {
				if (str.lastIndexOf(".") == (str.length - 1)) return false;				// dau cham nam o cuoi cung
//				if (!isAlpha(str.charCodeAt(str.lastIndexOf(".") + 1))) return false;	// sau dau cham khong phai ki tu Alphabet
			}
			for (var c=0; c<pattern.length; c++) {
				if (pattern.indexOf(str.charAt(c),0) == -1) return false;				// ki tu khong hop le
			}
		}
	}
	return true;	
}
//Kiem tra email
function isEmailAddr(email) {
	var pattern = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.0123456789@~";
	var theStr = new String(email)
	var index = theStr.indexOf("@");

	for (var a=0; a<pattern.length; a++) {
		if (pattern.indexOf(email.charAt(a),0) == -1) return false;
	}
	if (theStr.indexOf(" ",0) != -1) return false;
	if (index > 0) {
		var pindex = theStr.indexOf(".",index);
		if ((pindex > index+1) && (theStr.length > pindex+1)) return true;
	}
	return false;
}


//Open popup
function openWindow(filename, winname, width, height, feature) {
	var features, top, left;
	var reOpera = /opera/i ;
	var winnameRequired = ((navigator.appName == "Netscape" && parseInt(navigator.appVersion) == 4) || reOpera.test(navigator.userAgent));
	
	left = (window.screen.width - width) / 2;
	top = (window.screen.height - height) / 2;	
	if(feature == '')
		features = "width=" + width + ",height=" + height + ",top=" + top + ",left=" + left + ",status=0,location=0";
	else
		features = "width=" + width + ",height=" + height + ",top=" + top + ",left=" + left + "," + feature;
//	if(!winnameRequired)	winname = "";
	newwindow = window.open(filename,winname,features);
	newwindow.focus();
}
//
//Cookie
function GetCookie(sName)
{
  // cookies are separated by semicolons
 var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
      return unescape(aCrumb[1]);
  }

  // a cookie with the requested name does not exist
  return null;
}

function Search(keyword, type)
{    
	window.opener.location = "/mp3/search/do.html?t=" + type + "&q=" + encodeURI(keyword);
	window.opener.focus();
}

function PopUpSong(zing)
{
	if (window.ActiveXObject) {
		openWindow('/popupsong.php?zing=' + zing, 'popupsong', 575, 378, '');
	} else {
		openWindow('/popupsong.php?zing=' + zing, 'popupsong', 575, 378, '');
	}
}

function cut(text) {
	if(text.length > 28) {
		txt = text.substr(0,14) + '...' + text.substr(text.length-12,12);
		document.write(txt);
	} else {
		document.write(text);
	}
}

function cut_1(text) {
	if(text.length > 36) {
		txt = text.substr(0,17) + '...' + text.substr(text.length-16,16);
		document.write(txt);
	} else {
		document.write(text);
	}
}

function cut_2(text) {
	if(text.length > 33) {
		txt = text.substr(0,16) + '...' + text.substr(text.length-14,14);
		document.write(txt);
	} else {
		document.write(text);
	}
}

//Add song to playlist
function AddPlaylist(id)
{
	openWindow('/add/?sid=' + id, 'Add', 575, 280, 'scrollbars=0');
}

//Add song to cookie
function AddCookiePlaylist(id)
{
	var cooki, arr_cookie = new Array();
	if(confirm("Đăng nhập để add bài hát vào playlist của mình!\nChọn OK để đăng nhập vào hệ thống?\nNếu chọn Cancel thì bài hát sẽ được add vào Cookie Playlist."))
	{
//		document.frmReferer.action = '/mp3/login/index.html';
//		document.frmReferer.submit();
			window.opener.location = '/mp3/login/index.html';
			window.opener.focus();
	}
	else
	{
		cooki = GetCookie('Playlist');
		if(cooki != null)
		{
			arr_cookie = cooki.split(",");
			if(arr_cookie.length > 0)
				for(var i = 0; i < arr_cookie.length; i++)
				{
					if(arr_cookie[i] == id)
					{
						alert('Bài hát này đã tồn tại trong cookie playlist!');
						return;
					}
				}
			document.cookie = 'Playlist=' + cooki + ',' + id + ';expires=Fri, 30-Jun-2017 08:00:00 ;path=/';
		}
		else
			document.cookie = 'Playlist=' + id + ';expires=Fri, 30-Jun-2017 08:00:00 ;path=/';
//		alert("Bài hát này đã được lưu vào CookiePlaylist.");
	}
}
//Remove song from cookie playlist
function RemoveCookieSong(id)
{
	var cooki, arr_cookie = new Array();
	if(confirm("Bạn muốn xóa bài hát này khỏi cookie playlist?"))
	{
		cooki = GetCookie('Playlist');
		if(cooki != null)
		{
			arr_cookie = cooki.split(",");
			if(arr_cookie.length > 0)
				for(var i = 0; i < arr_cookie.length; i++)
				{
					if(arr_cookie[i] == id)
						delete arr_cookie[i];
				}
			arr_cookie.sort();
			cooki = arr_cookie.join(",");
			cooki = cooki.substr(0, cooki.length - 1);
			document.cookie = 'Playlist=' + cooki + ';expires=Fri, 30-Jun-2017 08:00:00 ;path=/';
		}
		return true;
	}
	else return false;
}
//Remove song from cookie playlist
function RemoveCookiePlaylist()
{
	var cooki, arr_cookie = new Array();
	if(confirm("Bạn muốn xóa cookie playlist?"))
	{
		cooki = GetCookie('Playlist');
		if(cooki != null)
		{
			document.cookie = 'Playlist=;expires=Fri, 30-Jun-2007 08:00:00 ;path=/';
			location.href = '/';
		}
		return true;
	}
	else return false;
}
//Remove song from cookie playlist
function RemoveSong()
{
	if(confirm("Bạn muốn xóa bài hát này khỏi playlist trong khi nghe?"))
	{
		return true;
	}
	else return false;
}
//Show 
function showFullList(object)
{
	object.style.overflow = 'auto';
	object.style.borderColor = '#EEEEEE';
	object.style.backgroundColor = '#EEEEEE';
}

function hideFullList(object)
{
	object.style.overflow = 'hidden';
	object.style.borderColor = '#FFFFFF';
	object.style.backgroundColor = '#FFFFFF';
	object.scrollTop = '0px';
}

//play media
function play()
{
	document.getElementById('wPlayer').controls.play();
}

//playlist media
function playlist(id, txt_search, txt_replace, player)
{
	document.getElementById(id).innerHTML = player.replace(txt_search, txt_replace);
	if(document.getElementById('wPlayer').controls) {
		window.setTimeout('play()', 1);
	}
}

function remove(href)
{
	if(confirm('Bạn thực sự muốn xóa?'))
	{
		window.location = href;
	}
	else return;
}

function checkDeletePlaylist(action){
	if(confirm('Bạn có thực sự muốn xóa?')) {
		document.frmReferer.action = action;
		document.frmReferer.submit();
	} 
	else return;
}


/*********************************************
* Search result page
**********************************************/
//clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6
//clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95
var searchPlayer = '<object classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" codebase="http:/activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,0,0" standby="Loading Microsoft Windows Media Player components..." type="application/x-oleobject" width="320" height="62">';
searchPlayer += '<param name="URL" value="songid"><param name="playCount" value="1"><param name="autoStart" value="true"><param name="volume" value="75"><param name="enabled" value="1"><param name="enableContextMenu" value="0"><param name="showstatusbar" value="true"><param name="enableErrorDialogs" value="0"><param name="autorewind" value="1"><param name="ShowTracker" value="false"><param name="ShowPositionControls" value="false"><param name="ShowControls" value="true">';
// searchPlayer += '<embed width="320" height="65" showtracker="1" showstatusbar="1" showpositioncontrols="1" showgotobar="0" showdisplay="0" showaudiocontrols="1" showcontrols="1" playcount="1" mute="0" enabletracker="1" enablefullscreencontrols="1" enablecontextmenu="0" displaysize="1" autostart="1" autosize="1" name="Player" src="songid" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" type="application/x-mplayer2" />';
searchPlayer += '<embed type="application/x-mplayer2" showtracker=1 showpositioncontrols=1 enableErrorDialogs="0" autostart="1" pluginspage="http:/activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,0,0" showstatusbar="1" showcontrols="1" enablecontextmenu="0" autorewind="1" volume="75" src="songid" name="Player" width="320" height="65" />';
searchPlayer += '</object>';

var searchPlayerVideo = '<object classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" codebase="http:/activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,0,0" standby="Loading Microsoft Windows Media Player components..." type="application/x-oleobject" width="320" height="260">';
searchPlayerVideo += '<param name="URL" value="songid"><param name="playCount" value="1"><param name="autoStart" value="true"><param name="volume" value="75"><param name="enabled" value="1"><param name="enableContextMenu" value="0"><param name="showstatusbar" value="true"><param name="enableErrorDialogs" value="0"><param name="autorewind" value="1"><param name="ShowTracker" value="false"><param name="ShowPositionControls" value="false"><param name="ShowControls" value="true">';
searchPlayerVideo += '<embed type="application/x-mplayer2" showtracker=1 showpositioncontrols=1 enableErrorDialogs="0" autostart="1" pluginspage="http:/activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,0,0" showstatusbar="1" showcontrols="1" enablecontextmenu="0" autorewind="1" volume="75" src="songid" filename="songid" name="Player" width="320" height="260" />';
searchPlayerVideo += '</object>';

var oldBlockID;
var oldPlayerID;

function showSongDetail(id, status)
{
	obj = document.getElementById(id);
	obj.style.display = status;
}

function replacePlayer(blockID, playerID, txt_search, txt_replace, searchPlayer)
{
	// open new
	if (blockID == oldBlockID) {		
		obj = document.getElementById(blockID);
		if (obj.style.display == 'none') {
			showSongDetail(blockID, 'block');
			playlist(playerID, txt_search, txt_replace, searchPlayer);
		} else {
			document.getElementById(playerID).innerHTML = '';
			showSongDetail(blockID, 'none');
		}
	} else {
		if (oldBlockID) {
			document.getElementById(oldPlayerID).innerHTML = '';
			showSongDetail(oldBlockID, 'none');
		}
		showSongDetail(blockID, 'block');
		playlist(playerID, txt_search, txt_replace, searchPlayer);
		oldBlockID = blockID;
		oldPlayerID = playerID;
	}
}

function download(msg, file)
{
	if (msg != '' && file == '') {
		alert(msg);
		return;
	}
	
	window.open('/download/?' + file);
}

// count song
/*function countSong()
{
	xmlhttp.open('get', '/mp3/vote/do.'+id_num+'.'+vote+'.html');
    xmlhttp.onreadystatechange = handleResponse;
    xmlhttp.send(null);	
}*/
function getEmbed(domain_flash, domain_site, url, extension, number)
{
	var embed = '';
	var id = "embed" + number;
	if(extension == "mp3")
		embed = '<object width="257" height="200"><param name="movie" value="' + domain_flash + 'skins/zing/flash/player/zingPlayer.swf?xmlurl=' + domain_site + '/blog/?' + url + '" /><param name="quality" value="high" /><param name="wmode" value="transparent" /><embed width="257" height="200" src="' + domain_flash + 'skins/zing/flash/player/zingPlayer.swf?xmlurl=' + domain_site + '/blog/?' + url + '" quality="high" wmode="transparent" type="application/x-shockwave-flash"></embed></object>';	
		//embed = '<embed type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" height="20" width="385" src="' + domain + '/skins/zing/flash/player.swf?data=/blog/?' + url + '&shuffle=false&autostart=true&repeat=true&showdownload=true" />';
	else if(extension == "flv")
		embed = '<object width="385" height="300"><param name="movie" value="' + domain_flash + 'skins/zing/flash/flvplayer.swf?file=http://mp3.zing.vn/blog/?' + url + '&shuffle=false&autostart=true&repeat=true&showdownload=true" /><param name="quality" value="high" /><param name="wmode" value="transparent" /><embed width="385" height="300" src="' + domain_flash + 'skins/zing/flash/flvplayer.swf?file=' + domain_site + '/blog/?' + url + '&shuffle=false&autostart=true&repeat=true&showdownload=true" quality="high" wmode="transparent" type="application/x-shockwave-flash"></embed></object>';
		//embed = '<embed type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" height="300" width="385" src="' + domain + '/skins/zing/flash/flvplayer.swf?file=/blog/?' + url + '&shuffle=false&repeat=true&showdownload=true&autostart=true" />';
	else if(extension == "wma")
		embed = '<object width="320" height="65"><param name="movie" value="' + domain_site + '/playsong/?' + url + '" /><param name="quality" value="high" /><param name="wmode" value="transparent" /><embed src="' + domain_site + '/playsong/?' + url + '" quality="high" wmode="transparent" type="application/x-shockwave-flash" width="320" height="65" enablecontextmenu="0"></embed></object>';
		//embed = '<embed type="application/x-shockwave-flash" width="320" height="65" allowfullscreen="true" flashvars="autostart=true" enablecontextmenu="0" src="' + domain + '/playsong/?' + url + '" />';		
	else if(extension == "wmv")
		embed = '<object width="320" height="240"><param name="movie" value="' + domain_site + '/playsong/?' + url + '" /><param name="quality" value="high" /><param name="wmode" value="transparent" /><embed src="' + domain_site + '/playsong/?' + url + '" quality="high" wmode="transparent" type="application/x-shockwave-flash" width="320" height="240" enablecontextmenu="0"></embed></object>';
	document.getElementById(id).value = embed;
	document.getElementById(id).style.display = 'block';
}