function init(){
		getArchives();
}

function echeck(str) {
		var at="@"
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1){
		   alert("Invalid email address")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid email address")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   alert("Invalid email address")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		   alert("Invalid email address")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		   alert("Invalid email address")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		   alert("Invalid email address")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		   alert("Invalid email address")
		    return false
		 }

 		 return true					
	}

	
function validateForm(action){
  if(action=="comment"){
  	var emailID=document.getElementById('email');
  	var nameID=document.getElementById('name');
  	var locationID=document.getElementById('location');
  	var commID=document.getElementById('comment');
  	var ipID=document.getElementById('ip');
  	var storyID=document.getElementById('story');
  	var recaptcha_challenge_field=document.getElementsByName('recaptcha_challenge_field');
  	var recaptcha_response_field=document.getElementsByName('recaptcha_response_field');
	var emailMeID=document.getElementById('emailme');
	} else {
  	var emailID=document.getElementById('formEmail');
  	var nameID=document.getElementById('formName');
  	var topicID=document.getElementById('formTopic');
  	var commID=document.getElementById('formComment');
	}
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please enter your email address")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	if ((nameID.value==null)||(nameID.value=="")){
		alert("Please enter your name")
		nameID.focus()
		return false
	}

	if ((commID.value==null)||(commID.value=="")){
		alert("Please enter a comment")
		commID.focus()
		return false
	}
	var request = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
	var myPage="http://www.feedthegrid.net/processForm20.php";
	request.open("POST", myPage, true);
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	request.onreadystatechange = function(){
	   if (request.readyState == 4 && request.status == 200) {
			 if(action=="comment"){
  			 var myResponse=request.responseText.replace(/^\s+|\s+$/g,"");
  			 if(myResponse == "Success"){
  				 alert("Thanks for your comment!");
  				 location.reload(true);
  			 } else {
  				 alert(myResponse);
  				 Recaptcha.reload();
  				 document.getElementById('comment').focus();
  			 }
			 } 
			 if(action=="contact") {
  			 document.getElementById("contactFormDiv").style.display="none";
  			 document.getElementById("contactFormThanks").style.display="block";
			 }
		}
	};
	if(action=="comment"){
	   request.send("action=comment&story="+encodeURIComponent(storyID.value)+"&name="+encodeURIComponent(nameID.value)+"&email="+encodeURIComponent(emailID.value)+"&location="+encodeURIComponent(locationID.value)+"&comment="+encodeURIComponent(commID.value)+"&emailme="+emailMeID.checked+"&ip="+encodeURIComponent(ipID.value)+"&recaptcha_challenge_field="+recaptcha_challenge_field[0].value+"&recaptcha_response_field="+recaptcha_response_field[0].value);	
	} 
	if(action=="contact"){
		 request.send("action=contact&name="+encodeURIComponent(nameID.value)+"&email="+encodeURIComponent(emailID.value)+"&topic="+encodeURIComponent(topicID.value)+"&comment="+encodeURIComponent(commID.value));	
	}
 }

function showForm(){
  	var emailID=document.getElementById('formEmail');
  	var nameID=document.getElementById('formName');
  	var topicID=document.getElementById('formTopic');
  	var commID=document.getElementById('formComment');
	emailID.value='';
	nameID.value='';
	topicID.value='';
	commID.value='';
	document.getElementById("contactFormDiv").style.display="block";
	document.getElementById("contactFormThanks").style.display="none";	
}


var divContent=new Array();
divContent['time']=new Array();
divContent['source']=new Array();
divContent['country']=new Array();
divContent['feed']=new Array();
divContent['category']=new Array();


function sortByHits(a, b) {
    var x = parseInt(a[1]);
    var y = parseInt(b[1]);
    return ((x < y) ? 1 : ((x > y) ? -1 : 0));
	//return b-a;
}
function sortByItems(a, b) {
    var x = a[0].toUpperCase();
    var y = b[0].toUpperCase();
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}


function vote(comment, val){
	var request = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
	var myPage="http://www.feedthegrid.net/processForm20.php";
	request.open("POST", myPage, true);
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	request.onreadystatechange = function(){
		if (request.readyState == 4 && request.status == 200) {
			 alert("Thanks for voting!");
			 location.reload(true);
		}
	};
	request.send("action=vote&id="+comment+"&val="+val);					 
}

function markComment(comment, action){
	var request = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
	var myPage="http://www.feedthegrid.net/processForm20.php";
	request.open("POST", myPage, true);
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	request.onreadystatechange = function(){
		if (request.readyState == 4 && request.status == 200) {
			 if(action=="delete"){
			 	alert("Comment deleted!");
			 } else {
			 	alert("Thanks for alerting us!  This comment will be taken offline until we check it.");
			 }
			 location.reload(true);
		}
	};
	request.send("action="+action+"&id="+comment);					 
}

function performSearch(){
	s =document.getElementById('search').value;
	c=document.getElementById('category').options[document.getElementById('category').selectedIndex].text;
	r=document.getElementById('region').options[document.getElementById('region').selectedIndex].text;
	c=c.replace(/\s/g,"-");
	r=r.replace(/\s/g,"-");	
	s=s.replace(/[^A-Za-z0-9 ]+/,"");
	/*
	var oldUrl=window.location.href;
	oldUrl=oldUrl.replace(/\/page\/[0-9]+/,"");
	odlUrlBits=oldUrl.split(".net/");
	oldUrl=odlUrlBits[1];
	oldUrl=oldUrl.replace(/(related|id)\/[0-9]+\/$/,"/");
	oldUrl=oldUrl.replace(/(category|region|search|ksearch)\/[\w|%]+\//g,"");
	oldUrl=oldUrl.replace(/\/[0-9]+\/$/,"/");
	var newUrl=odlUrlBits[0]+".net/category/"+c+"/region/"+r+"/"+oldUrl;
	*/
	var newUrl="http://www.feedthegrid.net/category/"+c+"/region/"+r+"/";
	newUrl+=(s=="")?"":"search/"+s+"/";
	//var newUrl=(oldUrl.indexOf("/search/")>-1)?oldUrl.replace(/\/search\/([^/\.]+)\// ,"/search/"+searchstr+"/"):oldUrl+"search/"+searchstr+"/";
	window.location.href=newUrl;
}

function getArchives(){
  var months=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	var nowDate=new Date();
	var archiveDate=new Date();
	var tempDate=new Date();
	var oneMinute = 60 * 1000;
	var oneHour = oneMinute * 60;
	var oneDay = oneHour * 24;
	var oneWeek = oneDay * 7;
	var oneMonth = oneWeek * 4;
	nowDate.setDate(1);
	var innerCode="<ul>";
	for(n=0;n<12;n++){
		 tempDate=nowDate.getTime()-(n*oneMonth);
		 archiveDate.setTime(tempDate);
		 archiveMonth=(archiveDate.getMonth()<9)?"0"+(archiveDate.getMonth()+1):""+(archiveDate.getMonth()+1);
		 archiveLink="<li><a href='http://www.feedthegrid.net/archive/"+archiveDate.getFullYear()+archiveMonth+"/'>"+months[archiveDate.getMonth()]+" "+archiveDate.getFullYear()+"</a></li>";
		 innerCode+=archiveLink;
		 
	}
	innerCode+="</ul>";
	document.getElementById("archive").innerHTML=innerCode;
}
