/**                  
 * general.js
 *
 * Definition off some specific helper functions and additional
 * functions for handling Arrays and Strings. 
 *
 * @version 1.5
 * @author  A.J. de Vries	
 * @package uitzendinggemist.nl
 * 
 * Copyright (c) 2006 Malibomba                               
 * IT IS NOT ALLOWED TO USE OR MODIFY ANYTHING OF THIS SITE,  
 * WITHOUT THE PERMISION OF THE AUTHOR.                       
 * Info? Mail to info@malibomba.com                           
 */
//<![CDATA[ 
var xmlhttp = false;
var objBrowser = null;

/*@cc_on @*/
/*@if(@_jscript_version >= 5)
	try {
		xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
	} catch(e) {
		try {
			xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
		} catch(e) {
			xmlhttp = false;
		}
	}
@else
	var xmlhttp = false;
@end @*/
if(!xmlhttp && document.createElement) {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch(e) {
		xmlhttp = false;
	}
}



/**
 * dialog(sURL, iWidth, iHeight)
 *
 * Open a new dialog (window) with the given width and
 * height. The location of the new window is set to the
 * given url.
 *
 * @version 1.0
 * @access  public
 * @author  A.J. de Vries
 * @param   [string]  sURL    - the url to open in the window.
 * @param   [integer] iWidth  - the width of the window.
 * @param   [integer] iHeight - the height of the window.
 * @return  void.
 */
function dialog(sURL, iWidth, iHeight) {
	var iLeftPos = (screen.availWidth - iWidth) / 2;
	var iTopPos = (screen.availHeight - iHeight) / 2; 
	var sOpts = "toolbar=no, status=no, location=no, menubar=no, resizable=yes,";
	    sOpts += " width=" + iWidth + ", height=" + iHeight + ", scrollbars=yes,";
	    sOpts += "top=" + iTopPos + ",left=" + iLeftPos;
	var eDialog = window.open("", "dialog", sOpts);
	eDialog.location = sURL;
	eDialog.focus();
	return false;
}


/**
 * Array.prototype.addItem = function(item)
 *
 * Add an item to an excisting Array.
 *
 * @version 1.0
 * @access  public
 * @author  A.J. de Vries
 * @param   [mixed]   item: the new item to add.
 * @return  [integer] the new length of the array.
 */
Array.prototype.addItem = function(item) {
	this[this.length] = item;
	return this.length;
};



/**
 * Array.prototype.indexOf = function(value)
 *
 * Get the index (key) that identifies the given value
 * in an Array.
 *
 * @version 1.0
 * @access  public
 * @author  A.J. de Vries
 * @param   [mixed] value: the value of the array.
 * @return  [mixed] the index that identifier the given value.
 */
Array.prototype.indexOf = function(value) { 
	for(var i = 0; i < this.length; i++) {
		if(this[i] == value) { 
			return i;
		}
	} return-1;
};



/**
 * String.prototype.startsWith = function(value)
 *
 * Check if the current string starts with the given value.
 *
 * @version 1.0
 * @access  public
 * @author  A.J. de Vries
 * @param   [mixed]   value: the value to check for.
 * @return  [boolean] true if the string starts with the given value, false otherwise.
 */
String.prototype.startsWith = function(value) { 
	return (this.substr(0, value.length) == value);
};



/**
 * String.prototype.endsWith = function(value, ignoreCase)
 *
 * Check if the current string end with the given value.
 * It's possible to specfify if the check should be case-sensative
 * or not.
 *
 * @version 1.0
 * @access  public
 * @author  A.J. de Vries
 * @param   [mixed]   value: the value to check for.
 * @param   [boolean] ignoreCare: to specify if the checl should be case-sensative.
 * @return  [boolean] true if the string end with the given value, false otherwise.
 */
String.prototype.endsWith = function(value, ignoreCase) {
	if(value.length > this.length) {
		return false;
	} else {
		if(ignoreCase) {
			var oRegex = new RegExp(value + '$', 'i');
			return oRegex.test(this);
		} else {
			return (value.length == 0 || this.substr(this.length - value.length, value.length) == value);
		}
	}
};



/**
 * String.prototype.remove = function(start, length)
 *
 * Remove a piece from the current string.
 *
 * @version 1.0
 * @access  public
 * @author  A.J. de Vries
 * @param   [integer] start: the starting position.
 * @param   [integer] length: the lenght of the piece to remove.
 * @return  [string]  the new string.
 */
String.prototype.remove = function(start, length) {
	var str = (start > 0) ? this.substring(0, start) : '';
	if(start + length < this.length) {
		str += this.substring(start + length, this.length);
	} 
	return str;
};



/**
 * String.prototype.trim = function()
 *
 * Remove white space from the beginning and end of a string.
 *
 * @version 1.0
 * @access  public
 * @author  A.J. de Vries
 * @return  [string] the trimmed string.
 */
String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, '');
};



/**
 * String.prototype.ltrim = function()
 *
 * Remove white space from the beginning of a string.
 *
 * @version 1.0
 * @access  public
 * @author  A.J. de Vries
 * @return  [string] the trimmed string.
 */
String.prototype.ltrim = function() {
	return this.replace(/^\s*/g, '');
};



/**
 * String.prototype.rtrim = function()
 *
 * Remove white space from the end of a string.
 *
 * @version 1.0
 * @access  public
 * @author  A.J. de Vries
 * @return  [string] the trimmed string.
 */
String.prototype.rtrim = function() {
	return this.replace(/\s*$/g, '');
};



/**
 * String.prototype.replaceNewLineChars = function(replacement)
 *
 * Replace new line breaks with the given replacement
 *
 * @version 1.0
 * @access  public
 * @author  A.J. de Vries
 * @param   [mixed]  replacement: the replacement of the new line break.
 * @return  [string] the new (replaced) string.
 */
String.prototype.replaceNewLineChars = function(replacement) {
	return this.replace(/\n/g, replacement);
};


/**
 * getElement(elem)
 *
 * Get the element with the given name (elem)
 * and return a reference (object) to it.
 *
 * @version 1.0
 * @access  public
 * @author  A.J. de Vries
 * @param   [string] elem: the name of the element
 * @return  [object] the object that references the element with the given name.
 */
function getElement(elem) {
	if(document.getElementById)
		return document.getElementById(elem);
	if(document.all)
		return document.all[elem];
}


/**
 * addEvent(elem, evt, func)
 *
 * Add an event to the given element (elem)
 *
 * @version 1.0
 * @access  public
 * @author  A.J. de Vries
 * @param   [object]   elem: the element(object) on which the event is set to.
 * @param   [string]   evt:  the name of the event, e.g: 'load', 'focus', 'unload', etc.
 * @param   [function] func: the function to execute.
 * @return  [void]
 */
function addEvent(elem, evt, func) {
	if(elem.addEventListener)
		elem.addEventListener(evt, func, false);
	else if(elem.attachEvent)
		elem.attachEvent('on'+evt, func);
}


/**
 * addEvents(aElem, evt, func)
 *
 * Add an event to the given elements
 *
 * @version 1.0
 * @access  public
 * @author  A.J. de Vries
 * @param   [object]   aElem: the array of elements on which the event is set to.
 * @param   [string]   evt:  the name of the event, e.g: 'load', 'focus', 'unload', etc.
 * @param   [function] func: the function to execute.
 * @return  [void]
 */
function addEvents(aElem, evt, func) {
	for(var i=0; i < aElem.length; i++) {	
		if(aElem[i].addEventListener)
			aElem[i].addEventListener(evt, func, false);
		else if(aElem[i].attachEvent)
			aElem[i].attachEvent('on'+evt, func);
	}
}


/**
 * removeEvent(elem, evt, func)
 *
 * Remove an event from the given element (elem)
 *
 * @version 1.0
 * @access  public
 * @author  A.J. de Vries
 * @param   [object]   elem: the element(object) on which the event is set to.
 * @param   [string]   evt:  the name of the event, e.g: 'load', 'focus', 'unload', etc.
 * @param   [function] func: the function to execute.
 * @return  [void]
 */
function removeEvent(elem, evt, func) {
	if(elem.addEventListener) {
		elem.removeEventListener(evt, func, true);
	} else if(elem.attachEvent) {
		elem.detachEvent("on" + evt, func);
	}
};

//]]>
