/**
 * @author Michele Conti Framework javascript = Mcf


netscape 4 	document.layers
netscape 6 	document.getelementbyid
ie4         document.all
ie 5 / 6 	document.all, document.getelementbyid

netscape 4 	document.layers["mioliv"].visibility= "hide";
ie (4, 5, 6) 	document.all["mioliv"].style.visibility= "hidden";
ie 5 e 5 - netscape 6 	document.getelementbyid("mioliv").style.visibility= "hidden";

 */

Mcf = {
    version: "0.0.0_1-beta",
    init: function(conf,o){
        if(conf!=undefined)
            for(var att in conf){
                o[att]=conf[att];
            }
    },
    crea: function(el){
        return document.createElement(el);
    },
    add: function(el){},
    w: window,
    n: navigator,
    d: document,
    gets: function(id,objNode){
        if(objNode==undefined)
            return document.getElementsByTagName(id);
        else
            return objNode.getElementsByTagName(id);
    },
    get: function(id){
        if(Mcf.isDom1){
            return document.getElementById(id);
        }else if(Mcf.isIE4){
            return document.all(id);
        }else if(Mcf.isNS4){
            return document.layers(id);
        }
    },
    setVisible: function(id){
        var el = Mcf.get(id);
        if(Mcf.isNS4)
            el.visibility= "visible";
        else
            el.style.visibility="visible";
    },
    setHidden: function(id){
        var el = Mcf.get(id);
        if(Mcf.isNS4)
            el.visibility= "hide";
        else
            el.style.visibility="hidden";
    },
    String: String,
    go: function(url){
        document.location = url;
    },
    highlightTableRows: function(table, onclick){
        if(table!=null){
            var tbody = Mcf.gets("tbody",table)[0];
            var rows = Mcf.gets("tr",tbody);
            for (i=0; i < rows.length; i++) {
                rows[i].onmouseover = function() {
                    this.style.cursor="pointer";
                    this.className='over_'+this.className;
                    onRow=this.id.substr(4);
                };
                rows[i].onmouseout = function() {
                    this.style.cursor="default";
                    if(this.className.substr(0, 5)=='over_')
                        this.className=this.className.substr(5);
                    onRow=-1;
                };
                if(onclick!=null)
                    rows[i].onclick = onclick;
            }
        }   
    },
    classe: function(c, config) {
        var toEval = "";
        toEval += c+" = function(){Mcf.init(config,this);}";
        eval(toEval);
    },
    Ajax: {
        readyState: {
			INATTIVO: 0,
			INIZIALIZZATO: 1,
			RICHIESTA: 2,
			RISPOSTA: 3,
			COMPLETATO: 4
		},
		statusText: (function(){
		    var statusText = [];
	        statusText[100] = "Continue";
            statusText[101] = "Switching Protocols";
            statusText[200] = "OK";
            statusText[201] = "Created";
            statusText[202] = "Accepted";
            statusText[203] = "Non-Authoritative Information";
            statusText[204] = "No Content";
            statusText[205] = "Reset Content";
            statusText[206] = "Partial Content";
            statusText[300] = "Multiple Choices";
            statusText[301] = "Moved Permanently";
            statusText[302] = "Found";
            statusText[303] = "See Other";
            statusText[304] = "Not Modified";
            statusText[305] = "Use Proxy";
            statusText[306] = "(unused, but reserved)";
            statusText[307] = "Temporary Redirect";
            statusText[400] = "Bad Request";
            statusText[401] = "Unauthorized";
            statusText[402] = "Payment Required";
            statusText[403] = "Forbidden";
            statusText[404] = "Not Found";
            statusText[405] = "Method Not Allowed";
            statusText[406] = "Not Acceptable";
            statusText[407] = "Proxy Authentication Required";
            statusText[408] = "Request Timeout";
            statusText[409] = "Conflict";
            statusText[410] = "Gone";
            statusText[411] = "Length Required";
            statusText[412] = "Precondition Failed";
            statusText[413] = "Request Entity Too Large";
            statusText[414] = "Request-URI Too Long";
            statusText[415] = "Unsupported Media Type";
            statusText[416] = "Requested Range Not Satisfiable";
            statusText[417] = "Expectation Failed";
            statusText[500] = "Internal Server Error";
            statusText[501] = "Not Implemented";
            statusText[502] = "Bad Gateway";
            statusText[503] = "Service Unavailable";
            statusText[504] = "Gateway Timeout";
            statusText[505] = "HTTP Version Not Supported";
            statusText[509] = "Bandwidth Limit Exceeded";
		    return statusText;
		})()
    }
};
Mcf.browser = Mcf.n.userAgent.toUpperCase();
Mcf.http = (function(){
    if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object")
        return new XMLHttpRequest();
    else if(window.ActiveXObject && Mcf.browser.indexOf("MSIE 4") < 0){
        if(Mcf.browser.indexOf("MSIE 5") < 0)
            return new ActiveXObject("Msxml2.XMLHTTP");
        else
            return new ActiveXObject("Microsoft.XMLHTTP");
    }else return null;
})();
Mcf.isDom1 = Mcf.d.getElementById ? true : false;
Mcf.isIE = Mcf.n.appName == "Microsoft Internet Explorer";
Mcf.isIE4 = (Mcf.d.all && !this.isDom1) ? true : false;
Mcf.isIe4 = Mcf.browser.indexOf("MSIE 4") >= 0;
Mcf.isIE5 = Mcf.browser.indexOf("MSIE 5") >= 0;
Mcf.isNS4 = Mcf.d.layers ? true : false;
Mcf.isAjax = Mcf.http != null;
Mcf.Ajax.Text = function(txt,el) {
            this.file = txt+"?rnd="+Math.random();
            if(Mcf.http) {
                Mcf.http.open("get", this.file, true);
                Mcf.http.setRequestHeader("connection", "close");
                Mcf.http.onreadystatechange = function(){
                    if(Mcf.http.readyState === Mcf.Ajax.readyState.COMPLETATO) {
                        if(Mcf.Ajax.statusText[Mcf.http.status] === "OK"){
                        
                                var old = el.innerHTML;
                                el.innerHTML =  old+Mcf.http.responseText;
                            
                        }else{
                            
                            el.innerHTML = "Impossibile effettuare l'operazione richiesta.<br />";
                            el.innerHTML += "Errore riscontrato: " + Mcf.Ajax.statusText[Mcf.http.status];
                            
                        }
                    } 
                }
                Mcf.http.send(null);
            }  
        }; 

Mcf.Ajax.TextArea = function(txt,edit) {
            this.file = txt;
            
            if(Mcf.http) {
                Mcf.http.open("get", this.file, true);
                Mcf.http.setRequestHeader("connection", "close");
                Mcf.http.onreadystatechange = function(){
                    if(Mcf.http.readyState === Mcf.Ajax.readyState.COMPLETATO) {
                        if(Mcf.Ajax.statusText[Mcf.http.status] === "OK"){
                            edit.value = Mcf.http.responseText;
                            merda = Mcf.http.responseText;
                        }else{
                            var el = Mcf.get(id);
                            el.value = "Errore riscontrato: " + Mcf.Ajax.statusText[Mcf.http.status];
                            
                        }
                    } 
                }
                Mcf.http.send(null);
            }  
        }; 

//String
Mcf.String.prototype.toFirstUpperCase = function(){
   return this.charAt(0).toUpperCase() + this.substr(1);
};

Mcf.String.prototype.getActionName = function(){
   return this.substr(0,this.indexOf("."));
};

Mcf.String.prototype.extrChToUniNuAndParseInt = function(a){
//va solo su firefox
  var b = "";
  alert(this);
  for(var c in this){
   alert(c)
   var d = this.charAt(c);
   alert(d);
   if(d != a && c >= 0){
    b += d;
   }
  }
  alert(b);
  return parseInt(b);
};

Mcf.String.prototype.getActionName = function() {
    return this.substr(0, this.indexOf("."));
};

//Form
Mcf.form = {
   submit: function(action){
      //var form = document.(action.getActionName());
      //form.action = action;
      //form.submit();
   }
};

//Action
Mcf.Action = function(model) {
        this.model = model;
        this.confirmToDel = function(){
           if(!confirm("Sei sicuro di voler cancellare questo/i "+model+"/i?"))
              return;
           if(!confirm("Procedere?"))
              return;
        };
        this.checkButtomOnOff = function(saveEneble){
           Mcf.get('ctrlDisable').disabled = saveEneble;
        };
        this.read = function() {
           if (linkEnable) {
              var arrayCell = Mcf.gets("td", this);
              var cellKey = arrayCell[0];
              var hiddenKeys = Mcf.gets("input", cellKey);
              var keys = [];
              for(var inp in hiddenKeys){
                 if(hiddenKeys[inp].type && hiddenKeys[inp].type.toLowerCase()=="hidden")
                    keys[hiddenKeys[inp].name] = hiddenKeys[inp].value;
              }
              Mcf.go("read" + model + ".action",model, keys);
           }
        }
        this.create = function() {
           Mcf.validate();
           var action = Mcf.formatUrl("create",model);
           (new Mcf.Form(model,action)).submit();
        };
        this.update = function() {
           Mcf.validate();
           var action = Mcf.formatUrl("update",model);
           (new Mcf.Form(model,action)).submit();
        };
        this.find = function(){
           //parent.showWaitMessage('Attendere prego.','Caricamento in corso, attendere...');
           Mcf.go("find" + this.model + ".action",this.model);
        };
        this.dels = function(action) {
           this.confirmToDel();
           var action = Mcf.formatUrl("delete",model);
           (new Mcf.Form(model,action)).submit();
        };
        this.del = function(action) {
           this.confirmToDel();
           var action = Mcf.formatUrl("delete",model);
           (new Mcf.Form(model,action)).submit();
        };

     };
     //Win
     Mcf.Win = function(url, name) {
        this.url = url;
        this.width = 500;
        this.height = 600;
        this.left = screen.width / 2 - width / 2;
        this.top = screen.height / 2 - height / 2;
        this.name = "";

        this.show=function(){
           Mcf.win = window.open(this.url, this.name, 'top=' + this.top + ',left=' + this.left + ',width=' + this.width + ',height=' + this.height + ',scrollbars,resizable,status');
        };
        this.setStatus = function(s) {
           Mcf.win.status = s;
        }
     };


//Project
Mcf.DisplayTagPage = function(config) {

        Mcf.init(config, this);
        this.table = (
                function(obj) {
                   var id = obj.model.toLowerCase();
                   return Mcf.get(id);
                })(this);

        if (this.isRicerca) {
           var action = new Mcf.Action(this.model);
           Mcf.highlightTableRows(this.table, action.read);
        }
     };
//gociinspector
Mcf.inspect = function(objs){
    
    for(var i = 0; i < objs.length; i++){
        alert(objs[i]);
    }   
};
//dd
var Drag = {

  elm: null,

  init: function(el) {
    Drag.elm = el;
    Drag.elm.style.position = 'relative';
    Drag.elm.style.cursor = 'move';
    Drag.elm.style.left = '0px';
    Drag.elm.style.top = '0px';
    Drag.elm.onmousedown = Drag.start;
  },

  start: function(e) {
    e = e || window.event;
    Drag.elm.lastX = e.clientX;
    Drag.elm.lastY = e.clientY;
    document.onmousemove = Drag.move;
    Drag.elm.onmouseup = Drag.end;
    Drag.setOpacity('0.5');
    return false;
  },

  move: function(e) {
    e = e || window.event;
    var ex = e.clientX;
    var ey = e.clientY;
    var currentX = parseInt(Drag.elm.style.left);
    var currentY = parseInt(Drag.elm.style.top);
    Drag.elm.style.left = currentX + (ex - Drag.elm.lastX) + 'px';
    Drag.elm.style.top = currentY + (ey - Drag.elm.lastY) + 'px';
    Drag.elm.lastX = ex;
    Drag.elm.lastY = ey;
    return false;
  },

  end: function() {
    Drag.setOpacity('1');
    document.onmousemove = null;
                Drag.elm.onmouseup = null;
  },

  setOpacity: function(value) {
    Drag.elm.style.opacity = value;
    Drag.elm.style.filter = 'alpha(opacity=' + value * 100 + ')';
  }

}
//Drag.init(document.getElementsByTagName("div")[0]);


