//starts the 'tabbed' content
function init () {
	var tabs = document.getElementsByClassName('tabs');
	for (var i = 0; i < tabs.length; i++) {
		$(tabs[i].id).onclick = function () {
			getTabData(this.id);
		}
	}
}
function getTabData(id) {
	var url = 'includes/get_content.php';
	var rand = Math.random(9999);
	var pars = 'id=' + id + '&rand=' + rand;
	var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onLoading: showLoad, onComplete: showResponse} );
}
function showLoad () {
	$('ajaxload').style.display = 'block';
	parent.parent.document.getElementById('ajaxload').style.display = 'block';
}
function showResponse (originalRequest) {
	var newData = originalRequest.responseText;
	$('ajaxload').style.display = 'none';
	
	if(newData.substring(0,7) == 'Element'){
		eval(newData);
	}
	else{
		$('ajaxmessage').style.display = 'none'; //display message
		$('ajaxcontent').innerHTML = newData;
	}
	
	originalRequest.responseText.evalScripts();
}


/****************************
	FORM FUNCTIONS
****************************/
function submit_form (f,b,n) {
   var pars = $(f).serialize(true);  // This is the ID attached to the form tag.\
   if(typeof(n) == 'undefined' || n == 'undefined'){ n=""; }
   
   //alert("n is equal to '" + n + "'");
   
   // Now let's send it to AJAX.
   new Ajax.Request(pars.FILE + '?n='+n, {
        method:'post',
		parameters: pars,
		onLoading: showLoad,
		onSuccess: function(transport) {
				
		   // This is the data returned by AJAX. Put it inside the DIV that you would like to refresh.
           // Replace "update_container" with the ID of your <div> element
		   parent.parent.document.getElementById(b).innerHTML = transport.responseText;
		   //Element.hide('addBox');
		   parent.parent.document.getElementById('ajaxload').style.display = 'none';
		   
		   //set message
		   if(pars.a == 'up'){
				parent.parent.document.getElementById('ajaxmessage').innerHTML = 'You have successfully updated "' + pars.NAME + '".';   
		   }
		   else if(pars.a == 'new'){
				parent.parent.document.getElementById('ajaxmessage').innerHTML = pars.NAME + ' has been added to the system.';   
		   }
		   
		   parent.parent.document.getElementById('ajaxmessage').style.display = 'block'; //display message
		   
		   transport.responseText.evalScripts();
		   
	},
	onFailure: function(transport) {
           alert("There was an error!");
	}
  });
}
//id, id number, name, table, box, file to load after
function delete_item(i,idn,n,t,b,f){
		
	user_conf = confirm("Are you sure you want to delete \"" + n + "\"?");
	
	if(user_conf){
		new Ajax.Request('includes/delete.php?i=' + i + '&idn=' + idn + '&t=' +t + '&f=' + f, {
			method: 'get',
			onLoading: showLoad,
			onSuccess: function(transport) {
				
				$(b).innerHTML = transport.responseText;
				
				$('ajaxmessage').innerHTML = '"' + n + '" has been deleted from the system.'; 
				$('ajaxmessage').style.display = 'block'; //displays message
				
				//document.getElementById('ajaxload').style.display = 'none';
				$('ajaxload').hide();
				
				transport.responseText.evalScripts();
				
			},
			onFailure: function(transport) {
				alert("ERROR DELETING ITEM!");	
			}
		});
	}
}

//sets value of select box
function setSelectValue(SelectName, Value) {
	eval('SelectObject = document.getElementById(\'' + SelectName + '\');');
	for(index = 0; index < SelectObject.length; index++) {
		if(SelectObject[index].value == Value)
		   SelectObject.selectedIndex = index;
	}
}