if (!window.My) My=[];
if (!My.Templates) My.Templates=[];
//****************************************************************
//	Simple text template. (My clone of core)
//*****************************************************************
My.Templates.Text_my = Active.Templates.Text.subclass();
My.Templates.Text_my.create = function(){

	var obj = this.prototype;

	obj.setClass("templates", "text");
	obj.setContent("text", function(){return this.getItemProperty("text")});
	obj.setEvent("onclick", function(){this.action("click")});
};

My.Templates.Text_my.create();
// *****************************************
//////////////////////////////////////////////////////


	
// ****************************************************************
//    Input Cell Template.
// ****************************************************************
My.Templates.Input_fc = Active.Templates.Text.subclass();
My.Templates.Input_fc.create = function(){
    var obj = this.prototype;
	
//    editor is not part of the template,
//    there is only one single instance of editor object.
    var editor = new Active.HTML.INPUT;
    editor.setClass("templates", "input");
    editor.setAttribute("type", "text");
    editor.setAttribute("value", function(){
        return template.getItemProperty("text");
    });

//    template variable provides temporary reference
//    to the parent template during edit mode.
    var template;

    function switchToEditMode(){ if (template) { switchToTextMode() }
        template = this;
        template.element().style.padding = 0;
        template.element().innerHTML = editor;
        editor.element().focus();
		editor.setEvent("ondblclick", editor.element().focus()); 
    }

    obj.setEvent("onfocus", switchToEditMode);

    function switchToTextMode(){
        var value = editor.element().value;
        template.setItemProperty("text", value);
        template.refresh();
        template = null;
    }

    editor.setEvent("onblur", switchToTextMode);
};

My.Templates.Input_fc.create(); 

// *********************************************
/////////////////////////////////////////////////////


// *********************************************
// DROP-DOWN TEXTAREA (editable) (Single click))
// *********************************************
My.Templates.DropEditTA_fc = Active.Templates.Text.subclass();
My.Templates.DropEditTA_fc.create = function()
{
var obj = this.prototype;

var editarea = new Active.HTML.TEXTAREA;
editarea.setId("myDDtxt");
editarea.setClass("templates", "dropdowntextarea");
editarea.setStyle("height", "52");
editarea.setAttribute("type", "text");
editarea.setContent("text", function(){ return template.getItemProperty("text") });

var editor = new Active.HTML.INPUT;
editor.setClass("templates", "input");
editor.setStyle("background-color", "lightyellow");
editor.setAttribute("type", "text");
editor.setAttribute("readonly", "true");
editor.setAttribute("value", function(){ return document.getElementById('myDDtxt').value } );

//EVENTS 
obj.setEvent("onfocus", switchToEditMode );
editor.setEvent("onclick", switchToTextMode );
editarea.setEvent("onblur", switchToTextMode );

//changes the enter and tab keys with a single space
editarea.setEvent("onkeydown", function(item){
tabspace = " "; //tab space
onespace = " "; //single space for Enter or other key 
if(event.keyCode==9){
item.selection=document.selection.createRange(); //cursor location
item.selection.text=tabspace; //insert at cursor location
event.returnValue = false; }
if(event.keyCode==13){
item.selection=document.selection.createRange(); //cursor location
item.selection.text=onespace; //insert at cursor location
event.returnValue = false; } 
} );


//FUNCTIONS
// templatet variable provides temporary reference
// to the parent template during edit mode.
var template;

function switchToEditMode(){
if (template) { switchToTextMode(); }
template = this;

var ddSpan11 = document.getElementById('ddarea11');
if(!ddSpan11) {
ddSpan11 = document.createElement('span');
ddSpan11.id = 'ddarea11';
document.body.appendChild(ddSpan11); 
}

document.getElementById('ddarea11').innerHTML = editarea ;

var el = template.element();
var pos = getAbsolutePos(el);
editarea.setStyle("left", pos.x+2);
editarea.setStyle("top", pos.y + el.offsetHeight+3);
editarea.setStyle("width", el.offsetWidth);

template.element().style.padding = 0;
template.element().innerHTML = editor;

editarea.element().focus();
}

function switchToTextMode(){
if( template ){
var value = document.getElementById('myDDtxt').value;
template.setItemProperty("text", value); 
template.refresh(); 
template=null;
}
document.getElementById('ddarea11').innerHTML ="";
 }

function getAbsolutePos(el) {
var SL = 0, ST = 0;
var is_div = /^div$/i.test(el.tagName);
if (is_div && el.scrollLeft)
SL = el.scrollLeft;
if (is_div && el.scrollTop)
ST = el.scrollTop;
var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
if (el.offsetParent) {
var tmp = getAbsolutePos(el.offsetParent);
r.x += tmp.x;
r.y += tmp.y;
}
return r;
};

};

My.Templates.DropEditTA_fc.create(); 
//**************************************
//////////////////////////////////////////////////////




// **************************************************************** 
// TEXTAREA Editable Cell Template. (single-click))
// **************************************************************** 

My.Templates.In_Textarea_fc = Active.Templates.Text.subclass(); 
My.Templates.In_Textarea_fc.create = function() 
{ 
var obj = this.prototype; 

// editor is not part of the template, 
// there is only one single instance of editor object. 
var editor = new Active.HTML.TEXTAREA; 
editor.setClass("templates", "textarea"); 
editor.setAttribute("type", "text"); 
editor.setContent("text", function(){return template.getItemProperty("text")}); 

// template variable provides temporary reference 
// to the parent template during edit mode. 
var template; 

function switchToEditMode(){ 
if (template) { switchToTextMode() } 
template = this; 
template.element().style.padding = 0; 
template.element().innerHTML = editor; 
editor.element().focus(); 
editor.setEvent("ondblclick", editor.element().focus());
} 


//EVENTS
obj.setEvent("onfocus", switchToEditMode); 
editor.setEvent("onblur", switchToTextMode); 

//changes the enter and tab keys with a single space
editor.setEvent("onkeydown", function(item){
onespace = " "; //single space for Enter or other key 
if(event.keyCode==9){
item.selection=document.selection.createRange(); //cursor location
item.selection.text=onespace; //insert at cursor location
event.returnValue = false; }
if(event.keyCode==13){
item.selection=document.selection.createRange(); //cursor location
item.selection.text=onespace; //insert at cursor location
event.returnValue = false; } 
} );

function switchToTextMode(){ 
var value = editor.element().value; 
template.setItemProperty("text", value); 
template.refresh(); 
template = null; 
} 

}; 

My.Templates.In_Textarea_fc.create(); 

//******************************************


// *********************************************
//     DROP-DOWN LIST TEMPLATE (single click)
// *********************************************

My.Templates.ListBox_fc = Active.Templates.Text.subclass();
My.Templates.ListBox_fc.create = function()
{
  var obj = this.prototype;

 //FOR CHANGE STYLES

   var ddlist = new Active.HTML.DIV; 
    ddlist.setTag("select"); 
    ddlist.setClass("templates", "dropdowntextarea"); 
//  ddlist.setStyle("widht", "82");  //manual width (see auto-width)
ddlist.setStyle("height", "64");
ddlist.setAttribute("size", 2);


  var editor = new Active.HTML.INPUT;
  editor.setClass("templates", "input");
  editor.setStyle("background-color", "lightyellow");
  editor.setAttribute("type", "text");
  editor.setAttribute("readonly", "true");
  editor.setAttribute("value", function(){ return template.getItemProperty("text")  } );
 //**********************************************  


//FUNCTIONS
  // templatet variable provides temporary reference
  // to the parent template during edit mode.
   var template;
var tmparrData = new Array(); 
   
  function switchToEditMode(){
      if (template) { switchToTextMode(); }
      template = this;

//retrieves the array list from a temporary span
tmparrData = eval(document.getElementById('Temp_Span_Param1').value) ;

// Options Constructor
var optionsHTML = new Array(); 
for(var i=0; i < tmparrData.length; ++i) {
var opt = new Active.System.HTML;
opt.setTag("option");
opt.setContent("text", tmparrData[i]);
optionsHTML.push( opt ); 
}

ddlist.setContent("options", optionsHTML.join(""));
  
  var el = template.element();
      var pos = getAbsolutePos(el);
       ddlist.setStyle("left", pos.x);
      ddlist.setStyle("top", pos.y + el.offsetHeight);
     ddlist.setStyle("width", el.offsetWidth); //auto-width

     template.element().style.padding = 0;
     template.element().innerHTML = editor;

var ddSpan12 = document.getElementById('myselect');
if(!ddSpan12) {
ddSpan12 = document.createElement('span');
ddSpan12.id = 'myselect';
document.body.appendChild(ddSpan12); 
}
document.getElementById('myselect').innerHTML = ddlist ;  
ddlist.element().focus();
    }

//EVENTS 
  obj.setEvent("onfocus", switchToEditMode );
  ddlist.setEvent("onblur", switchToTextMode);  
 ddlist.setEvent("onclick", switchToTextMode );
 //**********************************************  


  function switchToTextMode(){
if( template ){
var selectedIndex = ddlist.element().selectedIndex; 
if(selectedIndex != -1) {
var value = ddlist.element().options[selectedIndex].text;
  template.setItemProperty("text", value);
}
template.refresh(); 
template=null;
}
document.getElementById('myselect').innerHTML ="";
 }

  function getAbsolutePos(el) {
	  var SL = 0, ST = 0;
	  var is_div = /^div$/i.test(el.tagName);
	  if (is_div && el.scrollLeft)
		  SL = el.scrollLeft;
	  if (is_div && el.scrollTop)
		  ST = el.scrollTop;
	  var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
	  if (el.offsetParent) {
		  var tmp = getAbsolutePos(el.offsetParent);
		  r.x += tmp.x;
		  r.y += tmp.y;
	  }
	  return r;
  };

};

My.Templates.ListBox_fc.create(); 
//******************************************
///////////////////////////////////////////////////


// **************************************************************** 
// BigCheckbox Template. 
// ****************************************************************
My.Templates.BigCheckbox_fc = Active.Templates.Text.subclass();
My.Templates.BigCheckbox_fc.create = function()
{
var obj = this.prototype;

// editor is not part of the template,
// there is only one single instance of editor object.

var Acheckbox = new Active.HTML.INPUT;
Acheckbox.setClass("templates", "bigcheckbox");
Acheckbox.setStyle("width", "27");
Acheckbox.setStyle("height", "20");
Acheckbox.setAttribute("hidden", "true");
Acheckbox.setAttribute("readonly", "true"); 
Acheckbox.setAttribute("value", "Yes"); 

var Bcheckbox = new Active.HTML.INPUT;
Bcheckbox.setClass("templates", "bigcheckbox");
Bcheckbox.setAttribute("type", "text");
Bcheckbox.setStyle("width", "27");
Bcheckbox.setStyle("height", "20");
Bcheckbox.setAttribute("readonly", "true"); 
Bcheckbox.setAttribute("value", "No");

// EVENTS

Acheckbox.setEvent("onblur", switchToTextMode);

Acheckbox.setEvent("onclick", function(){
template.element().innerHTML = Bcheckbox;
template.setItemProperty("text", "No")
switchToTextMode(); } );

Bcheckbox.setEvent("onblur", switchToTextMode);

Bcheckbox.setEvent("onclick", function(){
template.element().innerHTML = Acheckbox;
template.setItemProperty("text", "Yes");
switchToTextMode(); } );

obj.setEvent("onfocus", switchToEditMode);

// FUNCTIONS 
// template variable provides temporary reference
// to the parent template during edit mode.
var template;

function switchToEditMode(){
if (template) {
switchToTextMode()
}
template = this;
template.element().style.padding = 0;

if(template.getItemProperty("text")=="Yes"){
template.element().innerHTML = Acheckbox;
Acheckbox.element().focus(); }

else{ template.element().innerHTML = Bcheckbox;
Bcheckbox.element().focus(); }
}


function switchToTextMode(){
template.element().innerHTML = "";
template.refresh();
template = null;
}

};

My.Templates.BigCheckbox_fc.create(); 
//**********************************
//////////////////////////////////////////////////////////



// *********************************************
//     DROP-DOWN FORM TEMPLATE
// *********************************************
My.Templates.DropForm = Active.Templates.Text.subclass();
My.Templates.DropForm.create = function() {
  var obj = this.prototype;

//************************************
//********* form div container *********************
   var ddcontainer = new Active.HTML.DIV; 
    ddcontainer.setId("myDDform");
ddcontainer.setStyle("background-color", "#708090");
    ddcontainer.setClass("templates", "ddgrid"); 
  ddcontainer.setStyle("width", "400");
ddcontainer.setStyle("visibility", "hidden");

   var ddmenu = new Active.HTML.DIV; 
    ddmenu.setId("myDDmenu");
ddmenu.setStyle("background-color", "#708090");
    ddmenu.setClass("templates", "ddgrid"); 
  ddmenu.setStyle("width", "400");
ddmenu.setStyle("visibility", "hidden");

//********GRID  ****************
    var objddgrid = new Active.Controls.Grid; 
       objddgrid.setId("myDDGrid");
    objddgrid.setClass("templates", "gridmenu"); 
var myColumed1 = ["SAVE", "CANCEL"];
 
objddgrid.setRowCount(1);
objddgrid.setColumnCount(2);
    objddgrid.setColumnProperty("text", function(i){return myColumed1[i]}); 

objddgrid.setStyle("margin-left", 130)

//DISABLE SELECT ROW 
obj.setAction("selectRow", null);
obj.setEvent("onkeydown", null);

//onmouseover buttons save or cancel to lost focus from edit field
// and acquire focus to te menu button ( available value into the cell)
objddgrid.setEvent("onmouseover", function(){this.element().focus() } );

//*************** end grid ******************

//EVENTS 
//**********************************
obj.setEvent("ondblclick", switchToEditMode );

//FUNCTIONS
// templatet variable provides temporary reference to the parent template during edit mode.
   var template=null;

var OBJNAME = "";
var OBJID = "";
 var tmpparam =""; 
var yesselectrow="";
var sortonheader = "";
var stylesheetX = document.styleSheets[document.styleSheets.length-1];

  function switchToEditMode(){   
if (!template) {  

 switchToTextMode(); 

      template = this;

tmpparam = eval(document.getElementById('Temp_Span_Param').value) ;
document.getElementById('Temp_Span_Param').value="";

OBJNAME = this.element().parentNode.parentNode.parentNode.objname ;
OBJID = this.element().parentNode.parentNode.parentNode.objid ;


// ******* capture in a var the row selected
var rowselected = eval(OBJNAME + ".getSelectionProperty('index')");

// ******* capture in a var row-select permited (to restore it later)
yesselectrow = eval(OBJNAME + ".getAction('selectRow')");

//******** disables row selection
eval(OBJNAME + ".setAction('selectRow', null)");

// capture in a var mousedown on headers (sorting) (to restore it later) 
sortonheader = eval(OBJNAME + ".getTemplate('top/item').getEvent('onmousedown')"); 

//******** disables click on headers (sorting)
function disablesort(){return false}
eval(OBJNAME + ".getTemplate('top/item').setEvent('onmousedown',disablesort)");

//disable scroll (only I.E.)
stylesheetX.addRule('#' + OBJID + ' .active-scroll-bars', 'overflow-x: hidden');
stylesheetX.addRule('#' + OBJID + ' .active-scroll-bars', 'overflow-y: hidden');


  var el = template.element();
      var pos = getAbsolutePos(el);
       ddcontainer.setStyle("left", pos.x+2);
      ddcontainer.setStyle("top", pos.y + el.offsetHeight+29);
     ddcontainer.setStyle("height", 5 + eval(tmpparam[0][2])*27);

      var pos = getAbsolutePos(el);
       ddmenu.setStyle("left", pos.x+2);
      ddmenu.setStyle("top", pos.y + el.offsetHeight+3);
    ddmenu.setStyle("height", 28);

var ddSpan1 = document.getElementById('ddarea1');
if(!ddSpan1) {
ddSpan1 = document.createElement('span');
ddSpan1.id = 'ddarea1';
document.body.appendChild(ddSpan1); 

ddSpan2 = document.createElement('span');
ddSpan2.id = 'ddarea2';
document.body.appendChild(ddSpan2); 

}
document.getElementById('ddarea2').innerHTML = ddmenu;
document.getElementById('ddarea1').innerHTML = ddcontainer ;
ddmenu.setStyle("visibility", "visible");

// FORM CONSTRUCTOR
var valsel="";
var rowsel="";
var tmparrFilter = new Array;

var arrayprincipal = tmpparam[0][0];
var colsarraymain = tmpparam[0][1];
var colsinmainarray = eval(tmpparam[0][2]);
var rowmainselected = eval(tmpparam[0][3]);
var templatetoput = eval(tmpparam[0][4]);
var templateparam = eval(tmpparam[0][5]);
var showfield = eval(tmpparam[0][6]);
var thecssname = eval(tmpparam[0][7]);
var fieldwidth = eval(tmpparam[0][8]);
var fieldheight = eval(tmpparam[0][9]);
var fieldalign = eval(tmpparam[0][10]);
var fieldcolor = eval(tmpparam[0][11]);
var multipleparam = new Array;


for(var x=0; x< eval(arrayprincipal)[x].length; x++) { 

eval('var FV' + x + '= new Active.Controls.Grid');

eval('FV' + x + ".setId('gridFV-" + x +"')");

eval('var DataFV' + x + "=[[eval(colsarraymain)[x], eval(arrayprincipal)[rowmainselected][x]]]");

eval('FV' + x + ".setDataProperty('text', function(i, j){return DataFV" + x + '[i][j]})');
eval('FV' + x + ".setDataText = function(value, i, j){DataFV" + x + '[i][j]=value}');
eval('FV' + x + '.setRowCount(1)');
eval('FV' + x + '.setColumnCount(2)');
eval('FV' + x + ".setRowHeaderWidth('0px')");
eval('FV' + x + ".setStyle('margin-left', 8)");
eval('FV' + x + ".setAction('selectRow', null)");
eval('FV' + x + ".setEvent('onkeydown', null)");
eval('FV' + x + ".getTemplate('top').setEvent('onselectstart', FV" + x + ".getEvent('onselectstart'))");
eval('FV' + x + ".setEvent('onselectstart', null)");


//function that on single click copy param insto span1.value
function GridClicked() {
var tempwhatgrid =  this.getId().indexOf("-" ,'1');                              
var whatgrid = this.getId().substr((tempwhatgrid+1),(this.getId().length)); 
document.getElementById('Temp_Span_Param1').value = eval(templateparam)[whatgrid]; ;
 };

//apply list-value params (in case of list-type templates)
if(eval(templateparam)[x]!=""){
eval('FV' + x + ".setEvent('onmouseover', GridClicked )");
}
////////////////////////////

//apply default css stylesheets
eval('FV' + x + ".setClass('templates', eval(thecssname)[x])");


//apply special width (not the default) 
if(eval(fieldwidth)[x]!=""){
var realwidth = eval(fieldwidth)[x]-100 ;
//eval('FV' + x + ".setStyle('WIDTH', (eval(fieldwidth)[x]))");
stylesheetX.addRule('#gridFV-' + x, 'WIDTH: ' + (eval(fieldwidth)[x]) + 'px');
stylesheetX.addRule('#gridFV-' + x + ' .active-column-1', 'WIDTH: ' + realwidth + 'px');
}

//apply special height (not the default) 
if(eval(fieldheight)[x]!=""){
//eval('FV' + x + ".setStyle('HEIGHT', (eval(fieldheight)[x]))");
stylesheetX.addRule('#gridFV-' + x, 'HEIGHT: ' + eval(fieldheight)[x] + 'px');
stylesheetX.addRule('#gridFV-' + x + ' .active-grid-row', 'HEIGHT: ' + eval(fieldheight)[x] + 'px');
stylesheetX.addRule('#gridFV-' + x + ' .active-scroll-left .active-list-item', 'HEIGHT: ' + eval(fieldheight)[x] + 'px');
}
//apply special text-align (not the default) 
if(eval(fieldalign)[x]!=""){
stylesheetX.addRule('#gridFV-' + x + ' .active-column-1', 'TEXT-ALIGN: ' + eval(fieldalign)[x] );
}

//apply special background-color (not the default) 
if(eval(fieldcolor)[x]!=""){
stylesheetX.addRule('#gridFV-' + x + ' .active-column-1', 'background-color: ' + eval(fieldcolor)[x] );
}

//apply templates
eval("var FVTPL" + x + "= new My.Templates." + eval(templatetoput)[x]);
eval("FV" + x + ".setColumnTemplate(FVTPL" + x + "," + 1 + ")"); 

//add to container grid with showfield = "" - otherwise hide the field (mini-grid)
if(eval(showfield)[x]==""){
eval("document.getElementById('myDDform').innerHTML += FV" + x ) ;
}
}
document.getElementById('myDDmenu').innerHTML = objddgrid;
ddcontainer.setStyle("visibility", "visible");

//*******************************************
//******** FUCTION CLICK ON SAVE/CANCEL MENU-BUTTONS
//************************************************
function headerClickeded1(e) {

var botonok = e.srcElement.id;                                         
var posbotonok = botonok.indexOf(":" ,9);                              
var posendbotonok = botonok.indexOf("/" ,9);                           
var numbotonok = botonok.substring((posbotonok+1),(posendbotonok)); 

////// SAVE
if(numbotonok =="0"){
eval(OBJNAME + ".setSelectionProperty('values', [rowmainselected])");

for(var x=0; x< eval(tmpparam[0][2]); x++) { 
eval('eval(tmpparam[0][0])[rowselected][' + x + '] = DataFV' + x + '[0][1]');
}
eval(OBJNAME + ".getRowTemplate([rowselected]).refresh()");
}
switchToTextMode();
} 
objddgrid.getTemplate("top/item").setEvent("onmousedown", headerClickeded1);
  } // final of !template
 };

  function switchToTextMode(){
if( template ){

//enables row selection
eval(OBJNAME + ".setAction('selectRow', yesselectrow)");

//enable sorting
eval(OBJNAME + ".getTemplate('top/item').setEvent('onmousedown',sortonheader)");

//enable scroll
stylesheetX.addRule('#' + OBJID + ' .active-scroll-bars', 'overflow-x: scroll');
stylesheetX.addRule('#' + OBJID + ' .active-scroll-bars', 'overflow-y: scroll');

//ddcontainer.setStyle("visibility", "hidden");
//ddmenu.setStyle("visibility", "hidden");
template.refresh(); 
template=null;
document.getElementById('ddarea1').innerHTML ="";
document.getElementById('ddarea2').innerHTML ="";
}
 }

  function getAbsolutePos(el) {
	  var SL = 0, ST = 0;
	  var is_div = /^div$/i.test(el.tagName);
	  if (is_div && el.scrollLeft)
		  SL = el.scrollLeft;
	  if (is_div && el.scrollTop)
		  ST = el.scrollTop;
	  var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
	  if (el.offsetParent) {
		  var tmp = getAbsolutePos(el.offsetParent);
		  r.x += tmp.x;
		  r.y += tmp.y;
	  }
	  return r;
  };
};

My.Templates.DropForm.create(); 
//******************************************
///////////////////////////////////////////////////////






