var avs = {
    lang:{
        vars:{},
        get:function(name){
            return avs.util.defaultVal(avs.lang.vars[name],'[Undefined JS Variable '+name+']');
        },
        set:function(name,val){
            if(typeof(name)=='object'){
                for(i in name){
                    avs.lang.vars[i]=name[i];
                }
            }else{
                avs.lang.vars[name]=val;
            }
        }
    },
    config:{
        vars:{},
        get:function(name){
            return avs.util.defaultVal(avs.config.vars[name],'[Undefined JS Config Variable '+name+']');
        },
        set:function(name,val){
            if(typeof(name)=='object'){
                for(i in name){
                    avs.config.vars[i]=name[i];
                }
            }else{
                avs.config.vars[name]=val;
            }
        }
    },
    util:{
        isDefined:function(obj){
            return(typeof(obj)!='undefined');
        },
        defaultVal:function(x,val){
            if(!avs.util.isDefined(x)){
                return val;
            }
            return x;
        },
        inArray:function(needle,array){
            for(i=0;i<array.length;i++){
                if(array[i]==needle){
                    return true;
                }
            }
            return false;
        },
        clone:function(oldObj){
            var newObj=(oldObj instanceof Array)?[]:{};

            for(i in oldObj){
                if(i=='clone')continue;
                if(oldObj[i]&&typeof oldObj[i]=="object"){
                    newObj[i]=avs.util.clone(oldObj[i]);
                }else newObj[i]=oldObj[i]
            }
            return newObj;
        },
        randomString:function(length){
            var chars="0123456789abcdefghiklmnopqrstuvwxyz";
            var string_length=avs.util.defaultVal(length,8);
            var randomstring='';
            for(var i=0;i<string_length;i++){
                var rnum=Math.floor(Math.random()*chars.length);
                randomstring+=chars.substring(rnum,rnum+1);
            }
            return randomstring;
        },
        htmlEscape:function(str){
            str=str.replace(/</g,'&lt;');
            str=str.replace(/>/g,'>');
            return str;
        },
        BindOnCheckShow:function(checkbox,displayItem){
            $(checkbox).bind('click',function(){
                if($(checkbox).attr('checked')==true){
                    $(displayItem).show();
                }else{
                    $(displayItem).hide();
                }
            });
            if($(checkbox).attr('checked')==true){
                $(displayItem).show();
            }else{
                $(displayItem).hide();
            }
        },
        BindOnCheckHide:function(checkbox,displayItem){
            $(checkbox).bind('click',function(){
                if($(checkbox).attr('checked')==true){
                    $(displayItem).hide();
                }else{
                    $(displayItem).show();
                }
            });
            if($(checkbox).attr('checked')==true){
                $(displayItem).hide();
            }else{
                $(displayItem).show();
            }
        }
    },
    form:{
        BindNumbersOnly:function(fieldId){
            $('#'+fieldId).bind('keyup',function(){
                var value=$(this).val();
                value=value.replace(/[^0-9]/g,'');
                $(this).val(value);
            });
        }
    },
    loadingIndiciatorTimeout:false,
    showLoadingIndicator:function(){
        avs.loadingIndiciatorTimeout=window.setTimeout(avs._showLoadingIndicator,150);
    },
    _showLoadingIndicator:function(){
        $('#statusdiv').removeClass('statusOff').addClass('statusOn');
        avs._positionLoadingIndicator();
    },
    _positionLoadingIndicator:function(){
        avs.loadingIndiciatorTimeout=window.setTimeout(avs._positionLoadingIndicator,16);
    },
    hideLoadingIndicator:function(){
        if(avs.loadingIndiciatorTimeout){
            window.clearTimeout(avs.loadingIndiciatorTimeout);
            avs.loadingIndiciatorTimeout=false;
        }
        $('#statusdiv').removeClass('statusOn').addClass('statusOff');
    },
    field:{
        DisableField:function(disabler,toDisable){
            var tmp=$('#'+disabler).attr('checked');
            if(tmp!=true){
                tmp=false;
            }
            $('#'+toDisable).attr('disabled',tmp);
            if(tmp==true){
                $('#'+toDisable).hide();
            }else{
                $('#'+toDisable).show();
            }
        },
        EnableField:function(enabler,toEnable){
            var tmp=$('#'+enabler).attr('checked');
            if(tmp!=true){
                tmp=false;
            }
            $('#'+toEnable).attr('disabled',!tmp);
            if(tmp==false){
                $('#'+toEnable).hide();
            }else{
                $('#'+toEnable).show();
            }
        },
        ShowField:function(enabler,toEnable,reverse){
            if(reverse===undefined){
                var reverse=false;
            }
            var tmp=$('#'+enabler).attr('checked');
            if(tmp!=true){
                tmp=false;
            }
            if(reverse){
                tmp=!tmp;
            }
            if(tmp==false){
                $('#'+toEnable).hide();
            }else{
                $('#'+toEnable).show();
            }
        },
        toggleCheckboxes:function(master,formName){
            if(formName==''){
                formName='ListForm';
            }
            if(master.checked==true){
                avs.field.checkAll(formName);
            }else{
                avs.field.uncheckAll(formName);
            }
        },
        checkAll:function(formName){
            formObj=document.getElementById(formName);
            for(var i=0;i<formObj.length;i++){
                fldObj=formObj.elements[i];
                if(fldObj.type=='checkbox'&&!fldObj.disabled){
                    fldObj.checked=true;
                }
            }
        },
        uncheckAll:function(formName){
            formObj=document.getElementById(formName);
            for(var i=0;i<formObj.length;i++){
                fldObj=formObj.elements[i];
                if(fldObj.type=='checkbox'&&!fldObj.disabled){
                    fldObj.checked=false;
                }
            }
        },
        checkSelectedBoxes:function(formName){
            formObj=document.getElementById(formName);
            for(var i=0;i<formObj.length;i++){
                fldObj=formObj.elements[i];
                if(fldObj.type=='checkbox'&&fldObj.checked==true){
                    return true;
                }
            }
            return false;
        }
    }
}

AVSUrlBrowser=function(clickElement,urlElement,textElement){
    var self=this;
    self.clickElement=clickElement;
    self.urlElement=urlElement;
    self.textElement=textElement;
    self.onBeforeBrowserClose=function(){
        var url=$('#urlbrowser-selected-url').val();
        if(!url){
            return;
        }
        $(self.urlElement).val(url);
        if(!self.textElement){
            return;
        }
        var text=$('#urlbrowser-selected-text').val();
        if(!text){
            return
        }
        $(self.textElement).val(text);
    };

    self.openBrowser=function(){
        AVSUrlBrowserWindow=$.iModal({
            type:'ajax',
            url:'remote.php?section=content&action=urlbrowser&type=file&mode=text',
            title:"URL Browser",
            buttons:'<div id="urlbrowser-buttons"><div class="urlbrowser-insertbuttons urlbrowser-insertbuttons--default" style="display:none;"><button disabled="disabled" class="urlbrowser-insert" id="urlbrowser-insert">'+"Insert Link"+'</button></div>'+'<button id="urlbrowser-cancel">'+"Cancel"+'</button></div><br style="clear: both;" />',
            width:700,
            onBeforeClose:self.onBeforeBrowserClose
        });
    };

    self.onClick=function(event){
        event.preventDefault();
        self.openBrowser();
    };

    self.bindClick=function(){
        $(self.clickElement).click(self.onClick);
    };

    self.unbindClick=function(){
        $(self.clickElement).unbind('click',self.onClick);
    };

    self.bindEvents=function(){
        self.bindClick();
    };

    self.unbindEvents=function(){
        self.unbindClick();
    };

    self.bindEvents();
};

var AVSUrlBrowserWindow={};

function in_array(needle,array){
    for(i=0;i<array.length;i++){
        if(array[i]==needle){
            return true;
        }
    }
    return false;
}

function SetCookie(cookieName,cookieValue,nDays)
{
    var today=new Date();
    var expire=new Date();
    if(nDays==null||nDays==0){
        nDays=1;
    }
    expire.setTime(today.getTime()+3600000*24*nDays);
    document.cookie=cookieName+"="+escape(cookieValue)+";expires="+expire.toGMTString();
}

function ReadCookie(n){
    var cookiecontent=new String();
    if(document.cookie.length>0){
        var cookiename=n+'=';
        var cookiebegin=document.cookie.indexOf(cookiename);
        var cookieend=0;
        if(cookiebegin>-1){
            cookiebegin+=cookiename.length;
            cookieend=document.cookie.indexOf(";",cookiebegin);
            if(cookieend<cookiebegin){
                cookieend=document.cookie.length;
            }
            cookiecontent=document.cookie.substring(cookiebegin,cookieend);
        }
    }
    return unescape(cookiecontent);
}

function animate_color(id,color){
    var htmlColor='';
    var duration=100;
    if(color=='green'){
        htmlColor='#99FF66';
    }else if(color=='orange'){
        htmlColor='#FFCC66';
    }else if(color=='red'){
        htmlColor='#FC7575';
        duration=300;
    }else if(color=='default'){
        htmlColor='#F9F9F9';
        duration=300;
    }
    $("#"+id).animate({
        backgroundColor:htmlColor
    },{
        queue:true,
        duration:duration
    });
}

$(function(){
    $(document).ajaxStart(avs.showLoadingIndicator);
    $(document).ajaxStop(avs.hideLoadingIndicator);
});

function ShowQuickHelp(container,title,desc)
{
    div=document.createElement("div");
    div.style.display='block';
    div.style.position='absolute';
    div.style.width='185px';
    div.style.backgroundColor='#FEFCD5';
    div.style.border='solid 1px #E7E3BE';
    div.style.padding='10px';
    div.className="helpHover";
    var offset=$(container).offset();
    div.style.top=(offset.top+35)+'px';
    div.style.left=offset.left+'px';
    if(title!=''){
        div.innerHTML='<div class="helpTip"><strong>'+title+'</strong></div><br />';
    }
    div.innerHTML+='<div style="width:185px; padding-left:0px" class="helpTip">'+desc+'</div>';
    document.body.appendChild(div);
}
function HideQuickHelp(p)
{
    $('.helpHover').remove();
}
function ShowHelp(divid,title,desc,left)
{
    var windowHeight=$(window).height();
    var div=document.getElementById(divid);
    div.style.display='block';
    div.style.position='absolute';
    div.style.width='190px';
    div.style.backgroundColor='#FEFCD5';
    div.style.color='#000000';
    div.style.border='solid 1px #E7E3BE';
    div.style.padding='10px';
    var offset=$('#'+divid).siblings('img').offset();
    div.style.top=offset.top+'px';
    if(typeof left!='undefined'){
        div.style.left=left+'px';
    }else{
        div.style.left=(offset.left+20)+'px';
    }
    div.innerHTML='<span class="helpTip"><b>'+title+'<\/b><\/span><br><img src="../../js/images/blank.gif" width="1" height="5" alt=""><br><div style="padding-left:5px; padding-right:5px" class="helpTip">'+desc+'<\/div>';
    var divOffset=$('#'+divid).offset();
    var divBottom=divOffset.top+$('#'+divid).height();
    if(divBottom>windowHeight){
        var difference=parseInt(divBottom-windowHeight);
        $('#'+divid).css('top',(parseInt(divOffset.top-difference)-15)+'px');
    }
}
function HideHelp(divid)
{
    var div=document.getElementById(divid);
    div.style.display='none';
}

