/*************************************************************
*	File Name : 			utils.js
*	Author : 				Domenic Polsoni - (FABUmarketing.com)
*
*	Date Created :			06/30/2010
*	Date Modified :
*
*
*	Various functions to be used on the FABUmarketing.com
****************************************************************/
/**** GLOBALS ****/
var	doc = document;
var	win = window;
/**** END OF GLOBALS ****/

/*
	Handles both methods of event handling (<=IE8 and DOM Level 2)
*/
var EventUtil =
{
	addHandler: function(element, type, handler)
	{
		if (element.addEventListener)
		{
			element.addEventListener(type, handler, false);
		}
		else if (win.attachEvent)
		{
			element.attachEvent('on' + type, handler);
		}
		else
		{
			element['on' + type] = handler;
		}
	},
	removeHandler: function(element, type, handler)
	{
		if (element.removeEventListener)
		{
			element.removeEventListener(type, handler, false);
		}
		else if (win.detachEvent)
		{
			element.detachEvent('on' + type, handler);
		}
		else
		{
			element['on' + type] = null;
		}
	},
	stopPropagation: function(event)
	{
		if (event.stopPropagation)
		{
			event.stopPropagation();
		}
		else
		{
			event.cancelBubble = true;
		}
	},
	getEvent: function(event)
	{
		return event ? event : window.event;
	},
	getTarget: function(event)
	{
		return event.target || event.srcElement;
	},
	getClipboardText: function(event)
	{
		var clipboardData = (event.clipboardData || window.clipboardData);
		return clipboardData.getData("text");
	},
	setClipboardText: function(event, value)
	{
		if (event.clipboardData)
		{
			return event.clipboardData.setData("text/plain", value);
		}
		else
		{
			return window.clipboardData.setData("text", value);
		}
	}
};


/*
	Makes the request for "getAttribute('class')" common to both <=IE7 and other browsers.
	<=IE7 requires "getAttribute('className')".
*/
function getAttr(elem, attr)
{
	if (elem.getAttribute(attr) !== null)
	{
		return elem.getAttribute(attr);
	}
	else
	{
		if (attr === "class")
		{
			return elem.getAttribute(attr + 'Name');
		}
	}

}

/*
	Makes the request for "setAttribute('class')" common to both <=IE7 and other browsers.
	<=IE7 requires "setAttribute('className')".
*/
function setAttr(elem, attr, attrName)
{
	var ieAttr = attr + 'Name';
	if (elem.getAttribute(ieAttr) === '')			// Need this in order to weed out <=IE7
	{
		elem.className = attrName;
	}
	else
	{
		return elem.setAttribute(attr, attrName);
	}
}

/*
	Provides cross-browser preventDefault functionality.
*/
function prevDefault(e)
{
	if (typeof(e.preventDefault) === "undefined")
	{
		e = win.event;
		e.returnValue = false;
	}
	else
	{
		e.preventDefault();
	}
}


/*
+-----------------------------------------------------------------+
| Fn name:    popwin2                                             |
+-----------------------------------------------------------------+
| Purpose:    Opens popup window w/customizable window options    |
+-----------------------------------------------------------------+
| Inputs:     string URL                                          |
|             string name (name of popup window)                  |
|             int w (width of popup window)                       |
|             int h (height of popup window)                      |
|             string opts (other popup window options)            |
+-----------------------------------------------------------------+
| Returns:    nothing                                             |
+-----------------------------------------------------------------+
| History:    10/31/2005 - cpn_id is set to empty string if not   |
|                          passed instead of "undefined" [ft]     |
+-----------------------------------------------------------------+
*/
function popwin2(URL, name, w, h, opts)
{
   var winl = (screen.width - w) / 2;
   var wint = (screen.height - h) / 2;
	window.open(URL, name, "width="+w+", height="+h+", left="+winl+", top="+wint + opts);
}


/*
 *	Returns the element matching the class of the specified element.  A class name,
 *	html tag, and element type must be specified.
 */
function getElementsByClass(node, htmlTag, searchClass)
{
	var classElements = new Array();
	if (node === null)
	{
		node = doc;
	}
	if (htmlTag === null)
	{
		htmlTag = 'body';
	}
	var elems = node.getElementsByTagName(htmlTag);
	var pattern = new RegExp('(^|\\s)' + searchClass + '(\\s|$)');

	for (var i = 0, j = elems.length; i < j; i++)
	{
		if (pattern.test(elems[i].className))
		{
			classElements.push(elems[i]);
		}
	}
	return classElements;
}

/*
 *	Converts any collection (NodeList etc.) to an array.
 *
 */
function collectionToArray(list)
{
	var arrayList = new Array();

	for (var i = 0, j = list.length; i < j; i++)
	{
		arrayList.push(list[i]);
	}
	return arrayList;
}

/*
 *	Returns the array index of the array element passed.
 */
function getArrayIndex(arr, arrayElem)
{
	for (var i = 0, j = arr.length; i < j; i++)
	{
		if (arr[i] === arrayElem)
		{
			return i;
		}
	}
}

/*
 *	Generic function that clears out the specified contact form when used.
 *	Used primarily with local merchants but can be applied anywhere.
 */
function clearContactForm(cForm)
{
	// Grab a collection of form elements for comparison
	var elems = cForm.elements;
	var elemType;

	for (var i = 0, j = elems.length; i < j; i++)
	{
		// Grab the element's 'type' attribtue for sniffing out
		switch (elems[i].getAttribute('type'))
		{
			case 'text':
			case 'password':
				elems[i].value = '';
				break;

			case 'radio':
			case 'checkbox':
				if (elems[i].checked)
				{
					elems[i].checked = false;
				}
				break;

			case 'select-one':
			case 'select-multi':
				elems[i].selectedIndex = 0;
				break;

			default:
			break;
		}

		if (elems[i].tagName.toLowerCase() === 'textarea')
		{
			elems[i].value = '';
		}
	}
}

