/**
 * toggle element visibility
 * @param   id      (string)  document element id
 * @param   vhstr   (string)  visible | hidden
 * @param   disp    (string)  block | inline
 */
function toggleVisibility(id, vhstr, disp)
{
    if (disp === null || disp == 'undefined' || !disp)
        disp = 'block';

    var el = document.getElementById(id);
    if (!el || el == 'undefined')
    	return;
    
    if (vhstr === null || vhstr == 'undefined' || !vhstr)
    {
        // toggle element visibility
    	vhstr = (el.style.visibility == "hidden" ? "visible" : "hidden");
    }
    el.style.visibility = vhstr;
    el.style.display = (vhstr == "visible" ? disp : "none");
}

function showDiv(div, disp) {
    toggleVisibility(div, 'visible', disp ? disp : 'block');	
}

function hideDiv(div) {
	toggleVisibility(div, 'hidden', 'none');
}

function dump_object(obj, objName)
{
    var result = "";
    for (var i in obj)
    {
        result += objName + "." + i + " = " + obj[i] + "\n";
    }
    return result;
}

function in_array(needle, haystack, argStrict) {
    var found = false, key, strict = !!argStrict;

    for (key in haystack) {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            found = true;
            break;
        }
    }
    return found;
}

/**
 * block UI
 * @param msg
 * @param timeout (in secs)
 * @return
 */
function mask(msg, timeout) {
    if (!msg) msg = 'Please wait';
    $.blockUI({ message: '<h1><img src="/images/busy.gif" /> ' + msg + '...</h1>'});
    if (timeout != 'undefined' && timeout > 0) {
    	if (timeout < 1000) timeout = timeout * 1000;
    	if (timeout >= 1000) setTimeout('unmask()', timeout);
    }
}

function unmask() {
    $.unblockUI();
}

/* parseUri JS v0.1.1, by Steven Levithan <http://stevenlevithan.com>
Splits any well-formed URI into the following parts (all are optional):
----------------------
- source (since the exec method returns the entire match as key 0, we might as well use it)
- protocol (i.e., scheme)
- authority (includes both the domain and port)
  - domain (i.e., host; can be an IP address)
  - port
- path (includes both the directory path and filename)
  - directoryPath (supports directories with periods, and without a trailing backslash)
  - fileName
- query (does not include the leading question mark)
- anchor (i.e., fragment) */
function parseUri(sourceUri){
	var uriPartNames = ["source","protocol","authority","domain","port","path","directoryPath","fileName","query","anchor"],
		uriParts = new RegExp("^(?:([^:/?#.]+):)?(?://)?(([^:/?#]*)(?::(\\d*))?)((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[\\?#]|$)))*/?)?([^?#/]*))?(?:\\?([^#]*))?(?:#(.*))?").exec(sourceUri),
		uri = {};

	for(var i = 0; i < 10; i++){
		uri[uriPartNames[i]] = (uriParts[i] ? uriParts[i] : "");
	}

	/* Always end directoryPath with a trailing backslash if a path was present in the source URI
	Note that a trailing backslash is NOT automatically inserted within or appended to the "path" key */
	if(uri.directoryPath.length > 0){
		uri.directoryPath = uri.directoryPath.replace(/\/?$/, "/");
	}

	return uri;
}

/**
 * get url parameters
 */
function gup(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function parseFilename(fname) {
    var slash = '/';
    var dot = '.';
    var parts = new Object;
    fname = fname.replace('\\', '/');

    if (fname.match(/\//))
        parts.path = fname.substring(1, fname.lastIndexOf(slash) - 1);

    parts.name = fname.substring(fname.lastIndexOf(slash), fname.lastIndexOf(dot));
    parts.ext = fname.substring(fname.lastIndexOf(dot) + 1);
    return parts;
}

function isFileName(fname) {
	var parts;
	var re = /([\/\\\:\*\?\"<>\|]+)/;
	
	if (fname.match(re)) {
		return {"isValid":false, "error":"Filename contains invalid characters"};
	}

	parts = parseFilename(fname);

	if (parts.name == 'undefined' || parts.name == null || !parts.name) {
		return {"isValid":false, "error":"Filename has no file extension"};
	}
	
	return {"isValid":true};
}

