﻿/***********************  Svenska Verb Programm functions ***********************/function initiateSearch(inputObj, words, noinit){	if(inputObj){		var w = words;		this.el = inputObj;		if(words.length >0){			this.el.value = words;			this.el.className = 'def'			var el = this.el;						if(typeof noinit == 'undefined' || !noinit){				addEvent(this.el, "focus", function(){if(el.value == w){el.value = ""; el.className = ''; }} );				addEvent(this.el, "blur", function(){if(el.value == ""){el.value = w; el.className = 'def';}} );								}		}	}}function switch_lang(l){ }/* cleaning up the array from empty elements */v = JSONverbs;function default_settings(){		v = JSONverbs;		initiateSearch(byId('search'), _v['search'], true);	byId('limit').value = 10;	byId('_g_1').checked = true;	byId('_g_2a').checked = true;	byId('_g_2b').checked = true;	byId('_g_3').checked = true;	byId('_g_4').checked = true;	byId('_g_4o').checked = true;	byId('_sfi_c').checked = true;	byId('_sfi_d').checked = true;	byId('_sas_gr').checked = true;		byId('field_show_group').checked = true;	byId('field_show_infinitiv').checked = true;	byId('field_show_imperativ').checked = false;	byId('field_show_presens').checked = true;		//byId('_l_1').checked = false;		byId('field_show_russian').checked = (_v['active_lang'] != 'en')? true : false;		byId('field_show_english').checked = (_v['active_lang'] != 'ru')? true : false;		var radio=document.getElementsByName("field_sel");	radio[0].checked = true;	//for(i in els){	//	if(els[i].checked){				//		this.mainSelected = els[i].value;	//		break;	//	}	//}		shuffle(v);	build_table();	start = 0;}function _search(){	start = 0;	var str = byId('search').value.toLowerCase();	var pattern = new RegExp("^"+str);	var _s = JSONverbs;	if(str.length > 0){		v = [];		for(var i = 0, l = _s.length; i < l; i++){			//making it read multi-values			var infArr = _s[i].infinitiv.toLowerCase().split(/~/);						for(var k=0,m=infArr.length;k<m;k++){				if(pattern.test(infArr[k])){  									v[v.length] = _s[i];					break;				}			}								//if(pattern.test(_s[i].infinitiv.toLowerCase())){  			//	v[v.length] = _s[i];			//}		}						//alert(i + " t:" +_s.length + " "+_s[1]);				//v = clean_up_array(_s);				}else{v = _s;}	build_table();	byId('search').focus();}function _groups(){	start = 0;	var _verbs = JSONverbs;		var arr = [];	var _gr = [];		var _lvl = [];			_gr[0] = byId('_g_1');	_gr[1] = byId('_g_2a');	_gr[2] = byId('_g_2b');	_gr[3] = byId('_g_3');	_gr[4] = byId('_g_4');	_gr[5] = byId('_g_4o');	_lvl['c'] = byId('_sfi_c');	_lvl['d'] = byId('_sfi_d');		_lvl['sasgr'] = byId('_sas_gr');		_lvl['sasa'] = byId('_sas_a');		_lvl['sasa'] = byId('_sas_b');		this.valInArrays = function(val, arr){				for(x in arr){			if(typeof arr[x] != 'function' && arr[x].checked && val == arr[x].value){				return true			}		}		return false;	}		for(var i = 0, l=_verbs.length; i < l; i++){				//alert()		if(this.valInArrays(_verbs[i].level, _lvl) && this.valInArrays(_verbs[i].group, _gr)){ arr.push(_verbs[i]); }	}		v = clean_up_array(arr,0);	build_table();		var txt = [];	if(_lvl['c'].checked == true){txt[txt.length] = 'SFI C';}	if(_lvl['d'].checked == true){txt[txt.length] = 'SFI D';}	if(_lvl['sasgr'].checked == true){txt[txt.length] = 'SAS Grundläggande';}	//if(_lvl['sasa'].checked == true){txt[txt.length] = 'SAS A';}	//if(_lvl['sasb'].checked == true){txt[txt.length] = 'SAS B';}		if(txt.length < 1){txt[0] = _v['not chosen'];}	var h3 = byId('verb_sel');	byId('_sel_lvl').innerHTML = txt.join(', ')+', ';		txt = [];	if(_gr[0].checked == true){txt[txt.length] = '1';}	if(_gr[1].checked == true){txt[txt.length] = '2a';}	if(_gr[2].checked == true){txt[txt.length] = '2b';}	if(_gr[3].checked == true){txt[txt.length] = '3';}	if(_gr[4].checked == true){txt[txt.length] = '4 starka';}	if(_gr[5].checked == true){txt[txt.length] = '4 oregelbundna';}	if(txt.length < 1){txt[0] = _v['not chosen'];}	byId('_sel_gr').innerHTML = txt.join(', ');}var v;var tries = 0;var correct = 0;function check(el, isLastPage){	if(typeof isLastPage == 'undefined') isLastPage = true;	//var el = byId('_val'+i+'_'+p);	var td = el.parentNode;	var id = el.id.substr(4);	id = id.split("_");	//alert(id);		var ans = v[id[0]][id[1]];		var changeFocus = true;		var elValue = trim(el.value);		if(elValue.length > 0){				var v_variats = ans.split('~');		var _correct = 0;		for(var k = 0, l=v_variats.length; k < l; k++){			if(elValue.toLowerCase() == v_variats[k].toLowerCase()){ _correct = 1; break;}		}				if(_correct==1){			//var pattern = new RegExp("<[input|INPUT]([^\n]+)>$");							//td.innerHTML = td.innerHTML.replace(pattern, '<strong>'+el.value+'</strong>');			el.disabled = true;									var re = 			td.innerHTML = elValue;			td.className = td.className.replace(new RegExp("\\bfel\\b") , "");														td.className+=' correct';						correct++;			//re-initiate enter-key result			if(!isLastPage) enter(byId('form'));		}		else{			td.className = td.className.replace(new RegExp("\\bcorrect\\b") , "");											td.className = td.className.replace(new RegExp("\\bfel\\b") , "");											td.className+=' fel';			changeFocus = false;					}		tries++;		show_results();		}			return changeFocus;}function show_results(){			byId('result').innerHTML = (tries > 0)? Math.round(correct*100/tries)+'%' : '0%';			byId('_res').childNodes[0].style.width = (tries > 0)? Math.round(correct*100/tries)+'px' : 0;		byId('tries').innerHTML = tries;		byId('correct').innerHTML = correct;}function flush_res(){	correct = '0';	tries = '0';		show_results();}var start = 0;function build_table(){		var limit = parseInt(byId('limit').value);	if(isNaN(limit) || limit <= 0){limit = 10;}	//limit = 2	//start = 5	var p = Math.round(v.length/limit);	//if(p < 0){p = 1;}	if(p*limit > v.length){		var last_limit = v.length - ((p-1)*limit);	}else if(p*limit < v.length){		var last_limit = v.length - ((p-1)*limit);		p+=1;	}else if(p*limit == v.length){var last_limit = limit;}		var num = start/limit;		switch_page(limit,start,num, p, last_limit);		var elems = byId('search_res').getElementsByTagName("strong");	replaceText( elems[1] , v.length);	replaceText(elems[2], p);	replaceText(elems[3], limit);		//dropD.changeTable();	//byId('pages_b').innerHTML = byId('pages').innerHTML.replace(/(id=["|']?\w+)(?=["|']?)/g,"$1_b");	//vTable.build(limit,start);			/*************TODO**************/	//to force to swich ( redirect? ) to verbs view if clicked on the buttons	//loadDiv('verbs');	/***************************/}//global array for pages link attributesvar pLinks = [];//starts the function from params[0] - other attributes are stored in param[1] and so on;function doFunction(params) {	call = "params[0](";	for(i = 1, l=params.length-1; i < l; i++) {		call += "params["+i+"],";	}	call += "params["+(params.length-1)+"]);";		return eval(call);}function createPageLink(l_func, l_id, l_limit, l_i, l_p, l_act, l_txt, l_class){	var a = document.createElement('a');		a.href = "javascript:void(0);";		a.id = l_id;		a.className = l_class;		if(l_class.indexOf('active') < 0 ){			pLinks[l_id] = [l_func, l_limit, l_limit*l_i, l_i, l_p];			if(l_class.indexOf('pass') < 0 ){				a.onclick = function(evt){ 					var id = this.id;					//var attr = pLinks[id];							doFunction(pLinks[id]);										//switch_page(attr[0],attr[1],attr[2],attr[3]);					};			}		}	var txt = document.createTextNode(l_txt);	a.appendChild(txt);	return a;}function switch_page(limit,_start,act,p){	//how many links before and after active page	var n = 7;	//before	var less = (act <= n)? act : n;	//after	var more = ((p-(act+1)) < n)? p-(act+1) : n;	less+= (more < n)? n-more : 0;	more+= (less < n)? n-less : 0;		//cleaning doubled content from parent	var pages = byId('pages');	var pages_b = byId('pages_b');	clearText(pages);	clearText(pages_b);		//link holders	var span = document.createElement("span");	span.className = "p";	var span2 = document.createElement("span");	span2.className = "p";		//prev page	var cl = (act > 0)? '' : 'pass';	span.appendChild( createPageLink(switch_page, "prev", limit, act-1, p, act,_v['previous'],cl+" servL") );	span2.appendChild( createPageLink(switch_page, "prev_b", limit, act-1, p, act,_v['previous'],cl+" servL") );		//creating page links	for(var i = 0; i < p; i++){		if(i >= (act-less) && i <= more+act){			cl = (i==act)? "active" : '';			span.appendChild( createPageLink(switch_page, "p_"+(i+1), limit, i, p, act,(i+1), cl) );						span2.appendChild( createPageLink(switch_page, "p_"+(i+1)+"_b", limit, i, p, act,(i+1), cl) );					}	}	//next page	cl = (act < p-1)? '' : 'pass';	span.appendChild( createPageLink(switch_page, "next", limit, act+1, p, act,_v['next'],cl+" servR") );	span2.appendChild( createPageLink(switch_page, "next_b", limit, act+1, p, act,_v['next'],cl+" servR") );				pages.appendChild(span);	pages_b.appendChild(span2);	vTable.build(limit,_start);		enter(byId('form'));	start = _start;}function help(ans){	var txt = _v['hint'] + ":\t\t";	txt+= ans.replace(/(~)/g,' '+_v['or']+' ').toUpperCase();	txt+="\n\n\n"+"("+_v['minus one point']+")";	alert(txt);	tries++;	show_results();}function objTable(){	this.init = function(){		this.fieldSet = [];		this.reInitiatefieldSet();				this.div = byId('form');					this.table = document.createElement('table');		this.table.id = 'verbs_table';		this.TRs = [];				this.allTypes = inputFieldTypes();		this.availableTypes = [];		}		this.reInitiatefieldSet = function(){											//	id		show/hide			value			training field or not                       always shown or not		this.fieldSet['russian'] = new Array( '_rus1', (byId('field_show_russian').checked),  byId('field_show_russian').value, false, false  );		this.fieldSet['english'] = new Array( '_eng1', (byId('field_show_english').checked),  byId('field_show_english').value, false, false );		this.fieldSet['group'] = new Array( '_g1', (byId('field_show_group').checked),  byId('field_show_group').value, false, false );		this.fieldSet['infinitiv'] = new Array( '_inf1', (byId('field_show_infinitiv').checked),  byId('field_show_infinitiv').value, true, false );		this.fieldSet['imperativ'] = new Array( '_imp1', (byId('field_show_imperativ').checked),  byId('field_show_imperativ').value, true, false );		this.fieldSet['presens'] = new Array( '_pres', true,  '', true, true );		this.fieldSet['preteritum'] = new Array( '_pret', true,  '', true, true );		this.fieldSet['supinum'] = new Array( '_sup', true,  '', true, true );	}		this.setAvailableTypes = function(){		this.availableTypes = [];		if(byId('field_show_infinitiv').checked) this.availableTypes['infinitiv'] = this.availableTypes[this.availableTypes.length] = new Array('infinitiv', byId('field_show_infinitiv'));		if(byId('field_show_imperativ').checked) this.availableTypes['imperativ'] = this.availableTypes[this.availableTypes.length] = new Array('imperativ', byId('field_show_imperativ'));		this.availableTypes['presens'] = this.availableTypes[this.availableTypes.length] = new Array('presens', byId('field_show_presens'));		this.availableTypes['preteritum'] = this.availableTypes[this.availableTypes.length] = new Array('preteritum', byId('field_show_preteritum'));		this.availableTypes['supinum'] = this.availableTypes[this.availableTypes.length] = new Array('supinum', byId('field_show_supinum'));	}		this.flush_table = function(){		for(var i=0, l=this.TRs.length; i<l; i++){			this.table.removeChild(this.TRs[i]);		}		this.TRs = [];		this.reInitiatefieldSet();		this.setAvailableTypes();	}		this.build = function(limit,start){		this.flush_table();				this.learningMode = true; //byId('_l_1').checked;				this.mainSelected = false;		var els=document.getElementsByName("field_sel");		for(var i=0,l=els.length;i<l;i++){			if(els[i].checked == true){							this.mainSelected = els[i].value;								break;			}		}						var tr = this.createElement('tr');		tr.appendChild( this.createElement('th', '№', '', '_nl', '') )				for(i in this.fieldSet){			if( typeof this.fieldSet[i] == "function" ){ continue; }			var elClass = (!this.fieldSet[i][1])? "hidden" : "";			elClass += (i == this.mainSelected) ? 'name' : '';			var txt = '<span class="btn">';									if(!this.fieldSet[i][4]){				txt+='<label for="field_show_'+i+'" class="col-close"><img src="'+home_url+'img/spacer.gif" alt="" title="" /></label>';			}						if(this.fieldSet[i][3]){				txt+='<label for="field_mode_'+i+'" class="col-mode"><img src="'+home_url+'img/spacer.gif" alt="" title="" /></label>';			}			txt+='<span class="lbl">'+_v['f_'+i]+'</span>';			txt+='</span>';			tr.appendChild( this.createElement('th', txt, elClass, this.fieldSet[i][0], i) )							}		this.TRs.push(tr);				//this.availableTypes = inputFieldTypesShown(this.TRs[0], this.allTypes);		var highLight = null;		var forceName = false;						for(var i=0; i<limit; i++){			var c = i+start;			if(typeof v[c] != 'undefined'){												var tr = this.createElement('tr');				tr.appendChild( this.createElement('td', (c+1), 'n') );								highLight = (this.mainSelected == 'random')? this.availableTypes[ Math.floor(Math.random()*this.availableTypes.length) ][0] : this.mainSelected;								for(_lType in this.fieldSet){					if( typeof this.fieldSet[_lType] == "function" ){ continue; }					var elClass = (!this.fieldSet[_lType][1])? "hidden" : "";										if(this.fieldSet[_lType][3]){						if(highLight == "learning"){							tr.appendChild( this.cell_learn(c,_lType,v[c][_lType],elClass) );													}else{							var val = trim(v[c][_lType]);														if(_lType == highLight || forceName){								if(val.length > 0){									tr.appendChild( this.cell_bold(c,_lType,v[c][_lType],elClass) );									forceName = false;								}else{									tr.appendChild( this.cell_txt(c,_lType,v[c][_lType],elClass) );										forceName = true;								}														}else{								if(val.length > 0){									tr.appendChild( this.cell_input(c,_lType,v[c][_lType],elClass) );								}else{									tr.appendChild( this.cell_txt(c,_lType,v[c][_lType],elClass) );									}														}						}					}else{											tr.appendChild( this.cell_txt(c,_lType,v[c][_lType],elClass) );											}				}				this.TRs.push(tr);			}		}						for(var i=0, l=this.TRs.length; i<l; i++){			this.table.appendChild(this.TRs[i]);		}				this.div.appendChild(this.table);						enter(byId('form'));			}				this.cell_learn = function(c, i, txt, elClass){		txt = ( txt.length > 0)? txt.replace(/(~)/g,' <small>'+_v['or']+'</small> ') : '-';				var content = txt.replace(/(~)/g,' <small>'+_v['or']+'</small> ');				return this.createElement('td', content, "learn "+elClass, '_td'+c+'_'+i, i) ; 	}		this.cell_input = function(c, i, txt, elClass){ 		var content = '<input type="text"  id="_val'+c+'_'+i+'" value=""  /> <span class="help" onclick="help(\''+txt+'\')"> </span>';		return this.createElement('td', content, "inp "+elClass, '_td'+c+'_'+i, i) ; 			}		this.cell_bold = function(c, i, txt, elClass){ 				if( isNaN(txt) ){			txt = ( txt.length > 0)? txt.replace(/(~)/g,' <small>'+_v['or']+'</small> ') : '-';				}		return this.createElement('td', txt, "name "+elClass, '_td'+c+'_'+i, i) ; 			}		this.cell_txt = function(c, i, txt, elClass){		txt = (typeof txt == "undefined")? "" : txt;		if( isNaN(txt) ){			if( txt.length > 0){				txt = txt.replace(/(;)/g,';<br/>');				txt = txt.replace(/(~)/g,' <small>'+_v['or']+'</small> ');			}else{				txt = '-';			}		}		return this.createElement('td', txt, "tdTXT "+elClass, '_td'+c+'_'+i, i) ; 	}				this.createElement = function(tag, content, css, id, type ){		var el = document.createElement(tag);		el.innerHTML = (content)? content : "";		el.className = (css)? css : "";		el.id = (id)? id : "";		el.setAttribute('type', type );		return el;			}	}function setFocus(parentNodeObj){	if(parentNodeObj){		var inputs=parentNodeObj.getElementsByTagName('input');		var re = new RegExp("\\bhidden\\b");		for(var i=0, l=inputs.length;i<l;i++){			var m = re.exec(inputs[i].parentNode.className);			if( m == null){				inputs[i].focus();								break;			}		}	}}function manageColumns(col){	var table = byId('verbs_table');		this.processColumn = function(arr, col){		for(var i=0,l=arr.length;i<l;i++){			if(arr[i].getAttribute('type') == col){				var re = new RegExp("\\bhidden\\b")				var m = re.exec(arr[i].className);				if (m == null) {					arr[i].className += " hidden";				}else{					arr[i].className = arr[i].className.replace(re , "");								}			}		}	}		if(table){			this.processColumn(table.getElementsByTagName('th'), col)		this.processColumn(table.getElementsByTagName('td'), col)				if(dropD.Active == col){			dropD.setActive(dropD.buttonEls[dropD.defaultActive], true);					}else{						dropD.changeTable();				}	}}function dropDown(){		this.ActiveEL = null;	this.Active = null;		this.defaultActive = "random";	this.allTypes = [];	this.availableTypes = [];							this.table = byId('verbs_table');	this.TRs  = [];	this.buttonEls = [];	var obj = this;			this.setActive = function(el, append){						//if(append){ alert(el);}		this.deActivate(this.ActiveEL, false);				this.Activate(el);	}		this.Activate = function(el, notable ){		notable = (typeof notable != 'underline' && notable)? true : false;		if(el){			el.checked = true;			el.className += " active";			this.ActiveEL = el;						if(!notable) this.changeTable();		}			}			this.changeTable = function(){		if(this.table){					if(this.TRs.length >0){				this.Active = this.ActiveEL.value;				this.availableTypes = [];				//alert('1');				var _lType = null;								for( var i=0, l = this.TRs[0].childNodes.length; i<l;i++){					_lType = this.TRs[0].childNodes[i].getAttribute('type')					var _cl = this.TRs[0].childNodes[i].className;					if(_lType == this.Active){						_cl = _cl.replace('hidden','');						if(_cl.indexOf('name') < 0){_cl+=" name";}					}else{						_cl = _cl.replace('name','');											}					this.TRs[0].childNodes[i].className = _cl;				}									this.availableTypes = inputFieldTypesShown(this.TRs[0], this.allTypes);												//var dump = byId('dump');				//dump.innerHTML = "";				for(x in this.availableTypes){					if(typeof this.availableTypes[x][1] == 'object'){						if(!this.availableTypes[x][1].checked){ 							this.availableTypes[x][1].checked = true; 								//alert(this.availableTypes[x][0]);						}						//dump.innerHTML+=x+". " + this.availableTypes[x][0]+"<br/>";					}				}				//dump.innerHTML+= "length = " + this.availableTypes.length+"<br/>";								var forceName = false;				var highLight = null				_lType = null								for(var i = 1, l=this.TRs.length;i<l;i++){					highLight = (this.Active == 'random')? this.availableTypes[ Math.floor(Math.random()*this.availableTypes.length) ][0] : this.Active;											for( var x=0, t = this.TRs[i].childNodes.length; x<t;x++){						var Cell = this.TRs[i].childNodes[x];						_lType = Cell.getAttribute('type');						if(typeof this.availableTypes[_lType] != 'undefined'){							var re = new RegExp(/(_td)(\d+)(_)/);							var m = re.exec(Cell.id);							if(m && v[m[2]] && typeof v[m[2]][_lType] != 'undefined'){														if(vTable){									Cell.className = Cell.className.replace('hidden','');									var newCell = null;									if(highLight == 'learning'){										newCell = vTable.cell_learn(m[2], _lType, v[m[2]][_lType], "");										this.TRs[i].replaceChild(newCell, Cell);										}else{										if(	_lType == highLight || forceName){											if(Cell.className.indexOf('name') < 0 || forceName){																								if( v[m[2]][_lType].length > 0 ){													newCell = vTable.cell_bold(m[2], _lType, v[m[2]][_lType], "");																								forceName = false;												}else{													newCell = vTable.cell_txt(m[2], _lType, v[m[2]][_lType], "");														forceName = true;																									}																								this.TRs[i].replaceChild(newCell, Cell);																										}										}else{											if(Cell.className.indexOf('name') > -1 || Cell.className.indexOf('learn') > -1){												if( v[m[2]][_lType].length > 0 ){													newCell = vTable.cell_input(m[2], _lType, v[m[2]][_lType], "");																							}else{													newCell = vTable.cell_txt(m[2], _lType, v[m[2]][_lType], "");																																			}																								this.TRs[i].replaceChild(newCell, Cell);																					}																						}									}									newCell = null;								}							}						}						Cell = null;					}					//break;				}						}			enter(byId('form'));		}			}			this.deActivate = function(el, append){			if(el){						el.className = el.className.replace(" active", "");					}	}				this.init = function(){			this.allTypes = inputFieldTypes();		 		if(this.table){						this.TRs  = this.table.getElementsByTagName('tr');				}				var els=document.getElementsByName("field_sel");		for(var i=0,l=els.length;i<l;i++){			addEvent(els[i],"click", function(){ obj.setActive(this); return false;} ) 						if(els[i].checked == true ){ 				this.defaultActive = els[i].value;				this.Activate(els[i], true);							}			else{ this.deActivate( els[i] , true); }			this.buttonEls[els[i].value] = els[i];		}	}		//initiate Object	this.init();}function inputFieldTypes(){	this.allTypes = [];	this.allTypes['infinitiv'] = new Array('infinitiv', byId('field_show_infinitiv'));		this.allTypes['imperativ'] =  new Array('imperativ', byId('field_show_imperativ'));	this.allTypes['presens'] =  new Array('presens', byId('field_show_presens'));	this.allTypes['preteritum'] =  new Array('preteritum', byId('field_show_preteritum'));	this.allTypes['supinum']= new Array('supinum', byId('field_show_supinum'));	return this.allTypes;}function inputFieldTypesShown(TR, allTypesArr){	this.availableTypes = []	if(TR){		for( var i=0, l = TR.childNodes.length; i<l;i++){			var _lType = TR.childNodes[i].getAttribute('type');			if(TR.childNodes[i].className.indexOf('hidden') < 0  && (typeof allTypesArr[_lType] != 'undefined') ){				this.availableTypes[this.availableTypes.length] =  allTypesArr[_lType];																this.availableTypes[_lType] =  allTypesArr[_lType];																		}		}	}	return this.availableTypes;}/*************************/var dropD = null;