
/* 
 * Standard MCG Library for Web Projects
 * by Merrill Consulting Group, LLC (MCG)
 * http://www.merrillconsultinggroup.us
 *
 * @package    CORE LIBRARY
 * @subpackage javascript common to all web pages
 * @author     Merrill Consulting Group, LLC
 * @license    see License Agreement
 * @copyright  (C) 2009 Merrill Consulting Group, LLC
 *
 * THIS FILE IS SOURCE CODE AND YOU ARE NOT ALLOWED TO ALTER OR COPY it except as authorized within the License Agreement.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY OF ANY KIND; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the License Agreement for more details.
 *
 */


var mcgCore = new function() {
	this.moduleId = "mcgCore";
	this.version = "1.1.0";
	this.versionDate = "23 May 2009";
	this.codeStatus = "stable";
	this.lastAuthor = "Ray Merrill";
	this.copyright = "Copyright 2009, Merrill Consulting Group, LLC";
	this.license = "Proprietary copyrights.  See your license agreement.";
}

mcgCore.doNothing = function() {
	// place holder only
	return true;
}

mcgCore.explicitTypeOf = function(testvar) {
  if (typeof(testvar) == "object") {
    if (testvar === null) return "null";
    if (testvar.constructor == (new Array).constructor) return "array";
    if (testvar.constructor == (new Date).constructor) return "date";
    if (testvar.constructor == (new RegExp).constructor) return "regular_expression";
    return "object";
  }
  return typeof(testvar);
}

mcgCore.language = new function() {
	this.inUse = "enUS";	//default is USA english	
}

mcgCore.constants = new function(){
	//	these variables support the Calendar function
	this.monthNames = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
	this.monthAbbrevs = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
}

//
//	mcgCore DEBUG OBJECT
//
mcgCore.debug = new function() {
	this.onOff = false;
	this.ajax = false;
	this.handler = false;
	this.scriplet = false;
	this.global = false;
	this.globalHandler = false;
	this.xmlWriter = false;
	this.showDialog = function() {
		if (this.onOff) {
			if (arguments.length < 1) {
				alert("ERROR:  mcgCore.debug.showDialog called with no arguments - must have at least 1:  [0]=message");
			} else {
				if (arguments.length == 1) {
					//a debug alert will always show if no supplemental parameters were passed and gb_showDebugAlerts is true
					alert("debugMode=ON  Message:  " + arguments[0]);
				} else {
					//determine whether to show debug alert based on passed parameters with message (message is always position [0] so start at 1)
					var lclstr_Temp = "";
					var lclb_showDebugAlerts = false;
					for (var i=1; i<arguments.length; i++) {
						if ((arguments[i].toLowerCase() == "ajax") && (this.ajax == true)) {lclb_showDebugAlerts = true;lclstr_Temp += " [Ajax]";}
						if ((arguments[i].toLowerCase() == "handler") && (this.handler == true)) {lclb_showDebugAlerts = true;lclstr_Temp += " [Handler]";}
						if ((arguments[i].toLowerCase() == "scriplet") && (this.scriplet == true)) {lclb_showDebugAlerts = true;lclstr_Temp += " [Scriplet]";}
						if ((arguments[i].toLowerCase() == "global") && (this.global == true)) {lclb_showDebugAlerts = true;lclstr_Temp += " [Global]";}
						if ((arguments[i].toLowerCase() == "globalhandler") && (this.globalHandler == true)) {lclb_showDebugAlerts = true;lclstr_Temp += " [GlobalHandler]";}
						if ((arguments[i].toLowerCase() == "xmlwriter") && (this.xmlWriter == true)) {lclb_showDebugAlerts = true;lclstr_Temp += " [XMLWriter]";}
					} // end for loop
					if (lclb_showDebugAlerts) {alert("debugMode=" + lclstr_Temp + "  Message:  " + arguments[0]);}
				} // end if arguments.length ==1
			} // end if arguments.length < 1
		} // end if this.onOff is true
	} // end showDialog method
} // end mcgCore.debug object

//
//	mcgCore OTHERINFO OBJECT
//
mcgCore.otherInfo = new function(){
	
	// detect document referrer, memorize as global
	if (document.referrer && (document.referrer!="")) {
		this.referralSite = String(document.referrer);
	} else {
		this.referralSite = "unknown";
	}
	
} // end mcgCore.otherInfo object

//
//	mcgCore EVENTINFO OBJECT
//
mcgCore.eventInfo = new function(){

	// posx and posy contain the mouse position relative to the document
	this.mouseUpPosX = 0;
	this.mouseUpPosY = 0;
	// method attached to event listener
	this.saveLastKnownMouseUpPositions = function(e) {
		if (e.pageX || e.pageY) 	{
			this.mouseUpPosX = e.pageX;
			this.mouseUpPosY = e.pageY;
		}
		else if (e.clientX || e.clientY) 	{
			this.mouseUpPosX = e.clientX + document.body.scrollLeft	 + document.documentElement.scrollLeft;
			this.mouseUpPosY = e.clientY + document.body.scrollTop	+ document.documentElement.scrollTop;
		}
	}
	
	// following used by mcgCore.calendar objects and methods
	this.calendarClick = new Array();

	
} // end mcgCore.eventInfo object

// attach the event listener for mouseup
if (document.addEventListener) {
	document.addEventListener('mouseup',function (e){mcgCore.eventInfo.saveLastKnownMouseUpPositions(e);},false);
} else {
	if (document.attachEvent) {
		var typeOfEvent = 'mouseup';
		var functionForEvent = function(e){mcgCore.eventInfo.saveLastKnownMouseUpPositions(e);}
		document['e'+typeOfEvent+functionForEvent] = functionForEvent;
		document[typeOfEvent+functionForEvent] = function() { document['e'+typeOfEvent+functionForEvent](window.event); }
		document.attachEvent('on'+typeOfEvent,document[typeOfEvent+functionForEvent]);
	} else {
		mcgCore.debug.showDialog('ERROR:  Global.js is unabled to attach an event listener to the document (mcgCore.eventInfo)');
	}
}

mcgCore.replaceStringsWith = function() {
	var sourceString = arguments[0];
	var stringToReplace = arguments[1];
	var newString = arguments[2];
	var answer = "";
	var parsedString = sourceString.split(stringToReplace);
	// loop through the array and add earliest date time string inbetween where the splits were
	for (var i=0; i<parsedString.length; i++) {
		if (i==0) {
			answer += parsedString[i];
		} else {
			answer += newString + parsedString[i];
		}
	} // end for loop
	return answer;
}

mcgCore.addAnEvent = function(el, evname, func) {
    if (el.attachEvent) { // IE
        el.attachEvent("on" + evname, func);
    } else if (el.addEventListener) { // Gecko / W3C
        el.addEventListener(evname, func, true);
    } else {
        el["on" + evname] = func;
    }
}

mcgCore.addLoadEvent = function(func) { 
    var oldonload = window.onload; 
    if (typeof window.onload != 'function') { 
        window.onload = func; 
    } else { 
        window.onload = function() { 
            if (oldonload) { 
                oldonload(); 
            } 
            func(); 
        } 
    } 
} 

mcgCore.fireEvent = function() {
	var docElement = arguments[0];
	if (typeof(docElement) != 'object') {
		if (typeof(docElement) == 'string') {
			if (!document.getElementById(docElement)) {
				alert("ERROR in [mcgCore.fireEvent]:  the document element id (string value passed ='" + docElement + "') cannot be located in the DOM.");
				return false;
			} else {
				// reassign docElement to be a reference to the element
				docElement = document.getElementById(docElement);
			}
		} else {
			alert("ERROR in [mcgCore.fireEvent]:  the document element id is not of type string or object.  You must pass either the reference to the element, or the element id as string.");
			return false;
		}
	}
	if (!docElement) {
		alert("ERROR in [mcgCore.fireEvent]:  the document element (reference to DOM object passed) does not appear to exist.");
		return false;
	}
	switch (arguments[1].toLowerCase()) {
		case 'onchange':
			//on IE
			if (docElement.fireEvent) {
				docElement.fireEvent('onchange');
			}
			//on Gecko based browsers
			if (document.createEvent) {
				var evt = document.createEvent('HTMLEvents');
				if (evt.initEvent) {
					evt.initEvent('change', true, true);
				}
				if (docElement.dispatchEvent) {
					docElement.dispatchEvent(evt);
				}
			}
			break;
		case 'onclick':
			//on IE
			if (docElement.fireEvent) {
				docElement.fireEvent('onclick');
			}
			//on Gecko based browsers
			if (document.createEvent) {
				var evt = document.createEvent('HTMLEvents');
				if (evt.initEvent) {
					evt.initEvent('click', true, true);
				}
				if (docElement.dispatchEvent) {
					docElement.dispatchEvent(evt);
				}
			}
			break;
		default:
			alert("ERROR in [mcgCore.fireEvent]:  event '"+arguments[1]+"' is not enabled for programmatic fire via javascript.");
			return false;
	}
	
	return true;
}

