/*jslint white: true, browser: true, devel: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, newcap: true, immed: true */

/**
	@fileOverview Basic JavaScript functions
	@version 11.5
*/

/**
	@function
	@description Cross-browser compatible function for adding events to DOM objects.<br />
					Immediately invokes to the event listener model supported by the browser.
	@author Revised by John Arthur (v.11.5) - Set up immediate invocation functionality
	@since v11.5
	@param {DOMOBJECT} elm The DOM Object (HTML Element) the Event Listener is attaching to.
	@param {DOMEVENT String} evType The Event being listened to (String, sans any "on")
	@param {FUNCTION} fn The function to be fired when the Listener is activated.
	@param {BOOLEAN} [useCapture = false] Optional param to indicate whether the Listener should be applied during Capturing (true) or Bubbling (false)
	@example addEvent(chkbox, 'click', submitGiftWrap, false);
	@see EONE.utils.core.addEvent
*/
var addEvent;

if (typeof window.addEventListener === 'function') {
	addEvent = function (elm, evType, fn, useCapture) {
		useCapture = useCapture || false;
		elm.addEventListener(evType, fn, useCapture);
	};
} else if (typeof document.attachEvent === 'object') {
	addEvent = function (elm, evType, fn) {
		var r = elm.attachEvent('on' + evType, fn);
	};
} else {
	addEvent = function(elm, evType, fn) {
		elm['on' + evType] = fn;
	};
}

/**
	@description This method is used to process embedded javascript because innerHTML doesn't execute JS.<br />
					The method gets all script tags from the container, creates new SCRIPT nodes, and appends those nodes to the document.
	@since Pre-v11.5
	@param {DOMOBJECT ID String} id The id of the container that was loaded by AJAX (where the script tags would be found).
	@example writeScriptTagsToDOM(thisSeg);	//	{@link publishResponseXML}
*/
var writeScriptTagsToDOM = function (id) {
	var d = document.getElementById(id).getElementsByTagName("script"),
		t = d.length,
		newScript,
		x;

	for (x = 0; x < t; x += 1) {
		newScript = document.createElement('script');
		newScript.type = "text/javascript";
		newScript.text = d[x].text;
		document.getElementById(id).appendChild(newScript);
	}
};

/**
	@description Returns an array of elements with a specific class name.
	@since Pre-v11.5
	@param {DOMOBJECT} node The container node in which the search is performed.
	@param {STRING} classname The class name we're searching on.
	@return {Array} DOM Nodes with a matching class name.
	@example tabList = getElementsByClassName(tabList, "toc"); //	{@link tabSwitch}
*/
var getElementsByClassName = function (node, classname) {
	var a = [],
		re = new RegExp('(^| )' + classname + '( |$)'),
		els = node.getElementsByTagName("*"),
		i, j;

	for (i = 0, j = els.length; i < j; i += 1) {
		if (re.test(els[i].className)) {
			a.push(els[i]);
		}
	}

	return a;
};

/**
	@description Opens a new browser window to the specified page.
	@since Pre-v11.5
	@param {URL String} href The URL the new window will open to.
	@param {STRING} name The name given to the pop-up window, for cross-window communication purposes.
	@param {NUMBER} w The width of the pop-up window.
	@param {NUMBER} h The height of the pop-up window.
	@example [...]Still not seeing it? &lt;a onclick="javascript:openPopup(this.href,'_blank','400','275');return false;" href="/images/content/checkout/creditCards.jpg"&gt;Click for examples.&lt;/a&gt;[...] //	sample code from xout payment screen.
*/
var openPopup = function (href, name, w, h) {
	window.open(href, name, 'fullscreen = no,toolbar = no,status = no,menubar = no,scrollbars = yes,resizable = yes,directories = no,location = no,width = ' + w + ',height = ' + h + '');
};



/**
	@description Opens a dialog box to confirm a delete.
	@since Pre-v11.5
	@param {STRING} The message to display to the user, if null a default message is displayed.
	@example [...]onclick="if(confirmDelete('Are you sure you want to delete this item from your list?')){this.form.actn.value='shoplistdeleteitem';this.form.submit()} else {return false};"
*/
var confirmDelete = function (message){
	message = message || null;

	if (message === null) {
		message = "Are you sure you want to delete?";
	}

	if (confirm(message)) {
		return true;
	} else {
		return false;
	}
};

