/*
$(document).ready( function() {
  $("input:text").focus(
    function (){
        this.select();
      });
});
*/
var win;

$(document).ready( function() {
    $('a#acakeSqlLog').toggle(
        function(){
            $('#cakeSqlLog').show();
        },
        function(){
            $('#cakeSqlLog').hide();
        }
    );
    $('a.external').each(
        function(){
            this.target='_blank';
            if( this.title == '' ) {
                this.title = this.href;
            }
            this.title += ' [open in new window]';
        }
    );
    $('a.popup').click(
        function() {
            popup(this.href,800,600);
            return false;
        }
    );


    $('textarea').each(
        function() {
            var oCntDown = document.getElementById('cnt4'+this.id);
            if(oCntDown) {
                var maxLen = oCntDown.innerHTML;
                $(this).keypress(
                    function(e) {
                        if (this.value.length<maxLen) {
                            return true;
                        }
                        keyCode     = e.keyCode  ? e.keyCode : e.which;
                        if(e.charCode==0) { 
                            return keyCode != 13
                        } else {
                            return false;
                        }
                    }
                );
                $(this).keyup(
                    function() {maxLength(maxLen,this,oCntDown)}
                );
                $(this).change(
                    function() {maxLength(maxLen,this,oCntDown)}
                );
                maxLength(maxLen,this,oCntDown);
            }
        }
    );
});


//common JS function
function popup(page,w,h) {

    var winl = (screen.width-w)/2;
    var wint = (screen.height-h)/2;

    var settings ='height='+h+', width='+w+', top='+wint+', left='+winl+', scrollbars=1, toolbar=0, resizable=1, modal=1';

    if(win && !win.closed) {
        win.close();
    }
    win=window.open(page,'popup',settings);

    if(window.focus) {
        win.window.focus();
    }
}

function maxLength(maxLen,oArea,oCounter) {
    var cLen = oArea.value.length;
    if(cLen>maxLen) {
        //alert(oArea.name+': Your input is too bigger and will be truncated to '+maxLen+' chars');
        oArea.value = oArea.value.substring(0, maxLen);
        oCounter.innerHTML = maxLen+', 0 remaining';
    } else if(cLen>0) {
        oCounter.innerHTML = maxLen+', '+(maxLen-cLen)+' remaining';
    } else {
        oCounter.innerHTML = maxLen;
    }
}

function toggle(id) {
    if(oElement=document.getElementById(id)) {
        oElement.style.display = (oElement.style.display=='block'?'none':'block');
    }
}


//change option elements of select oSel with lines from aLines 
//oSel must to be an select element 
//(use getElementById('id of select') in caller function
//aLines must to have all lines in format value###title 
//(lines without ### will be ignored)

function changeSelect(oSel,aLines) {
    var iSel=0;
    var opt; //array obtain after split('###')
    oSel.length=0;  //remove old elements
    for(var i=0;i<aLines.length;i++) {
        opt=aLines[i].split("###");
        if(opt.length >= 2) { //ignore line without ###
            oSel[iSel++]=new Option(opt[1],opt[0]);
        }
    }
}

//addOption from an select to another (from must to be multiple)
function addOption(to,from) 
{

    if(!(to && from)) { alert('Missing (to,from) parameters');return false; }

    var oFrom = document.getElementById(from);
    if(!oFrom) {  alert('Missing '+fromName+' select'); return false; }

    var oTo = document.getElementById(to);
    if(!oTo) {  alert('Missing '+toName+' select'); return false;  }

    var kFrom = oFrom.value;
    if(!kFrom) {  alert('Please select an element'); oFrom.focus(); return false; }

    var vFrom = oFrom.options[oFrom.selectedIndex].text;

    var toBeSelected = -1;

    for(var i=0;i<oTo.length;i++) {
        if(oTo[i].value == kFrom) {
            toBeSelected = i;
        }
        oTo[i].selected = false;
    }

    if(toBeSelected>=0) {
        oTo[toBeSelected].selected = true;
    } else {
        oTo[oTo.length]=new Option(vFrom,kFrom);
        oTo[oTo.length-1].selected = true;
    }
}

//remove selected Options from an select 
function removeSelectedOptions(selectName)
{
    if (!selectName) {alert('Missing selectName parameter');return false;}

    var oSelect = document.getElementById(selectName);
    if(!oSelect) {  alert('Missing '+selectName+' select'); return;  }

    var index = oSelect.selectedIndex;
    var selectLength  = oSelect.length - 1;

    if (index == -1) {alert('Nothing selected');return false;}
    var toRemove =  new Array();
    for(var i=0;i<oSelect.length;i++) {
        if(oSelect.options[i].selected) {
            toRemove[toRemove.length] = i;
        }
    }
    if(toRemove.length>1 && !confirm('Remove '+toRemove.length+' elements?')) {
            return false;
    }
    var removed = 0;
    for(i=0;i<toRemove.length;i++) {
        oSelect.remove(toRemove[i]-removed);
        removed++;
    }
    return true;
}


//move up Option (into an select)
function moveUpOption(selectName) 
{
    if (!selectName) {alert('Missing selectName parameter');return false;}

    var oSelect = document.getElementById(selectName);
    if(!oSelect) {  alert('Missing '+selectName+' select'); return;  }

    var index = oSelect.selectedIndex;

    //nothing selected
    if (index == -1) {alert('Nothing selected!');return false;}
    if(oSelect[0].selected) {
        alert('Cannot move up anymore!');return false;
    }
    for(var i=1;i<oSelect.length;i++) {
        if(oSelect.options[i].selected) {
            moveOption(oSelect,i,i-1);
        }
    }
    return true;
}

//move down Option (into an select)
function moveDnOption(selectName) 
{
    if (!selectName) {alert('Missing selectName parameter');return false;}

    var oSelect = document.getElementById(selectName);
    if(!oSelect) {  alert('Missing '+selectName+' select'); return;  }

    var index = oSelect.selectedIndex;

    //nothing selected
    if (index == -1) {alert('Nothing selected!');return false;}

    if(oSelect[oSelect.length - 1].selected) {
        alert('Cannot move down anymore!');return false;
    }
    for(var i=oSelect.length-1;i>=0;i--) {
        if(oSelect.options[i].selected) {
            moveOption(oSelect,i,i+1);
        }
    }
    return true;
}


// move Option from fromIndex to toIndex
function moveOption(oSelect,fromIndex,toIndex)
{

    //store first
    fromText  = oSelect.options[fromIndex].text;
    fromValue = oSelect.options[fromIndex].value;

    //make first = second
    oSelect.options[fromIndex].text  = oSelect.options[toIndex].text;
    oSelect.options[fromIndex].value = oSelect.options[toIndex].value;

    //make second = first
    oSelect.options[toIndex].text = fromText;
    oSelect.options[toIndex].value = fromValue;

    //make new one be selected
    oSelect.options[fromIndex].selected = false;
    oSelect.options[toIndex].selected = true;    
}

//select All option (select must to be multiple)
function selectAllOptions(selectName) {

    if (!selectName) {alert('Missing selectName parameter');return false;}
    var oSelect = document.getElementById(selectName);
    if(!oSelect) {  alert('Missing '+selectName+' select'); return false;  }

    for(var i=0;i<oSelect.length;i++) {
        oSelect.options[i].selected = true;
    }

    return true;
}
