// Common functions for Audio Player Pages
// December 28, 2007

CONST_BHAJAN_PLAYER_URL = "player.asp";
CONST_IE6_BHAJAN_PLAYER_URL = "ie6_player.asp";
var xmlHttp;
var rootpath;
var firsttime = true;
// Flag to check if the operation is Add
var isAddItemFlag = false;


function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1 (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}


function playerInNewWindow(mode, link, title) {
	var ver = getInternetExplorerVersion();
	if (2 == mode){
		// play list
		if ( ver > -1 ) // Test for Internet Explorer
		{
			if ( ver <= 6.0 ) // If Version 6.0 or older
			{
				newWinUrl = CONST_IE6_BHAJAN_PLAYER_URL + "?mode=" + mode + "&link=0&title=0";
				newWin = window.open(newWinUrl, "AudioPlayer", "width=425, height=256,menubar=no,resizable=false,scrollbars=no,status=no,toolbar=no,location=no");
				newWin.focus();				
			
			} else {			
				newWinUrl = CONST_BHAJAN_PLAYER_URL + "?mode=" + mode + "&link=0&title=0";
				newWin = window.open(newWinUrl, "AudioPlayer", "width=425, height=256,menubar=no,resizable=false,scrollbars=no,status=no,toolbar=no,location=no");
				newWin.focus();
			}
		} else {
			// All other browsers
			window.alert("'Play Selected' functionality is ONLY supported on IE, version 6.0+. For other browsers, please use the 'Save Playlist' link to save your play list. You can open the saved playlist in Windows Media Player or any other ASX compatible media player.");
			self.scrollTo(0,0);		
			showList(true);
			return;		
		}
	} else {
		// play single file - allowed for all browsers
		newWinUrl = CONST_BHAJAN_PLAYER_URL + "?mode=" + mode + "&link=" + link + "&title=" + title;
		newWin = window.open(newWinUrl, "AudioPlayer", "width=425, height=256,menubar=no,resizable=false,scrollbars=no,status=no,toolbar=no,location=no");
		newWin.focus();
	}
}

function updatePlaylist(checked, link, title)
{
    if ((title == 0) && (link == 0)) {
    	var url = "updatePlaylist.asp"
    } else {
	if (checked == true) {
		var url= "updatePlaylist.asp?link=" + link + "&title=" + title + "&mode=1"
		isAddItemFlag = true;
	} else {
		var url= "updatePlaylist.asp?link=" + link + "&title=" + title + "&mode=2"		
	}
    }
    xmlHttp=GetXmlHttpObject(stateChanged)
    xmlHttp.open("GET", url , true)
    xmlHttp.send(null)
}

function removeFromPlaylist(id, link, title)
{	
	var listItemChkBox = document.getElementById(id);
	if (null != listItemChkBox) {		
		listItemChkBox.click();	
	} else 	{
		updatePlaylist(0, link, title);
	}
}

function removeAllFromPlaylist()
{	
	var url= "updatePlaylist.asp?link=" + "removeAll" + "&title=" + "removeAll" + "&mode=3";
	var c = document.forms[0].elements["audioCheckBox"];
	if (c) {
		for (var i = 0; i < c.length; i++)
		{
			c[i].checked = false;
		}
	}
	xmlHttp=GetXmlHttpObject(stateChanged)
	xmlHttp.open("GET", url , true)
	xmlHttp.send(null)
}



function selectAllOnPage(checkflag)
{

	var c = document.forms[0].elements["audioCheckBox"];
	if (c) {
		for (var i = 0; i < c.length; i++)
		{		
			if (c[i].checked != checkflag) {
				c[i].click();
				c[i].checked = checkflag;				
			}
		}
	}

}


function checkForNetscapeAndWarn(){
	if(navigator.userAgent.indexOf("Netscape")>=0){
		
		window.alert("NOTE: Netscape Users! Please rename the saved file to have '.asx' extention");
	}

}


function stateChanged()
{
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
        document.getElementById("playlist").innerHTML = xmlHttp.responseText
	if (isAddItemFlag) {
		var list1 = document.getElementById("playlist");
		list1.scrollTop = 0;
		list1.scrollTop = list1.scrollHeight;
	}
	// Reset value of flag
	isAddItemFlag = false;
    }
}

function GetXmlHttpObject(handler)
{
    var objXmlHttp=null

    if (navigator.userAgent.indexOf("Opera")>=0)
    {
        alert("Opera not supported...")
        return;
    }
    if (navigator.userAgent.indexOf("MSIE")>=0)
    {
        var strName="Msxml2.XMLHTTP"
        if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
        {
            strName="Microsoft.XMLHTTP"
        }
        try
        {
            objXmlHttp=new ActiveXObject(strName)
            objXmlHttp.onreadystatechange=handler
            return objXmlHttp
        }
        catch(e)
        {
            alert("Error. Scripting for ActiveX might be disabled")
            return
        }
    }
    if (navigator.userAgent.indexOf("Mozilla")>=0)
    {
        objXmlHttp=new XMLHttpRequest()
        objXmlHttp.onload=handler
        objXmlHttp.onerror=handler
        return objXmlHttp
    }
}


function showList(scrollToTop) {
	if (firsttime == true) {
		updatePlaylist(0,0,0);
		firsttime = false;
	}
	var list1 = document.getElementById("playlist");
	list1.style.display = '';
	if (scrollToTop) {
		list1.scrollTop = 0;
	}
}


function hideList() {
	var list1 = document.getElementById("playlist");
	list1.style.display = 'none';
}