function foo(href) {
	a = document.getElementById('rus').checked;   /*tomes=rus*/
	b = document.getElementById('bre').checked;   /*tomes=bre*/
	c = document.getElementById('bse').checked;   /*tomes=bse*/
	href = href.replace(/&tomes=[^&]+/g, "");
	if (a) href+='&tomes=rus';
	if (b) href+='&tomes=bre';
	if (c) href+='&tomes=bse';
	return href;
}



//preload image
var collimg = new Image();
collimg.src = "/style/collapse.gif";
var expimg = new Image();
collimg.src = "/style/expand.gif";

function ShowHideLayer(boxID) {
	/* Obtain reference for the selected boxID layer and its button */
	var box = document.getElementById("box"+boxID);
	var boxbtn = document.getElementById("btn"+boxID);
	
	//alert(cookie_name);
	
	/* If the selected box is currently invisible, show it */
	if(box.style.display == "none") {
		box.style.display = "block";
 		boxbtn.src = "/style/collapse.gif";
		createCookie("box"+boxID,'1',0)
	}
	/* otherwise hide it */
	else {
		box.style.display = "none";
		boxbtn.src = "/style/expand.gif";
		createCookie("box"+boxID,'1',-1)
	}
}



function ShowHideCover() {
	var box = document.getElementById("cover");
	var boxbtn = document.getElementById("coverimg");
	var tdbox= document.getElementById("tdcover");
	var tdrest= document.getElementById("tdresarticles");
	
	//alert('box.display='+box.style.display+', boxbtn.dispay='+boxbtn.style.display);

	if (box.style.display == "none" || boxbtn.style.display == "block") {
		box.style.display = "block";
		boxbtn.style.display = "none";
		tdbox.style.width = "20%";
		tdrest.style.width = "80%";
		createCookie('showCover','1',0)
	}
	else {
		box.style.display = "none";
		boxbtn.style.display = "block";
		tdbox.style.width = "0%";
		tdrest.style.width = "100%";
		createCookie('showCover','1',-1)
	}
}



function ChangeLang() {
	if (readCookie('lang') == 'en')
		createCookie('lang','ru',0)
	else
		createCookie('lang','en',0)
}


function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}




function addflabel(label) {
	if (document.forms.main != null) {
		document.forms.main.action="/view/view-cat?addflabel="+label;
		document.forms.main.submit();
	} else
	if (document.forms.ftsearch !=null) {
		document.forms.ftsearch.fcat.value=label;
		//document.forms.ftsearch.page.value=1;
		document.forms.ftsearch.submit();
	} 
}

function delflabel(label) {
	if (document.forms.main != null) {
		/* categories page */
		document.forms.main.action="/view/view-cat?delflabel="+label;
		document.forms.main.submit();
	} else
	if (document.forms.ftsearch !=null) {
		/* ftsearch page */
		document.forms.ftsearch.fcat.value='not_def';
		document.forms.ftsearch.submit();
	}  
}


function goToPage(page) {
	if (document.body.id == 'ai') {
		var ai = document.getElementById("active-index");
		var letter = ai.attributes.name.value;
		document.location.href=foo('/view/alph-index?letter='+letter+'&page='+page);
	} else
	if (document.body.id == 'fts') {
		document.forms.ftsearch.action="/view/ftsearch?page="+page;
		document.forms.ftsearch.submit();
	}
}


function gotocat(label) {
	document.forms.main.action="/view/view-cat?label="+label;
	document.forms.main.submit();
}

function compareVersions() {
	var cnt=0;
	var id1=-1;
	var id2=-1;
	
	for (i=0; i<document.forms.version.id.length; i++)
		if (document.forms.version.id[i].checked==true) {
			cnt++;
			if (id1==-1) id1=document.forms.version.id[i].id; else id2=document.forms.version.id[i].id;
		}
	if (cnt!=2)
		alert("Select two versions to compare")
	else {
		var groupId=document.getElementById('groupId').value;
	    document.location.href="/edit/diff/?id="+groupId+"&v1="+id2+"&v2="+id1;
	}
	
}


function checkNewEnc() {
	var box = document.getElementById("box1");
	var i=document.forms.newArticle.encid.selectedIndex;
	if (i==(document.forms.newArticle.encid.length-1)) {
		box.style.display = "block";		
	} else {
		box.style.display = "none";
	}
}


var q = new Array(13);


	q[0] =  "(: Retrieve summary and 'see also' links for the article 'User-generated content' (this query addresses article's content)  :)  \n\n"+
			"declare default element namespace \"http://www.mediawiki.org/xml/export-0.3/\";  \n\n"+
			"let $a := index-scan('article-by-title','User-generated content','EQ')\n"+
			"return \n"+
			"  <summary> \n"+
			"    <title>{$a/title}</title> \n"+
			"    <body>{($a/revision/text//p)[1]}</body> \n"+
			"    <seealso>{$a/revision/text/section[title='See also']//link}</seealso> \n"+
			"  </summary>";

	q[1] =  "(: Tennis players from Moscow (this query addresses articles' infoboxes) :)  \n\n"+
			"declare default element namespace \"http://www.mediawiki.org/xml/export-0.3/\";  \n\n"+
			"declare ordering unordered; \n\n" +
			"index-scan('article-by-cat','Category:Russian tennis players','EQ')[contains(.//template[@head=\"Infobox Tennis player\"]/field[@name=\"residence\"]/string(),\"Moscow\")]/title";
			

	q[2] = 	"(: Find redirects (i.e. synonyms) to a given article :) \n\n"+
			"declare default element namespace \"http://www.mediawiki.org/xml/export-0.3/\"; \n"+
			"declare ordering unordered; \n\n" +
			"index-scan('article-by-link','Ivan IV of Russia','EQ')[revision/text/redirect]/title/text()";

	q[3] =	"(: Retrieve the 'History' section of the 'NASA' article :)\n\n" +
			"declare default element namespace \"http://www.mediawiki.org/xml/export-0.3/\";\n\n" +
			"index-scan('article-by-title','NASA','EQ')/revision/text/section[title='History']";

	q[4] =	"(: Retrieve all links from a given article :)\n\n" +
			"declare default element namespace \"http://www.mediawiki.org/xml/export-0.3/\";\n\n" +
			"index-scan('article-by-title','Data mining','EQ')//link/@label/string(.)";

	q[5] = 	"(: Full text search by 'sedna xml' :) \n\n"+
			"declare default element namespace \"http://www.mediawiki.org/xml/export-0.3/\"; \n"+
			"declare ordering unordered; \n\n" +
			"ftindex-scan('fti','sedna xml')/title/text()";


	q[6] = 	"(: Retrieve all articles with 'Sedna' in title :) \n\n"+
			"declare default element namespace \"http://www.mediawiki.org/xml/export-0.3/\"; \n"+
			"declare ordering unordered; \n\n" +
			"ftindex-scan('fti','(title contains sedna)')/title/text()";


	q[7] = 	"(: Find Danish mathematicians :) \n\n"+
			"declare default element namespace \"http://www.mediawiki.org/xml/export-0.3/\"; \n"+
			"declare ordering unordered; \n\n" +
			"index-scan('article-by-cat','Category:Danish mathematicians','EQ')/title/text()";
		   
		   
	q[8] =	"(: Get all articles that referes to the 'Norway' article which belong to 'Computer pioneers' category :) \n\n"+
			"declare default element namespace \"http://www.mediawiki.org/xml/export-0.3/\";\n\n" +
			"(index-scan('article-by-link','Norway','EQ') \n"+
			"intersect \n"+
			"index-scan('article-by-cat','Category:Computer pioneers','EQ'))/title/text()";
		   

	q[9] =	"(: Summary of commercial text mining tools :)\n\n" +
			"declare default element namespace \"http://www.mediawiki.org/xml/export-0.3/\";\n" +
			"declare ordering unordered; \n\n" +
			"for $name in index-scan('article-by-title','Text mining','EQ')/revision/text//section[title=\"Software and applications\"]//link/@label\n"+
			"let $company := index-scan('article-by-title',$name,'EQ')\n"+
			"return \n"+
			"  <summary> \n"+
			"    <title>{$name/string()}</title> \n"+
			"    {$company/revision/text//p[1]} \n"+
			"  </summary>";
			

	q[10] = "(: retrieve all neighbors of a given article  \n"+
			"  (i.e. all articles which are reffered by a given article) :)  \n\n"+
			"declare default element namespace \"http://www.mediawiki.org/xml/export-0.3/\";  \n"+
			"declare ordering unordered; \n\n" +
			"declare function local:neighbors($title as xs:string) as node()* {  \n"+
			"  for $n in index-scan('article-by-title',$title,'EQ')//link/@label  \n"+
			"  return  \n"+
			"     index-scan('article-by-title',$n,'EQ') \n"+
			"}; \n\n"+
			"local:neighbors(\"MapReduce\")/title/text()";
			
		   
	q[11] =	"(: retrieve all dual-neighbors of a given article  \n"+
			"  (i.e. all articles which are reffered by and refers to a given article) :)  \n\n"+
			"declare default element namespace \"http://www.mediawiki.org/xml/export-0.3/\";  \n\n"+
			"declare function local:dual-neighbors($title as xs:string) as node()* {  \n"+
			" (for $link in index-scan('article-by-title',$title,'EQ')//link/@label  \n"+
			"  return index-scan('article-by-title',$link,'EQ'))  \n"+
			"    intersect \n"+
			"  index-scan('article-by-link',$title,'EQ')  \n"+
			"};  \n\n"+
			"local:dual-neighbors(\"MapReduce\")/title/text()";
		   
	q[12] = "(: compare Dice and Jaccard coeffs for all dual-neighbors of the target article  :)  \n"+
			"declare default element namespace \"http://www.mediawiki.org/xml/export-0.3/\";  \n"+
			"declare ordering unordered; \n\n" +
			"declare function local:neighbors($title as xs:string) as node()* {  \n"+
			"  for $n in index-scan('article-by-title',$title,'EQ')//link/@label  \n"+
			"  return  \n"+
			"     index-scan('article-by-title',$n,'EQ') \n"+
			"}; \n\n"+
			"declare function local:dual-neighbors($title as xs:string) as node()* {  \n"+
			" (for $link in index-scan('article-by-title',$title,'EQ')//link/@label  \n"+
			"  return index-scan('article-by-title',$link,'EQ'))  \n"+
			"    intersect \n"+
			"  index-scan('article-by-link',$title,'EQ')  \n"+
			"};  \n\n"+
			"declare function local:dice-coeff($n1 as node()*, $n2 as node()*) as xs:double { \n"+
			"  count($n1 intersect $n2) \n"+
			"  div \n"+
			"  (count($n1)+count($n2)) \n"+
			"}; \n\n"+
			"declare function local:jaccard-coeff($n1 as node()*, $n2 as node()*) as xs:double { \n"+
			"  count($n1 intersect $n2) \n"+
			"  div \n"+
			"  count($n1 union $n2) \n"+
			"}; \n\n"+
			"let $target-title:='Anarchism', \n"+
			"    $neighbors := local:neighbors($target-title), \n"+
			"    $dual-neighbors := local:dual-neighbors($target-title) \n"+
			"return \n"+
			"	for $a in $dual-neighbors \n"+
			"	let $an := local:neighbors($a/title/text()), \n"+
			"            $dice-coeff := local:dice-coeff($neighbors, $an), \n"+
			"            $jaccard-coeff := local:jaccard-coeff($neighbors, $an) \n"+
			"	order by $dice-coeff \n"+
			"	return \n"+
			"       <dual-neighbor target-artile=\"{$target-title}\"> \n"+
			"           <dual-neighbor>{$a/title/text()}</dual-neighbor> \n"+
			"           <dice>{$dice-coeff}</dice> \n"+
			"           <jaccard>{$jaccard-coeff}</jaccard> \n"+
			"       </dual-neighbor> \n";



function predefinedQuery(num) {
	if (document.location.pathname!='/xq/') {
		document.location.pathname='/xq/?mode=edit&q='+escape(q[num]);
		return;
	}
	document.query_form.qq.value= q[num];
	qid.edit('qid_cp','xquery')
}

function runPredefinedQuery(num, mode) {
	document.location.pathname='/predefined/?num='+num+'&mode='+mode+'&q='+escape(q[num]);
}


function querySubmit() {
	document.query_form.q.value = qid.getCode();
	document.forms.query_form.submit();
}
