/**
 * © 2008 Artifice Investments, LLC
 *
 * This file and all of the source code contained within are the intellectual 
 * property of Artifice Investments, LLC. Any reproduction, description (either
 * written or verbal), or derivative of this code is strictly prohibited without
 * the written consent of Artifice Investments, LLC.
 *
 * -------------------------------------------------------------------------------
 *
 * Description: Common javascript functions.
 *      Author: Daniel Jennings (dsj@artificeinvestments.com)
 *    Revision: A
 *  References: None
 *       Notes: None
 */


//add trim method to string class
String.prototype.trim = function() { 
	return this.replace(/^\s+|\s+$/g, ''); 
}

//string comparison
String.prototype.equalToIgnoreCase = function(str) {
	return this.toLowerCase() === str.toLowerCase();
}


/**
 * Get an element.
 * @param element Element ID espected, but if DOM element is given will simply return.
 * @return DOM element.
 */
function getElement(element) {
    if (typeof element == 'string') { 
		element = document.getElementById(element); 
	}
	return element;
}


/**
 * Get an XML node's inner text value.
 * @param node XML node.
 * @return The given node's inner text value.
 */
function getInnerText(node) {
	 return (node.textContent || node.innerText || node.text);
}


/**
 * Get the value of a tag
 * @param xml XML object
 * @param tagName Name of tag.
 * @return Value of tag
 */
function getTagValue(xml, tagName) {
	return getInnerText(xml.getElementsByTagName(tagName).item(0));
}


/**
 * Check if a value is in an array.
 * @param array Array to check
 * @param value Value to check for.
 * @return True if value is in array, otherwise false.
 */
function inArray(array, value) {
	for (var ii=0; ii<array.length; ii++) {
		if (array[ii] == value) {
			return true;
		}
	}

	return false;
} //end method inArray


/**
 * Get the value of a radio button.
 * @param name Name of the radio button.
 * @return Value of the radio button.
 */
function getRadioButtonValue(name) {
	var value = null;
	var radio = document.getElementsByName(name);
	for (var ii=0; ii<radio.length; ii++) {
		if (radio[ii].checked) {
			value = radio[ii].value;
			break;
		}
	}
	return value;
} //end method getRadioButtonValue


/**
 * Set the value of a radio button.
 * @param name Name of the radio button.
 * @param value Value to select
 */
function setRadioButtonValue(name, value) {
	var radio = document.getElementsByName(name);
	for (var ii=0; ii<radio.length; ii++) {
		if (value == radio[ii].value) {
			radio[ii].checked = true;
		}
		else {
			radio[ii].checked = false;
		}
	}
} //end method setRadioButtonValue


/**
 * Get the value of a select box.
 * @param element Select box DOM object.
 * @return Selected value.
 */
function getSelectBoxValue(element) {
	//get the element
	element = getElement(element);
    return element[element.selectedIndex].value;
} //end method getSelectBoxValue


/**
 * Get the value of a select box.
 * @param element Select box DOM object.
 * @return Selected inner HTML.
 */
function getSelectBoxSelection(element) {
	//get the element
	element = getElement(element);
    return element[element.selectedIndex].innerHTML;
} //end method getSelectBoxSelection


/**
 * Set the select box value.
 * @param element DOM Element object (or element ID).
 * @param selectedValue Value to select.
 */
function setSelectBoxValue(element, selectedValue) {
	//get the element
	element = getElement(element);
	
	//loop through all options, search for a match
	for(ii=0; ii<element.length; ii++) {
		if (element.options[ii].value == selectedValue) {
			element.selectedIndex = ii;
			break;
		}
	}
} //end method setSelectBoxValue


/**
 * Determine if a select box has a given value.
 * @param element DOM element object (or element ID).
 * @param value Value to check
 * @return True if the value exists in the select box, otherwise false.
 */
function doesSelectBoxValueExist(element, value) {
	//initialize output
	var ret = false;
	
	//get the element
	element = getElement(element);
	
	//loop through all options, search for a match
	for(ii=0; ii<element.length; ii++) {
		if (element.options[ii].value == value) {
			ret = true;
			break;
		}
	}
	
	return ret;
} //end method doesSelectBoxValueExist


/**
 * Set a cookie.
 * @param name Name of the cookie.
 * @param value Value of the cookie.
 * @param expireNumDays (Optional) Number of days until expiration.
 * NOTE: modified from: http://www.w3schools.com/JS/js_cookies.asp
 */
function setCookie(name, value, expireNumDays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expireNumDays);
	document.cookie = name + "=" + escape(value) + ((expireNumDays == null) ? "" : ";expires=" + exdate.toGMTString());
} //end method setCookie


/**
 * Get a cookie.
 * @param name Name of the cookie.
 * @return Value of the cookie.
 * NOTE: modified from: http://www.w3schools.com/JS/js_cookies.asp
 */
function getCookie(name) {
	//initialize output
	var value = "";

	if (document.cookie.length > 0) {
		var start_index = document.cookie.indexOf(name + "=");

		if (start_index != -1) { 
			start_index = start_index + name.length + 1; 
			var end_index = document.cookie.indexOf(";", start_index);
			
			if (end_index == -1) {
				end_index = document.cookie.length;
			}

			value = unescape(document.cookie.substring(start_index, end_index));
		} //end if
	} //end if

	return value;
} //end method getCookie
 


/**
 * Create an XML HTTP request object.
 * @return An XML HTTP request object.
 */
function createXMLHTTPRequestObject(){
	//create variable to hold request object
	var request_obj;

	//get the browser name
	var browser = navigator.appName;

	//depending on the browser name create the object
	if (browser == "Microsoft Internet Explorer") {
		request_obj = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else {
		request_obj = new XMLHttpRequest();
	}

	return request_obj;
} //end method createXMLHTTPRequestObject


/**
 * Trim a text field.
 * @param subject Original text.
 * @param maxNumCharacters Maximum number of characters to allow.
 * @param appendedText Text to append of text is trimmed. Defaults to "...".
 * @return Trimmed text.
 */
function trimText(text, maxNumCharacters, appendedText) {
    //only trim if number of number of characters is greater than max
    if (text.length > maxNumCharacters) {
		if (appendedText == null) {
			appendedText = '...';
		}

        text = text.substring(0, maxNumCharacters).trim() + appendedText;
    }

    return text;
} //end method trimText


/**
 * Mimics the PHP wordwrap function.
 * @param str String to wrap.
 * @param width The width to wrap.
 * @param breakStr The break string.
 * @param cut Flag indicating whether to cut words.
 * @return The wrapped string.
 *
 * NOTE: modified from http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_wordwrap/
 */
function wordwrap(str, width, breakStr, cut) {
    //use PHP Defaults
    var m = ((arguments.length >= 2) ? arguments[1] : 75   );
    var b = ((arguments.length >= 3) ? arguments[2] : "\n" );
    var c = ((arguments.length >= 4) ? arguments[3] : false);
 
    var i, j, l, s, r;
 
    str += '';
 
    if (m < 1) {
        return str;
    }
 
    for (i = -1, l = (r = str.split("\n")).length; ++i < l; r[i] += s) {
        for(s = r[i], r[i] = ""; s.length > m; r[i] += s.slice(0, j) + ((s = s.slice(j)).length ? b : "")){
            j = c == 2 || (j = s.slice(0, m + 1).match(/\S*(\s)?$/))[1] ? m : j.input.length - j[0].length || c == 1 && m || j.input.length + (j = s.slice(m).match(/^\S*/)).input.length;
        }
    }
 
    return r.join("\n");
} //end method wordwrap


/**
 * Get the key that was pressed.
 * @param event Key press event.
 * @return The keycode for the key that was pressed.
 */
function getKeyPress(event) {
	if (window.event) {
		return window.event.keyCode; //IE
	}
	else if (event.which) {
		return event.which; //Netscape/Firefox/Opera
	}
} //end method getKeyPress


/**
 * Hide an element.
 * @param element DOM Element object (or element ID) to hide.
 */
function hideElement(element) {
	//get the element
	element = getElement(element);
	
	//change style
	if (element != null) {
		if ((element.style.visibility == '') || (element.style.visibility == 'visible')) {
			element.style.visibility = 'hidden';
			element.style.display = 'none';
		}
	}
} //end method hideElement


/**
 * Show an element.
 * @param element DOM Element object (or element ID) to show.
 */
function showElement(element, displayType) {
	//see if display type was given
	if (displayType == null){
		displayType = 'block';
	}

	//get the element
	element = getElement(element);
	
	//change style
	if (element != null) {
		if ((element.style.visibility == '') || (element.style.visibility == 'hidden')) {
			element.style.visibility = 'visible';
			element.style.display = displayType;
		}
	}
} //end method showElement


/**
 * Show/Hide an element.
 * @param element DOM Element object (or element ID) to show/hide.
 */
function showHideElement(element) {
	//get the element
	element = getElement(element);
	
	//change style
	if (element.style.visibility == 'hidden') {
		element.style.visibility = 'visible';
		element.style.display = 'block';
	}
	else {
		element.style.visibility = 'hidden';
		element.style.display = 'none';
	}
} //end method showHideElement



/*************************************************************************************
 *									Style Sheet Methods
 *************************************************************************************/

 /**
 * Checks a given class attribute for the presence of a given class.
 * @param element DOM Element object (or element ID) to add the class to
 * @param className Name of the class.
 * @param True if the exists, otherwise false.
 *
 * @reference http://fluidmind.org/software/javascript/css-class-functions.php
 */
function checkForClass(element, className) {
	//get the element
	element = getElement(element);

    if (element.className == '') {
        return false;
    } 
	else {
        return new RegExp('\\b' + className + '\\b').test(element.className);
    }
} //end method checkForClass


/**
 * Adds a class to an element's class attribute.
 * @param element DOM Element object (or element ID) to add the class to
 * @param className Name of the class to add.
 *
 * @reference http://fluidmind.org/software/javascript/css-class-functions.php
 */
function addClass(element, className) {
	//get the element
	element = getElement(element);
	
	//check if class already exists
    if (!checkForClass(element, className)) {
        element.className += (element.className ? ' ' : '') + className;
    }
} //end method addClass


/**
 * Removes a class from an element's class attribute
 * @param element DOM Element object (or element ID) to add the class to
 * @param className Name of the class to remove.
 *
 * @reference http://fluidmind.org/software/javascript/css-class-functions.php
 */
function removeClass(element, className) {
    //get the element
	element = getElement(element);

	//make sure class exists
    if (checkForClass(element, className)) {
        element.className = element.className.replace((element.className.indexOf(' ' + className) >= 0 ? ' ' + className : className), '');
    }
} //end method removeClass


/**
 * Set the class of an element.
 * @param element DOM Element object (or element ID) to add the class to
 * @param className Class to use
 */
function setClass(element, className) {
	//get the element
	element = getElement(element);
	
	//change the class
	element.className = className;
} //end method setClass


/**
 * Get the current price for a stock
 * @param symbol Stock symbol
 */
function getCurrentPrice(symbol) {
	//init output
	var price = null;
	
	try {
		//create http request object
		var http = createXMLHTTPRequestObject();
	
		//open URL for php data provider
		var url = COMMON_FILE_URL + "utilities/stocks/get_current_stock_data.php?symbol=" + symbol;
		http.open("GET", url, false);
		http.send(null);
		
		//make sure valid response is received from the server
		if (http.readyState == 4) {
			if (http.status == 200) {
		     	var response = http.responseXML;
		     	price = getTagValue(response, 'current_price');
			}
		}
	}
	catch (exception) {
		//do nothing
	}
        
	return price;
} //end method getCurrentPrice