dojo.require("dijit.layout.BorderContainer");
dojo.require("dijit.layout.TabContainer");
dojo.require("dijit.layout.AccordionContainer");
dojo.require("dijit.layout.ContentPane");
dojo.require("dijit.TitlePane");
dojo.require("dijit.form.Button");
dojo.require("dojo.data.ItemFileReadStore");
dojo.require("dojox.widget.Iterator");
dojo.require("dojo.parser");
dojo.require("dojox.xml.DomParser");
dojo.require("dijit.form.CheckBox");

dojo.addOnLoad(function()
{
    //Versionning
    checkVersionning();
    dojo.connect(dijit.byId("versionningcb"), "onChange", checkVersionning);
    dojo.connect(dijit.byId("Mercurial"), "onChange", check_Config);
    dojo.connect(dijit.byId("USVN"), "onChange", check_Config);
 
    function checkVersionning() {
        var  isChecked = true;
        check_Config();
        if (dijit.byId("versionningcb").checked) {
            isChecked = false;
        }
        dijit.byId("Mercurial").setAttribute("disabled", isChecked);
        dijit.byId("USVN").setAttribute("disabled", isChecked);
    }

    //FTP
    checkFTP();
    dojo.connect(dijit.byId("ftpcb"), "onChange", checkFTP);
    dojo.connect(dijit.byId("ProFTPD"), "onChange", check_Config);
    dojo.connect(dijit.byId("vsFTPD"), "onChange", check_Config);
    
    function checkFTP() {
        var  isChecked = true;
        check_Config();
        if (dijit.byId("ftpcb").checked) {
            isChecked = false;
        }
        dijit.byId("ProFTPD").setAttribute("disabled", isChecked);
        dijit.byId("vsFTPD").setAttribute("disabled", isChecked);
    }
    
    //Mailing List
    checkMailing();
    dojo.connect(dijit.byId("Mailingcb"), "onChange", checkMailing);
    dojo.connect(dijit.byId("ecartis"), "onChange", check_Config);
    dojo.connect(dijit.byId("mailman"), "onChange", check_Config);
    
    function checkMailing() {
        var  isChecked = true;
        check_Config();
        if (dijit.byId("Mailingcb").checked) {
            isChecked = false;
        }
        dijit.byId("ecartis").setAttribute("disabled", isChecked);
        dijit.byId("mailman").setAttribute("disabled", true);
    }
    
    //BugTracker
    checkBugTracker();
    dojo.connect(dijit.byId("bugtrackercb"), "onChange", checkBugTracker);
    dojo.connect(dijit.byId("Redmine"), "onClick", check_Config);
    dojo.connect(dijit.byId("MantisBT"), "onClick", check_Config);
    
    function checkBugTracker() {
        var  isChecked = true;
        check_Config();
        if (dijit.byId("bugtrackercb").checked) {
            isChecked = false;
        }
        dijit.byId("MantisBT").setAttribute("disabled", isChecked);
        dijit.byId("Redmine").setAttribute("disabled", isChecked);
    }
    
    //Wiki
    checkWiki();
    dojo.connect(dijit.byId("wikicb"), "onChange", checkWiki);
    dojo.connect(dijit.byId("dokuwiki"), "onChange", check_Config);
    dojo.connect(dijit.byId("mediawiki"), "onChange", check_Config);
    
    function checkWiki() {
        var  isChecked = true;
        check_Config();
        if (dijit.byId("wikicb").checked) {
            isChecked = false;
        }
        dijit.byId("dokuwiki").setAttribute("disabled", isChecked);
        dijit.byId("mediawiki").setAttribute("disabled", isChecked);
    }    
    
    //Database
    checkDatabase();
    dojo.connect(dijit.byId("databasecb"), "onChange", checkDatabase);
    dojo.connect(dijit.byId("MySQL"), "onClick", checkMySQL);
    dojo.connect(dijit.byId("PostgreSQL"), "onClick", checkPostGreSQL);
    dojo.connect(dijit.byId("DBUser"), "onClick", checkDBUser);
    
    function checkMySQL(){
    	document.getElementById("DBhost").disabled = true;
        document.getElementById("DBtype").disabled = true;
        document.getElementById("DBpass").disabled = true;
    }
    
    function checkDatabase() {
        var  isChecked = true;
        check_Config();
        if (dijit.byId("databasecb").checked) {
            isChecked = false;
        }
        
        dijit.byId("MySQL").setAttribute("disabled", isChecked);
        dijit.byId("PostgreSQL").setAttribute("disabled", isChecked);
        dijit.byId("DBUser").setAttribute("disabled", isChecked);
        if (isChecked){
        	document.getElementById("DBhost").disabled = true;
            document.getElementById("DBtype").disabled = true;
            document.getElementById("DBpass").disabled = true;
        }
    }
    
    function checkPostGreSQL(){
    	document.getElementById("DBhost").disabled = true;
        document.getElementById("DBtype").disabled = true;
        document.getElementById("DBpass").disabled = true;
    }
 	
    function checkDBUser(){
    	document.getElementById("DBhost").disabled = false;
        document.getElementById("DBtype").disabled = false;
        document.getElementById("DBpass").disabled = false;
    }
    
    //SSO
    checkSSO();
    dojo.connect(dijit.byId("ssocb"), "onChange", checkSSO);

    function checkSSO() {
        var  isChecked = true;

        if (dijit.byId("ssocb").checked) {
            isChecked = false;
        }
        dijit.byId("Kerberos").setAttribute("disabled", isChecked);
        checkSSORequirement();
    }

    function checkSSORequirement() {
    	if (dijit.byId("ssocb").checked) {
            document.getElementById("ldapkerberos").innerHTML = " (<font color='red'>Must be compatible with Kerberos</font>)";
        }
        else {
            document.getElementById("ldapkerberos").innerHTML = "";
        }
    }
    
    //LDAP
    checkLDAP();
    dojo.connect(dijit.byId("OpenLDAP"), "onChange", checkLDAP);
    dojo.connect(dijit.byId("LDAPUser"), "onChange", checkLDAP);
    
    function checkLDAP() {
        var  isChecked = dijit.byId("LDAPUser").checked;
        
        document.getElementById("ldapserver").disabled = !isChecked;
        document.getElementById("base_dn").disabled = !isChecked;
        document.getElementById("user_dn").disabled = !isChecked;
        document.getElementById("root_dn").disabled = !isChecked;
    }

    function check_Config(){
    	if (dojo.byId("versionningcb").checked)
    		if (dojo.byId("USVN").checked)
    			dijit.byId("databasecb").setAttribute("checked", true);
    	if (dojo.byId("bugtrackercb").checked){
    		dijit.byId("databasecb").setAttribute("checked", true);
    		if (dojo.byId("MantisBT").checked)
    			dijit.byId("Mailingcb").setAttribute("checked", true);
    	}
    	if (dojo.byId("wikicb").checked)
    		if (dojo.byId("mediawiki").checked)
    			dijit.byId("databasecb").setAttribute("checked", true);
    }
    
    /*
     * button - validate form
     */
    dojo.connect(dijit.byId("validate"), "onClick", validate);
});

/*
 * Validate Form
 */
function validate()
{
    var  tab = new Array();
    var  output = "";
    
    if (dijit.byId("ftpcb").checked) {
        if (dijit.byId("vsFTPD").checked) {
            tab.push("vsFTPD");
        }
        else {
            tab.push("ProFTPD");
        }
    }
    if (dijit.byId("Mailingcb").checked) {
        if (dijit.byId("ecartis").checked) {
            tab.push("ecartis");
        }
        else {
            tab.push("mailman");
        }
    }
    if (dijit.byId("versionningcb").checked) {
        if (dijit.byId("Mercurial").checked) {
            tab.push("hg");
        }
        else {
            tab.push("USVN");
        }
    }
    if (dijit.byId("bugtrackercb").checked) {
        if (dijit.byId("MantisBT").checked) {
            tab.push("Mantis");
        }
        else {
            tab.push("Redmine");
        }
    }
    if (dijit.byId("ldapcb").checked) {
        tab.push("OpenLDAP");
        tab.push("phpLDAPadmin");
    }
    else if (dijit.byId("ldapreqcb").checked) {
        if (dijit.byId("OpenLDAP").checked) {
            tab.push("OpenLDAP");
            tab.push("phpLDAPadmin");
        }
        else {
        	output += '&ldaphost=' + dijit.byId("ldapserver").value;
        	output += '&base_dn=' + dijit.byId("base_dn").value;
        	output += '&user_dn=' + dijit.byId("user_dn").value;
        	output += '&root_dn=' + dijit.byId("root_dn").value;
        }
    }
    if (dijit.byId("ssocb").checked) {
        tab.push("Kerberos");
    }
    if (dijit.byId("wikicb").checked) {
        if (dijit.byId("dokuwiki").checked) {
        	tab.push("dokuwiki");
        }
        else if (dijit.byId("mediawiki").checked){
        	tab.push("mediawiki");
        }
    }
    if (dijit.byId("databasecb").checked) {
        if (dijit.byId("MySQL").checked) {
            tab.push("MySQL");
            tab.push("phpMyAdmin");
        }else if (dijit.byId("PostgreSQL").checked) {
            tab.push("PostgreSQL");
            tab.push("phpPgAdmin");
        }else if (dijit.byId("DBUser").checked){
        	output += '&dbtype=' + dijit.byId("DBtype").value;
        	output += '&dbhost=' + dijit.byId("DBhost").value;
        	output += '&dbpass=' + dijit.byId("DBpass").value;
        }
    }

    output = "http://diva-cloud.org/generator.php?act=gen" + output + "&instance=";
    for (var item in tab) {
        output += tab[item]+".";
    }
    output += "&domain_name="+document.getElementById("domain_name").value;
    output += "&mail_admin="+document.getElementById("mail_admin").value;
    output += "&pass_admin="+document.getElementById("pass_admin").value;
    output += "&project_name="+document.getElementById("project_name").value;
    document.getElementById("mainContent").innerHTML = "<code style='width: 400px;'>wget \"<a href='"+output+"'>"+output+"</a></code>\" -O install.sh && sh install.sh";
}
