﻿/*
 *  Copyright Outpost Technology Frontier Corp.
 *
 */


/**
 * Utility Class
 * @constructor
 */
HtmlUtility = function () {}

//-------------------------
//  STATIC METHODS
//-------------------------

//  detect a special case of "web browser"
HtmlUtility.is_ie = ( /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent) );
HtmlUtility.is_ie5 = ( HtmlUtility.is_ie && /msie 5\.0/i.test(navigator.userAgent) );
/// detect Opera browser
HtmlUtility.is_opera = /opera/i.test(navigator.userAgent);
/// detect KHTML-based browsers
HtmlUtility.is_khtml = /Konqueror|KHTML/i.test(navigator.userAgent);
HtmlUtility.is_safari = /safari/i.test(navigator.userAgent);


/**
 *
 */
HtmlUtility.SetRemoveCustomTextOnInputBox = function (element, customText) {
    if (typeof element == 'string')
        element = document.getElementById(element);

    if (!element)
        return;

    element.customText = customText;
    HtmlUtility.addEvent(element, 'focus', HtmlUtility.RemoveCustomTextHandler);
    HtmlUtility.addEvent(element, 'blur', HtmlUtility.SetCustomTextHandler);
};

HtmlUtility.SetCustomTextHandler = function (evt) {
    var element = HtmlUtility.getEventSource(evt);
    if (element && element.customText && element.value == '')
        element.value = element.customText;
};

HtmlUtility.RemoveCustomTextHandler = function (evt) {
    var element = HtmlUtility.getEventSource(evt);
    if (element && element.customText && element.value == element.customText)
        element.value = '';
};

HtmlUtility.SetElementEnterDoNothing = function (srcTextBox) {
    if (srcTextBox)
        HtmlUtility.addEvent(srcTextBox, 'keypress', HtmlUtility.SetElementEnterDoNothingHandler);
};

HtmlUtility.SetElementEnterDoNothingHandler = function (evt) {
    var txtBox = HtmlUtility.getEventSource(evt);
    if (txtBox) {
        var code = (evt.keyCode)? evt.keyCode : evt.which;
        if (code == 13)
            return false;
    }
};

HtmlUtility.SetTextBoxClickButton = function (srcTextBox, destButton) {
    srcTextBox.defaultButton = destButton;
    HtmlUtility.addEvent(srcTextBox, 'keypress', HtmlUtility.SetTextBoxClickButtonHandler);
};

HtmlUtility.SetTextBoxClickButtonHandler = function (evt) {
    var txtBox = HtmlUtility.getEventSource(evt);
    if (txtBox && txtBox.defaultButton) {
        var code = (evt.keyCode)? evt.keyCode : evt.which;
        if (code == 13)
            txtBox.defaultButton.click();
    }
};


/**
 * Add event to an element
 * @param {HTMLElement} element element to hookup the event
 * @param {string} eventName name of the event to hookup to
 * @param {function} func function that will handle the event
 */
HtmlUtility.addEvent = function (element, eventName, func) {
	if (element.attachEvent) { // IE
		element.attachEvent("on" + eventName, func);
	} else if (element.addEventListener) { // Gecko / W3C
		element.addEventListener(eventName, func, true);
	} else {
		element["on" + eventName] = func;
	}
};

/**
 * remove an event from an element
 * @param {HTMLElement} element element to remove an event from
 * @param {string} eventName name of the event to remove
 * @param {function} func handler of the event to remove
 */
HtmlUtility.removeEvent = function (element, eventName, func) {
	if (element.detachEvent) { // IE
		element.detachEvent("on" + eventName, func);
	} else if (element.removeEventListener) { // Gecko / W3C
		element.removeEventListener(eventName, func, true);
	} else {
		element["on" + eventName] = null;
	}
};

/// create an element before the specified element
HtmlUtility.createElementBefore = function (elementType, elementAfter, parent) {
    var element = null;
	if (document.createElementNS) {
		// use the XHTML namespace; IE won't normally get here unless
		// _they_ "fix" the DOM2 implementation.
		element = document.createElementNS("http://www.w3.org/1999/xhtml", elementType);
	} else {
		element = document.createElement(elementType);
	}
	
	if (typeof parent != "undefined")
	    element = parent.insertBefore(element, elementAfter);
	return element;
};

/// create an element
HtmlUtility.createElement = function (elementType, parent) {
	var element = null;
	if (document.createElementNS) {
		// use the XHTML namespace; IE won't normally get here unless
		// _they_ "fix" the DOM2 implementation.
		element = document.createElementNS("http://www.w3.org/1999/xhtml", elementType);
	} else {
		element = document.createElement(elementType);
	}
	if (typeof parent != "undefined") {
		parent.appendChild(element);
	}
	return element;
};

HtmlUtility.removeAllElements = function (parent) {
    if (!parent)
        return;
    
    if (parent.hasChildNodes()) {
        for (var i = parent.childNodes.length; --i >= 0;)
            parent.removeChild(parent.childNodes[i]);
    }
}

/// retrieves the event source element
HtmlUtility.getEventSource = function (evt) {
    return (evt.srcElement)? evt.srcElement : evt.currentTarget;
};


//
//  CSS Related
//

HtmlUtility.addClass = function(element, className) {
    HtmlUtility.removeClass(element, className);
    element.className += " " + className;
};

HtmlUtility.removeClass = function(element, className) {
    if (!(element && element.className))
        return;
    var cls = element.className.split(" ");
    var ar = new Array();
    for (var i = cls.length; i > 0;) {
        if (cls[--i] != className) {
            ar[ar.length] = cls[i];
        }
    }
    element.className = ar.join(" ");
};