var pageModified = false;
var pageUpdated = false;
var targetPage;

var modalContents;
var inputValue;
var currentInput;
var currentNum;
var currentColumn;

// must be set at start of page
var hidePencil, hidePencil2;

var storedInputs = new Array();
var storedInputs2 = new Array();
var modalFields;
var hiddenFields = new Array();

var tempModified = false;


//var open = function(hash){ modalContents = hash.w.html(); savedInputs = hash.w.prev("span.adjInputs").html(); hash.w.show(); };
// var close = function(hash){ if ( !modalCompleted ) { hash.w.html(modalContents); } hash.w.hide(); hash.o.remove(); };

var open = function(hash)
{
	modalContents = hash.w.html();
	hash.w.show();

	modalFields = new Array();
	$('#'+hash.w.attr('id')+' select, #'+hash.w.attr('id')+' input').each( function(index) {
		if ( $(this).is(':checkbox, :radio') ) {
			modalFields[index] = this.checked;
		} else {
			modalFields[index] = this.value;
		}
	});
};

jQuery(document).ready(function()
{
	$('.getInfoDialog').jqm({modal:1, overlay:85, onShow:open});

	freezeInputs();

});

function freezeInputs() { setTimeout('freezeInputsWorkings()', 1); }
function freezeInputsWorkings()
{
	$('.adjInputs > select, .adjInputs > input').each( function(index) {
		storedInputs[index] = this.value;
	});

	if ( $('.adjInputs2')[0] != null ) {
		$('.adjInputs2 > select, .adjInputs2 > input').each( function(index) {
			storedInputs2[index] = this.value;
		});
	}
}

function displayEdit(column)
{
	var modID, theHideCondition;
	if ( column == 0 ) {
		modID = '#mod_';
		theHideCondition = hidePencil;
	} else {
		modID = '#mod2_';
		theHideCondition = hidePencil2;
	}

	if ( currentInput.val() == theHideCondition ) {
		$(modID+currentNum).hide();
		return false;
	} else {
		$(modID+currentNum).show();
		return true;
	}
}

function prepareSwaps(column, editing, num)
{
	currentNum = num;
	currentColumn = column;

	var tempStore, qSelector;

	if ( currentColumn == 0 ) {
		tempStore = storedInputs;
		qSelector = '#adjInputs';
	} else {
		tempStore = storedInputs2;
		qSelector = '#adjInputs2_';
	}

	currentInput = $(qSelector+currentNum+' > select, ' + qSelector+currentNum+' > input');
	
	currentInput.parent().siblings(':input').each( function(index) {
		hiddenFields[index] = this.value;
	});

	if ( editing ) {
		freezeInputsWorkings();
		inputValue = tempStore[currentNum];
	} else {
		if ( displayEdit(currentColumn) ) {
			inputValue = tempStore[currentNum];
		} else {
			freezeInputs();
		}
	}
	
	pageModified = true;
}

function cancelBySwap(theModal)
{
	theModal.jqmHide().html('').html(modalContents);
	currentInput.val(inputValue);
	$('#'+theModal.attr('id')+' select, #'+theModal.attr('id')+' input').each( function(index) {

		if ( $(this).is(':checkbox, :radio') ) {
			this.checked = modalFields[index];
		} else {
			this.value = modalFields[index];
		}

	});

	currentInput.parent().siblings(':input').each( function(index) {
		 this.value = hiddenFields[index];
	});

	displayEdit(currentColumn);
}


function selectTab(submitPage) {
	if ( pageModified ) {
		$('#confirmDialog').jqmShow();

		targetPage = submitPage;
	} else {
		submitForm(submitPage);
	}
}

function determineAddEditComment(accountIndex)
{
	var researchValue = $('#researchNotes-'+accountIndex).val();
	var commentLink = $('#addEditComment-'+accountIndex);

	if ( researchValue == null || researchValue.length == 0 ) {
		commentLink.html('Add Comment')
	} else {
		commentLink.html('Edit Comment')
	}
}