/*****************************************************************

	ActiveWidgets Grid 1.0.0 (Free Edition).
	Copyright (C) 2004 ActiveWidgets Ltd. All Rights Reserved. 
	More information at http://www.activewidgets.com/

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*****************************************************************/

if (!window.My) My=[];
if (!My.Templates) My.Templates=[];

// *********************************************
// DROP-DOWN LIST+GRID TEMPLATE
// *********************************************
My.Templates.DropListplusGrid = Active.Templates.Text.subclass();
My.Templates.DropListplusGrid.create = function() {
var obj = this.prototype;

 //FOR CHANGE STYLES

var ddlist = new Active.HTML.DIV; 
ddlist.setTag("select"); 
//ddlist.setId("myDDmdselect");
ddlist.setClass("templates", "dropdowntextarea"); 
ddlist.setStyle("height", "72");
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") } );

//************************************
//********* GRID *********************

var objddgrid = new Active.Controls.Grid; 
objddgrid.setId("myDDGrid");
objddgrid.setClass("templates", "ddgrid"); 
objddgrid.setStyle("display", "none");
objddgrid.setStyle("height", "152");
objddgrid.setStyle("Width", "462");

objddgrid.setColumnHeaderHeight('16px');
objddgrid.setRowHeaderWidth('20px');

var stylesheetX = document.styleSheets[document.styleSheets.length-1];

stylesheetX.addRule('#myDDGrid', 'FONT: MENU');
stylesheetX.addRule('#myDDGrid', 'WIDTH: 200px');
stylesheetX.addRule('#myDDGrid', 'HEIGHT: 100px');
stylesheetX.addRule('#myDDGrid', 'background: #ffcccc');
stylesheetX.addRule('#myDDGrid .active-grid-row', "height: 17px");
stylesheetX.addRule("#myDDGrid .active-scroll-left .active-list-item", "height:17px");
//*************** end grid ******************

//FUNCTIONS
// templatet variable provides temporary reference
// to the parent template during edit mode.
var template;
var tmpparam =""; 

function switchToEditMode(){
if (template) { switchToTextMode(); }
template = this;

//retrieves the array list from a temporary span
tmpparam = eval(document.getElementById('Temp_Span_Param').value) ;
var tmparrData = eval(tmpparam[0][0]);

// Options Constructor
var optionsHTML = new Array(); 
for(var i=0; i < eval(tmpparam[0][0]).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+3);
ddlist.setStyle("top", pos.y + el.offsetHeight+5);
ddlist.setStyle("width", el.offsetWidth);

template.element().style.padding = 0;
template.element().innerHTML = editor;

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); 

}
objddgrid.setStyle("left", pos.x + el.offsetWidth+3);
objddgrid.setStyle("top", pos.y + el.offsetHeight+5 );

document.getElementById('ddarea1').innerHTML = ddlist ; 
document.getElementById('ddarea2').innerHTML = objddgrid ;
ddlist.element().focus();
}

//EVENTS 
obj.setEvent("ondblclick", switchToEditMode );
editor.setEvent("onclick", switchToTextMode );
ddlist.setEvent("onclick", showgridspan );
objddgrid.setEvent("onblur", switchToTextMode );
//********************************************** 

function switchToTextMode(){
if( template ){
objddgrid.setStyle("display", "none");
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;
};

function showgridspan(){
var valsel="";
var rowsel="";
var tmparrFilter = new Array;
var selectedIndex = ddlist.element().selectedIndex; 

if(selectedIndex == 0){
objddgrid.setRowCount(eval(tmpparam[1][0]).length);
objddgrid.setColumnCount(eval(tmpparam[1][1]).length);
objddgrid.getDataText = function(i, j){return eval(tmpparam[1][0])[i][j]}
objddgrid.setDataText = function(value, i, j){eval(tmpparam[1][0])[i][j]=value}
objddgrid.setColumnProperty("text", function(i){return eval(tmpparam[1][1])[i]});
}

if(selectedIndex > 0){
if(selectedIndex == 1){
var keytosearch= eval(tmpparam[2][3]);
var coltosearch= eval(tmpparam[2][2]);
var arraytosearch = tmpparam[2][0];
var colgroup = tmpparam[2][1];
}
if(selectedIndex == 2){
var keytosearch= eval(tmpparam[3][3]);
var coltosearch= eval(tmpparam[3][2]);
var arraytosearch = tmpparam[3][0];
var colgroup = tmpparam[3][1];
}
//alert(arraytosearch);
for(var i=0; i<eval(arraytosearch).length; i++){
if(eval(arraytosearch)[i][coltosearch]==keytosearch){tmparrFilter.push(eval(arraytosearch)[i])}
}

objddgrid.setRowCount(tmparrFilter.length);
objddgrid.setColumnCount(eval(arraytosearch)[0].length);
objddgrid.getDataText = function(i, j){return tmparrFilter[i][j]}
objddgrid.setDataText = function(value, i, j){tmparrFilter[i][j]=value}
objddgrid.setColumnProperty("text", function(i){return eval(colgroup)[i]});
//objddgrid.refresh();
}

for(var i=1; i < 6; ++i) { eval("var inputsear" + i + "= new My.Templates.Input"); }
for(var i=1; i < 6; ++i){ eval("objddgrid.setColumnTemplate(inputsear" + i + "," + i + ")"); }

objddgrid.setAction('click', function(src){
valsel = src.getItemProperty("text");
rowsel = src.getRowProperty("index");
});

objddgrid.refresh();
objddgrid.setStyle("display", "block");
}

};

My.Templates.DropListplusGrid.create(); 
//******************************************

