﻿function getXmlNode(xml, name)
{
    var value = "";
    
    if(xml)
    {
        var nodes = $(xml).find(name);
        
        if(nodes && nodes.length > 0 && nodes[0].firstChild)
        {
            value = nodes[0].firstChild.nodeValue;
        }
    }
    return value;
}

function clearTable(tbodyId)
{
	var tbody = d.getElementById(tbodyId);

    	var element;

    	while(element = tbody.firstChild)
    	{
        	tbody.removeChild(element);
    	}
}

function escapeSpecialChars(str)
{
    var index = 0;

    //replace ampersand
    while(index < str.length && str.indexOf("&", index) >= 0)
    {
        index = str.indexOf("&", index);
        
        //split the string at the index
        var str1 = str.substr(0, index + 1);
        var str2 = str.substring(index + 1, str.length)
        
        str = str1 + "amp;" + str2;
        index = index + 4;
    }
    
    //replace >
    while(str.indexOf(">") >= 0)
    {
        str = str.replace(">", "&gt;");
    }
    
    //replace <
    while(str.indexOf("<") >= 0)
    {
        str = str.replace("<", "&lt;");
    }
    
    //replace %
    while(str.indexOf("%") >= 0)
    {
        str = str.replace("%", "&#37;");
    }
    
    //replace '
    while(str.indexOf("'") >= 0)
    {
        str = str.replace("'", "&apos;");
    }
    
    //replace \
    while(str.indexOf("\"") >= 0)
    {
        str = str.replace("\"", "&quot;");
    }
    
    //alert(str);
    return str;
}

function escapeforXML(str)
{
	var re = new RegExp("&", "g");
	str = str.replace(re, "&amp;");
    
	var re1 = new RegExp(">", "g");
	str = str.replace(re1, "&gt;");
    
	var re2 = new RegExp("<", "g");
	str = str.replace(re2, "&lt;");
    
	var re3 = new RegExp("'", "g");
	str = str.replace(re3, "&apos;");
    
	var re4 = new RegExp("\"", "g");
	str = str.replace(re4, "&quot;");
    
    return str;
}

function replaceSpecialChars(str)
{
    var index = 0;
	
    while(str.indexOf("&gt;") >= 0)
    {
        str = str.replace("&gt;", ">");
    }
	//str.replace(/&gt;/g, ">");
    
    while(str.indexOf("&lt;") >= 0)
    {
        str = str.replace("&lt;", "<");
    }
	//str.replace(/&lt;/g, "<");
	
    while(str.indexOf("&#37;") >= 0)
    {
        str = str.replace("&#37;", "%");
    }
    //str.replace(/&#37;/g, "%");
	
    while(str.indexOf("&apos;") >= 0)
    {
        str = str.replace("&apos;", "'");
    }
    //str.replace(/&apos;/g, "'");
	
    while(str.indexOf("&quot;") >= 0)
    {
        str = str.replace("&quot;", "\"");
    }
    //str.replace(/&#37;/g, "%");
	
    str = replaceAmps(str);
    
    return str;
}

function replaceAmps(str)
{
    while(str.indexOf("&amp;") >= 0)
    {
        str = str.replace("&amp;", "&");
    }
    
    return str;
}

function checkForHTML(str) 
{
	//URLs starting with http://, https://, or ftp://
    var replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
    var replacedText = str.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');

    //URLs starting with www. (without // before it, or it'd re-link the ones done above)
    var replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
    var replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');

    //Change email addresses to mailto:: links
    var replacePattern3 = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;
    var replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');

    return replacedText;
}

function getWindowSize()
{
    var myWidth = 0, myHeight = 0;
    
    if( typeof( window.innerWidth ) == "number" ) 
    {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } 
    else if(d.documentElement && (d.documentElement.clientWidth || d.documentElement.clientHeight)) 
    {
        //IE 6+ in 'standards compliant mode'
        myWidth = d.documentElement.clientWidth;
        myHeight = d.documentElement.clientHeight;
    } 
    else if(d.body && (d.body.clientWidth || d.body.clientHeight)) 
    {
        //IE 4 compatible
        myWidth = d.body.clientWidth;
        myHeight = d.body.clientHeight;
    }
    
    return [myWidth, myHeight];
}

function createCookie(name,value,days) 
{
	if (days) 
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else
	{   
	    var expires = "";
	}
	
	d.cookie = name+"="+value+expires+"; path=/";
}

function removeCookie(name) 
{
	///createCookie(name, "", -1);
}

function readCookie(name) 
{
	var nameEQ = name + "=";
	var ca = d.cookie.split(';');
	
	for(var i=0;i < ca.length;i++) 
	{
		var c = ca[i];
		while (c.charAt(0)==' ')
		{ 
		    c = c.substring(1,c.length);
		}
		
		if (c.indexOf(nameEQ) == 0)
		{ 
		    return c.substring(nameEQ.length,c.length);
		}
	}
	
	return null;
}

function getDomain()
{
    //look at the current domain
    if(document.domain == "grant.blacklightnewmedia.com")
    {
        return "grant.blacklightnewmedia.com/sotd";
    }
    else
    {   
    	if(document.domain == "localhost")
    	{
    		return "localhost/indiestreetcred";
    	}
    	
    	return document.domain;
    }
}

function findPos(obj) 
{
	var curleft = curtop = 0;
	if (obj.offsetParent) 
	{
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		
		while (obj = obj.offsetParent) 
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	
	return [curleft,curtop];
}

function roundAvg(avg)
{
    return Math.round(avg*10)/10;
}

function getAlbumArt(id)
{
    var tr = d.getElementById(id);
    
    if(tr == null)
    {
        return "";
    }
    
    var hiddens = tr.getElementsByTagName("input");
    
    //albumart is the second
    return hiddens[1].value;
}

function getFullAlbumText(id)
{
    var tr = d.getElementById(id);
    
    if(tr == null)
    {
        return "";
    }
    
    var spans = tr.getElementsByTagName("span");
    
    var artist = spans[1].firstChild.nodeValue;
    var album = spans[2].firstChild.nodeValue;
    
    return artist + " - " + album;
}