//
// Standard form control Buttons/tabs/pages
//
//
// enable/disabl fields routine routine to enable/disable tabs,navigation,tmenu,pel
//
var frmflds = new Array();    // filed names
var frmfcnt = -1;
var lodfrs=0;
var redonl  = new Array();  // read only fields
var redocn = 0;                  // how many read only fields are there
///
// Function to handle inputs/select/textarea
//
function   getInput(xml){
    ///    inps=document.getElementsByTagName('input');
    inps=document.getElementsByTagName(xml);
    inpsl=inps.length;
    for (idx=0;idx<inpsl;idx++){
        //
        /// skip all inputs that are not fields
        //
        inid=inps[idx].id;
        if (inps[idx].parentNode.tagName !='DIV'){
            continue;
        }
        //
        /// skip all invisible inputs
        //
        if (inps[idx].parentNode.tagName=='FORM'){
            continue;
        }
        inpnam=inps[idx].name;
        //
        /// skip predifned fields
        //
        if (inpnam == 'pagsiz'){
            continue;
        }
        if (inpnam == 'message'){
            continue;
        }
        if (inpnam.substr(0,3) == 'nav'){
            continue;
        }
        inid=inps[idx].id;
        idspla=inid.split(dolar);
        idl=idspla.length;
        fulid=idspla[idl-1];
        if (fulid == inpnam) {
            frmfcnt ++;
            frmflds[frmfcnt] = inid;           // fields id's'
        }
    }
    return;
}
////
///
// Function to handle  label fields
//
function   getLabel(xml){
    inps=document.getElementsByTagName(xml);
    inpsl=inps.length;
    manfl=gelid('mainfl');
    for (idx=0;idx<inpsl;idx++){
        //
        /// skip all labels that are not fields
        //
        inid=inps[idx].id;
        if (inps[idx].parentNode.tagName !='DIV'){
            continue;
        }
        if (inid.search(manfl) ==-1){
            continue;
        }
        //
        /// skip all invisible  labels ?
        //
        if (inps[idx].parentNode.tagName=='FORM'){
            continue;
        }
        //
        /// skip predifned fields  not applicable
        //
        ////        lbl='div$fldval';
        lbl='flddiv';
        if(inid.indexOf(lbl) == -1){
            continue;
        }
        frmfcnt ++;
        frmflds[frmfcnt] = inid;           // fields id's'
    }
    return;
}
//Function to  add new field to form analysis
//
function addnfld(fldnam) {
    if (lodfrs>0) {
        return ;
    }
    frmflds[frmfcnt]=fldnam;
    frmfcnt=frmfcnt+1;
    return;
}
//
// test if tab is clickable
//
function oktab (tabid,nav) {
    vars=tabid.split(dolar);
    tabnam=vars[2];
    return;   // tmeporaray
    metab='nav'+tabnam+'able';
    tabsta=gelid(metab).content.substr(0,1);
    if( tabsta != 't'){
        mesagw(tabnam+' is currently disabled');
        openDialog();
        return false;
    }
    return true;
}
//
// Make Tabs Visible/invisble
//
function tabvis (objid,able) {
  tabcnt=gelid('frmpagcnt').content;          // get the  tab  count
  cur=objid.split(dolar);
  curl=cur.length;
  curtab=cur[curl-1];
    for (idx=1;idx<=tabcnt;idx++){
            newtab=pretab+idx;
               vis='invisible';
            if (newtab == reltab){
                vis='visible';
            }
         tabnew=document.getElementById(newtab);
         visinvis(tabnew,vis);
       }  // end for
    gelid('actpag').content=curtab;
    return true;
}
//
// Make Tabs disabled/enabled
//
function tabable (tabid,able) {
    metab='nav'+tabid+'able';
    switch(able){
        case 'enable':
            work='true';
            break;
        case 'disable':
            work='false';
            break;
        default:
            work='true';
    }
    gelid(metab).content=work;
    return;
}
function formable (able,nav) {
    dis='disable'
    invis='invis';
    switch (able){
        case 'enable':                   // all functions
            //
            /// tmenu section
            //
            $('.cmdAdd').removeClass(dis);
            $('.cmdEdit').removeClass(dis);
            $('.cmdDelete').removeClass(dis);
            makadc('cmdAccepr',dis);
            makadc('cmdDiscard',dis);
            //
            /// navtol section
            //
            $('.cmdFirst').removeClass(dis);
            $('.cmdPrev').removeClass(dis);
            $('.cmdNext').removeClass(dis);
            $('.cmdLast').removeClass(dis);
            //
            /// PEL section
            //
            $('.cmdPost').removeClass(dis);
            $('.cmdList').removeClass(dis);
            $('.cmdExit').removeClass(dis);
            $('.topsel').removeClass(dis);
            break;
        case 'disable':                   // diable all buttons except the accept/discard
            //
            /// tmenu section
            //
            makadc('cmdAdd',dis);
            makadc('cmdEdit',dis);
            makadc('cmdDelete',dis);
            $('.cmdAccept').removeClass(dis);
            $('.cmdDiscard').removeClass(dis);
            //
            /// navtol section
            //
            makadc('cmdFirst',dis);
            makadc('cmdPrev',dis);
            makadc('cmdNext',dis);
            makadc('cmdLast',dis);
            makadc('navrec',dis);
            makadc('navpag',dis);
            //
            /// PEL section
            //
            makadc('cmdPost',dis);
            makadc('cmdList',dis);
            makadc('cmdExit',dis);
            makadc('topsel',dis);
            break;
        case 'visible':
            switch(nav){
                case 'navrec':
                    $('.navrec').removeClass(invis);
                    makadc('navpag',invis);
                    break;
                case 'navpag':
                    $('.navpag').removeClass(invis);
                    makadc('navrec',invis);
                    break;
            }
            break;
    }
    return;
}
//
// finction to handle all fields abling/disabling
//
function ablfld(able) {
    dis='disabled';
    for (ifx=0;ifx<frmfcnt;ifx++){
        for (ird=0;ird<=redocn;ird++){                      ///  check to see readonly field
            ///               if (redonl[ird]  == frmflds[ifx]) {
            if (frmflds[ifx].indexOf(redonl[ird])  != -1){
                continue;
            }
            break;
        }
        frmfld=frmflds[ifx];
        switch(able){
            case 'disabled':                                          // disable fields
                ////                      $("#"+frmfld).attr(able,able);
                document.getElementById(frmfld).disabled=true;
                ///                    makadc(frmfld,able);
                break;
            case 'enabled':
                ///                   alert(ifx+'     enabled dis    '+dis +'  fields '+frmfld);
                ///                        abl=$("#"+frmfld).attr(dis);
                document.getElementById(frmfld).disabled=false;
                /*
                       alert(ifx+' before remove attr '+abl+'   id '+frmfld);
                      $("#"+frmfld).removeAttr(dis);
                        abl=$("#"+frmfld).attr(dis);
                        abl=$("#"+frmfld).attr('class');
                       alert(ifx+' before remove attr '+abl+'   id '+frmfld);
                      $("#"+frmfld).removeClass(dis);
                        abl=$("#"+frmfld).attr('class');
                       alert(ifx+' after remove attr '+abl+'   id '+frmfld);
*/
                break;
        }
    }
    // enable all fields except the readonly fields
    return;
}
// objnam  tab1,tab2,tab3 etc visible , the other invisibale
// make pages visible and invisible
//
function visible(objnam,viser){
    invis='invis';
    pageno=0;
    totpag=gelid('pf1').title;
    objtyp=objnam;
    if (objnam.substr(0,3) == 'tab') {
        objtyp='tab';
    }
    switch (viser){
        case 'visible':
            switch (objtyp){
                case 'navrec':
                    $(nav).removeClass(invis);
                    $(nav).removeClass(invis);
                    makadc('navpag',invis);
                    break;
                case 'navpag':
                    $(nav).removeClass(invis);
                    $(nav).removeClass(invis);
                    makadc('navrec',invis);
                    break;
                case 'tab':
                    for (idx=1; idx<=totpag;idx++){
                        newtab=objtyp+idx;
                        divobj='pf1$'+newtab;
                        obj=gelid(divobj);
                        if (objnam == newtab) {
                            visinvis(obj,viser);             // nav.js
                            continue;
                        }
                        visinvis(obj,'invisible');             // nav.js
                    }
                    break;
                case 'tr':                    /// not working 8/12/09 sbk
                    $(nav).removeClass(invis);
                    break;
            }
    }
    return;
}
//  onclick="mclkdf(this.id,this.parentNode.id)"
//Function to make invisible and visible by adding the  invis class
//
////function visinvis(obj,vis){

//
/// function to make sure the class has to be added
//
 function makadc(obj,cls){
    objid='#'+obj;
    hascls=$(objid).hasClass(cls);
    ////   alert(objid+' in makdc frmctl.js  '+cls+'  has class '+hascls);
    if (hascls== true) {
        return;
    }
    //css=$(objid).attr("class");
    ///ids=$(objid).attr("id");
    $(objid).addClass(cls);
    /// claser=gelid(obj).getAttribute('class');
    //   alert(objid+' in makdc frmctl.js  '+cls+'  after add calss   css=navtol    '+css+'  clasre  '+claser);
    //css=$(objid).attr('class');
    ///alert(objid+' in mkadc frmctl.js   '+css);
    return;
}
// obj- active page  (page1,page2,page3 etc
// // able enable/disable
//  Enable nav/tmenu,pel buttons on page activation
//
function pagacb(obj,cls){
    nav=obj+'nav';
    tmenu=obj+'tmenu';
    pel=obj+'pel';
    valbut(nav,cls);
    valbut(tmenu,cls);
    valbut(pel,cls);
    return;
}
//
/// Validat metatag buttons
//
function valbut(metag,cls){
    ///allbut=$(metag).attr('content');
    allbut=gelid(metag).content;
    inm=allbut.split(',');
    inml=inm.length;
    button=new Array()
    redbut=0;
    //
    /// no buttons defined means all
    //
    if (allbut.length<1) {
        return;
    }
    //
    /// only one field
    //
    if ((allbut.length>0)  &&  (inml<2)){
        button[redbut]=allbut;
        redbut++;
        inml=-1;
    }
    for (idx=0;idx<inml;idx++){
        button[redbut]=inm[idx];
        redbut++;
    }
    for (ibx=0; ibx<redbut;ibx++){
        testbt=gelid(button[ibx]).id;
        //       butid='#'+button[ibx];
        //       testbt=$(butid).attr('id');
        if (testbt == null){
            continue;
        }
        makadc(testbt,cls);
    }
    return;
}
//
/// Prepare form fields /+readonly called by each form frmBegin.js
//
function prepflds(metag,cls){
    getInput('input');                         // frmctl.js
    getInput('select');
    getInput('textarea');
    getLabel('label');                         // frmctl.js
    /*
    alert(lblcnt+' before labels'+frmfcnt);
    for (ilb=lblcnt;ilb<frmfcnt;ilb++){
        alert(ilb+' label id'+frmflds[ilb]);
    }
    */
    reds=gelid('redonly');
    if (reds == null){
        return;
    }
    allfld=reds.content;
    inm=allfld.split(',');
    inml=inm.length;
    //
    /// only one field
    //
    if ((allfld.length>0)  &&  (inml<2)){
        redonl[redocn]=allfld;
        redocn++;
        inml=-1;
    }
    for (idx=0;idx<inml;idx++){
        redonl[redocn]=inm[idx];
        redocn++;
    }
    return;
}
