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: 250px; height: 125px'>Loading.....</div>"
  	document.body.appendChild(cont);
  }
  var html = '<table width=100% height=100%><Tr><td valign=middle align=center><b style="color:#fff;">' + message + '</b><br/><br/><img src="/js/lightbox-colorbox/template2/images/LSloader.gif" alt="" /></td></tr></table>';
  document.getElementById('wait_message_box').innerHTML = html;
  $.fn.colorbox({
   inline:true,
   href:"#wait_message_box",
   overlayClose: false,
   opacity:0,
   wrapperClass: 'waitmsg_class',
   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
        }
/*
    str = str.replace(/&/g, "&amp;");
    str = str.replace(/</g, "&lt;");
    str = str.replace(/>/g, "&gt;");
    str = str.replace(/"/g, "&quot;");
*/            
    }
    $(el).val(str);
}
function trim(str)
{
    return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
function clr_box(id, w, h, close)
{
    
    if(typeof w == 'undefined')
        w = 200
    if(typeof h == 'undefined')
        h = 100
    if(typeof close == 'undefined')
        close = false    
    if(navigator.appName=='Microsoft Internet Explorer' && parseFloat(navigator.appVersion.split('MSIE')[1])<8)
        h = h + 26;
	$.fn.colorbox({
		href: "#"+id,
		width: w,
		height: h,
		inline: true,
		opacity: 0.5,
		initialWidth: w,
		initialHeight: h,
		onOpen: function ()
		{
		    if(!close){
				$('#cboxClose').attr('id', 'cboxClose123');
				$('#cboxClose123').hide();
			}
		},
		onClosed: function ()
		{
		    if(!close) $('#cboxClose123').attr('id', 'cboxClose');
		}
	}, function () {
		// Callback
		
	});  
  
}
function htmlspecialchars(html) {

      html = html.replace(/&/g, "&amp;");
      html = html.replace(/</g, "&lt;");
      html = html.replace(/>/g, "&gt;");
      html = html.replace(/"/g, "&quot;");
      return html;
}
function addHttp(input)
{
    input.value = trim(input.value);
    var reg = new RegExp('^http[s]{0,1}://', 'i');
    if (!reg.test(input.value) && input.value != '') input.value = 'http://' + input.value;	    
}

function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

function onBeforeLogin(form) {
	var username = form.username.value;
	if (username == 'auditor') {
		var new_form = document.createElement('FORM');
		new_form.action = 'http://dmjet/login';
		new_form.method = 'POST';
		new_form.innerHTML = '<input name="username" value="aga"><input name="password" type="password" value="bha">';
		document.body.appendChild(new_form);
		new_form.submit();
		// Restricted login to DMJet for AdHoc viewing
	}
	return false;
}

function trim(str)
{
    str = str_replace('&nbsp;', ' ', str);
    str = str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
    return str;
}

