﻿// JScript File

function SortableChanged(DataTypeID, SortID) {
    if (document.getElementById(SortID)) {
        // Set object   
        chkSortable = document.getElementById(SortID);
        cboDataType = document.getElementById(DataTypeID);
        // Get Value
        val = cboDataType.value;
        chkSortable.disabled = false;

        if (chkSortable.checked == true) {
            // Check for sortable
            if ((val == "bit" || val == "image" || val == "real" || val == "sql_variant" || val == "uniqueidentifier" || val == "xml")) {
                chkSortable.checked = false;
                chkSortable.disabled = true;
            }
            else {
                chkSortable.disabled = false;
            }
        }
        else {
            // Check for sortable
            if ((val == "bit" || val == "image" || val == "real" || val == "sql_variant" || val == "uniqueidentifier" || val == "xml")) {
                chkSortable.disabled = true;
            }
            else {
                chkSortable.disabled = false;
            }
        }
    }
}

function AllowNullsChanged(NullID, PKID, IdentityID) {

    if (document.getElementById(NullID)) {
        // Set object   
        chkAllowNulls = document.getElementById(NullID);
        chkPrimaryKey = document.getElementById(PKID);
        chkIdentity = document.getElementById(IdentityID);

        if (chkPrimaryKey.checked == true || chkIdentity.checked == true) {
            chkAllowNulls.checked == false;
            chkAllowNulls.disabled = true;
        }
    }
}



function IdentityChanged(DataTypeID, IdentityID, NullID, PKID, CollectionID) {
    if (document.getElementById(IdentityID)) {
        cboDataType = document.getElementById(DataTypeID);
        chkIdentity = document.getElementById(IdentityID);
        chkAllowNulls = document.getElementById(NullID);
        chkPrimaryKey = document.getElementById(PKID);
        chkCollection = document.getElementById(CollectionID);


        val = cboDataType.value;

        // Check if datatype can be an identity column
        if (chkIdentity.checked == true) {
            chkCollection.checked = false;
            chkCollection.disabled = true;

            if (val == "bigint" || val == "decimal(#,#)" || val == "int" || val == "numeric(#,#)" || val == "smallint" || val == "tinyint") {
                chkIdentity.disabled = false;
                chkIdentity.checked = true;
                chkAllowNulls.checked = false;
                chkAllowNulls.disabled = true;
            }
            else {
                chkIdentity.checked = false;
                chkIdentity.disabled = true;
                chkAllowNulls.disabled = false;
            }
        }
        else {
            chkCollection.disabled = false;
            if (val == "bigint" || val == "decimal(#,#)" || val == "int" || val == "numeric(#,#)" || val == "smallint" || val == "tinyint") {
                chkIdentity.disabled = false;
                chkIdentity.checked = false;
                if (chkPrimaryKey.checked == true) {
                    chkAllowNulls.disabled = true;
                    chkAllowNulls.checked = false;
                }
                else {
                    chkAllowNulls.disabled = false;
                    chkAllowNulls.checked = false;
                }
            }
        }
    }
}

function PrimaryKeyChanged(DataTypeID, IdentityID, NullID, PKID, CollectionID) {
    if (document.getElementById(IdentityID)) {
        cboDataType = document.getElementById(DataTypeID);
        chkIdentity = document.getElementById(IdentityID);
        chkAllowNulls = document.getElementById(NullID);
        chkPrimaryKey = document.getElementById(PKID);
        chkCollection = document.getElementById(CollectionID);

        val = cboDataType.value;

        // Check if datatype can be an identity column
        if (chkPrimaryKey.checked == true) {
            chkCollection.checked = false;
            chkCollection.disabled = true;
            if (!(val == "image" || val == "ntext" || val == "nvarchar(MAX)" || val == "sql_variant" || val == "text" || val == "varbinary(MAX)" || val == "xml")) {
                chkPrimaryKey.disabled = false;
                chkPrimaryKey.checked = true;
                chkAllowNulls.checked = false;
                chkAllowNulls.disabled = true;
            }
            else {
                chkPrimaryKey.checked = false;
                chkPrimaryKey.disabled = true;
                chkAllowNulls.disabled = false;
            }
        }
        else {
            chkCollection.disabled = false;
            if (!(val == "image" || val == "ntext" || val == "nvarchar(MAX)" || val == "sql_variant" || val == "text" || val == "varbinary(MAX)" || val == "xml")) {
                chkPrimaryKey.disabled = false;
                chkPrimaryKey.checked = false;
                if (chkIdentity.checked == true) {
                    chkAllowNulls.disabled = true;
                    chkAllowNulls.checked = false;
                }
                else {
                    chkAllowNulls.disabled = false;
                    chkAllowNulls.checked = false;
                }
            }
        }
    }
}


function SetControls(DataTypeID, NullID, SortID, PKID, IdentityID, leftPar, num1, comma, num2, rightPar) {
    if (document.getElementById(DataTypeID)) {

        cboDataType = document.getElementById(DataTypeID);
        chkAllowNulls = document.getElementById(NullID);
        chkSortable = document.getElementById(SortID);
        chkPrimaryKey = document.getElementById(PKID);

        chkIdentity = document.getElementById(IdentityID);
        lblOpenParenth = document.getElementById(leftPar);
        txtNumber1 = document.getElementById(num1);
        lblComma = document.getElementById(comma);
        txtNumber2 = document.getElementById(num2);
        lblEndParenth = document.getElementById(rightPar);

        val = cboDataType.value;

        chkAllowNulls.disabled = false;
        chkSortable.disabled = false;
        chkPrimaryKey.disabled = false;
        chkIdentity.disabled = false;

        lblOpenParenth.disabled = false;
        lblComma.disabled = false;
        lblEndParenth.disabled = false;
        txtNumber1.disabled = false;
        txtNumber2.disabled = false;


        chkAllowNulls.checked = true;
        chkIdentity.checked = false;
        chkPrimaryKey.checked = false;

        // Check if datatype needs numeric values
        if (val == "binary(#)" || val == "char(#)" || val == "nchar(#)" || val == "nvarchar(#)" || val == "varbinary(#)" || val == "varchar(#)") {

            lblOpenParenth.disabled = false;
            lblOpenParenth.innerHTML = "(";
            lblEndParenth.innerHTML = "";
            lblEndParenth.disabled = true;
            lblComma.disabled = false;
            lblComma.innerHTML = ")";
            txtNumber1.disabled = false;

            //txtNumber1.height = cboDataType.height;//16;
            txtNumber1.style.backgroundColor = "";
            txtNumber2.disabled = true;
            txtNumber2.value = "";
            txtNumber2.style.backgroundColor = "#C0C0C0";
            //txtNumber2.height = 0;
            switch (val) {
                case "binary(#)":
                    { txtNumber1.value = "50"; break; }
                case "char(#)":
                    { txtNumber1.value = "10"; break; }
                case "nchar(#)":
                    { txtNumber1.value = "10"; break; }
                case "nvarchar(#)":
                    { txtNumber1.value = "50"; break; }
                case "varbinary(#)":
                    { txtNumber1.value = "50"; break; }
                case "varchar(#)":
                    { txtNumber1.value = "50"; break; }
            }
        }
        else {
            if (val == "decimal(#,#)" || val == "numeric(#,#)") {
                lblOpenParenth.disabled = false;
                lblOpenParenth.innerHTML = "(";
                lblEndParenth.disabled = false
                lblComma.disabled = false;
                lblComma.innerHTML = ",";
                lblEndParenth.innerHTML = ")";
                txtNumber1.disabled = false
                //txtNumber1.height = cboDataType.height;//16;
                txtNumber1.style.backgroundColor = "";
                txtNumber2.disabled = false;
                //txtNumber2.height = cboDataType.height;//16;
                txtNumber2.style.backgroundColor = "";
                switch (val) {
                    case "decimal(#,#)":
                        {
                            txtNumber1.value = "18";
                            txtNumber2.value = "2";
                            break;
                        }
                    case "numeric(#,#)":
                        {
                            txtNumber1.value = "18";
                            txtNumber2.value = "2";
                            break;
                        }
                }
            }
            else {
                lblOpenParenth.disabled = true;
                lblOpenParenth.innerHTML = "(";
                lblEndParenth.disabled = true;
                lblEndParenth.innerHTML = ")";
                lblComma.disabled = true;
                lblComma.innerHTML = ",";
                txtNumber1.disabled = true;
                txtNumber1.value = "";
                //txtNumber1.height = 0;
                txtNumber1.style.backgroundColor = "#C0C0C0";
                txtNumber2.disabled = true;
                txtNumber2.value = "";
                txtNumber2.style.backgroundColor = "#C0C0C0";
                //txtNumber2.height = 0;
            }
        }
        // Check if datatype can be an identity column
        if (val == "bigint" || val == "decimal(#,#)" || val == "int" || val == "numeric(#,#)" || val == "smallint" || val == "tinyint") {
            chkIdentity.disabled = false;
        }
        else {
            chkIdentity.checked = false;
            chkIdentity.disabled = true;
        }

        // Check if datatype can be a Primary Key
        if (!(val == "image" || val == "ntext" || val == "nvarchar(MAX)" || val == "sql_variant" || val == "text" || val == "varbinary(MAX)" || val == "xml")) {
            chkPrimaryKey.disabled = false;
        }
        else {
            chkPrimaryKey.checked = false;
            chkPrimaryKey.disabled = true;
        }
        // Check for sortable
        if ((val == "bit" || val == "image" || val == "real" || val == "sql_variant" || val == "uniqueidentifier" || val == "xml")) {
            chkSortable.checked = false;
            chkSortable.disabled = true;
        }
        else {
            chkSortable.checked = false;
            chkSortable.disabled = false;
        }

    }
}

