﻿// JScript File
/***********************************
            Directly Play
************************************/
var isIE = window.navigator.appName == "Microsoft Internet Explorer";

function PlayDirectly()
{
    var playInfo = Querystring("Playing", "none");
    var id = Querystring("ID", "-1");
    
    var comc = Querystring("Comc", "none");
    var albumID = Querystring("AlbumID", "-1");
    var bookID = Querystring("BookID", "-1");     

	if(comc != "none" && albumID != "-1")
	{
		 location.href="Album.aspx?AlbumID="+  albumID + "#Album," +  albumID; 
	}
	
	if(comc != "none" && bookID != "-1")
	{
		 location.href="BookDetail.aspx?BookID="+  bookID + "#Book," +  bookID; 
	}	

    if(playInfo != "none")
    {
		if(playInfo == "Direct")
		{
			changeSource(id);	
		}    
		else if(playInfo == "VC")
		{
			location.href="TVO.aspx#Media," + id; 
		}
        else if((playInfo == "TVO") || (playInfo == "THTN"))
        {
            location.href="TVO.aspx#Media," + id; 
        }
        else if(playInfo == "Radio")
        {
            location.href="Radio.aspx#Media," + id; 
        }
        else if(id != "-1")
        {
            location.href="TVO.aspx#Media," + id;  
        }
        else
        {
            changeSource('PlayList.aspx?' + playInfo); 
        }
    }
    
    if(playInfo == "")
    {
		location.href="TVO.aspx#Media," + id;
    }    
}


/***********************************
            Datalist Paging
************************************/
function ShowPreviousPage(pageSize, currentPageIndex)
{
    currentPageIndex -= pageSize;
    if (currentPageIndex < 0)
    {
        currentPageIndex = 0;
    }
    return currentPageIndex;
}

function ShowNextPage(pageSize, recordCount, currentPageIndex)
{
    if ((currentPageIndex + pageSize) < recordCount)
    {
        currentPageIndex += pageSize;
    }
    return currentPageIndex;
}

function ShowLastPage(pageSize, recordCount, currentPageIndex)
{
    if (recordCount > 0)
    {
        var intMod = recordCount % pageSize;
        if (intMod > 0)
        {
            currentPageIndex = recordCount - intMod;
        }
        else
        {
            currentPageIndex = recordCount - pageSize;
        }
    }
    return currentPageIndex;    
}

function GetPreviousPage(pageSize, itemCount, currentPage)
{
    if (currentPage > 1)
        currentPage--;
    return currentPage;
}

function GetNextPage(pageSize, itemCount, currentPage)
{
    if (currentPage * pageSize < itemCount)
        currentPage++;
    return currentPage;
}

function GetLastPage(pageSize, itemCount, currentPage)
{
    if (itemCount > 0)
    {
        mod = itemCount % pageSize;
        if (mod > (pageSize / 2))
            currentPage = Math.round(itemCount / pageSize);
        else
            currentPage = Math.round(itemCount / pageSize) + 1;
    }
    return currentPage;
}


/***********************************
            String Cut
************************************/
function cutStrExper( head, length )
{
    if(head == "?")
    {
        document.write("");
		return;
    }
        
   	var strLen = head.length;
	if (strLen > length)
	{
		head = head.substr(0, length+5) ;
		
		var lastIndexOfSpace = head.lastIndexOf(' ');
		
		if (lastIndexOfSpace > 0)
        {
            head = head.substr(0, lastIndexOfSpace) ;
        }
        else 
        {
            head = head.substr(0, length) ;
        }
		
		document.write(head + " ...");
		return;
	}
	else
	{
		document.write(head);		
	}		
}

function cutStrExperJs( head, length )
{
    if(head == "?")
    {
        return "";	
    }
    
   	var strLen = head.length;
	if (strLen > length)
	{
		head = head.substr(0, length+5) ;
		
		var lastIndexOfSpace = head.lastIndexOf(' ');
		
		if (lastIndexOfSpace > 0)
        {
            head = head.substr(0, lastIndexOfSpace) ;
        }
        else 
        {
            head = head.substr(0, length) ;
        }
		
		return head + " ...";
	}
	else
	{
		return head;		
	}		
}

function standardizeNumber( number, length )
{
	number=number+'';
   	var strLen = number.length;
	if (strLen < length)
	{
		for(i=length-strLen; i>0; i--)
		{
		    number = "0" + number;
		}				
	}
	document.write(number);	
	return;				
}

function getFormatNumber( number, length )
{
	number=number+'';
   	var strLen = number.length;
	if (strLen < length)
	{
		for(i=length-strLen; i>0; i--)
		{
		    number = "0" + number;
		}				
	}
	
	return number;				
}

function cutStrExperForObject( obj, length )
{
    var head = (obj.value);
    
	var strLen = head.length;
	if (strLen > length)
	{
		head = head.substr(0, length+5) ;
		head = head.substr(0, head.lastIndexOf(' '))
		//document.write(head + " ...");
		
		obj.value = head + " ...";
		
		return;
	}
	else
	{
		//document.write(head);				
	}		
}


/*------------------------------------------------------
				Display Vega Mail																								 
------------------------------------------------------*/
function AboutVietAd()
{
	alert("mailto:" + "Info" + "@" + "vietad.vn");
}

/*------------------------------------------------------
				CallBack Error Load Info																								
------------------------------------------------------*/
function onCallbackError()
{
    if(confirm('Tạm thời không kết nối được tới Internet\n Bạn hãy thử nạp lại trang?'))
    {
       location.reload();
    }
}

function OnError()
{
    return false;
}

/*------------------------------------------------------
				Listening the song																								
------------------------------------------------------*/
function changeSource( strSource )
{
	var width=317;
	var height=263;
    var s;
    
    s="<OBJECT id=winMediaPlayerID ";
            s+="codeBase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715 ";
            s+="type=application/x-oleobject height=" + height + " ";
            s+="standby=\"Loading Microsoft Windows Media Player components...\" "; 
            s+="width=" + width + " classid=CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6 ";
            s+="name=winMediaPlayerID>";
        s+="<PARAM NAME=\"URL\" VALUE=\""+strSource+"\"> ";
        s+="<PARAM NAME=\"rate\" VALUE=\"1\">";
        s+="<PARAM NAME=\"balance\" VALUE=\"0\">";
        s+="<PARAM NAME=\"currentPosition\" VALUE=\"0\">";
        s+="<PARAM NAME=\"defaultFrame\" VALUE=\"0\">";
        s+="<PARAM NAME=\"playCount\" VALUE=\"999\">";
        s+="<PARAM NAME=\"CursorType\" VALUE=\"-1\">";
        s+="<PARAM NAME=\"autoStart\" VALUE=\"1\">";
        s+="<PARAM NAME=\"autoplay\" VALUE=\"1\">";
        s+="<PARAM NAME=\"currentMarker\" VALUE=\"0\">";
        s+="<PARAM NAME=\"invokeURLs\" VALUE=\"-1\">";
        s+="<PARAM NAME=\"volume\" VALUE=\"50\">";
        s+="<PARAM NAME=\"mute\" VALUE=\"0\">";
        s+="<PARAM NAME=\"stretchToFit\" VALUE=\"-1\">";
        s+="<PARAM NAME=\"windowlessVideo\" VALUE=\"0\">";
        s+="<PARAM NAME=\"enabled\" VALUE=\"1\">";
        s+="<PARAM NAME=\"fullScreen\" VALUE=\"0\">";
        s+="<PARAM NAME=\"enableContextMenu\" VALUE=\"0\">";
        s+="<PARAM NAME=\"enableErrorDialogs\" VALUE=\"0\"> \n";
        
        s+="<Embed id='winMediaPlayerIDFF' type='application/x-mplayer2' pluginspage='http://www.microsoft.com/windows/windowsmedia/download/' filename='"+strSource+"' src='"+strSource+"' Name='winMediaPlayerIDFF' "; 
            s+="width='" + width + "' ";
            s+="height='" + height + "' ";
            s+="AutoSize='1' ";
            s+="AutoStart='1' ";
            s+="AutoPlay='1' ";
           
            s+="DisplaySize='1' ";
            s+="EnableContextMenu='0' ";
            s+="EnableFullScreenControls='1' "; 
            s+="EnableTracker='1' ";
            s+="Mute='0' ";
            s+="PlayCount='999' "; 
            s+="ShowControls='1' "; 
            s+="ShowAudioControls='1' "; 
            s+="ShowDisplay='0' ";
            s+="ShowGotoBar='0' ";
            s+="ShowPositionControls='1' "; 
            s+="ShowStatusBar='1' ";
            s+="ShowTracker='1'> ";
            s+="</embed> ";
    s+="</OBJECT>";
	
    document.getElementById("MediaPlayer").innerHTML=s;
    //scrollTo(0,100);
}
/* gioithieu*/


/*------------------------------------------------------
				Parse Search KeyWord																								
------------------------------------------------------*/
function onCmdSearchClick( txtBox )
{
    var q = (txtBox.value);
    
    if ((q.indexOf('AND') == -1) && (q.indexOf('OR') == -1) && (q.indexOf('"') == -1) && (q != ''))
	{
		q = '\'' + q + '\'';
	    txtBox.value = q;
	}
}

/*------------------------------------------------------
				Write Short DateTime																								
------------------------------------------------------*/
function writeDayTime(s)
{
	var mydate=new Date(s)
	
	var year = mydate.getYear()
	if (year < 1000)
		year += 1900
	var month = mydate.getMonth() + 1
	if (month < 10)
		month = "0" + month
	var day = mydate.getDate()
	if (day < 10)
		day = "0" + day

	var dayw = mydate.getDay()
	
	document.write( day+"/"+month+"/"+year )
}


/*------------------------------------------------------
				Get Query String																							
------------------------------------------------------*/
function Querystring(strKey,strDefault)
{
    // get the query string, ignore the ? at the front.
	var querystring=location.search.substring(1,location.search.length);

    // parse out name/value pairs separated via &
	var args = querystring.split('&');

    // split out each name = value pair
	for (var i=0;i<args.length;i++)
	{
		var pair = args[i].split('=');

		// Fix broken unescaping
		temp = unescape(pair[0]).split('+');
		name = temp.join(' ');

		temp = unescape(pair[1]).split('+');
		value = temp.join(' ');
        
        if(name == strKey)
        {
            return value;
        }		
	}
    
    return strDefault;
}

function customCallback(e,s) { e.Callback(s); }
function customCallback2(e,s,p) { e.Callback(s,p); }
function tonggleDisplay(id) { document.getElementById(id).style.display = document.getElementById(id).style.display=='block' ? 'none' : 'block';}
function hide(id) { document.getElementById(id).style.display='none';}
function show(id) { document.getElementById(id).style.display='block';}

function trimAll(sString)
{
	while (sString.substring(0,1) == " " || sString.substring(0,1) == "\n"  || sString.substring(0,1) == "\t")
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == " " || sString.substring(sString.length-1, sString.length) == "\n" || sString.substring(sString.length-1, sString.length) == "\t")
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function tonggle(checkboxID)
{
	var i=0;
	var chkSong;
	
	for (i=0;i<document.aspnetForm.length;i++)
	{
		chkSong=document.aspnetForm[i];
		if (chkSong.type=='checkbox' && chkSong.id.indexOf(checkboxID)>-1)
		{
			chkSong.checked = !chkSong.checked;
		}
	}
}

function getIDFromQueryString()
{
	var id=0 + '';
	var query = window.location.href;
	var sharpIndex = query.indexOf('#');
	if (sharpIndex>0)
		id = query.substr(query.indexOf('#')+1);
	if (id.indexOf(",")>-1) id = id.substr(id.indexOf(",") + 1);
	return parseInt(id);
}

function getSharpFromQueryString()
{
	var id=0 + '';
	var query = window.location.href;
	var sharpIndex = query.indexOf('#');
	if (sharpIndex>0)
		id = query.substr(query.indexOf('#')+1);
	id = id.replace(",","=");
	return id;
}

function playFromQuery()
{
	if (getIDFromQueryString() != 0) 
		changeSource('Playlist.aspx?' + getSharpFromQueryString());
}

function setQuerySong(type,id)
{
	if (getIDFromQueryString()==0)
		location = location + "#" + type + "," + id;
	else
	{
		var a = location + '';
		location = a.replace(/#.*/,'#' + type + "," + id);	
	}
}

function getTimeString(s)
{
	document.write("<span class=LargeTime2>");
	writeTime(s);
	document.write("</span>")
}

function writeTime(s)
{
	var mydate=new Date(s)
	
	var year = mydate.getYear()
	if (year < 1000)
		year += 1900
	var month = mydate.getMonth() + 1
	if (month < 10)
		month = "0" + month
	var day = mydate.getDate()
	if (day < 10)
		day = "0" + day

	var dayw = mydate.getDay()
	
	var hour = mydate.getHours()
	if (hour < 10)
		hour = "0" + hour
	
	var minute=mydate.getMinutes()
	if (minute < 10)
		minute = "0" + minute
	var dayarray=new Array("Ch&#7911; Nh&#7853;t","Th&#7913; Hai","Th&#7913; Ba","Th&#7913; T&#432;","Th&#7913; N&#259;m","Th&#7913; S&#225;u","Th&#7913; B&#7843;y")
	document.write(dayarray[dayw]+", "+day+"/"+month+"/"+year+",&nbsp;"+hour+":"+minute+" (GMT+7)")
}

function ReadCookie(cookieName) {
	var theCookie=""+document.cookie;
	var ind=theCookie.indexOf(cookieName);
	if (ind==-1 || cookieName=="") return ""; 
	var ind1=theCookie.indexOf(';',ind);
	if (ind1==-1) ind1=theCookie.length; 
	return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

function PlayMedia(id) {setQuerySong('Media', id); playFromQuery();}
function PlaySong(id) {setQuerySong('Song', id); playFromQuery();}
function PlayBook(id) {setQuerySong('Book', id); playFromQuery();}
function PlayAlbum(id) {setQuerySong('Album', id); playFromQuery();}

function isEmailAddress(str)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return (filter.test(str));
}

function swapImage(e, src)
{
	if (e)
		e.src = src;
}

function checkLength(id, length)
{
	if (document.getElementById(id).value.length < length)
	{
		alert('Bạn phải nhập ít nhất ' + length + ' ký tự');
		return false;
	}
	return true;
}