// Page controls 
function RAT(options)
{
	// Properties
	this.NavLocked  			= 0;
	this.NavPreChangeFnDisabled	= 0;
	this.NavLockedMsg			= "";
	this.pageDivs 				= options['pageDivs'];
	this.NavOnChangeFn			= options['navOnChange'];
	this.NavPreChangeFn			= options['navPreChange'];
	this.NavValidateFn			= options['navValidate'];

	this.nextLink 				= "pageControlNext";
	this.prevLink 				= "pageControlPrevious";
	this.stepDiv				= "stepDisplay";
	
	// Function definitions
	this.NavShowPage 			= RAT_NavShowPage;
	this.NavChangePage 			= RAT_NavChangePage;
	this.NavHidePageDivs 		= RAT_NavHidePageDivs;
	this.NavActivePage 			= RAT_NavActivePage;
	this.NavUpdatePageControls 	= RAT_NavUpdatePageControls;
	this.NavOnChange			= RAT_NavOnChange;
	this.NavPreChange			= RAT_NavPreChange;
	this.NavValidate			= RAT_NavValidate;
	this.NavLock				= RAT_NavLock;
	this.NavIsLocked			= RAT_NavIsLocked;
	this.NavLockedMessage		= RAT_NavLockedMessage;
}

///////////////////////////////////////////////////////////////
// NAVIGATION ROUTINES
// Pre-pended with "Nav"
///////////////////////////////////////////////////////////////
function RAT_NavLockedMessage(value)
{
	this.NavLockedMsg = value;
}

function RAT_NavLock(value)
{
	this.NavLocked = value;
}

function RAT_NavIsLocked(index, divID)
{
	if (this.NavLocked)
	{
		if (!index)
		{
			for (var i=0; i<this.pageDivs.length; i++)
			{
				if (this.pageDivs[i].id == divID)
				{
					index = i;
					break;
				}
			}
		}

		if (this.pageDivs[index].tab != this.NavLocked)
		{
			return true;
		}
	}
	return false;
}

function RAT_NavShowPage(divID)
{
	if (this.NavIsLocked(null, divID)) { alert(this.NavLockedMsg); return; }
	if (!this.NavPreChangeFnDisabled) { this.NavPreChange(this.pageDivs[this.NavActivePage()].id); }

	this.NavHidePageDivs();
	$(divID).show();
	this.NavUpdatePageControls();
	
	this.NavOnChange(divID);
}

function RAT_NavChangePage(change)
{
	// Find the index of the visible div
	var active = this.NavActivePage(change);

	if (this.NavIsLocked(active+change)) { alert(this.NavLockedMsg); return; }
	
	// Show the confirm if required
//alert($H(this.pageDivs[active]).inspect());
	if (this.pageDivs[active].confirmOnProgress && change == 1)
	{
		if(confirm(this.pageDivs[active].confirmOnProgress))
		{
			if(this.pageDivs[active].onConfirmOnProgress)
			{
				// Run the onConfirm function if required
				this.pageDivs[active].onConfirmOnProgress();
			}
		}
		else
		{
			return false;
		}
	}
	
	// Run the prechange function if it isn't disabled
	if (!this.NavPreChangeFnDisabled) { this.NavPreChange(this.pageDivs[active].id); }
	
	// If visible is returned null, then we can't go further in that direction
	if (active == null) { return; }
	
	// Check we can move to the next page based on validation of page
	if (!this.NavValidate(this.pageDivs[active])) { return; }
	
	// Now hide the divs
	this.NavHidePageDivs();
	
	// Now change the page as requested
	for (var i=0; i<pageDivs.length; i++)
	{
		if(i == active)
		{
			$(this.pageDivs[i+change].id).show();
			break;
		}
	}

	this.NavUpdatePageControls();
	this.NavOnChange(this.pageDivs[this.NavActivePage()].id);
}

function RAT_NavOnChange(id)
{
	if (this.NavOnChangeFn)
	{
		this.NavOnChangeFn(id);
	}
}

function RAT_NavPreChange(id)
{
	if (this.NavPreChangeFn)
	{
		this.NavPreChangeFn(id);
	}	
}

function RAT_NavValidate(id)
{
	if (this.NavValidateFn)
	{
		return this.NavValidateFn(id);
	}
	return true;
}

function RAT_NavHidePageDivs()
{
	for (var i=0; i<pageDivs.length; i++)
	{
		//alert(this.pageDivs[i].id);
		$(this.pageDivs[i].id).hide();
	} 
}

function RAT_NavActivePage(change)
{
	var active;
	for (var i=0; i<this.pageDivs.length; i++)
	{
		if($(this.pageDivs[i].id).visible())
		{
			active = i;
			break;
		}
	}
	
	if (change != null)
	{
		if (this.pageDivs[i+change] == null)
		{
			return null;
		}
	}
	return active;
}

function RAT_NavUpdatePageControls()
{
	if (RAT_NavActivePage(+1) == null) { $(this.nextLink).className = 'pageControlInactive'; } else { $(this.nextLink).className = 'pageControlActive'; }
	if (RAT_NavActivePage(-1) == null) { $(this.prevLink).className = 'pageControlInactive'; } else { $(this.prevLink).className = 'pageControlActive'; }

	if ($('nextBtn')) {
		if (RAT_NavActivePage(+1) == null) { $('nextBtn').disabled = true; } else { $('nextBtn').disabled = false; }
	}
	if ($('prevBtn')) {
		if (RAT_NavActivePage(-1) == null) { $('prevBtn').disabled = true; } else { $('prevBtn').disabled = false; }
	}
	
	if ($(this.stepDiv)) {
		$(stepDiv).update(this.pageDivs[this.NavActivePage()].step);
	}

	var headers = document.getElementsByClassName('tabOn');
	for (var i=0; i<headers.length; i++)
	{
		$(headers[i]).className = 'tabOff';
	}
	$(this.pageDivs[this.NavActivePage()].tab).className ='tabOn';
}

///////////////////////////////////////////////////////////////
/// Validation routines
// Pre-pended with "Validate"
///////////////////////////////////////////////////////////////

function RAT_ValidateEmail(email)
{
	var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
	var regex = new RegExp(emailReg);
	return regex.test(email);
}

function RAT_ValidateForceNumbersOnly(myfield, e, dec)
{   
    var key;
    var keychar;
    if (window.event)
    {   
        key = window.event.keyCode;
    }
    else if (e)
    {
        key = e.which;
    }
    else
    {
        return true;
    }
    if(key != 46 && key != 45 && key > 31 && (key < 48 || key > 57))
    {   
        return false;
    }       
    else    
    {
        return true;
    }
}

function RAT_ValidateRemoveNonNumeric(myfield)
{
    var re = /[^0-9\.\-]/g;
    if(re.test(myfield.value))
    {
        myfield.value = myfield.value.replace(re, '');
        myfield.value = myfield.value.replace(/\./, '');
    }
}
