// JavaScript Document
// Plus Credit Union Core Javascript
//alert("Script Loaded");
var pcuCore = {};
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function Ajax() {
	this.req = null;
	this.url = null;
	this.method = 'GET';
	this.async = true;
	this.status = null;
	this.statusText = '';
	this.postData = null;
	this.readyState = null;
	this.responseText = null;
	this.responseXML = null;
	this.handleResp = null;
	this.responseFormat = 'text', // 'text', 'xml', or 'object'
	this.mimeType = null;
	
	this.init = function() {
		if (!this.req) {
			try {
			// Try to create object for Firefox, Safari, IE7, etc.
				this.req = new XMLHttpRequest();
			}
			catch (e) {
				try {
				// Try to create object for later versions of IE.
					this.req = new ActiveXObject('MSXML2.XMLHTTP');
				}
				catch (e) {
					try {
					// Try to create object for early versions of IE.
						this.req = new ActiveXObject('Microsoft.XMLHTTP');
					}
					catch (e) {
					// Could not create an XMLHttpRequest object.
					return false;
					}
				}
			}
		}
		return this.req;
	};
	
	this.doReq = function() {
		if (!this.init()) {
			alert('Could not create XMLHttpRequest object.');
		return;
		}
		this.req.open(this.method, this.url, this.async);
		if (this.mimeType) {
			try {
				req.overrideMimeType(this.mimeType);
			}
			catch (e) {
				// couldn't override MIME type -- IE6 or Opera?
			}
		}
		var self = this; // Fix loss-of-scope in inner function
		
		this.req.onreadystatechange = function() {	
			var resp = null;
			if (self.req.readyState == 4) {
				switch (self.responseFormat) {
					case 'text':
						resp = self.req.responseText;
						break;
					case 'xml':
						resp = self.req.responseXML;
						break;
					case 'object':
						resp = req;
						break;
				}
				if (self.req.status >= 200 && self.req.status <= 299) {
					self.handleResp(resp);
				}
				else {
					self.handleErr(resp);
				}
			}
		};
		this.req.send(this.postData);
	};
	
	this.setMimeType = function(mimeType) {
		this.mimeType = mimeType;
	};
	
	this.handleErr = function() {
		var errorWin;
		try {
			errorWin = window.open('', 'errorWin');
			errorWin.document.body.innerHTML = this.responseText;
		}
		catch (e) {
			alert('An error occurred, but the error message cannot be '
				+ 'displayed. This is probably because of your browser\'s '
				+ 'pop-up blocker.\n'
				+ 'Please allow pop-ups from this web site if you want to '
				+ 'see the full error messages.\n'
				+ '\n'
				+ 'Status Code: ' + this.req.status + '\n'
				+ 'Status Description: ' + this.req.statusText);
		}
	};
	
	this.abort = function() {
		if (this.req) {
			this.req.onreadystatechange = function() { };
			this.req.abort();
			this.req = null;
		}
	};
	
	this.doGet = function(url, hand, format) {
		this.url = url;
		this.handleResp = hand;
		this.responseFormat = format || 'text';
		this.doReq();
	};
}


//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Javascript Funtion to add event
// To use: pcuCore.addEvent(element, "Event Type", function, true | false);
// Eventype = blur, change, click, dblclick, error, focus, keydown, keypress, keyup, mousedown, mousemove
//   mouseover, mouseout, mouseup, select, unload, altKey, button, clientX, clientY, ctrlKey, metaKey, relatedTarget,
//   screenX, screenY, shiftKey, bubble, cancelable, currentTarget, target, timeStamp, type
//
pcuCore.addEvent = function(elm, eventType, fn, useCapture)
{
	// If Firefox then use addEventListener method. If IE use attachEvent method add ON to the event type
	if (elm.addEventListener) {
		elm.addEventListener(eventType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent("on" + eventType, fn);
		return r;
	}
	else {
		elm["on" + eventType] = fn;
	}
	alert("event added");
};

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Javascript Function to remove a class
// To use: pcuCore.addClass(element, "className")
pcuCore.removeClass = function(target, theClass)
{
	var pattern = new RegExp("(^| )" + theClass + "( |$)"); // Stores the expression for the class to find
	
	target.className = target.className.replace(pattern, "$1"); // Using replace method to replace pattern with empty string
	target.className = target.ClassName.replace(/ $/, ""); // Using replace method to remove any spaces left over
};

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Javascript Function that addes a class to an element
// Dependant on rsCore.hasClass
// To use: pcuCore.addClass(element, "className")

pcuCore.addClass = function(target, theClass)
{
	// Test if the element already has the class to be added
	if (!pcuCore.hasClass(target, theClass))
	{
		// Test if the element class is blank
		if (target.className == "")
		{
			target.className = theClass; // if it is just add the class
		}
		else
		{
			target.className += " " + theClass; // if not add a space and the class
		}
	}
};

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Javascript Function to check if an Element has a particular Class
// Use inside contional statements like: if (pcuCore.hasClass(element, "className"))
// this use will return true or false

pcuCore.hasClass = function(target, theClass) // Target is the element and theClass is class name
{
	var pattern = new RegExp("(^| )" + theClass + "( |$)"); // Stores the expression for the class to find
	
	// Test if the target has class name that matches pattern
	if (pattern.test(target.className))
	{
		return true; // If true return true
	}
	
	return false; // Default answer
};

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Java Script Function to get all Elements By Class
// To call function - var elementArray = pcuCore.getElementsByClass("enterClassName");

pcuCore.getElementsByClass = function(theClass)
{
	var elementArray = []; //Array for all elements in document
	var pattern = new RegExp("(^| )" + theClass + "( |$)"); // Stores the expression for the class to find
	var matchedArray = []; //Array for all elements that match the class
	
	// Test if an object exist and then
	// Gets all elements in the document
	if (typeof document.all != "undefined") // If doument.all returns anything but undefined
	{
		elementArray = document.all; // For IE 5x does not understand * value
	}
	else
	{
		elementArray = document.getElementsBytagName("*"); // For all other browsers.
	}
	
	// Loops through all elements and find matches to class then stores them in matchedArray var.
	for (var i=0; i<elementArray.lenght; i++)
	{
		if (pattern.test(elementArray[i].className)) // If class matches the pattern varible
		{
			matchedArray[matchedArray.length] = elementArray[i]; // Then stores matching element
		}
	}

	return matchedArray; // Returns matching elements to calling function
};

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Java Script Function to display date
// To use: pcuCore.getCurrentDate();

pcuCore.getCurrentDate = function()
{
	var curDateObj = new Date();
	return curDateObj.toLocaleDateString();
};

pcuCore.getCurrentTime = function()
{
	var curDateObj = new Date();
	return curDateObj.toLocaleTimeString();
};

