var waitMsg = {
 wait: function(message) {
  if (typeof message == 'undefined') message = 'Loading...';
  var id = document.getElementById('wait_message_box');
  if (!id) {
  	var cont = document.createElement('DIV');
  	cont.style.display = 'none';
  	cont.innerHTML = "<div id='wait_message_box' style='width: 200px; height: 20px'>Loading.....</div>"
  	document.body.appendChild(cont);
  }
  var html = '<table width=100% height=100%><Tr><td valign=middle align=center><b>' + message + '</b></td></tr></table>';
  document.getElementById('wait_message_box').innerHTML = html;
  $.fn.colorbox({
   inline:true,
   href:"#wait_message_box",
   overlayClose: false,
   open:true
  });
  document.getElementById('cboxClose').style.display = 'none';
 },
 hide: function() {
  document.getElementById('cboxClose').style.display = 'block';
  $.fn.colorbox.close();
 }
}


function is_array(input){
    return typeof(input)=='object'&&(input instanceof Array);
}

function is_null(input){
	return input==null;
}

function str_replace(needle, replacement, haystack) { 

	if (haystack == undefined)
	{
		haystack = '';
	}

	var temp = haystack.split(needle); 
	return temp.join(replacement); 
} 

function trim( str, charlist ) {	// Strip whitespace (or other characters) from the beginning and end of a string
	charlist = !charlist ? ' \s\xA0' : charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
	var re = new RegExp('^[' + charlist + ']+|[' + charlist + ']+$', 'g');
	return str.replace(re, '');
}

function dump(arr, level)
{
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}

function in_array(needle, haystack, argStrict) {
    var key = '', strict = !!argStrict; 
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    } 
    return false;
}

function explode( delimiter, string ) {	// Split a string by string
	// 
	// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +   improved by: kenneth
	// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)

	var emptyArray = { 0: '' };

	if ( arguments.length != 2
		|| typeof arguments[0] == 'undefined'
		|| typeof arguments[1] == 'undefined' )
	{
		return null;
	}

	if ( delimiter === ''
		|| delimiter === false
		|| delimiter === null )
	{
		return false;
	}

	if ( typeof delimiter == 'function'
		|| typeof delimiter == 'object'
		|| typeof string == 'function'
		|| typeof string == 'object' )
	{
		return emptyArray;
	}

	if ( delimiter === true ) {
		delimiter = '1';
	}

	return string.toString().split ( delimiter.toString() );
}

function addOption (oListbox, text, value, isDefaultSelected, isSelected) {
	var oOption = document.createElement("option");
	oOption.appendChild(document.createTextNode(text));
	oOption.setAttribute("value", value);
	oOption.setAttribute("label", text);
	
	if (isDefaultSelected) oOption.defaultSelected = true;
	else if (isSelected) oOption.selected = true;

	oListbox.appendChild(oOption);
}

function removeOption (oListbox, oOption) {	
	oListbox.removeChild(oOption);
}

function addListItem(sourceListName, destListName) {
	var sourceList = byId(sourceListName);
	var destList = byId(destListName);
	
	if (!sourceList || !destList) return false;
	
	if (sourceList.selectedIndex < 0) return false;
	
	var destItems = destList.options;
	while (sourceList.selectedIndex != -1) { 
		
		var newItem = sourceList.options[sourceList.selectedIndex];
		if (newItem) {
			var present = false;
			for (var i = 0; i < destItems.length; i++) {
				if (destItems[i].value == newItem.value) {
					present = true;
					break;
				}
			}
			if (!present) {
				addOption(destList, newItem.label, newItem.value);
			}
		}
		
		removeOption(sourceList, sourceList.options[sourceList.selectedIndex]);
	}
			
	sourceList.selectedIndex = 0;
	
	return true;
}

function removeListItem(listName, destListName) {
	var list = byId(listName);
	var destList = byId(destListName);
	if (!list) return false;
	
	if (list.selectedIndex < 0) return false;
	
	while (list.selectedIndex != -1) { 
		
		var item = list.options[list.selectedIndex];
		if (item) {
			removeOption(list, item);
			addOption(destList, item.label, item.value);
		}
		
	}
	list.selectedIndex = 0;
	return true;
}

function getListItems(listName) {
	var items = new Array();
	
	var list = byId(listName);
	if (!list) return res;
	
	for (var i = 0; i < list.options.length; i++) {				
		items.push(list.options[i].value);
	}
	
	return items;
}

function openDate(chbox) {
	
	if (chbox.checked) {
		var prefix = 'to_';
		
		var names = ["Year", "Month", "Day", "Hour", "Date"];
		
		for (var i = 0; i < names.length; i++) {
			names[i] = prefix + names[i];
		}
		
		var els = document.getElementsByTagName("select");	
		for (var i = 0; i < els.length; i++) {				
			if (in_array(els[i].name, names)) {
				if (els[i].name == "to_Hour") els[i].value = "00";
				els[i].value = null;			
			}
		}
		var els1 = document.getElementsByTagName("input");	
		for (var i = 0; i < els1.length; i++) {				
			if (in_array(els1[i].name, names)) {
				els1[i].value = "";			
			}
		}
		
		byId('datex1').disabled = true;
		byId('datex2').disabled = true;
		byId('popupDatepickerEnd').disabled = true;
		
	} else {
		
		byId('datex1').disabled = false;
		byId('datex2').disabled = false;
		byId('popupDatepickerEnd').disabled = false;
		
	}
	
}

function tip(section, id) {
	$.fn.colorbox({	  
	    href: "/help/tip/" + section + "/" + id,
		width: "600px",
		height: "500px",		  
		opacity: 0.3,
		initialWidth: 100,
		initialHeight: 50
	});
}
function strip_tags(el)
{
    str = $(el).val();
    
    var key = '', allowed = false;
    var matches = [];
    var allowed_array = [];
    var allowed_tag = '';
    var i = 0;
    var k = '';
    var html = '';

    var replacer = function(search, replace, str) {
        return str.split(search).join(replace);
    };

    // Build allowes tags associative array
    /*
    if (allowed_tags) {
        allowed_array = allowed_tags.match(/([a-zA-Z]+)/gi);
    }
    */
    str += '';

    // Match tags
    matches = str.match(/(<\/?[\S][^>]*>)/gi);

    // Go through all HTML tags
    for (key in matches) {
        if (isNaN(key)) {
            // IE7 Hack
            continue;
        }

        // Save HTML tag
        html = matches[key].toString();

        // Is tag not in allowed list? Remove from str!
        allowed = false;

        // Go through all allowed tags
        for (k in allowed_array) {
            // Init
            allowed_tag = allowed_array[k];
            i = -1;

            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+'>');}
            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+' ');}
            if (i != 0) { i = html.toLowerCase().indexOf('</'+allowed_tag)   ;}

            // Determine
            if (i == 0) {
                allowed = true;
                break;
            }
        }

        if (!allowed) {
            str = replacer(html, "", str); // Custom replace. No regexing
        }
    }
    $(el).val(str);
}