// Title: Tigra Tables PRO
// URL: http://www.softcomplex.com/products/tigra_tables_pro/
// Version: 1.1
// Date: 12-11-2002 (mm-dd-yyyy)
// Technical Support: support@softcomplex.com (specify product title and order ID)
// Notes: Registration needed to use this script legally.
//	Visit official site for details.

var path_to_files = '/ttp_files/';

var d = document, TTablePRO = [], NUM = 'NUM', STR = 'STR',DATE='DATE',CURRENCY='CURRENCY';

function TCastSTR(s_){return s_.replace(/<[^>]+>/ig,'')+'';}
// accept first valid number in string as sort id and accept ','  as well as '.'
function TCastNUM(s_){return s_.replace(/^[^0-9\-\,]*/g, '').replace(/[\.]/g, '').replace(/,/g,'.')*1;}

function TCastDATE(s_,s_format){
if(s_format) return set_date_by_format(s_format,stripHTML(s_));
else return set_date_by_format('Y-m-d',stripHTML(s_));
}
// Basically same an NUM
function TCastCURRENCY(s_){return TCastNUM(s_)}

// Added by EvL for exporting to newly opened window and writing content as text
// Use this one for exporting tables with a selection box in the first column only.
// The standard export uses Ttable_export, defined below this function
function TTP_export(id,sTitle,nSkip) {with (TTablePRO[id]){
	var s,i,j,c,w=window.open("about:blank", "exportwindow");
	var d = w.document.open("text/html", "replace")
	var v,r
	var sMsg, pct, d1, d0 = new Date(), sBut;
	s = "";
	sTS = get_date_by_format(d0,"Ymd_His");
	sMsg = "";

	r = new RegExp("<[/a-zA-Z][^>]*>","g");
	sBut = '<input type=button value="Kopiëer naar klembord" onclick="window.clipboardData.setData(\'Text\',this.form.data.value);alert(\'Opgeslagen in klembord\');"> '+
		   '<input type=button value="Venster sluiten" onclick="window.close();">';
	d.write('<html><head><title>Export Table</title><link href="/css/pkl.css" REL="stylesheet" TYPE="text/css"></head><body><form method=post action=TableExport.asp>');
	d.write('<h3>'+sTitle+'</h3><table>');
	d.write('<tr><td><textarea name=data rows=20 cols=100>');
	// header
	for(j=nSkip;j<a_capt.length;j++) {
	  s += a_capt[j].name.replace(r,"")+((j==a_capt.length-1)?"\r\n":"\t");
	}
	// find the values selected
	v = ','+TTPGetSelection()+',';
	// xx.yy;
	for(i=0;i<a_f_body.length;i++) {
		if(v.indexOf(','+a_body[i][n_key]+',') >= 0) {
			for(j=nSkip;j<a_capt.length;j++) {
				s += a_body[i][j].replace(r,"")+((j==a_capt.length-1)?"\r\n":"\t");
				if ((i%100)==0) {
				  d.write(stripHTML(s));
				  s = "";
				  d1 = new Date();
				  if ((d1.getTime() - d0.getTime()) > 10000) {
					pct = Math.round((i/a_f_body.length)*100);
				    if(! w.confirm('Dit lijkt erg lang te duren(' + pct + '% gereed). Doorgaan?')) {
				      i = rows;
				      d.write(stripHTML(s));
				      s = "******* AFGEBROKEN *******";
				      sMsg = "Export afgebroken";
				    } else {
				      d0 = new Date();
				    }
				  }
				}
			}
		}
	}
	d.write(stripHTML(s));
	d.write('</textarea></td></tr>')
	d.write('<tr><th align=left>Bestandsnaam: <input type=text size=40 name=Naam value="'+sTitle+'_'+sTS+'.txt"> <input type=submit name=actie value="Opslaan als"></th></tr>');
	d.write('<tr><th>'+sBut+'</th></tr>');
	if(sMsg!="") w.alert(sMsg);
	d.close();
}}

function stripHTML(sVar){ 
	var	Re= /<\S[^>]*>/g; 
	sVar = sVar.replace(Re,""); 
	sVar = sVar.replace("&amp;", "&");
	sVar = sVar.replace("&gt;", ">");
	sVar = sVar.replace("&lt;", "<");
	sVar = sVar.replace("&euro;", "€");
	return sVar; 
} 

// Added by EvL for exporting to newly opened window and writing content as text
function Ttable_export(id,sTitle,iType) {with (TTablePRO[id]){
	var s,i,j,w,d,a,ix=0;
	a = new Array(a_f_body.length+1);
	s = "";
	// header
	for(j=0;j<a_capt.length;j++) {
      s += a_capt[j].name+((j==a_capt.length-1)?"\r\n":"\t");
	}
	a[ix++]= stripHTML(s);
	s = "";
	// data
	for(i=0;i<a_f_body.length;i++) {
		for(j=0;j<a_capt.length;j++) {
		  s += a_f_body[i][j].replace("&nbsp;","")+((j==a_capt.length-1)?"\r\n":"\t");
		}
		a[ix++]= stripHTML(s);
		s = "";
	}
	switch(iType) {
	case 1:
		window.clipboardData.setData('Text',a.join(""));
		alert('Opgeslagen in klembord');
		break;
	case 2:
	case 4:
		var w=window.open("about:blank", "exportwindow");
		var d = w.document;//.open("text/html", "replace");
	
		d.write('<html><head><title>Export Table</title><link href="/css/pkl.css" REL="stylesheet" TYPE="text/css">');
		d.write('<script language="javascript">\nfunction f()\n{\nf=document.forms[0];\nf.submit();\n}\n</script>\n');
		d.write('</head><body><form method=post action=TableExport.asp>');
//		d.write('</head><body><form method=post '+((iType==2)?'':'target=_blank ')+'action=TableExport.asp>');
		d.write('<input type=hidden name=srt value="'+((iType==2)?'text':'excel')+'">');
		d.write('<input type=hidden name=naam value="'+sTitle.replace(" ","_")+'">');
		d.write('<h3>'+sTitle+'</h3>');
		d.write('Let op! wordt de download geblokkeerd en krijg je een gele balk van Internet Explorer ? Klik dan op de knop \'Opnieuw Exporteren\'<P>');
		d.write('<div style="visibility:hidden"><textarea name=data rows=1 cols=20>'+a.join("")+'</textarea></div>');
		d.write('<input type=button onclick="window.close();" value=Sluiten>');
		d.write('<input type=submit value="Opnieuw Exporteren">');
		d.write('</form>');
		d.write('<script language="javascript">\nf();\n</script>');
		d.write('</body></html>');
		break;
	default:
		alert('Interne fout. Onbekend export type ('+iType+')');
	}
	return false;
}}

function TTPGetSelection() {
var i,j,frm,s = ''
	for(i=0;i<document.forms.length;i++){
		frm=document.forms[i];
		for(j=0;j<frm.length;j++){
			if(frm[j].name=='TTPSel') {
				if(frm[j].checked){
					if(s != '') s+=',';
					s+=frm[j].value;
				}
			}
		}
	}
	return s;
}
function TTPToggleAll(bOnOf) {
	// find form with TTPSEL field to adjust value
	for(i=0;i<document.forms.length;i++){
		frm=document.forms[i];
		for(j=0;j<frm.length;j++){
			if(frm[j].name == 'TTPSel') frm[j].checked=bOnOf;
		}
	}
	return false;
//	tt.tt;
}
function TQSRT(l, h) {
	var x = this.a_cont_d[(h + l) >> 1][this.o_sort.n_col], i = l, j = h, t = [];
	do {
		while (i <= h && (this.a_cont_d[i][this.o_sort.n_col]< x)) i ++;
		while (j >= l && (x< this.a_cont_d[j][this.o_sort.n_col])) j --;
		if (i <= j) {
			t = this.a_body[i];
			this.a_body[i] = this.a_body[j];
			this.a_body[j] = t;
			t = this.a_cont_d[i];
			this.a_cont_d[i] = this.a_cont_d[j];
			this.a_cont_d[j] = t;
			i++;
			j--
		}
	} while (i < j);
	if (l < j) this.exeQSRT(l, j);
	if (i < h) this.exeQSRT(i, h);
}

function TTable(a_capt, a_cont, o_look) {
	var c = 0, filters = ['substring','match','regexp'], i, 
	a_css = ['main', 'captCell', 'captText', 'head', 'foot', 'pagnCell', 'pagnText', 'pagnPict', 'filtCell', 'filtPatt', 'filtSelc'];
	this.a_capt = []; this.a_head = []; this.a_body = [];this.a_sel_capt=[], this.a_foot =		[];this.a_cont_d=[];
	this.a_part = ['Capt()', 'HdFt("a_head")', 'Body()', 'HdFt("a_foot")', 'Page()', 'Filt()'];
	this.id = TTablePRO.length;
	TTablePRO[this.id] = this;
	this.n_fcol = null;
	this.s_fpat = '';
	this.s_light = '';
	this.s_click = '';
	this.a_index=[];
	this.a_index_d=[];
	this.a_marked=[];
	this.buildCapt = TTPBuildCapt;
	this.buildHdFt = TTPBuildFreeze;
	this.buildBody = TTPBuildBody;
	this.buildPage = TTPBuildPage;
	this.buildFilt = TTPBuildFilt;
	this.build     = TTPBuild;
	this.f_show      = TTPShow;
	this.buildCell = TTPBuildCell;
	this.exeSort   = TTPExeSort;
	this.exeQSRT   = TQSRT;
	this.exePage   = TTPExePage;
	this.exeFilt   = TTPExeFilt;
	this.callFilt  = TTPCallFilt;
	this.callSort  = TTPCallSort;
	this.go = TTPGo;
	this.o_css = [];
	this.s_filthtml='';
	this.b_date=false;
	this.f_onclick = o_look.onclick;
	this.n_key = o_look.key || 0;
	with (o_look) {
			for (i in a_css) 
				this.o_css[a_css[i]] = css != null && css[a_css[i]] ? ' class="' + css[a_css[i]] + '"' : '';
			this.o_css.body = [];
			this.n_padding = params[0] || 0;
			this.n_spacing = params[1] || 0;
			this.o_color = colors || {};
			this.a_struc = structure;
			if (!paging) var paging = [];
			this.o_paging = {
			's_pf' : paging.pf || '&laquo;&laquo;',
			's_pp' : paging.pp || '&laquo;',
			's_pn' : paging.pn || '&raquo;',
			's_pl' : paging.pl || '&raquo;&raquo;',
				's_tt' : paging.tt || '&nbsp;',
				'b_sh' : paging.sh,
				'n' : paging.by && paging.by > 0 ? paging.by : a_cont.length,
				'c' : 0
			};
			if (!sorting) var sorting = [];
			this.o_sort = {
			's_as' : sorting.as || '',
			's_ds' : sorting.ds || '',
			's_no' : sorting.no || '',
				'n_col' : sorting.cl,
				'b_ord' : sorting.or
			};
			this.btn_ok=filter.btn_ok;
			this.btn_no=filter.btn_no;
			this.exp_to = exp_to.type;
			this.exp_name = exp_to.name;
			this.btn_exp_text = exp_to.btn_exp_text;
			this.btn_exp_excel = exp_to.btn_exp_excel;
			this.btn_exp_clip = exp_to.btn_exp_clip;
			this.a_filters = filters;
			this.n_filters = 3;
			for (i = filters.length-1; i >=0; i--){
					if (!((1 << i) & filter.type)) {
						this.a_filters[i] = 0; 
						this.n_filters --;
					}
					else this.n_ftype = i;
			}
		var n_head = freeze[0] * 1, n_foot = freeze[1] * 1;
	}
	this.b_markmulty = o_look.multy_mark;
	this.n_rows = a_cont.length - n_head - n_foot;
	this.n_cols = a_capt.length;
	for (var i in a_capt) {
		this.o_css.body[i] = !o_look.css || !o_look.css.body ? '' : 
			typeof(o_look.css.body) != 'object' ? o_look.css.body : 
				o_look.css.body[i] ? ' class="' + o_look.css.body[i] + '"' : '';
		this.a_sel_capt[i]={'name': a_capt[i].name.replace(/<[^>]+>/ig,'')};
		this.a_capt[i] = {
			'name' : a_capt[i].name,
			'type' : a_capt[i].type,
			'b_hide' : a_capt[i].hide,
			'cast' : typeof(window['TCast'+a_capt[i].type]) == 'function' 
				? window['TCast'+a_capt[i].type] 
				: function (s_) {return s_},
			'f_css' : typeof(a_capt[i].format) == 'function' 
				? a_capt[i].format 
				: function () {return null}
		};
		if(a_capt[i].type==DATE){
			if(!this.b_date){
				this.getparseDate = get_date_by_format;
				this.b_date=true;
			};
			this.a_capt[i].format_input = a_capt[i].format_input || '';
			this.a_capt[i].format_output = a_capt[i].format_output || '';
		}
	}
	for (i = 0; i < a_cont.length; i ++) for (j = 0; j < a_cont[0].length; j ++) 
		if (a_cont[i][j] + '' == ''||a_cont[i][j]==null) a_cont[i][j] = '&nbsp;';
	for (i = 0; i < n_head; i ++) this.a_head[i] = a_cont[c++];
	for (i = 0; i < this.n_rows; i ++){
		this.a_cont_d[i]=[];
		for (var _j = 0; _j < this.n_cols; _j ++) {
			var s_ = a_cont[i][_j]+'';
			if(this.a_capt[_j]['type']==DATE){
				this.a_cont_d[i][_j]= this.a_capt[_j].cast(s_,this.a_capt[_j].format_input);
				a_cont[i][_j] = this.getparseDate(this.a_cont_d[i][_j],this.a_capt[_j].format_output);
			}
			else this.a_cont_d[i][_j]= this.a_capt[_j].cast(s_);
		}
		this.a_body[i] = a_cont[c++];
	}
	for (i = 0; i < n_foot; i ++) this.a_foot[i] = a_cont[c++];
	this.go();
}

function TTPBuildCapt() {
	var a_ = ['<tr>'], i, b_order, s_img, n_cnt = 1;
	for (i in this.a_capt) if (!this.a_capt[i].b_hide)
		if (this.a_capt[i].type) {
			b_order = Boolean(this.o_sort.n_col == i && this.o_sort.b_ord != 1);
			s_img = this.o_sort[this.o_sort.n_col != i ? 's_no' : this.o_sort.b_ord ? 's_ds' : 's_as'];
			s_lnk = 'TTablePRO[' + this.id + '].exeSort(' + i + ',' + b_order + ')';
			a_[n_cnt++] = this.buildCell(['<table cellpadding="0" cellspacing="0" border="0" width="100%"><tr valign="middle">', '<td width="99%"', this.o_css.captText, '><a title="Sorteer deze kolom" href="javascript:', s_lnk, '"', this.o_css.captText, '>', this.a_capt[i].name, '</a></td><td><a href="javascript:', s_lnk, '">', s_img, '</a></td></tr></table>'].join(''), this.o_css.captCell);
		}
		else a_[n_cnt++] = this.buildCell(['<table cellpadding="0" width="100%" cellspacing="0" border="0"><tr valign="middle"><td width="99%"', this.o_css.captText, '>', this.a_capt[i].name, '</td></tr></table>'].join(''), this.o_css.captCell);
	a_[n_cnt++] = '</tr>';
  return a_.join('');
}

function TTPBuildFilt() {
	if (this.s_filthtml) 
		return this.s_filthtml;
	if (this.n_filters==0&&this.exp_to==0) return '';
	var a_ = ['<tr><td ', this.o_css.filtCell, ' colspan="', this.n_cols, '" nowrap><table width="100%"><form name="TTForm' + this.id + '" onsubmit="return TTablePRO[', this.id, '].exeFilt(\'1\');"><tr>'], n_cnt = 7;
	if(this.exp_to>0) {
		a_[n_cnt++] = '<td align=left nowrap>';
		if(this.exp_to&1) { // clipboard
			a_[n_cnt++] = '<a href="#" onclick="javascript:Ttable_export(' + this.id + ',\''+this.exp_name.replace("'","\\'")+'\',1)">'+this.btn_exp_clip+'</a>&nbsp;';
		}
		if(this.exp_to&2) { // text
			a_[n_cnt++] = '<a href="#" onclick="javascript:Ttable_export(' + this.id + ',\''+this.exp_name.replace("'","\\'")+'\',2)">'+this.btn_exp_text+'</a>&nbsp;';
		}
		if(this.exp_to&4) { // excel
			a_[n_cnt++] = '<a href="#" onclick="javascript:Ttable_export(' + this.id + ',\''+this.exp_name.replace("'","\\'")+'\',4)">'+this.btn_exp_excel+'</a>';
		a_[n_cnt++] = '</td>';
		}
	}
	if(this.n_filters > 0) {
		a_[n_cnt++]='<td align=right nowrap>Filter <input'+this.o_css.filtPatt+ ' type="text" name="filtPatt" size="10'+ (this.s_fpat ? '" value="' + this.s_fpat : '')+ '"> als ';
		if (this.n_filters > 1) {
			a_[n_cnt++] = '<select' + this.o_css.filtSelc + ' name="filtType">';
			for (var i = 0; i < this.a_filters.length; i++) if (this.a_filters[i]) 
				a_[n_cnt++] = ['<option value=', i, (i == this.n_ftype*1 ? ' selected' : ''), ">", this.a_filters[i]].join('');
			a_[n_cnt++] = "</select>";
		}
		else a_[n_cnt++] = this.a_filters[0];
		a_[n_cnt++] = ' voor kolom <select' + this.o_css.filtSelc + ' name="filtCol">';
		for (var i in this.a_capt) if (!this.a_capt[i].b_hide)
			a_[n_cnt++] = '<option value=' + i + (this.n_fcol*1 == i ? ' selected' : '') + ">" + this.a_sel_capt[i].name;
		a_[n_cnt++] = '</select><a href="#" onclick="javascript:TTablePRO[' + this.id + '].exeFilt(\'1\')">'+this.btn_ok+'</a> <a href="#" onclick="javascript:TTablePRO[' + this.id + '].exeFilt()">'+this.btn_no+'</a>';
		a_[n_cnt++] = '</td>';
		//xx.xx;
	}
	a_[n_cnt++] = '</tr></form></table></td></tr>';
	this.s_filthtml=a_.join('');
	return this.s_filthtml;
}
	
function TTPBuildPage() {
	var n_a = Math.ceil(this.n_rows / this.o_paging.n) - 1;
	if (n_a <= 0 && !this.o_paging.b_sh) return '';
	var a_ = ['<tr><td colspan=', this.n_cols, this.o_css.pagnCell, '><table cellspacing="0" cellpadding="0" border="0" width="100%"><tr>'], n_cnt = 4;
	this.o_paging.c = this.o_paging.c * 1;
	var s_ = '<td nowrap><a' + this.o_css.pagnPict + ' href="javascript:TTablePRO[' + this.id + '].exePage(';
	if (this.o_paging.c > 0) {
		if (this.o_paging.s_pf) a_[n_cnt ++] = s_ + '0)" title="Eerste pagina">' + this.o_paging.s_pf + '</a></td>';
		if (this.o_paging.s_pp) a_[n_cnt ++] = s_ + (this.o_paging.c - 1) + ')" title="Vorige pagina">' + this.o_paging.s_pp + '</a></td>';
	}
	a_[n_cnt++] = '<td width="99%"' + this.o_css.pagnText + '>' + this.o_paging.s_tt.replace('%ind', this.o_paging.c+1).replace('%pgs', n_a+1).replace('%rcs', this.n_rows) + '</td>';
	if (this.o_paging.c < n_a) {
		if (this.o_paging.s_pn) a_[n_cnt ++] = s_ + (this.o_paging.c + 1) + ')" title="Volgende pagina">' + this.o_paging.s_pn + '</a></td>';
		if (this.o_paging.s_pl) a_[n_cnt ++] = s_ + n_a + ')" title="Laatste pagina">' + this.o_paging.s_pl + '</a></td>';
	}
	a_[n_cnt++] = '</tr></table></td></tr>';
	return a_.join('');
}

function TTPBuildCell(value, s_CSS, s_add) {
	return '<td' + s_CSS + (s_add ? ' ' + s_add : '') + '>' + value + '</td>';
}
	
function TTPBuildFreeze(s_what) {
	var s_ = '', i, j;
	for (i = 0; i < this[s_what]; i++) {
		s_ += '<tr>';
		for (j in this.a_capt) if (!this.a_capt[j].b_hide) 
			s_ += this.buildCell(this[s_what][i][j], this.o_css[s_what]);
		s_ += '</tr>';
	}
	return s_;
}

function TTPBuild () {
	var i, s_ = "<table cellpadding=" + this.n_padding + " cellspacing=" + this.n_spacing + this.o_css.main + " width=100% border=0>";
	for (i in this.a_struc) {
		if(typeof(bNoFormTable)=="undefined"  && this.a_part[this.a_struc[i]] == 'Body()') s_ += '<form name=TTPBody'+this.id+'>';
		s_ += eval('this.build' + this.a_part[this.a_struc[i]]);
		if(typeof(bNoFormTable)=="undefined" && this.a_part[this.a_struc[i]] == 'Body()') s_ += '</form>';
	}
	s_ += "</table>";
	return s_ ;
}


function TTPExeFilt(i_) {
	if (i_) {
		var e_form=d.forms['TTForm'+this.id];
		this.n_fcol = !e_form.filtCol 
			? 0
			: e_form.filtCol.type.indexOf('select-') > -1
				? e_form.filtCol.options[e_form.filtCol.selectedIndex].value
				: e_form.filtCol.value;
		this.s_fpat = e_form.filtPatt.value;
		this.n_ftype = e_form.filtType && e_form.filtType.type.indexOf('select-') > -1
			? e_form.filtType.options[e_form.filtType.selectedIndex].value
			: this.n_ftype;
	}
	else {
		this.n_fcol = null;
		this.s_fpat = '';
	}
	this.o_paging.c = 0;
	this.f_show();
	return false;
}

function TTPExePage(n) {
	this.o_paging.c = n; 
	this.f_show();
}

function TTPExeSort(n_col, n_ord) {
	var n_col_old = this.o_sort.n_col;
	this.o_sort.n_col = n_col;
	this.o_sort.b_ord = n_ord;
	this.o_paging.c = 0;
	this.f_show(true, n_col_old);
}

function TTPCallSort(n_col_old) {
	var i,j=0;
	if (n_col_old == this.o_sort.n_col){
		this.a_body = this.a_body.reverse();
		this.a_cont_d = this.a_cont_d.reverse();
	}
	else {
		if(!this.a_index[this.o_sort.n_col]){
			this.exeQSRT(0, this.a_cont_d.length - 1);
			this.a_index[this.o_sort.n_col]=[];
			this.a_index_d[this.o_sort.n_col]=[];
			for (i = 0; i < this.a_body.length; i ++){
				this.a_index[this.o_sort.n_col][j]=this.a_body[i];
				this.a_index_d[this.o_sort.n_col][j++]=this.a_cont_d[i];
			}
			this.a_index[this.o_sort.n_col]['b_ord']=this.o_sort.b_ord;
		}else{
			if (this.a_index[this.o_sort.n_col]['b_ord']!=this.o_sort.b_ord){
				this.a_body = this.a_index[this.o_sort.n_col].reverse();
				this.a_cont_d = this.a_index_d[this.o_sort.n_col].reverse();
			}else{
				this.a_body = this.a_index[this.o_sort.n_col];
				this.a_cont_d = this.a_index_d[this.o_sort.n_col];
			}
		}
		if (this.o_sort.b_ord){
			this.a_body = this.a_body.reverse();
			this.a_cont_d = this.a_cont_d.reverse();
		}
	}
}

function TTPCallFilt() {
	if (!this.s_fpat || this.n_fcol == null) return this.a_body;
	var a_body = [];
	if (this.n_ftype == 2) var r_ = eval('/' + this.s_fpat + '/i');
	for (i = 0; i < this.a_body.length; i ++)
		if (
				(this.n_ftype == 2 && (''+this.a_cont_d[i][this.n_fcol]).search(r_) > -1) ||
				(this.n_ftype == 1 && (''+this.a_cont_d[i][this.n_fcol]).toLowerCase() == this.s_fpat.toLowerCase()) ||
				(this.n_ftype == 0 && (''+this.a_cont_d[i][this.n_fcol]).toLowerCase().indexOf(this.s_fpat.toLowerCase()) > -1)
			)	a_body[a_body.length] = this.a_body[i];
	return a_body;
}

d.write('<SC','RIPT LANGUAGE="JavaScript" src="', path_to_files, 'dt_parser.js"></SCR','IPT>');
d.write('<SC','RIPT LANGUAGE="JavaScript" src="', path_to_files, 'table.do.', d.implementation||d.styleSheets ? 'ok' : 'no', '.js"></SCR','IPT>')
