// -----------------------------------------------------
// ------------------ Setting Class --------------------
// -----------------------------------------------------
//var myClass = new MyProfileClass( 'myClass', arrSkills, 'MySkills', [idxMainDesc=0] );
//myClass.setupSecondary(arr_skillExperiencelist);
//myClass.title = "My Skills";
//myClass.phraseNoRows = "Please Add education from the left side";
//myClass.initializateTable();

//Drawing the table
//myClass.buildTable();

// -----------------------------------------------------
// -------------------- Class Setup --------------------
// -----------------------------------------------------
MyProfileClass.prototype.buildTable = buildTable;
MyProfileClass.prototype.initializateTable = initializateTable;
MyProfileClass.prototype.setupSecondary = setupSecondary;
MyProfileClass.prototype.setSelectDefault = setSelectDefault;
MyProfileClass.prototype.selectDefault = selectDefault;
MyProfileClass.prototype.delRow = delRow;
MyProfileClass.prototype.updateRow = updateRow;

MyProfileClass.prototype.populateFields = populateFields;
MyProfileClass.prototype.chgSec = changeSecondaryMyProfile;

MyProfileClass.prototype.title;
MyProfileClass.prototype.phraseNoRows;
MyProfileClass.prototype.tableMainTitle;
MyProfileClass.prototype.tableSecondaryTitle;

MyProfileClass.prototype.strForDBAttributes = strForDBAttributes;
MyProfileClass.prototype.strForDB = strForDB;

// -----------------------------------------------------
// -------------------- Constructor --------------------
// -----------------------------------------------------
function MyProfileClass(strClassName, objArrMain, DIVname, idxMainDesc, runAfterDeleteRow, runAfterchangeSecondary, linkFunction, runBeforeDeleteRow, runBeforeSelectDefault, strIndexToDisplay, objArrDynamicColumns, runAfterChangeDynamicColumn)
{
    this.XML = new Array();
    this.objArrSecondary = null;
    this.phraseNoRows = null;
    this.isSelectDefault = false;
    this.tableMainTitle = 'No Title';
    this.tablesecondaryTitle = 'Delete';

    this.strClassName = strClassName;
    this.objArrMain = objArrMain;
    this.idxMainDesc = idxMainDesc;
    this.objDIV = getObj(DIVname);

    this.runBeforeSelectDefault = (typeof (runBeforeSelectDefault) == 'function') ? runBeforeSelectDefault : null;
    this.runAfterDeleteRow = (typeof (runAfterDeleteRow) == 'function') ? runAfterDeleteRow : null;
    this.runBeforeDeleteRow = (typeof (runBeforeDeleteRow) == 'function') ? runBeforeDeleteRow : null;
    this.runAfterchangeSecondary = (typeof (runAfterchangeSecondary) == 'function') ? runAfterchangeSecondary : null;

    this.runAfterChangeDynamicColumn = (typeof (runAfterChangeDynamicColumn) == 'function') ? runAfterChangeDynamicColumn : null;
    this.linkFunction = (linkFunction != '') ? linkFunction : null;

    this.strSubMenu = createSubMenuDIV(strClassName, "SubmenuLayer");
    this.strIndexToDisplay = strIndexToDisplay;
    this.objArrDynamicColumns = objArrDynamicColumns;
}

// -----------------------------------------------------
// ---------------------- Methods ----------------------
// -----------------------------------------------------
function setSelectDefault(defaultIdx, defaultText, runOnSelectDefault)
{
    this.isSelectDefault = true;
    this.defaultIdx = defaultIdx;
    this.defaultText = defaultText;

    this.runOnSelectDefault = (typeof (runOnSelectDefault) == 'function') ? runOnSelectDefault : null;
}

function setupSecondary(objArrSecondary, idxRelationMain, idxRelationSec, idxSubSelection)
{
    this.objArrSecondary = objArrSecondary;
    this.idxRelationMain = (typeof (idxRelationMain) != 'undefined') ? idxRelationMain : null;
    this.idxRelationSec = (typeof (idxRelationSec) != 'undefined') ? idxRelationSec : null;
    this.idxSubSelection = idxSubSelection;

    this.isSecType = false;
    if (this.idxRelationMain != null && isFinite(this.idxRelationMain))
        if (this.idxRelationSec != null && isFinite(this.idxRelationSec))
        this.isSecType = true;
}

function initializateTable(isDraw)
{
    var mainDIV = document.createElement("DIV");
    mainDIV.className = "ProfileBoxSelected";

    var h5 = document.createElement("h5");
    h5.id = "ProfileBoxSelectedItems";
    h5.innerHTML = this.title;
    mainDIV.appendChild(h5);

    var selectedItems = document.createElement("DIV");
    selectedItems.id = "ProfileBoxSelectedItems";
    selectedItems.className = "ProfileBoxSelectedItems";
    mainDIV.appendChild(selectedItems);

    if (this.isSelectDefault)
    {
        var radioDefault = document.createElement("DIV");
        radioDefault.className = "ProfileBoxRadioSel";
        radioDefault.innerHTML = '<img src="../Cultures/Images/SelectArrow.gif" alt="Select" width="17" height="16" align="top" /> ' + this.defaultText;
        mainDIV.appendChild(radioDefault);
    }

    this.objDIV.appendChild(mainDIV);

    this.objDIV = selectedItems;

    if (typeof (isDraw) == 'undefined' || isDraw == true || typeof (strIdx) != 'undefined')
        this.buildTable();
}

function buildTable()
{
    var HTML = '';

    if (this.objArrMain.length == 0)
    {
        if (this.phraseNoRows != null)
        {
            HTML += '<div class="Message">' + this.phraseNoRows + '</div>';
        }
        else
        {
            getObj('ProfileBoxSelectedItems').style.visibility = 'hidden';
            getObj('ProfileBoxSelectedItems').parentNode.parentNode.style.visibility = 'hidden';
        }
    }
    else
    {
        getObj('ProfileBoxSelectedItems').style.visibility = 'visible';
        getObj('ProfileBoxSelectedItems').parentNode.parentNode.style.visibility = 'visible';

        HTML += '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
        HTML += '<TBODY class="ProfileBoxSelectedItemsCol">';
        HTML += '<tr>';

        if (this.isSelectDefault)
            HTML += '<td class="Col0">&nbsp;</td>';

        HTML += '<td class="Col1">' + this.tableMainTitle + '</td>';

        if (this.objArrSecondary != null)
            HTML += '<td class="Col2">' + this.tableSecondaryTitle + '</td>';

        if (this.objArrDynamicColumns != null)
        {
            for (var idx = 0; idx < this.objArrDynamicColumns.length; idx++)
            {
                if (this.objArrDynamicColumns[idx][1] != null)
                {
                    HTML += '<td class="ColDynamic">' + this.objArrDynamicColumns[idx][1] + '</td>';
                }
            }
        }


        HTML += '<td class="Col3" align="center">Borrar</td>';
        HTML += '</tr>';
        HTML += '</TBODY>';

        for (var i = 0; i < this.objArrMain.length; i++)
        {
            HTML += '<tr>';

            if (this.isSelectDefault)
            {
                if (this.objArrMain[i][this.defaultIdx] == 'checked')
                    this.objArrMain[i][this.defaultIdx] = 1;

                HTML += '<td class="Col0"><input id="radiobutton' + this.strClassName + '" name="radiobutton' + this.strClassName + '" type="radio" value="radiobutton" onClick="' + this.strClassName + '.selectDefault(' + i + '); onDeleteFromList(); "' + (Number(this.objArrMain[i][this.defaultIdx]) == 1 ? 'checked' : '') + '/></td>';
            }

            HTML += '<td class="' + ((this.objArrSecondary != null || this.objArrDynamicColumns != null) ? "Col1" : "Col1Full") + '">';
            if (this.linkFunction != null)
                HTML += '<a id="link' + this.strClassName + i.toString() + '" href="javascript:' + this.linkFunction + '(' + i + ')"' + '/>' + this.objArrMain[i][this.idxMainDesc];
            else
                HTML += this.objArrMain[i][this.idxMainDesc];

            if (this.strIndexToDisplay != null)
            {
                if (this.strIndexToDisplay.length == 1)
                {
                    HTML += ' (' + this.objArrMain[i][this.strIndexToDisplay] + ')';
                }
                else if (this.strIndexToDisplay.length > 1)
                {
                    var arrAddedValues = this.strIndexToDisplay.split(',');
                    for (var idx = 0; idx < arrAddedValues.length; idx++)
                    {
                        if (this.objArrMain[i][arrAddedValues[idx]] != null)
                        {
                            HTML += ' (' + this.objArrMain[i][arrAddedValues[idx]] + ')';
                        }
                    }
                }

            }
            HTML += '</td>';

            if (this.objArrSecondary != null)
            {
                HTML += '<td class="Col2" id="' + this.strClassName + i.toString() + '" ><select id="' + this.strClassName + 'sec' + i + '" class="FormListSecondary" onchange="' + this.strClassName + '.chgSec(' + i + ',this); onDeleteFromList(); " ><option value=-1>--Seleccionar--</option></select></td>';
            }

            if (this.objArrDynamicColumns != null)
            {
                for (var idx = 0; idx < this.objArrDynamicColumns.length; idx++)
                {
                    if (this.objArrDynamicColumns[idx][0] != null)
                    {
                        if (this.objArrMain[i][this.objArrDynamicColumns[idx][0]] == 'checked')
                        {
                            HTML += '<td class="ColDynamic"><input id="checkbox' + this.strClassName + i.toString() + '" name="checkbox' + this.strClassName + '" type="checkbox" value="checkbox" onClick="' + this.strClassName + '.selectDefault(' + i + ');' + this.strClassName + '.updateRow(' + i + ',' + this.objArrDynamicColumns[idx][0] + ',true);" checked /></td>';
                        }
                        else if (this.objArrMain[i][this.objArrDynamicColumns[idx][0]] == 'notchecked')
                        {
                            HTML += '<td class="ColDynamic"><input id="checkbox' + this.strClassName + i.toString() + '" name="checkbox' + this.strClassName + '" type="checkbox" value="checkbox" onClick="' + this.strClassName + '.selectDefault(' + i + ');' + this.strClassName + '.updateRow(' + i + ',' + this.objArrDynamicColumns[idx][0] + ',false); " /></td>';
                        }
                        else
                            HTML += '<td class="ColDynamic" id="' + this.strClassName + i.toString() + '">' + this.objArrMain[i][this.objArrDynamicColumns[idx][0]] + '</td>';
                    }
                }
            }


            HTML += '<td><a href="javascript:' + this.strClassName + '.delRow(' + i + ')"><img src="../Cultures/Images/Icons/DeleteRow.gif" alt="Borrar Texto" width="15" height="16" border="0" tabIndex="100" /></a></td>';
            HTML += '</tr>';
        }
        HTML += '</table>';
        //HTML += '<SCRIPT>this.populateFields()</SCRIPT>';
    }

    this.objDIV.innerHTML = HTML;

    this.populateFields();
}

function populateFields()
{
    if (this.objArrSecondary != null && this.objArrMain.length > 0)
    {
        if (this.isSecType)
        {
            var tmpSec = new Array();
            var tmpType;

            for (var i = 0; i < this.objArrSecondary.length; i++)
            {
                tmpType = Number(this.objArrSecondary[i][this.idxRelationSec]);

                if (typeof (tmpSec[tmpType]) == 'undefined')
                    tmpSec[tmpType] = new Array();

                tmpSec[tmpType][tmpSec[tmpType].length] = this.objArrSecondary[i].slice();
            }
        }

        for (var i = 0; i < this.objArrMain.length; i++)
        {
            if (this.isSecType)
                populateSelect(getObj(this.strClassName + 'sec' + i), tmpSec[this.objArrMain[i][this.idxRelationMain]], this.objArrMain[i][this.idxSubSelection]);
            else
                populateSelect(getObj(this.strClassName + 'sec' + i), this.objArrSecondary, this.objArrMain[i][this.idxSubSelection]);
        }
    }

}

function delRow(idx)
{
    //Catch changes to alert the user.
    if (onDeleteFromList() != undefined)
        onDeleteFromList();
    var IdToDelete = this.objArrMain[idx][0];

    if (this.runBeforeDeleteRow != null)
    {
        if (this.runBeforeDeleteRow(IdToDelete, idx) == false)
            return;
    }

    var isSelectedDefault = false;
    if (this.isSelectDefault)
    {
        if (Number(this.objArrMain[idx][this.defaultIdx]) == 1)
        {
            isSelectedDefault = true;
        }
    }

    this.objArrMain.splice(idx, 1);

    if (isSelectedDefault && this.objArrMain.length > 0)
        this.objArrMain[0][this.defaultIdx] = "1";

    if (this.runAfterDeleteRow != null)
        this.runAfterDeleteRow(IdToDelete, idx);

    this.buildTable();
}

function updateRow(idx,IndexToUpdate,ValToUpdate)
{
    var IdToUpdate = this.objArrMain[idx][0];
    if (this.runAfterChangeDynamicColumn != null)
        this.runAfterChangeDynamicColumn(IdToUpdate, IndexToUpdate, ValToUpdate);
   
}

function changeSecondaryMyProfile(idxMainArr, obj)
{
    this.objArrMain[idxMainArr][this.idxSubSelection] = obj.value;

    if (this.runAfterchangeSecondary != null)
        this.runAfterchangeSecondary(false);
}

function selectDefault(selectedIdx)
{
    if (this.runBeforeSelectDefault != null)
    {
        if (this.runBeforeSelectDefault(selectedIdx) == false)
            return;
    }
    for (var i = 0; i < this.objArrMain.length; i++)
    {
        this.objArrMain[i][this.defaultIdx] = "0";
    }

    this.objArrMain[selectedIdx][this.defaultIdx] = "1";

    if (this.runOnSelectDefault != null)
        this.runOnSelectDefault(this.objArrMain, selectedIdx);
}
// -------------------------------------------------------------------------
// -------------------------Common Functions -------------------------------
// -------------------------------------------------------------------------
function getSecondaryTextMyProfile(obj, value)
{
    var secondaryIdxId = 0;
    var secondaryIdxDesc = 1;

    var str2Return = '';
    if (isFinite(value) && value >= 0)
    {
        for (var i = 0; i < obj.length; i++)
            if (obj[i][secondaryIdxId] == value)
        {
            str2Return = '[ ' + obj[i][secondaryIdxDesc] + ' ]';
            break;
        }
    }

    if (str2Return == '')
        str2Return = '--Seleccionar--';

    return str2Return;
}

// -------------------------------------------------------------------------
//----------------------------- XML functions ------------------------------
// -------------------------------------------------------------------------
function strForDBAttributes(attName, arrColId)
{
    arrColId = (typeof (arrColId) == 'undefined') ? null : arrColId;

    this.XML[this.XML.length] = new Array(attName, arrColId);
}

function strForDB()
{
    return (getStringForDB(this.objArrMain, this.XML));
}

function onDeleteFromList()
{
    if (typeof (g_isDirty) != 'undefined')
        g_isDirty = true;
}