if (!window.RunHCommand) {
    window.RunHCommand = function(cmdline, callback) {
        var req = new XMLHttpRequest();
        req.open('POST', "/run_command/", true);
        req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        req.setRequestHeader("Content-length", cmdline.length);
        req.setRequestHeader("Connection", "close");
        
        req.onreadystatechange = function() {
            if (req.readyState == 4 && req.status == 200) {
                if (callback) {
                    callback(req.responseText);
                }
            } else if (req.readyState == 4) {
                alert(req.responseText + " :: " + req.status);
            }
        }
        
        req.send("cmd="+escape(cmdline));
    }
}


function loadLocalExample(file) {
    try {
        var dot = file.lastIndexOf(".");
        var ext = file.substr(dot);
        var cmdLine;
        if (ext == ".cmd") {
            cmdLine = "source loadHelpcardExample.cmd $HFS/houdini/help/" + file + " INTERNAL";
        } else if (ext == ".otl") {
            cmdLine = "source loadHelpcardOTLExample.cmd $HFS/houdini/help/" + file + " INTERNAL";
        }
        result = RunHCommand(cmdLine);
        if (result && result.toLowerCase().substr(0,3) == "err") {
            alert(result);
        }
    } catch (e) {
        alert("loadLocalExample: "+e);
    }
}

function loadLocal(file) {
    try {
        var cmdLine = "mread -i \"" + file + "\"";
        RunHCommand(cmdLine);
    } catch (e) {
        alert("loadLocal: "+e);
    }
}

var productMap = {
    "master":"houdini",
    "select":"hselect",
    "escape":"hescape",
    "halo":"hhalo"
}

function checkSafeCommands(){
	// this function tests to see if the Preference
	// "Restrict Browser to Safe Commands" is turned on
	// returns true or false
	var prefStatus = RunHCommand("echo \`system(\"pwd\")\`");
	
	return (prefStatus.indexOf("Unsafe") > -1);
}

function getProductCommand() {
	var version = RunHCommand("version -n").split(" ");
	var product = version[1].toLowerCase();
	
	var command = "houdini";
	if (productMap[product]) {
		command = productMap[product];
	}
	return command;
}

function loadExternal(file) {
    try {
        var cmd = getProductCommand();
        var desk = "Build";
        if (cmd == "hhalo") {
            desk = "Image";
        }
    
        var unsafeOn = checkSafeCommands();
    
        if (!unsafeOn) {
            var cmdLine = "unix " + cmd + " -s " + desk + " " + file;
	    var otl_ext = ".otl";

	    // If our target file is an OTL, then we need to run
	    // a script to create the example node after installing the OTL.
	    if (file.lastIndexOf(otl_ext) == file.length - otl_ext.length)
		cmdLine += " $HFS/houdini/scripts/python/create_example_node.py"

            RunHCommand(cmdLine);
        } else {
            var local = confirm("Unsafe browser commands are turned OFF, load into current session instead?");
            
            if(local) {
                loadLocal(file);
            }
        }
    } catch (e) {
        alert("loadExternal: "+e);
    }
}

function loadExternalOTL(file) {
    try {
        var cmd = getProductCommand();
        var desk = "Build";
        if (cmd == "hhalo") {
            desk = "Image";
        }
    
        var unsafeOn = checkSafeCommands();
    
        if (!unsafeOn) {
            var cmdLine = "unix " + cmd + " -s " + desk + " "
			    + "loadHelpcardOTLExample.cmd " 
			    + file + "EXTERNAL";
            RunHCommand(cmdLine);
        } else {
            var local = confirm("Unsafe browser commands are turned OFF, load into current session instead?");
            
            if(local) {
                loadLocal(file);
            }
        }
    } catch (e) {
        alert("loadExternalOTL: "+e);
    }
}
