/* borrowed from the core */
function isViper() {
	var pv = plyrObj.PlayerProperty("PRODUCTVERSION");
	var beamerVersion = "6.0.11.000";
	return versionCompare( pv, beamerVersion ) == -1;
}

function isBoxter() {
	var pv = plyrObj.PlayerProperty("PRODUCTVERSION");
	var boxterVersion = "6.0.11.999";
	return versionCompare( pv, boxterVersion ) == 1;
}

/* Compares 2 versions as #.#.#.# strings... */
function versionCompare(a,b) {
	var testver1 = a.split(".");
	var testver2 = b.split(".");
	if( testver1.length != testver2.length ) {
		return 0;
	}
	for( var i=0 ; i<testver1.length ; i++ ) {
		var t1 = testver1[i]-0;
		var t2 = testver2[i]-0;

		if( t1 != t2 ) {
			if( t1 < t2 ) {
				return -1;
			}
			if( t1 > t2 ) {
				return 1;
			}
		}
	}
	return 0;
}

function navigateToTab( tab, theURL ) {
	if( is_in_player && plyrObj != null ) {
		if (!isBoxter() && (tab == "home" || tab == "musicguide" || tab == "musicstore" || tab == "browser" || tab == "channels")) {
			tab = "web";
		}
		plyrObj.OpenURLInPlayerBrowser(theURL,"_rp" + tab);
	} else {
		document.location.href = theURL; 
	}
}


function errorHandler() { 
    var results;
    return true;
} 
/* want to see js errors if any? comment out this line */
window.onerror = errorHandler;


function refreshMyRadioBox(){
	theSrc = radioBaseURL+"/myradio";
	setTimeout("frames['myRadioBox'].location.href=theSrc;",1500);
}

/*
COOKIE MANAGEMENT STUFF - 

To store a cookie, use:

	var cookieAccepted = setCookie( cookieName, cookieValue[, lifeTime[, path[, domain[, isSecure]]]] )

		cookieName is the name of the cookie (as a string) and can contain any characters.

		cookieValue is the value that the cookie stores (as a string) and can contain any characters.

		lifeTime is the amount of time in seconds that you want the cookie to last for (after which
		the user's computer will delete it). The default is until the browser is closed.

		path gives the path or directories that the cookie should be accessible from. The default is
		the current path. Alter this using ../ (up one directory) / starting at the base directory
		and subdirectoryName/ to start from the currentDirectory/subdirectoryName/

		domain gives the domain that the cookie is accessible from. This must have at least one . in it
		and in many browsers it must have at least two. The default is the current domain.

		isSecure can be true or false and says whether or not the cookie is only accessible on sites
		with a secure (https) connection.

If the user rejected the cookie, cookieAccepted will be false. In Opera, if cookies are on prompt,
the failure responce is received immediately, even if the user then accepts the cookie.

To retrieve a cookie, use:

	var myCookie = retrieveCookie( cookieNameAsAString );

To modify a cookie, simply set it again with the new settings;

To delete a cookie, use:

	var cookieDeletePermitted = setCookie( cookieNameAsAString, '', 'delete' );

	or set lifeTime to a less than 0 value.

If the user rejected the attempt to delete the cookie, cookieDeletePermitted will be false.
_______________________________________________________________________________________*/

function retrieveCookie( cookieName ) {
	/* retrieved in the format
	cookieName4=value; cookieName3=value; cookieName2=value; cookieName1=value
	only cookies for this domain and path will be retrieved */
	var cookieJar = document.cookie.split( "; " );
	for( var x = 0; x < cookieJar.length; x++ ) {
		var oneCookie = cookieJar[x].split( "=" );
		if( oneCookie[0] == escape( cookieName ) ) { return unescape( oneCookie[1] ); }
	}
	return null;
}

function setCookie( cookieName, cookieValue, lifeTime, path, domain, isSecure ) {
	if( !cookieName ) { return false; }
	if( lifeTime == "delete" ) { lifeTime = -10; } //this is in the past. Expires immediately.
	/* This next line sets the cookie but does not overwrite other cookies.
	syntax: cookieName=cookieValue[;expires=dataAsString[;path=pathAsString[;domain=domainAsString[;secure]]]]
	Because of the way that document.cookie behaves, writing this here is equivalent to writing
	document.cookie = whatIAmWritingNow + "; " + document.cookie; */
	document.cookie = escape( cookieName ) + "=" + escape( cookieValue ) +
		( lifeTime ? ";expires=" + ( new Date( ( new Date() ).getTime() + ( 1000 * lifeTime ) ) ).toGMTString() : "" ) +
		( path ? ";path=" + path : "") + ( domain ? ";domain=" + domain : "") + 
		( isSecure ? ";secure" : "");
	//check if the cookie has been set/deleted as required
	if( lifeTime < 0 ) { if( typeof( retrieveCookie( cookieName ) ) == "string" ) { return false; } return true; }
	if( typeof( retrieveCookie( cookieName ) ) == "string" ) { return true; } return false;

}


function setRecentlyPlayed(stationId){
	var rpCookie = retrieveCookie("RNRadioRecentlyPlayed");
	if (rpCookie == null) {
		setCookie("RNRadioRecentlyPlayed", stationId+"|", 1000000);
		return null;
	}
	setCookie("RNRadioRecentlyPlayed", ' ', 'delete' );
	setCookie("RNRadioRecentlyPlayed", rpCookie+stationId+"|", 1000000, "/");
	refreshMyRadioBox();

}

/* Primarily these are for the Edit My Radio popup */

function goAway(){
	window.opener.focus();
	window.self.close();
}

function openWin(name,url,width,height){
	newwindow = window.open(url,name,"width="+width+",height="+height+",toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0");
	newwindow.focus();
}

function checkAllowedOption(theOption){
	/* if theOption is either of these strings, it can not be added nor removed. */
	if(theOption != "rpNotOption" && theOption != "pNotOption"){
		return true;	
	}
}

function hasOptions(obj){
	if (obj!=null && obj.options!=null){
		return true;
	}
	return false;
}

function swapOptions(obj,i,j) {
	var o = obj.options;
	var i_selected = o[i].selected;
	var j_selected = o[j].selected;
	var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
	var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
	o[i] = temp2;
	o[j] = temp;
	o[i].selected = j_selected;
	o[j].selected = i_selected;
}

function sortSelect(obj) {
	var o = new Array();
	if (!hasOptions(obj)){
		return;
	}
	for (var i=0; i<obj.options.length; i++){
		o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;
		}
	if (o.length==0){
		return;
	}
	o = o.sort( 
		function(a,b){ 
			if ((a.text+"") < (b.text+"")) { return -1; }
			if ((a.text+"") > (b.text+"")) { return 1; }
			return 0;
			} 
		);
	
	for (var i=0; i<o.length; i++){
		obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
	}
}

function cleanUpOptions(obj){
	if (!hasOptions(obj)){
		return;
	}
	for (var i=(obj.options.length-1); i>=0; i--){ 
		var o=obj.options[i]; 
		if(!checkAllowedOption(obj.options[i].value)){
			obj.options[i] = null; 
		}
	} 
	obj.selectedIndex = -1; 

}


function removeOption(from){ 
	if (!hasOptions(from)){
		return;
	}
	for (var i=(from.options.length-1); i>=0; i--){ 
		var o=from.options[i]; 
		if (o.selected){
			if(checkAllowedOption(from.options[i].value)){
				from.options[i] = null; 
			}
		} 
	} 
	from.selectedIndex = -1; 
} 

function moveOptionUp(obj){
	if (!hasOptions(obj)){
		return;
	}
	for (i=0; i<obj.options.length; i++){
		if (obj.options[i].selected){
			if (i != 0 && !obj.options[i-1].selected){
				swapOptions(obj,i,i-1);
				obj.options[i-1].selected = true;
			}
		}
	}
}

function moveOptionDown(obj){
	if (!hasOptions(obj)){
		return;
	}
	for (i=obj.options.length-1; i>=0; i--){
		if (obj.options[i].selected){
			if (i != (obj.options.length-1) && ! obj.options[i+1].selected){
				swapOptions(obj,i,i+1);
				obj.options[i+1].selected = true;
			}
		}
	}
}

function copyOptions(from,to){
	var options = new Object();
	/* if the list is already at 20, can't add more */
	if(to.options.length == 20){
		alert("You can save up to 20 favorite stations.\n Please delete a station in your Favorite Stations list in order to save this station.");
		return;
	}
	if (hasOptions(to)){
		for (var i=0; i<to.options.length; i++){
			options[to.options[i].value] = to.options[i].text;
		}
	}
	if (!hasOptions(from)){
		return;
	}
	for(var i=0; i<from.options.length; i++){
		var o = from.options[i];
		if (o.selected){
			/* if it's empty or just not there, don't add it */
			if(checkAllowedOption(from.options[i].value)){					
				if (from.options[i].value && options[o.value] == null || options[o.value] == "undefined" || options[o.value]!=o.text){
					if (!hasOptions(to)){
						var index = 0;
					}else{
						var index=to.options.length;
					}
					to.options[index] = new Option( o.text, o.value, false, false);
				}
			}
			
		}
	}
	/*
		don't alphabetize
		if ((arguments.length<3) || (arguments[2]==true)){
			sortSelect(to);
		}
	*/
	cleanUpOptions(to);
	from.selectedIndex = -1;
	to.selectedIndex = -1;
}

function saveMyStations(obj){
	var theList = "";
	/* get all items in the favorite stations list and write them all into a pipe-delimited list*/
	for (var i = 0; i < obj.length; i++){
		if(i != obj.length-1){theList+=obj.options[i].value+"|";}
		else{theList+=obj.options[i].value;}	
	}
	/* put that list into the hidden form field and submit the form */
	document.getElementById("savedStations").value=theList;
	document.getElementById("stationsManager").submit();
}

function goToUrl(url,isPopUp){
	if(isPopUp == true){
		window.opener.document.location.href=url;
		goAway();
	}else{
		document.location.href=url;
	}
}

function utf8(wide) {
	 var c, s;
	 var enc = "";
	 var i = 0;
	 while(i<wide.length) {
	   c= wide.charCodeAt(i++);
	   // handle UTF-16 surrogates
	   if (c>=0xDC00 && c<0xE000) continue;
	   if (c>=0xD800 && c<0xDC00) {
	     if (i>=wide.length) continue;
	     s= wide.charCodeAt(i++);
	     if (s<0xDC00 || c>=0xDE00) continue;
	     c= ((c-0xD800)<<10)+(s-0xDC00)+0x10000;
	   }
	   // output value
	   if (c<0x80) enc += String.fromCharCode(c);
	   else if (c<0x800) enc += String.fromCharCode(0xC0+(c>>6),0x80+(c&0x3F));
	   else if (c<0x10000) enc += String.fromCharCode(0xE0+(c>>12),0x80+(c>>6&0x3F),0x80+(c&0x3F));
	   else enc += String.fromCharCode(0xF0+(c>>18),0x80+(c>>12&0x3F),0x80+(c>>6&0x3F),0x80+(c&0x3F));
	 }
	 return enc;
}
   
var hexchars = "0123456789ABCDEF";
   
function toHex(n) {
	return hexchars.charAt(n>>4)+hexchars.charAt(n & 0xF);
}
   
var okURIchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
   
function encodeURIComponentNew(s) {
     var s = utf8(s);
     var c;
     var enc = "";
     for (var i= 0; i<s.length; i++) {
       if (okURIchars.indexOf(s.charAt(i))==-1)
         enc += "%"+toHex(s.charCodeAt(i));
       else
         enc += s.charAt(i);
     }
}

function RPOnLogin(){
	location.reload();
}