//********************************************************
//*****************  MOOTOOLS WRAPPER  *******************
//*****************     CREATED BY     *******************
//*****************    DENNIS BLAKE    *******************
//*****************      COPYRIGHT     *******************
//********************************************************

var http_update = new Array();

function sndReq(link, div) {
	var box = $(div);
	var myFader = new Fx.Styles(box);
	myFader.options.duration = 200;
	myFader.chain(function() {
		new Ajax (link+'&nocache='+Date(), {
			evalScripts: true,
			onComplete: function() {
				loading(div, 'none');
				myFader.start({
					'opacity':[0,1]
				});
			},
			update: box
		}).request();
	}).start({
		'opacity':[1,0]
		});
	loading(div, 'block');
}

function sndNow(link, div) {
	var box = $(div);
	myAjax = new Ajax(link+'&nocache='+Date(), {evalScripts: true, onComplete: function() { loading(div, 'none'); }, update: box}).request();
	loading(div, 'block');
}

function sndPost(div) {
	var query = $('form').toQueryString();

	var box = $(div);
	var myFader = new Fx.Styles(box);
	myFader.options.duration = 200;
	myFader.chain(function() {
		new Ajax ('index.php?nocache='+Date(), {
			evalScripts: true,
			method: 'post',
			postBody: query,
			onComplete: function() {
				loading(div, 'none');
				myFader.start({
					'opacity':[0,1]
				});
			},
			update: box
		}).request();
	}).start({
		'opacity':[1,0]
		});
	loading(div, 'block');
}

function sndPostinfo(div, myForm) {
	var query = $(myForm).toQueryString();

	var box = $(div);
	var myFader = new Fx.Styles(box);
	myFader.options.duration = 200;
	myFader.chain(function() {
		new Ajax ('index.php?nocache='+Date(), {
			method: 'post',
			postBody: query,
			evalScripts: true,
			onComplete: function() {
				loading(div, 'none');
				myFader.start({
					'opacity':[0,1]
				});
			},
			update: box
		}).request();
	}).start({
		'opacity':[1,0]
		});
	loading(div, 'block');
}

function loading(div, set) {
	var loading = $(div+'_loading');
	loading.style.display = set;
	loading.style.zIndex = '102';
}

function sndDelete(link, div) {
	myAjax = new Ajax(link+'&nocache='+Date(), {onComplete: function() { handle_delete(div); }}).request();
}

function handle_delete(div)
{
	var box = $(div);
	box.style.display = 'none';
}

function submit_comment(div, button) {
	var theForm = document.forms['form'];
	var query = $('form').toQueryString();

	if(theForm.text.value != 'Enter Response Here.' && theForm.text.value != '') {
		var button_div = $(button+'_button');
		var button_loading = $(button+'_loading');
		button_div.style.display = 'none';
		button_loading.style.display = 'block';

		myAjax = new Ajax('index.php?nocache='+Date(), {method: 'post', postBody: query, evalScripts: true, update: $(div), onComplete: function() { handle_save(button); }}).request();
	} else {
		alert('Please enter a Response!');
	}
}

function check_comment() {
	var theForm = document.forms['form'];

	if(theForm.text.value != 'Enter Response Here.' && theForm.text.value != '') {
		return true;
	} else {
		alert('Please enter a Response!');
		return false;
	}
}

function submit_topic() {
	var topic = $('topic');
	topic.style.display = 'none';
	loading('topic', 'block');
}


function handle_save(button)
{
	var theForm = document.forms['form'];
	var button_div = $(button+'_button');
	var button_loading = $(button+'_loading');

	button_loading.style.display = 'none';
	button_div.style.display = 'block';

	theForm.text.value = 'Enter Response Here.';
	theForm.reset;
}

function request_friend(step)
{
	var Req = $('request_friend');
	var ReqConfirm = $('request_friend_confirm');

	if(step == 'add') {
		Req.style.display = 'none';
		ReqConfirm.style.display = 'block';
	} else
	if(step == 'confirm') {
		Req.style.display = 'none';
		ReqConfirm.style.display = 'none';

		var theForm = document.forms['add_friend'];
		var query = $('add_friend').toQueryString();
		myAjax = new Ajax('index.php?nocache='+Date(), {method: 'post', postBody: query, evalScripts: true, update: $('ajax_blank'), onComplete: function() { handle_request_save(); }}).request();

	} else
	if(step == 'reset') {
		Req.style.display = 'block';
		ReqConfirm.style.display = 'none';
	}
}

function handle_request_save()
{
	var ReqSuccess = $('request_friend_success');
	ReqSuccess.style.display = 'block';
}

function sndUpdate(id) {
	var query = $('form').toQueryString();
	myAjax = new Ajax('index.php?nocache='+Date(), {method: 'post', postBody: query, evalScripts: true, update: $(id)}).request();
}

function edit_message(link, id)
{
	myAjax = new Ajax(link+'&nocache='+Date(), {update: $(id) }).request();
}

function save_message(id) {
	var query = $('form_'+id).toQueryString();
	myAjax = new Ajax('index.php?nocache='+Date(), {method: 'post', postBody: query, evalScripts: true, update: $('post_'+id)}).request();
}


function uploading(div) {
	var upload = $(div);
	var loading = $(div+'_loading');
	upload.style.visibility = 'hidden'
	loading.style.display = 'block';
}

function add_base(div) {
	var myForm = $('form_add');
	var query = myForm.toQueryString();
	var to = $('to');
	var to_value = $('to').value;

	//error checking
	if($('base').value == '') {
		alert("Please choose a Base!");
		return false;
	} else
	if($('from').value == '') {
		alert("Please choose a From year!");
		return false;
	} else
	if($('to').value == '') {
		alert("Please choose a To year!");
		return false;
	} else
	if($('rank').value == '') {
		alert("Please choose your Rank / Affiliation!");
		return false;
	}

	$('add_button').disabled = true;

	var box = $(div);
	new Ajax ('index.php?nocache='+Date(), {
		method: 'post',
		postBody: query,
		onComplete: function() {
			myForm.reset();
			$('add_button').disabled = false;
			if(to_value == 'Present') {
				for(i=(to.options.length - 1); i>0; i--) {
					if(to.options[i].value == 'Present') {
						to.options[i]=null;
					}
				}
			}
		},
		update: box
	}).request();
}

function check_present(base)
{
	var myForm = $('form');
	var records = myForm.elements['records[]'];

	//var to hold which option has present selected
	var selected;
	var present_removed;
	var present_array = new Array;

	for(i=0; i < records.length; i++) {
		//setup active dropdown
		var to = $('to_'+records[i].value);
		for(j=(to.options.length - 1); j>0; j--) {
			if(to.options[j].value == "Present") {
				if(to.options[j].selected) {
					//remove from all other dropdowns
					selected = records[i].value;
				} else {
					//add present to all "other" dropdowns
					present_removed = records[i].value;
				}
			}
		}
	}

	//now do the loop again and add present or remove from the remaining
	for(i=0; i < records.length; i++) {
		//setup active dropdown
		var to = $('to_'+records[i].value);
		var added = false;
		for(j=(to.options.length - 1); j>0; j--) {
			if(selected != undefined) {
				//remove from all other dropdowns as its now selected
				if(selected != records[i].value && to.options[j].value == 'Present') {
					//remove from dropdown
					to.options[j]=null;
				}
			} else
			if(present_removed != undefined) {
				//add to all other dropdowns as we unselected present
				if(present_removed != records[i].value && added == false) {
					var present_option = document.createElement('option');
					present_option.text = 'Present';
					present_option.value = 'Present';
					//add to dropdown
					to.options.add(present_option, 1);
					added = true;
				}
			}

		}
	}
}


function check_guest_list()
{
	var myForm = $('form');

	if(myForm.guest_list.value == 'years') {
		//show years
		$('guest_list_years').style.display = '';
	} else {
		//hide years
		$('guest_list_years').style.display = 'none';
	}
}

function sndGraph(link, div) {
	var box = $(div);
	var myFader = new Fx.Styles(box);
	var complete = false;
	myFader.chain(function() {
		new Ajax (link+'&nocache='+Date(), {
			evalScripts: true,
			onComplete: function() {
				loading_graph(div, 'none');
				myFader.start({
					'opacity':[0,1]
				});
			},
			update: box
		}).request();
	}).start({
		'opacity':[1,0]
		});
	loading_graph(div, 'block');
}

function get_stats(div) {
	sndGraph('index.php?action=ajax_graph', div);
}

function change_graph(year, month) {
	sndGraph('index.php?action=ajax_graph_days&mont='+month+'&year='+year, 'stats');
	sndReq('index.php?action=ajax_users_signups&record='+year+'-'+month, 'user_signups');
}

function loading_graph(div, set) {
	var div = $(div);
	//var screen = $('screen');
	var loading = $('screen_loading');

	//screen.style.height = div.offsetHeight + 'px';
	//screen.style.width = div.offsetWidth + 'px';
	//screen.style.display = set;

	loading.style.top = (div.offsetHeight / 2) + 'px';
	loading.style.display = set;
}


//setup to return array with height/width
function docWidthHeight() {
	this.windowX = (document.documentElement && document.documentElement.clientWidth) || window.innerWidth || self.innerWidth || document.body.clientWidth;
	this.windowY = (document.documentElement && document.documentElement.clientHeight) || window.innerHeight || self.innerHeight || document.body.clientHeight;
	this.scrollX = (document.documentElement && document.documentElement.scrollLeft) || window.pageXOffset || self.pageXOffset || document.body.scrollLeft;
	this.scrollY = (document.documentElement && document.documentElement.scrollTop) || window.pageYOffset || self.pageYOffset || document.body.scrollTop;
	this.pageX = (document.documentElement && document.documentElement.scrollWidth) ? document.documentElement.scrollWidth : (document.body.scrollWidth > document.body.offsetWidth) ? document.body.scrollWidth : document.body.offsetWidth;
	this.pageY = (document.documentElement && document.documentElement.scrollHeight) ? document.documentElement.scrollHeight : (document.body.scrollHeight > document.body.offsetHeight) ? document.body.scrollHeight : document.body.offsetHeight;

	pageWidth = this.windowX;
	pageHeight = this.windowY;

	totalWidth = this.pageX;
	totalHeight = this.pageY;
}


function api_popup(url, contact)
{
	window.open(url, 'api_popup', 'width=780px,height=650,left=100,top=100,scrollbars=yes,location=yes');
	sndNow('index.php?action=ajax_contacts_loading&contact='+contact, 'contacts');
}

function consent(allow, type)
{
	if(allow) {
		sndNow('index.php?action=ajax_contacts_'+type, 'contacts');
	} else {
		sndNow('index.php?action=ajax_contacts_decline', 'contacts');
	}
}

function report_problem(last_action)
{
	var box = $('report_problem');
	myAjax = new Ajax('/index.php?action=ajax_report_problem&last_action='+last_action+'&nocache='+Date(), {evalScripts: true, onComplete: function() { slide_in(); }, update: box}).request();
}
function submit_problem()
{
	var query = $('problem').toQueryString();
	var box = $('report_problem');
	myAjax = new Ajax('/index.php?nocache='+Date(), {evalScripts: true, method: 'post', postBody: query, update: box}).request();
}


window.addEvent('domready', function() {
	slideme = new Fx.Slide('report_problem', {duration: 500, mode: 'vertical'});
	slideme.hide();	
});

function slide_in()
{
	slideme.element.style.visibility = 'visible';
	slideme.slideIn();
}

function slide_out()
{
	slideme.slideOut();
}


function show_rank()
{
	var myform = $('form_info');
	var rank = myform.military_affiliation.value;

	var div = $('rank');
	div.style.display = 'none';

	myAjax = new Ajax('/index.php?action=ajax_rank_display&record='+rank+'&nocache='+Date(), {evalScripts: true, onComplete: function() { loading('rank', 'none'); div.style.display = 'block'; }, update: div}).request();
	loading('rank', 'block');
}

function show_badge(badge_div)
{
	var badge = $(badge_div).value;

	var div = $(badge_div+'_holder');
	div.style.display = 'none';

	myAjax = new Ajax('/index.php?action=ajax_badges_display&record='+badge+'&selector='+badge_div+'&nocache='+Date(), { evalScripts: true, onComplete: function() { loading(badge_div+'_holder', 'none'); div.style.display = 'block'; }, update: div}).request();
	loading(badge_div+'_holder', 'block');
}

function get_badges()
{
	var myform = $('form_info');
	var branch = $('military_branch').value;

	var div = $('branch');
	div.style.display = 'none';

	myAjax = new Ajax('/index.php?action=ajax_branch_display&branch='+branch+'&nocache='+Date(), { evalScripts: true, onComplete: function() { loading('branch', 'none'); div.style.display = 'block'; }, update: div}).request();
	loading('branch', 'block');

	myAjax = new Ajax('/index.php?action=ajax_badges_select&branch='+branch+'&nocache='+Date(), { evalScripts: true, onComplete: function() { }, update: $('add_badges')}).request();

}




//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}
//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
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;
}

function showLightbox(link, width, height)
{
	// prep objects
	var objOverlay = document.getElementById('overlay');
	var objLightbox = document.getElementById('popup');
	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// set height of Overlay to take up whole page and show
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';


	// center lightbox and make sure that the top and left values are not negative
	// and the image placed outside the viewport
	var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - height) / 2);
	var lightboxLeft = ((arrayPageSize[0] - 20 - width) / 2);
		
	objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
	objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";


	objLightbox.style.width = width+'px';
	objLightbox.style.height = height+'px';
	objLightbox.style.display = 'block';

	//load up popup
	myAjax = new Ajax(link+'&nocache='+Date(), {evalScripts: true, onComplete: function() {  }, update: $('popup')}).request();
	
	return false;
}

//
// hideLightbox()
//
function hideLightbox()
{
	// get objects
	objOverlay = document.getElementById('overlay');
	objLightbox = document.getElementById('popup');

	// hide lightbox and overlay
	objOverlay.style.display = 'none';
	objLightbox.style.display = 'none';
}

