function debug(textArea, message, append) {
    try { 
        if(g_Debug) {
            var currentTime = new Date();
            var timeNow = currentTime.getHours() + ":" + currentTime.getMinutes() + ":" + currentTime.getSeconds();
                        
            var html = $("#" + textArea).text();
            
            if(append || g_DebugMsgAppend) {
                html = " ; " + timeNow + " " + message + "\r\n" + html;
            }
            else {
                html = " ; " + timeNow + " " + message + "\r\n";
            }
            $("#" + textArea).text(html);    
        }
    }
    catch(e)
    {
    }
}

function isUrlImage(url) 
{
	var regExp = /(gif|jpg|png)$/i ;
    if(url != "") 
    {
        return regExp.test(url);
    }

    return false;
}


function isUrlFlash(url) 
{
	var regExp = /(swf)$/i ;
    if(url != "") 
    {
        var result = regExp.test(url);
        
        if(!result) 
        {   
            if(url.indexOf(".swf?") > 0) 
            {
                result = true;
            } 
        }
        
        return result;
    }

    return false;
}

function isUrlWinVideo(url)  
{
	var regExp = /(wmv|asf|asx)$/i ;
    if(url != "") 
    {
        return regExp.test(url);
    }

    return false;
}

function isUrlWinAudio(url) 
{
	var regExp = /(wma|mp3|asf|asx)$/i ;
    if(url != "") 
    {
        return regExp.test(url);
    }

    return false;
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function isUrlEmpty(url) 
{
	if(url == null || trim(url) == "" || trim(url) == "http://")
	{
		return true;
	}

	return false;
}

//function urlDecode(input)
//{
//    var output = "";
//
//    output = unescape(input);
//
//	output = output.replace(/&&/g,"&");
//	output = output.replace(/\+/g," ");
//	output = output.replace(/&apos;/g, "'");
//
//    return output;
//}

function urlEncode(input)
{
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = input;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

    return encoded;
}

function urlDecode(input)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = input;
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
	
   return plaintext;
}


function IsBrowser()
{   
    var agt=navigator.userAgent.toLowerCase();

    //this.major = parseInt(navigator.appVersion);
    //this.minor = parseFloat(navigator.appVersion);

    //debug("testMsg", agt, true); 
    
    this.opera = (agt.indexOf("opera") != -1);
   
    //this.ie = (agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1);
    this.ie = (agt.indexOf("msie") != -1);
    
    this.nav  = ((agt.indexOf('mozilla') != -1) && 
                 (agt.indexOf('spoofer')==-1 && agt.indexOf('compatible') == -1));
                 
    this.chrome = (agt.indexOf("chrome") != -1);
    
    this.linux = (agt.indexOf("linux") != -1);
    
    this.ie8 = (agt.indexOf("msie 8") != -1);
    
}

function bookmark(title, url)
{
    try {
        var isBrowser = new IsBrowser();    
        
        if(isBrowser.ie) 
        {
            window.external.AddFavorite(url, title);
        }
        else if(isBrowser.opera) {
            var a = document.createElement("A");
            a.rel = "sidebar";
            a.target = "_bookmark";
            a.title = title;
            a.href = url;
            a.click();
        }
        else 
        {
            window.sidebar.addPanel(title, url, "");
        }
    }
    catch(e) {}
}

function openClickThruWindow(windowName, clickThruUrl)
{  
    try
    {
        window.open(clickThruUrl, windowName, 'resizable=yes,scrollbars=1,status=yes,toolbar=yes'); 
    }
    catch (e)
    {
    }

	return true;
} 

function openWindow(windowName, clickThruUrl)
{   
    var url = clickThruUrl; 
    if(url != "") 
    {
        window.open(url, windowName, 'resizable=yes,scrollbars=1,status=yes,toolbar=yes,width=800,height=600'); 
    }

	return true;
}

function popunderWindow(windowName, url)
{   
    try {
        if(url != "") 
        {
            window.open(url, windowName, 'resizable=yes,scrollbars=1,status=yes,toolbar=yes,width=800,height=600').blur(); 
        }
    }
    catch(err) {}
    
	return true;
}

function getPlayerCookieName() 
{   
    return g_PlayerRegistrationCookieName + "_" + g_SAStationID;
}

function getPlayerCookieZip() 
{   
    return g_PlayerRegistrationCookieZip + "_" + g_SAStationID;
}

function getPlayerCookieGender() 
{   
    return g_PlayerRegistrationCookieGender + "_" + g_SAStationID;
}

function getWindowSize(currWidth, currHeight)
{
    var w = 0;
    var h = 0;
    try {
        if(typeof(window.innerWidth ) == 'number' ) {
            //Non-IE
            w = window.innerWidth;
            h = window.innerHeight;
        } 
        else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
            //IE 6+ in 'standards compliant mode'
            w = document.documentElement.clientWidth;
            h = document.documentElement.clientHeight;
        } 
        else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
            //IE 4 compatible
            w = document.body.clientWidth;
            h = document.body.clientHeight;
        }
    
    	return {width:w,height:h};
    }
    catch(e)
    {
	    return {width:currWidth,height:currHeight};
    }
    
}  

function getWindowSize1(currWidth, currHeight)
{
    var w = 0;
    var h = 0;
    try {
        if(!window.innerWidth)
    	{
    	    if(document.documentElement.clientWidth != 0)
    	    {   
        		w = document.documentElement.clientWidth;
        		h = document.documentElement.clientHeight;
    	    }
    	    else
    	    {
        	    //w = document.body.clientWidth;
        		//h = document.body.clientHeight;
        		w = currWidth;
        		h = currHeight;
    	    }
    	}
    	else
    	{
    	    w = window.innerWidth;
    	    h = window.innerHeight;
    	}
    
    	return {width:w,height:h};
    }
    catch(e)
    {
	    return {width:currWidth,height:currHeight};
    }
    
}  

function resizePlayerScreen(width, height)
{   
    try 
    {
        var isBrowser = new IsBrowser();
        
    	if(width > 0)
    	{                
    	    if(!window.innerWidth || isBrowser.ie)
    	    {
    		    window.resizeTo(width + 10, height + 50);
    	    }
    	    else
    	    {
        		window.innerWidth = width;
        		window.innerHeight = height;                
    	    }

    	    var winSize = getWindowSize(width, height);
    
    	    var curWidth = winSize.width;
    	    var curHeight = winSize.height;
    
    	    var diffWidth = width - curWidth;
    	    var diffHeight = height - curHeight;
    	        
    	    //alert(diffWidth + " x " + diffHeight);
    	    var newWidth = width + diffWidth;
    	    var newHeight = height + diffHeight;
    	    
    	    if(!window.innerWidth)
    	    {
                if(isBrowser.ie8) 
                {
                    window.resizeTo(width + 20, height + 95);
                }
    	    }
    	    else
    	    {
        		window.innerWidth = newWidth;
        		window.innerHeight = newHeight;                
    	    }
    	}
    }   
    catch(e)
    {
    }
}   

function generateClickThruURL(url, adId, callLetter)
{
    try 
    {
        if(url.length > 8 && url.indexOf("streamaudio") < 0 && url.indexOf("adtrack.asp") < 0 && parseInt(adId) > 0) 
        {
            return "http://www.streamaudio.com/stations/player/pages/newplayer/nowplay/adtrack.asp?type=Replacement&adid=" + adId + "&nexturl=" + urlEncode(url) + "&station=" + callLetter;
        }
        else 
        {
            return url;
        }  
    } 
    catch(e)
    {
        return url;
    }
}

function generateGWClickThruURL(url, gwId, stationId)
{
    try 
    {
        if(url.length > 8 && url.indexOf("streamaudio") < 0 && url.indexOf("TrackClickThru.aspx") < 0) 
        {
            return "http://www.streamaudio.com/client/GateWayUtility/TrackClickThru.aspx?Gateway_ID=" + gwId + "&Station_ID=" + stationId + "&NextURL=" + urlEncode(url);
        }
        else 
        {
            return url;
        }
    }
    catch(e)
    {
        return url;
    }
}