﻿//**********************************************************************************************************************************
//*																																   *
//*												Administrative Related Javascript for myrunbuddy.com							   *
//*												Developed by Brevetoxin															   *
//* 											4/30/2008																		   *
//*																																   *
//**********************************************************************************************************************************							

var adminPath = "http://www.newstudentdriver.com/admin/";
var actionReceiver = adminPath+"actionReceiver.php";
var listReceiver = adminPath+"listReceiver.php";

//**********************************************************************************************************************************
//*												Scriptaculous Stuff																   *
//*												Scriptacilus.js is required														   *
//**********************************************************************************************************************************

Effect.OpenUp = function(element) {
     element = $(element);
     new Effect.SlideDown(element, arguments[1] || {});
 }

 Effect.CloseDown = function(element) {
     element = $(element);
     new Effect.SlideUp(element, arguments[1] || {});
 }

 Effect.Combo = function(element) {
	 objOverlay = document.getElementById('overlay'); //Find the greyscale background element
     element = $(element); 
     if(element.style.display == 'none') { //Figure out if the animated elements is currently displayed or not
          new Effect.OpenUp(element, arguments[1] || {}); //If not, display it
 			arrayPageSize = getPageSize(); //Get the size of the page for the greyscale background
 			objOverlay.style.height = (arrayPageSize[1] + 'px'); //Set the height of the background
			objOverlay.style.display = 'block'; //Display the background.
     }else {  
          new Effect.CloseDown(element, arguments[1] || {}); //If it's displayed, close it.
          objOverlay.style.display = 'none'; //Remove the background.
     }
 }
 
 //********************************************************************************************************************************
 //*											Administrative Functions														  *
 //********************************************************************************************************************************
 
 //********************************************************************************************************************************
 //*filterList = filters a list based on the filter field and criterion specified                                                 *
 //********************************************************************************************************************************
 function filterList(element,value)
 {
 	if(element == "field")
 	{
        if(!value)
        {
        	document.getElementsByName("filterCriterion")[0].value = "";
        	listObj.filterCriterion = "";
        }
 		listObj.filterField = value;
 	}
 	if(element == "criterion")
 	{
 		listObj.filterCriterion = value;
 	}
 	refreshList(listObj);
 }

 //********************************************************************************************************************************
 //*getPageSize - retrieve the size of the page in the active window															  *
 //********************************************************************************************************************************
 function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

//**************************************************************************************************************************************************
//*handleSuccess - Results in some action (or inaction) when an AJAX action is successful														   *
//**************************************************************************************************************************************************
function handleSuccess(action,obj)
{
	switch(action)
	{
		case "refreshList":
			refreshList(obj);
		break;
	}
}
			
//**************************************************************************************************************************************************
//*initOverlay - Initializes the page inactivating semi-transparent overlay																		   *
//**************************************************************************************************************************************************	
function initOverlay()
{
	var objBody = document.getElementsByTagName("body").item(0);
	var objOverlay = document.createElement('div');
	objOverlay.setAttribute('id','overlay');
	objOverlay.style.display = 'inline';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '90';
 	objOverlay.style.width = '100%';
 	objBody.insertBefore(objOverlay, objBody.firstChild);
 }
 
//****************************************************************************************************************************************************
//*newList - Chooses a new list and then refreshes the list                                                                                          *
//****************************************************************************************************************************************************
function newList(listName)
{
	listObj.filterField = '';
	listObj.filterCriterion = '';
	listObj.sortField = '';
	listObj.sortDirection = 'ASC';
	listObj.list = listName;
	refreshList(listObj,"new");
}
//****************************************************************************************************************************************************
//*refreshList - Refreshes the list on the screen - Requires prototype                                                                               *
//****************************************************************************************************************************************************
function refreshList(listObj,tag)
{
    var tag = (tag == null) ? "old" : tag;
	var url = listReceiver;
	var listContainer = document.getElementById(listObj.divID);
    params = 'list='+listObj.list+
       			'&filterField='+listObj.filterField+
       			'&filterCriterion='+listObj.filterCriterion+
       			'&sortField='+listObj.sortField+
       			'&sortDirection='+listObj.sortDirection;
	var req = new Ajax.Request(url, {
	method: 'post',
	parameters: params,
	onSuccess: function(response){ 
//			alert(response.responseText);
			var json = response.responseText.evalJSON();
			listTable =document.createElement("table");
			if(json.columns)
			{
				newRow = listTable.insertRow(-1);
				for(i=0;i<json.columns.length;i++)
				{
					newCell = newRow.insertCell(-1);
					newCell.innerHTML = json.columns[i].header;
				}				
			}
			listContainer.appendChild(listTable);
			if(json.rows)
			{	
				if(tag == "new") //If the tag is set to "new", we need to create the list header
				{
					var headerContents = '<div id="listHeader">'+json.header+'</div><div id="listContents">';
				}
				var listContents = '<table class="listTable">';		
				for(i=0;i<json.rows.length;i++)
				{
					newRow = listTable.insertRow(-1);
//					listContents+='<tr';
					if(json.rows[i]["active"] == 'Deleted')
					{
						newRow.className="deleted";
//						listContents+=' class="deleted"';
					}
//					listContents+='>';
					for(var j in json.rows[i])
					{
						newCell = newRow.insertCell(-1);
						newCell.innerHTML = json.rows[i][j];
//						listContents+='<td align=center>'+json.rows[i][j]+'</td>';
           			}
//   					listContents+='</tr>'; 
				}
//				listContents+='</table>';
				if(tag == "new") //If the tag is set to "new", we need to end the listContents div and post the header + contents
				{
					listContents+='</div>';
//					document.getElementById(listObj.divID).innerHTML = headerContents+listContents;
				}
				else //Otherwise, just update the listContents div with the listContents
				{
//					document.getElementById("listContents").innerHTML = listContents;
				}
			}
			else
			{
				document.getElementById(listObj.divID).innerHTML = "There is no such list";
			}
	},
	onFailure: function(){alert("it didn't work!")}		
	});
}


//****************************************************************************************************************************************************
//*sendAction - Sends a misc. action to the server for processing - Requires Prototye                                                                *
//****************************************************************************************************************************************************
function sendAction(id,action,successAction,obj)
{
	var url = actionReceiver;
	params = 'action='+action+'&id='+id;
	var req = new Ajax.Request(url, {
	method: 'post',
	parameters: params,
	onSuccess: function(response){
		var json = response.responseText.evalJSON();
		if (json.code == 1)
		{
			handleSuccess(successAction,obj);
			return true;
		}
		else
		{
			alert(json.message);
			return false;
		}
	},
	onFailure: function(){alert("it didn't work!")}
	});
}

//*****************************************************************************************************************************************************
//*sortList - sets sort criterions and refreshes list                                                                                                 *
//*****************************************************************************************************************************************************
function sortList(sortField)
{
	if(sortField == listObj.sortField) //If the current sortField is equal to the field we're already sorting on, switch direction
	{
		if(listObj.sortDirection == "ASC")
		{
			listObj.sortDirection = "DESC";
		}
		else
		{
			listObj.sortDirection = "ASC";
		}
	}
	else //If it's new, set the sort direction to ASC by default
	{
		listObj.sortDirection = "ASC";
	}
//	alert(listObj.sortDirection);
	listObj.sortField = sortField;
	refreshList(listObj);
}
