//short hand functions
function $(divID) {
   return document.getElementById(divID);
}
//essentially a GET url parameter for javascript
function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

//creates variable ajaxRequest for XMLHttpRequest for different browsers
function ajaxFunction(){
	var ajaxRequest;
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				//browsers all not support, rare case
				alert("Browser does not support HTTP Request");
				return false;
			}
		}
	}
	return ajaxRequest;
}
/****************************************************************************************/
/********************** NEWS.PHP ********************************************************/
/*****************************************************************************************/
//displays news feeds
//type can be "feed" or "title"
function show_news(numFeeds, type, divID) { 
   if($(divID) != null) { //check if the div exists
      var htmlRequest = ajaxFunction();
      $(divID+"_load").style.display = 'block'; //display the loading screen, assumes that the loading div is the id followed by _load
      
      htmlRequest.onreadystatechange = function(){
         if(htmlRequest.readyState == 4){
            $(divID+"_load").style.display = 'none';//hide the loading screen
            $(divID).innerHTML = htmlRequest.responseText;
         } 
      }
      //send query to backend and get the output, num acts as the story id if it's to display a story
      //and acts as the number of stories to display if it's to show feeds
      var url = "public_php/news_feed.php?action="+type+"&num="+numFeeds;
      htmlRequest.open("GET", url, true);
      htmlRequest.send(null);
   }
}
function show_story(id, divID) {
   if($(divID) != null) { //check if the div exists
      var htmlRequest = ajaxFunction();
      $(divID+"_load").style.display = 'block'; //display the loading screen, assumes that the loading div is the id followed by _load
      
      htmlRequest.onreadystatechange = function(){
         if(htmlRequest.readyState == 4){
            $(divID+"_load").style.display = 'none';//hide the loading screen
            $(divID).innerHTML = htmlRequest.responseText;
         } 
      }
      //send query to backend and get the output, num acts as the story id if it's to display a story
      //and acts as the number of stories to display if it's to show feeds
      var url = "public_php/news_feed.php?action=story&num="+id;
      htmlRequest.open("GET", url, true);
      htmlRequest.send(null);
   }
}


/*
note: htmlRequest.readyState == the following:
0	The request is not initialized
1	The request has been set up
2	The request has been sent
3	The request is in process
4	The request is complete
*/