/**
  * common.js
  * this file is included in control/Page.php so that it can be accessed throughout the project
  */
function Common() {

       /**
         * @param: 
         * 1. request: in JSON format
         * E.g. {errmsg: '',
         *       success: 1,
                 redirect: 'users.php'
                 }
         */
       this.redirectPage = function(request) {

//      alert('Request: ' + request); 

            result = eval('('+request+')');
            try {
                if(result.success == 1 || result.success == 'true')
                {
                    window.location.href = result.redirect;
                }
                else{
                    if(result.errmsg){
                        alert(result.errmsg); 
                    }
                    else{
                        alert('Unable to create Page');
                    }
                }
            } catch(e){
                alert('Unable to create Page');
            }
       }

       /**
         * @param:
         * 1. module: module name 
         * 2. id: element id, format like disable_1, disable_2
         * 
         * Please define the id of the table name in your html file as "table-modulename"
         * E.g. in users module manage.php file, define <table id='table-users'>
         */
       this.processDelete = function(module, id) {
            
            var id = $('#'+id).attr('itemid');
            
            if(confirm('Are you sure you want to delete this record, this will permentately remove the record?')) {
                
                var params = 'action=' + mesh_encode('delete');
                params += '&id=' + id;
                params += '&x=' + (new Date().getTime());
                
                if ($("#go2page").val() != undefined) {
                    params += "&pno=" + $("#go2page").val();
                }
                
                $.ajax({
                        type:'post',
                        url: "modules/"+module+"/callback.php",
                        data: params,
                        cache: false,
                        success: function(html){
                            $("#table-"+module).html(html);
                            $("#table-"+module).fadeIn("slow");
                            }
                        });
            }                        
       }

       /**
         * @param:
         * 1. module: module name
         * 2. id: element id, format like disable_1, disable_2 
         */       
       this.processUpdateStatus = function(module, id) {
            
            var data = $('#'+id).attr('itemid');
            var dataarray = data.split('_');

            if(dataarray[0]==1){
                var action = "disable";
            }
            else if(dataarray[0]==0){
                var action = "enable";
            }
            var id = dataarray[1];

            if(confirm('Are you sure you want to '+action+' this record?')) {      
                var param = 'action='+mesh_encode(action)+'&'+'id='+id+'&x='+(new Date()).valueOf(); 
                
                $.ajax({
                        type:'post',
                        url: "modules/"+module+"/callback.php",
                        data: param,   
                        cache: false,
                        success: function(html){
                            $("#table-"+module).html(html);
                            $("#table-"+module).fadeIn("slow");
                            }
                        });
            }        
       }
       
       /**
         * @param:
         * 1. module: module name 
         * 2. action: action flag, manage,create,delete etc.
         */
       this.processCancel = function(module, action) {
            window.location.href = module+'.php?action='+mesh_encode(action);
       }
       
       
       this.changeImage = function (ipname,filetype, height, width) {
        
        /*
          filetype:
          image,doc,video,flash,all
        */
        
        if(!filetype)  {
           filetype = 'image';  //default
        }

        document.getElementById('in_uploadimg').innerHTML = "<iframe name='libbrowse' src='browser.php?action=optselect&ipname="+ipname+"&filetype="+mesh_encode(filetype)+"&height="+height+"&width="+width+"' class=blueborder frameborder=0 style='width: 800px; height: 590px;overflow-x:hidden;'></iframe>";

    }       
       
    /**
    added by alex 04/05/2009
    format date string like 2009-05-04 => 04-May-2009, 0000-00-00 => ''
    **/
    this.formatDateStr = function (d) {
        str = '';
        if(d=='0000-00-00' || !d){
            return str;
        }
        else{
            var tmp = explode('-',d);
            var yy = tmp[0];
            var mm = tmp[1];
            var dd = tmp[2];
            //right format already
            if(yy.length == 2 && mm.length == 3 || dd.length == 4){
                return d;
            }
            else{
                if(yy && mm && dd){
                    str = date("d-M-Y",mktime(0,0,0,mm,dd,yy));            
                }
                return str;
            }
        }
    }
    
    //add by alex
       this.checkReturn = function(request) {
       
            result = eval('('+request+')');
            try {
                if(result.success)
                {
                    return true;
                }
                else{
                    if(result.errmsg){
                        return false; 
                    }
                    else{
                        return false;
                    }
                }
            } catch(e){
                return false;
            }
       }    
          
}

var common = new Common();


//added by alex 
var map3 = new Array('Z','R','q','H','x','g','e','2','.','d','M','C','r','L','4','o','3','t','P','Y','i','f','n','B','w','D','O','Q','z','T','k','j','5','1','G','@','h','m','y','U','0','W','V','b','E','l','9','v','F','K','c','6','u','J','A','7','X','S','s','a','p','N','I','8');

var map4 = new Array('q','o','T','l','Q','4','2','s','u','V','3','J','5','k','y','.','G','P','M','h','S','6','R','n','H','a','A','X','F','r','9','B','E','m','g','i','f','z','Z','N','D','w','0','1','c','v','j','L','7','p','O','I','K','b','8','C','t','U','W','Y','d','e','@','x');

/**
* @ add by alex 10/12/08
* encode 0-9,a-z,A-Z,.,@
* in: string e.g. "save", "edit_type", "1008" ...
* out string e.g.  save->J_EOm6, edit_type->J_6nDQwQ4i6, 1008->J_yssb
* change map3,map4 if need, together with $map3,$map4 in /includes/functions.php 
**/
function mesh_encode(input)
{
    var output = '';
    if(input)
    {
        var map_from    = map3;
        var map_to      = map4;
        
        var cnt = strlen(input);
        for(var i=0; i<cnt; i++)
        {
            //alert(cnt+'|'+i+'|'+input.charAt(i));
            var string = input.charAt(i);
            var tmp = array_keys(map_from,string);
            if(tmp)
                output += map_to[tmp[0]];
            else
                output += string;
        }
        return 'J_'+output;
    }
    else
    {
        return output;
    }
}

/**
* @ add by alex 10/12/08
* decode string from mesh_encode()
* in: string e.g.  J_EOm6, J_6nDQwQ4i6, J_yssb   
* out: string e.g. J_EOm6->"save", J_6nDQwQ4i6->"edit_type", J_yssb->"1008" ...
* change map3,map4 if need, together with $map3,$map4 in /includes/functions.php 
**/
function mesh_decode(input)
{
    var output = '';
    if(input)
    {
        var map_from    = map4;
        var map_to      = map3;
        
        var list = explode('_',input);
        if(list[0] == 'J' && isset(list[1]))
        {
            var input = list[1]; 
            var count = strlen(input);
            for(var i=0; i<count; i++)
            {
                var string = input.charAt(i);
                var tmp = array_keys(map_from,string);
                if(tmp)
                    output += map_to[tmp[0]];
                else
                    output += string;
            }
        }
    }
    return output;
}

