var loader = new Image();
loader.src = 'images/loader_red.gif';
function doNav(theUrl) {
	document.location.href = theUrl;
}
function openWindow(url,w,h) {
	day = new Date();
	id = day.getTime();
	window.open(url,id,'toolbar=1,scrollbars=1,location=0,statusbar=0,menubar=1,resizable=0,width='+w+',height='+h);
	return false;
}
function pleaseWait(id) {
	$('pleaseWait'+id).show();
}
function showLoader() {
	$('loaderProcessing').style.display = 'block';
}
function deleteAccount(id) {
	if(confirm('Are you sure you want to delete this account?')) {
		doNav('admin_accounts.php?action=process&section=delete&id='+id);
	}
	return false;
}
function deleteParagraph(id,page,ret) {
	if(confirm('Are you sure you want to delete this paragraph?')) {
		doNav('page_info.php?action=process&section=delete&id='+id+'&return='+ret+'&page='+page);
	}
}
function deleteThis(id,type) {
	if(confirm('Are you sure you want to completely remove this '+type+'?')) {
		var url = $(id).href;
		doNav(url);
	}
	return false;
}
function toggleOptions(id) {
	var divs = $$('div.optionsMenu');
	for(i=0;i<divs.length;i++) {
		if(divs[i]['id'] == 'optionsMenu'+id) {
			new Effect.toggle(divs[i]['id'],'appear',{duration:.2});		
		} else {
			new Effect.Fade(divs[i]['id'],{duration:.2});
		}
	}
	return false;
}
new Event.observe(window,'load',function() {
	var divs = $$('div.trans_box');
	for(i=0;i<divs.length;i++) {	
		var id = divs[i]['id'];
		var h = $(id).getHeight();
		var w = $(id).getWidth();
		$(id).setStyle({
			margin:'-'+h+'px 0 0 0',
			position:'relative'
		});
		var newh = (h*1)-10;
		var current = $(id).innerHTML;
		$(id).update('<div class="trans_bg" style="width:'+w+'px;height:'+h+'px;margin:-10px 0 0 -10px;"></div><div style="z-index:200;position:relative;">'+current+'</div>');
	}
});
new Event.observe(window,'load',function() {
	var divs = $$('div.trans_bg');
	for(i=0;i<divs.length;i++) {	
		var id = divs[i]['id'];
		var h = $(id).getHeight();
		var w = $(id).getWidth();
		$(id).setStyle({
			margin:'-'+h+'px 0 0 0',
			position:'relative'
		});
	}
});
function updateProjectId(projectid) {
	var sel = $('category').selectedIndex;
	var cat = $('category').options[sel].value;
	var ccat = $F('current_category');
	var cpid = $F('current_projectid');
	new Ajax.Request('ajax_projects.php?action=projectid&category='+cat+'&current_category='+ccat+'&projectid='+cpid,{
		method:'get',
		onSuccess:function(transport) {
			var response = transport.responseText;
			$('projectidContainer').update(response);
			$('projectid').value = response;
		},
		onFailure:function() { alert('There was a problem forming the project id.  Please try again.'); }
	});
}
function editProjectId(el) {
	el = $(el).identify();
	var pid = $('projectid').value;
	$('projectid').remove();
	$('projectidContainer').update('<input type="text" name="projectid" id="projectid" value="'+pid+'" />');
	$(el).hide();
	return false;
}
function loadDDCal(field,a,date) {
	a = $(a).identify();
	var parent = $(a).up().identify();
	if(Object.isElement($(field+'1'))) {
		$(field+'1').update('<div id="calendarLoader"><img src="images/loader_gray.gif" border="0" /></div>');
	} else {
		$(parent).insert('<div id="'+field+'1" class="calendarContainer"><div id="calendarLoader"><img src="images/loader_gray.gif" border="0" /></div></div>');
	}
	$(a).onclick = function() { return closeDDCal(field,a); };
	if(!date) {
		var date = $(field).value;
	}
	new Ajax.Updater(field+'1','ajax_calendar.php?date='+date+'&field='+field+'&a='+a+'&parent='+parent);
	return false;
}
function changeDDCalMonth(id,el,field,a) {
	$(el).hide();
	if(Object.isElement($(id))) {
		$(id).show();
	} else {
		loadDDCal(field,a,id+'-01');
	}
	return false;
}
function selectDay(day,field,a) {
	$(field).value = day;
	closeDDCal(field,a);
	return false;
}
function closeDDCal(id,a) {
	$(id+'1').remove();
	$(a).onclick = function() { return loadDDCal(id,a); };
	return false;
}

function addContact(i) {
	$('contactsContainer').insert('<div id="contact'+i+'"><div style="clear:left;height:5px;"></div><div class="inputlabel" style="width:100px;">Full Name&nbsp;&nbsp;</div><div class="inputcontainer"><input type="text" name="contacts['+i+'][name]" id="contactName'+i+'" style="width:200px;" /></div><div class="inputcontainer">&nbsp;&nbsp;<input type="radio" name="contacts[primary]" id="contactPrimary'+i+'" value="'+i+'" /> <label for="contactPrimary'+i+'">Primary</label>&nbsp;&nbsp;<a href="#" onclick="return removeContact(\''+i+'\');" class="adminLink"><img src="images/icons/delete.png" border="0" align="absbottom" /> Remove Contact</a></div><div style="clear:left;height:5px;"></div><div class="inputlabel" style="width:100px;">Phone&nbsp;&nbsp;</div><div class="inputcontainer"><input type="text" name="contacts['+i+'][phone]" id="contactPhone'+i+'" style="width:100px;" /></div><div style="clear:left;height:5px;"></div><div class="inputlabel" style="width:100px;">Email&nbsp;&nbsp;</div><div class="inputcontainer"><input type="text" name="contacts['+i+'][email]" id="contactEmail'+i+'" style="width:220px;" /></div><div style="clear:left;height:5px;"></div><div class="inputlabel" style="width:100px;">Address&nbsp;&nbsp;</div><div class="inputcontainer"><textarea name="contacts['+i+'][address]" id="contactAddress'+i+'" style="width:220px;height:80px;"></textarea></div></div>');
	i++;
	$('addContactA').onclick = function() { return addContact(i); };
	return false;
}
function removeContact(i) {
	if(confirm('Are you sure you want to completely remove this contact?')) {
		$('contact'+i).remove();
	}
	return false;
}
function editContact() {
	
}
function addNote(n) {
	var d = new Date();
	var year = d.getFullYear();
	var month = d.getMonth()+1;
	var day = d.getDate();
	if(month.toString().length == 1) {
		month = '0'+month;
	}
	if(day.toString().length == 1) {
		day = '0'+day;
	}
	i = n;
	var date = month+'/'+day+'/'+year;
	new Ajax.Request('ajax_misc.php?action=notepriority&i='+i,{
		method:'get',
		onSuccess: function(transport) {
			var ajaxReturn = transport.responseText;
			$('notesContainer').insert('<div id="note'+i+'" style="border-bottom:1px dashed #fff;padding:5px 8px 5px 0;"><div style="clear:left;height:5px;"></div><div class="inputcontainer"><input type="checkbox" name="notes['+i+'][complete]" value="yes" />&nbsp;</div><div class="inputcontainer"><textarea name="notes['+i+'][text]" id="noteText'+i+'" style="width:350px;height:120px;"></textarea>&nbsp;</div><div class="inputcontainer"><select name="notes['+i+'][visibility]" id="noteVisibility'+i+'" size="1"><option value="private">Private</option><option value="public">Public</option></select>&nbsp;&nbsp;</div><div class="inputcontainer"><a href="#" onclick="return removeNote(\''+i+'\');" class="adminLink"><img src="images/icons/delete.png" border="0" align="absmiddle" /> Remove Note</a></div><div style="clear:left;height:5px;"></div><div class="inputlabel" style="width:60px;padding-left:25px;">File</div><div class="inputcontainer"><input type="file" name="noteFiles['+i+']" id="noteFile'+i+'" /></div><div style="clear:left;height:5px;"></div><div class="inputlabel" style="width:60px;padding-left:25px;">Due Date</div><div class="inputcontainer"><input type="text" name="notes['+i+'][date]" id="noteDate'+i+'" style="width:80px;" value="'+date+'" /></div><div class="inputcontainer" style="padding:5px 0 0 5px;"><a href="#" onclick="return loadDDCal(\'noteDate'+i+'\',this);"><img src="images/icons/calendar.png" border="0" alt="Select Date" /></a></div>'+ajaxReturn+'<div style="clear:both;"></div></div>');
			$('notesContainer').setStyle({borderTop:'1px dashed #fff'});
		},
		onFailure: function() { alert('There was a problem communicating with the server. Please try again.'); }
	});
	n++;
	$('addNoteA').onclick = function() { return addNote(n); };
	return false;
}
function removeNote(i) {
	if(confirm('Are you sure you want to completely remove this note?')) {
		$('note'+i).remove();
		if($('notesContainer').innerHTML == '') {
			$('notesContainer').setStyle({'border':'none'});
		}
	}
	return false;
}
function saveNoteState(el) {
	var el = $(el).identify();
	if($(el).checked) {
		var complete = 'yes';
	} else {
		var complete = 'no';
	}
	var id = $(el).value;
	new Ajax.Request('ajax_projects.php?action=savenotestate&id='+id+'&complete='+complete,{
		method:'get',
		onSuccess:function(transport) {
			var response = transport.responseText;
			if(response != '') {
				alert(response);
			} else {
				if(complete == 'yes') {
					var parent = $(el).up().up();
					$(parent).hide();
				}
			}
		},
		onFailure:function() { alert('We had trouble communicating with the server. Please try again.'); }
	});
}
function saveNoteStateReport(el) {
	var el = $(el).identify();
	if($(el).checked) {
		var complete = 'yes';
	} else {
		var complete = 'no';
	}
	var id = $(el).value;
	new Ajax.Request('ajax_projects.php?action=savenotestate&id='+id+'&complete='+complete,{
		method:'get',
		onSuccess:function(transport) {
			var response = transport.responseText;
			if(response != '') {
				alert(response);
			} else {
				if(complete == 'yes') {
					var parent = $(el).up().up();
					$(parent).hide();
					var line = $(el).up().up().next();
					$(line).hide();
				}
			}
		},
		onFailure:function() { alert('We had trouble communicating with the server. Please try again.'); }
	});
}
function uploadNewFile(key,el) {
	if(confirm('Are you sure you want to permanently remove this attachment?')) {
		$(el.up()).update('<input type="file" name="noteFiles[]" id="noteFile'+key+'" />');
	}
	return false;
}
var loadingProjects = false;
function loadProjects() {
	if(loadingProjects) {
		loadingProjects.transport.abort();
	}
	$('projectDisplay').update('<div align="center"><br /><br /><br /><br /><img src="images/loader_gray_lg.gif" border="0" /><br /><br /><span class="dkgray t16">Loading Projects...Please Wait</span><br /><br /><br /><br /></div>');
	var search = $F('ajaxSearch');
	if($('ajaxCompleted').checked) {
		var completed = 'yes';
	} else {
		var completed = '';
	}
	new Ajax.Request('ajax_projects.php?action=projects&search='+search+'&completed='+completed,{
		method:'get',
		onCreate:function(request) { var loadingProjects = request; },
		onSuccess:function(transport) {
			var response = transport.responseText;
			$('projectDisplay').update(response);
			loadingProjects = false;
		},
		onFailure:function() { alert('We encountered an error while trying to communicate with the database. Please refresh the page.'); }
	});
	return false;
}
function sortProjects(sort) {
	if(loadingProjects) {
		loadingProjects.transport.abort();
	}
	$('projectDisplay').update('<div align="center"><br /><br /><br /><br /><img src="images/loader_gray_lg.gif" border="0" /><br /><br /><span class="dkgray t16">Loading Projects...Please Wait</span><br /><br /><br /><br /></div>');
	var search = $F('ajaxSearch');
	new Ajax.Request('ajax_projects.php?action=projects&search='+search+'&sort='+sort,{
		method:'get',
		onCreate:function(request) { var loadingProjects = request; },
		onSuccess:function(transport) {
			var response = transport.responseText;
			$('projectDisplay').update(response);
			loadingProjects = false;
		},
		onFailure:function() { alert('We encountered an error while trying to communicate with the database. Please refresh the page.'); }
	});
	return false;
}