google.load("feeds", "1");
 
function getRss(url, divid) {
  var feed = new google.feeds.Feed(url);
  feed.divid = divid;
  feed.load(function(result) {
    if (!result.error) {
		
      var container = document.getElementById(feed.divid);
	  
      for (var i = 0; i < result.feed.entries.length; i++) {
        var entry = result.feed.entries[i];
  
		container.innerHTML += getRssPost(entry.link,entry.title, entry.publishedDate);
      }
    }
  });
}
function getLastBlogItem(url,divid){
	var feed = new google.feeds.Feed(url);
  feed.divid = divid;
 
  feed.load(function(result) {
  
    if (!result.error||result.error == undefined) {
		
      var container = document.getElementById(feed.divid);
	  
      for (var i = 0; i < result.feed.entries.length; i++) {
        var entry = result.feed.entries[i];
        
		
		var str = container.innerHTML;
		str = str.replace("{{TITLE}}",entry.title);
		if (entry.publishedDate != "") {
			var d = new Date(entry.publishedDate);
			//alert(d.getFullYear())
			var year = d.getFullYear();
			var month = d.getMonth() + 1;
			if (month < 10) {
				month = "0" + month;
			}
			var day = d.getDate();
			if (day < 10) {
				day = "0" + day;
			}
			var dStr= year + "-" + month + '-' + day;
			str = str.replace("{{DATE}}",dStr);
		}
		
		container.innerHTML = str;
      }
    }
  });
}
google.setOnLoadCallback(initialize);


function submitNewsletter(){
	var obj = document.getElementById("newsletter_email");
	
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	
	if (reg.test(obj.value) == false) {
		var objresp = document.getElementById("newsletter_email_error");
		objresp.innerHTML = '<span class="red">E-post har fel format</span><br />';
	}
	else {
	
	
	
		var xmlHttp;
		try {
			/* Firefox, Opera 8.0+, Safari */
			xmlHttp = new XMLHttpRequest();
		} 
		catch (e) {
			/* newer IE */
			try {
				xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			} 
			catch (e) {
				/* older IE */
				try {
					xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
				} 
				catch (e) {
					alert("Your browser is old and does not have AJAX support!");
					return false;
				}
			}
		}
		xmlHttp.onreadystatechange = function(){
			if (xmlHttp.readyState == 4) {
				/* this puts the value into an alert */
				
				rewriteNewsletterDiv(xmlHttp);
			}
		}
		xmlHttp.open("GET", "/ajax/newsletter_signup.php?email="+obj.value, true);
		xmlHttp.send(null);
	}
}
function rewriteNewsletterDiv(xmlHttp){
	 var obj = document.getElementById("puff_content_newsletter");
	 var str = '<div class="puff_newsletter_h1">Nyhetsbrev</div>';
     str += '<span class="white">Din e-post har blivit registrerad</span><br />';
	 obj.innerHTML = str;
        
	 
}



//OBJECTS

//objects inside the RSS2Item object
function RSS2Enclosure(encElement)
{
	if (encElement == null)
	{
		this.url = null;
		this.length = null;
		this.type = null;
	}
	else
	{
		this.url = encElement.getAttribute("url");
		this.length = encElement.getAttribute("length");
		this.type = encElement.getAttribute("type");
	}
}

function RSS2Guid(guidElement)
{
	if (guidElement == null)
	{
		this.isPermaLink = null;
		this.value = null;
	}
	else
	{
		this.isPermaLink = guidElement.getAttribute("isPermaLink");
		this.value = guidElement.childNodes[0].nodeValue;
	}
}

function RSS2Source(souElement)
{
	if (souElement == null)
	{
		this.url = null;
		this.value = null;
	}
	else
	{
		this.url = souElement.getAttribute("url");
		this.value = souElement.childNodes[0].nodeValue;
	}
}

//object containing the RSS 2.0 item
function RSS2Item(itemxml)
{
	//required
	this.title;
	this.link;
	this.description;

	//optional vars
	this.author;
	this.comments;
	this.pubDate;
	this.datePosted;

	//optional objects
	this.category;
	this.enclosure;
	this.guid;
	this.source;

	var properties = new Array("title", "link", "description", "author", "comments", "pubDate","datePosted");
	var tmpElement = null;
	for (var i=0; i<properties.length; i++)
	{
		tmpElement = itemxml.getElementsByTagName(properties[i])[0];
		if (tmpElement != null)
			eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
	}

	this.category = new RSS2Category(itemxml.getElementsByTagName("category")[0]);
	this.enclosure = new RSS2Enclosure(itemxml.getElementsByTagName("enclosure")[0]);
	this.guid = new RSS2Guid(itemxml.getElementsByTagName("guid")[0]);
	this.source = new RSS2Source(itemxml.getElementsByTagName("source")[0]);
}

//objects inside the RSS2Channel object
function RSS2Category(catElement)
{
	if (catElement == null)
	{
		this.domain = null;
		this.value = null;
	}
	else
	{
		this.domain = catElement.getAttribute("domain");
		this.value = catElement.childNodes[0].nodeValue;
	}
}

//object containing RSS image tag info
function RSS2Image(imgElement)
{
	if (imgElement == null)
	{
	this.url = null;
	this.link = null;
	this.width = null;
	this.height = null;
	this.description = null;
	}
	else
	{
		imgAttribs = new Array("url","title","link","width","height","description");
		for (var i=0; i<imgAttribs.length; i++)
			if (imgElement.getAttribute(imgAttribs[i]) != null)
				eval("this."+imgAttribs[i]+"=imgElement.getAttribute("+imgAttribs[i]+")");
	}
}

//object containing the parsed RSS 2.0 channel
function RSS2Channel(rssxml)
{
	//required
	this.title;
	this.link;
	this.description;

	//array of RSS2Item objects
	this.items = new Array();

	//optional vars
	this.language;
	this.copyright;
	this.managingEditor;
	this.webMaster;
	this.pubDate;
	this.datePosted;
	this.lastBuildDate;
	this.generator;
	this.docs;
	this.ttl;
	this.rating;

	//optional objects
	this.category;
	this.image;

	var chanElement = rssxml.getElementsByTagName("channel")[0];
	var itemElements = rssxml.getElementsByTagName("item");

	for (var i=0; i<itemElements.length; i++)
	{
		Item = new RSS2Item(itemElements[i]);
		this.items.push(Item);
		//chanElement.removeChild(itemElements[i]);
	}

	var properties = new Array("title", "link", "description", "language", "copyright", "managingEditor", "webMaster","datePosted", "pubDate", "lastBuildDate", "generator", "docs", "ttl", "rating");
	var tmpElement = null;
	for (var i=0; i<properties.length; i++)
	{
		tmpElement = chanElement.getElementsByTagName(properties[i])[0];
		if (tmpElement!= null)
			eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
	}

	this.category = new RSS2Category(chanElement.getElementsByTagName("category")[0]);
	this.image = new RSS2Image(chanElement.getElementsByTagName("image")[0]);
}

//PROCESSES

//uses xmlhttpreq to get the raw rss xml
function getRSS2(url, divid)
{
	//call the right constructor for the browser being used
	var xmlHttp;
	try {
		/* Firefox, Opera 8.0+, Safari */
		xhr = new XMLHttpRequest();
	} 
	catch (e) {
		/* newer IE */
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) {
			/* older IE */
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) {
				alert("Your browser is old and does not have AJAX support!");
				return false;
			}
		}
	}
	

	//prepare the xmlhttprequest object
	//xhr.setRequestHeader("Cache-Control", "no-cache");
	//xhr.setRequestHeader("Pragma", "no-cache");
	
	xhr.open("GET",url,true);
	
	xhr.divid = divid;
	xhr.onreadystatechange = function() {
		
		if (xhr.readyState == 4)
		{
			
			if (xhr.status == 200)
			{
				if (xhr.responseText != null)
					processRSS(xhr.responseXML, this.divid);
				else
				{
					//alert("Failed to receive RSS file from the server - file not found.");
					return false;
				}
			}
			//else
				//alert("Error code " + xhr.status + " received: " + xhr.statusText+" : "+xhr.responseXML);
		}
	}
	
	//send the request
	xhr.send(null);
}

//processes the received rss xml
function processRSS(rssxml, divid)
{
	
	var tempRSS = new RSS2Channel(rssxml);
	showRSS(tempRSS, divid);
}

//shows the RSS content in the browser
function showRSS(tempRSS,divid)
{
	
	var container = document.getElementById(divid);
	  

	var len = (tempRSS.items.length<5)?tempRSS.items.length:4;
	for(var i = 0;i<len;i++){
		
		
		//alert(RSS.items[i].datePosted);
		if(divid == "rss_feed_damfotboll"){
			container.innerHTML += getRssPost(tempRSS.items[i].link,tempRSS.items[i].title, tempRSS.items[i].datePosted);
		}else{
			container.innerHTML += getRssPost(tempRSS.items[i].link,tempRSS.items[i].title, tempRSS.items[i].pubDate);
		}
		
	}
	//we're done
	//document.getElementById("chan").style.visibility = "visible";
	return true;
}


var xhr;


function getRssPost(link, title, pubdate){
	var str = "";
		
		str += '<a href="'+link+'" target="_blank">';
		if (pubdate != "") {
			var d = new Date(pubdate);
			
			if (d == "Invalid Date"||d == "NaN") {
				str += pubdate.split(" ")[0] + '&nbsp;';
			}else {
					
				var year = d.getFullYear();
				var month = d.getMonth() + 1;
				if (month < 10) {
					month = "0" + month;
				}
				var day = d.getDate();
				if (day < 10) {
					day = "0" + day;
				}
				str += year + "-" + month + '-' + day + '&nbsp;';
			}
		}
		str += '<span class="highlight">'+title+'</span></a>';
		//str += entry.contentSnippet+"<br />";
		
		
		str += "<br />";
		return str;
}
