<div id="main">
<!--MAIN PART-->
<div id="mainpartwrapper3">
  <div id="mainpart4">
	<div id="content-menu" style="overflow:auto;">				

<?php 

function process_post(){
	$lefeat = array('all_gr' => 'EA_category=/G_.*/','grammar1' => 'G_Wo_type="womaincl"','grammar2' => 'G_Wo_type="wosubcl"','grammar3' => 'G_Neg_g_neg_type="neggen"','grammar4' => 'G_Neg_g_neg_type="negdoub"','grammar5' => 'EA_category=/G_Valency/','grammar6' => 'EA_category=/G_Agr/','grammar7' => 'G_Refl_type="pronrefl"','grammar8' => 'G_Refl_type="pronreflposs"','grammar9' => 'EA_category=/G_Inflect_Inexist/','grammar10' => 'G_Inflect_Inexist_type="adj"','grammar11' => 'G_Inflect_Inexist_type="noun"','grammar12' => 'G_Inflect_Inexist_type="verb"','grammar13' => 'EA_category=/G_Morphol_Wrong/','grammar14' => 'G_Morphol_Wrong_type="case"','grammar15' => 'G_Morphol_Wrong_type="numb"','grammar16' => 'G_Morphol_Wrong_type="gend"','grammar17' => 'G_Morphol_Wrong_type="ambig"','grammar18' => 'EA_category=/G_Verb/','grammar19' => 'G_Verb_type="tns"','grammar20' => 'G_Verb_type="vc"','grammar21' => 'G_Verb_type="md"','grammar22' => 'G_Verb_type="asp"','grammar23' => 'EA_category=/G_Verb_compl/','grammar24' => 'EA_category=/G_Verb_main/','grammar25' => 'EA_category=/G_Prep/','grammar26' => 'EA_category=/G_Art/','grammar27' => 'EA_category=/G_Conj/','grammar28' => 'EA_category=/G_Clit/','grammar29' => 'EA_category=/G_Pos/','all_or' => 'EA_category=/O_.*/','orthog1' => 'O_Graph_type="graphgen"','orthog2' => 'O_Graph_type="trans"','orthog3' => 'O_Graph_type="act"','orthog4' => 'EA_category=/O_Capit/','orthog5' => 'EA_category=/O_Wordbd/','orthog6' => 'EA_category=/O_Abbrev/','orthog7' => 'EA_category=/O_Punct/','orthog8' => 'EA_category=/O_Apostr/','all_ge' => 'EA_category=/H_.*/','general1' => 'EA_category=/H_Intelltxt/','general2' => 'EA_category=/H_Intellts/','all_vo' => 'EA_category=/V_.*/','voc1' => 'V_FS_type="colloc"','voc2' => 'V_FS_type="colloc_compeq"','voc3' => 'V_FS_type="idiom"','voc4' => 'V_FS_type="commphras"','voc5' => 'EA_category=/V_Sequence_lexgrammer_inc/','voc6' => 'EA_category=/V_form_word_fs_nonexist/','voc7' => 'EA_category=/V_form_FS_incompr/','voc8' => 'EA_category=/V_semdenot_word_fs/','voc9' => 'EA_category=/V_semconn_at_word_fs/','voc10' => 'EA_category=/V_semimprec/','voc11' => 'V_Wordform_type="deriv"','voc12' => 'V_Wordform_type="comp"','voc13' => 'EA_category=/V_FS_form/','all_co' => 'EA_category=/C_.*/','coherence1' => 'EA_category=/C_Con_accur/','coherence2' => 'EA_category=/C_Coh_jump/','coherence3' => 'EA_category=/C_Coh_ref/','coherence4' => 'EA_category=/C_Coh_txtstruct/','all_so' => 'EA_category=/S_.*/','socioling1' => 'S_Txt_type="grfw"','socioling2' => 'S_Txt_type="opcl"','socioling3' => 'S_Form_type="gen"','socioling4' => 'S_Form_type="addr"','socioling5' => 'S_Var_type="clit"','socioling6' => 's_var_type="duppron"','socioling7' => 'S_Var_type="synstr"','socioling8' => 'S_Var_type="che"','socioling9' => 'S_Var_type="woweil"','socioling10' => 'S_Var_type="partik"','all_pr' => 'EA_category=/P_.*/','pragmatics1' => 'EA_category=/P_Pol_dir/','pragmatics2' => 'EA_category=/P_Request/','pragmatics3' => 'EA_category=/P_Request/');

	if(isset($_POST)){
		$count=1;  		// sequencing counter to refer to search entity (e.g. word or lemma or pos)
		$elements;		// query part defining the search units
		$combination; 		// query part defining the combination of search units of the first position (e.g. regarding word1)
		$combination2=0;	// query part defining the combination of search units of the second position (e.g. regarding word2)
		$previous=0;		// indicates how many elements are already part of the query (e.g. if we arrive to POS, did we already record a word?
		$snd_prev=0;		// like '$previous' but for the second position
		$second_el=0;		// sequencing counter for units of the second position (e.g. word2, lemma2, etc)
		$wd='.';			// word distance between position 1 and 2
		if($_POST['word_dist']!='0'){
			if($_POST['word_dist'] == '1'){
				$wd = '.2,2';
			}
			else if($_POST['word_dist'] == '2'){
				$wd = '.3,3';
			}
			else if($_POST['word_dist'] == '3'){
				$wd = '.1,4';
			}
			else if($_POST['word_dist'] == '4'){
				$wd = '.3,6';
			}
			else if($_POST['word_dist'] == '5'){
				$wd = '.7,50';
			}
		}		
		if(! empty($_POST['word_lemma_value1'])){
			if($_POST['word_lemma_cat1'] == 'word'){
				$elements = $_POST['searchin'].'="'.$_POST['word_lemma_value1'].'"';
				$count++;
				$previous=1;
			}
			else if($_POST['word_lemma_cat1'] == 'lemma'){
				$elements = 'tok_lemma="'.$_POST['word_lemma_value1'].'"';
				$previous++;
				$count++;
			}
		}
		if(! empty($_POST['tok_pos1'])){
			if($_POST['tok_pos1'] != "all"){
			if($previous > 1){
				if($_POST['lang']=="Czech"){
					$elements .= ' & tok_pos=/'.$_POST['tok_pos1'].'.*/';
				}
				else{
					$elements .= ' & tok_pos="'.$_POST['tok_pos1'].'"';
				}
				$combination .= ' _=_ #'.$count; 
			}
			else if($previous){
				if($_POST['lang']=="Czech"){
					$elements .= ' & tok_pos=/'.$_POST['tok_pos1'].'.*/';
				}
				else{
					$elements .= ' & tok_pos="'.$_POST['tok_pos1'].'"';
				}
				$combination = ' & #'.($count-1).' _=_ #'.$count; 
				$previous++;
			}
			else{
				if($_POST['lang']=="Czech"){
					$elements = 'tok_pos=/'.$_POST['tok_pos1'].'.*/';
				}
				else{
					$elements = 'tok_pos="'.$_POST['tok_pos1'].'"';
				}
				$previous=1;
			}
			$count++;
			}
		}
		if($_POST['lfeat1_1']!='none'){
			if($previous > 1){
				
				// no fine selection
				if(preg_match("/all_.*/", $_POST['lfeat1_2'])){
					if($_POST['lfeat1_1']=='grammar'){
						$elements .= ' & EA_category=/G_.*/';
						$combination .= ' _o_ #'.$count;
					}
					else if($_POST['lfeat1_1']=='orthog'){
						$elements .= ' & EA_category=/O_.*/';
						$combination .= ' _o_ #'.$count;
					}
					else if($_POST['lfeat1_1']=='general'){
						$elements .= ' & EA_category=/H_.*/';
						$combination .= ' _o_ #'.$count;
					}
					else if($_POST['lfeat1_1']=='voc'){
						$elements .= ' & EA_category=/V_.*/';
						$combination .= ' _o_ #'.$count;
					}
					else if($_POST['lfeat1_1']=='coherence'){
						$elements .= ' & EA_category=/C_.*/';
						$combination .= ' _o_ #'.$count;
					}
					else if($_POST['lfeat1_1']=='socioling'){
						$elements .= ' & EA_category=/S_.*/';
						$combination .= ' _o_ #'.$count;
					}
					else if($_POST['lfeat1_1']=='pragmatics'){
						$elements .= ' & EA_category=/P_.*/';
						$combination .= ' _o_ #'.$count;
					}
					else{
						$elements .= ' & EA_category=/X_/';
						$combination .= ' _o_ #'.$count;
					}
				}
				else{  // only fine selection
						$elements .= ' & '.$lefeat[$_POST['lfeat1_2']];
						$combination .= ' _o_ #'.$count;
				}
			}
			else if($previous){
				// no fine selection
				if(preg_match("/all_.*/", $_POST['lfeat1_2'])){
					if($_POST['lfeat1_1']=='grammar'){
						$elements .= ' & EA_category=/G_.*/';
						$combination = ' & #'.($count-1).' _o_ #'.$count;
					}
					else if($_POST['lfeat1_1']=='orthog'){
						$elements .= ' & EA_category=/O_.*/';
						$combination = ' & #'.($count-1).' _o_ #'.$count;
					}
					else if($_POST['lfeat1_1']=='general'){
						$elements .= ' & EA_category=/H_.*/';
						$combination = ' & #'.($count-1).' _o_ #'.$count;
					}
					else if($_POST['lfeat1_1']=='voc'){
						$elements .= ' & EA_category=/V_.*/';
						$combination = ' & #'.($count-1).' _o_ #'.$count;
					}
					else if($_POST['lfeat1_1']=='coherence'){
						$elements .= ' & EA_category=/C_.*/';
						$combination = ' & #'.($count-1).' _o_ #'.$count;
					}
					else if($_POST['lfeat1_1']=='socioling'){
						$elements .= ' & EA_category=/S_.*/';
						$combination = ' & #'.($count-1).' _o_ #'.$count;
					}
					else if($_POST['lfeat1_1']=='pragmatics'){
						$elements .= ' & EA_category=/P_.*/';
						$combination = ' & #'.($count-1).' _o_ #'.$count;
					}
					else{
						$elements .= ' & EA_category=/X_/';
						$combination = ' & #'.($count-1).' _o_ #'.$count;
					}		
				}
				else{  // only fine selection
					
					$elements .= ' & '.$lefeat[$_POST['lfeat1_2']];
					$combination = ' & #'.($count-1).' _o_ #'.$count;
				}
				$previous++;
			}
			else{
				// no fine selection
				if(preg_match("/all_.*/", $_POST['lfeat1_2'])){
					if($_POST['lfeat1_1']=='grammar'){
						$elements .= ' & EA_category=/G_.*/';
					}
					else if($_POST['lfeat1_1']=='orthog'){
						$elements .= ' & EA_category=/O_.*/';
					}
					else if($_POST['lfeat1_1']=='general'){
						$elements .= ' & EA_category=/H_.*/';
					}
					else if($_POST['lfeat1_1']=='voc'){
						$elements .= ' & EA_category=/V_.*/';
					}
					else if($_POST['lfeat1_1']=='coherence'){
						$elements .= ' & EA_category=/C_.*/';
					}
					else if($_POST['lfeat1_1']=='socioling'){
						$elements .= ' & EA_category=/S_.*/';
					}
					else if($_POST['lfeat1_1']=='pragmatics'){
						$elements .= ' & EA_category=/P_.*/';
					}
					else{
						$elements .= ' & EA_category=/X_/';
					}
				}			
				else{  // only fine selection
					$elements = $lefeat[$_POST['lfeat1_2']];
				}
				$previous=1;
			}
			$count++;
		}
		
		if(! $previous){
		
			//echo "ERROR";
		
		}
		else{
			if(! empty($_POST['word_lemma_value2'])){
				if($_POST['word_lemma_cat2'] == 'word'){
					$elements .= ' & '.$_POST['searchin'].'="'.$_POST['word_lemma_value2'].'"';
					$snd_prev=1;
					if(! $second_el){
						$second_el=$count;
					}
					$count++;
				}
				else if($_POST['word_lemma_cat2'] == 'lemma'){
					$elements .= ' & tok_lemma="'.$_POST['word_lemma_value2'].'"';
					if(! $second_el){
						$second_el=$count;
					}
					if($snd_prev){
						$combination2 = ' & #'.($count-1).' _=_ #'.$count;
					}
					$snd_prev++;
					$count++;
				}
			}
			if(! empty($_POST['tok_pos2'])){
				if($_POST['tok_pos2'] != "all"){
					if($_POST['lang']=="Czech"){
						$elements .= ' & tok_pos=/'.$_POST['tok_pos2'].'.*/';
					}
					else{
						$elements .= ' & tok_pos="'.$_POST['tok_pos2'].'"';
					}
				if(! $second_el){
					$second_el=$count;
				}
				if($snd_prev > 1){
					$combination2 .= ' _=_ #'.$count;
				}
				else if ($snd_prev){
					$combination2 = ' & #'.($count-1).' _=_ #'.$count;
				}
				$snd_prev++;
				$count++;
				}
			}
			// ex
			if($_POST['lfeat2_1']!='none'){
				if($snd_prev > 1){
					if(! $second_el){
						$second_el=$count;
					}
					// no fine selection
					if($_POST['lfeat2_2']=='all_in'){
						if($_POST['lfeat2_1']=='grammar'){
							$elements .= ' & EA_category=/G_.*/';
							$combination2 .= ' _o_ #'.$count;
						}
						else if($_POST['lfeat2_1']=='orthog'){
							$elements .= ' & EA_category=/O_.*/';
							$combination2 .= ' _o_ #'.$count;
						}
						else{
							$elements .= ' & EA_category=/X_/';
							$combination2 .= ' _o_ #'.$count;
						}
					}
					else{  // only fine selection
						if(preg_match("/grammar.*/", $_POST['lfeat2_2'])){
							$elements .= ' & '.$lefeat[$_POST['lfeat2_2']];
							$combination2 .= ' _o_ #'.$count;
						}
						else if (preg_match("/orthog.*/", $_POST['lfeat2_2'])){
							$elements .= ' & '.$lefeat[$_POST['lfeat2_2']];
							$combination2 .= ' _o_ #'.$count;
						}
						else{
							$elements .= ' & '.$lefeat[$_POST['lfeat2_2']];
							$combination2 .= ' _o_ #'.$count;
						}
					}
					
				}
				else if($snd_prev){
					if(! $second_el){
						$second_el=$count;
					}
					// no fine selection
					if(preg_match("/all_.*/", $_POST['lfeat2_2'])){
						if($_POST['lfeat2_1']=='grammar'){
							$elements .= ' & EA_category=/G_.*/';
							$combination2 = ' & #'.($count-1).' _o_ #'.$count;
						}
						else if($_POST['lfeat2_1']=='orthog'){
							$elements .= ' & EA_category=/O_.*/';
							$combination2 = ' & #'.($count-1).' _o_ #'.$count;
						}
						else{
							$elements .= ' & EA_category=/X_/';
							$combination2 = ' & #'.($count-1).' _o_ #'.$count;
						}
					}
					else{  // only fine selection
						if(preg_match("/grammar.*/", $_POST['lfeat2_2'])){
							$elements .= ' & '.$lefeat[$_POST['lfeat2_2']];
							$combination2 = ' & #'.($count-1).' _o_ #'.$count;
						}
						else if (preg_match("/orthog.*/", $_POST['lfeat2_2'])){
							$elements .= ' & '.$lefeat[$_POST['lfeat2_2']];
							$combination2 = ' & #'.($count-1).' _o_ #'.$count;
						}
						else{
							$elements .= ' & '.$lefeat[$_POST['lfeat2_2']];
							$combination2 = ' & #'.($count-1).' _o_ #'.$count;
						}
					}
					$snd_prev++;
				}
				else{
					// no fine selection
					if(! $second_el){
						$second_el=$count;
					}
					if($_POST['lfeat2_2']=='all_in'){
						if($_POST['lfeat2_1']=='grammar'){
							$elements .= ' & '.'EA_category=/G_.*/';
						}
						else if($_POST['lfeat2_1']=='orthog'){
							$elements .= ' & '.'EA_category=/O_.*/';
						}
						else{
							$elements .= ' & '.'EA_category=/X_/';
						}
					}
					else{  // only fine selection
						if(preg_match("/grammar.*/", $_POST['lfeat2_2'])){
							$elements .= ' & '.$lefeat[$_POST['lfeat2_2']];
						}
						else if (preg_match("/orthog.*/", $_POST['lfeat2_2'])){
							$elements .= ' & '.$lefeat[$_POST['lfeat2_2']];
						}
						else{
							$elements .= ' & '.$lefeat[$_POST['lfeat2_2']];
						}
					}
					$snd_prev=1;
				}
				$count++;
				//$second_el++;
			}
			//ex
				
		}
		if($second_el){
			if($previous>1){
				if($combination2){
					//$combination .= $combination2.' & #1 '.$wd.' #'.($count-1);
					$combination .= $combination2.' & #1 '.$wd.' #'.$second_el;
				}
				else if ($snd_prev){
					$combination .= ' & #1 '.$wd.' #'.$second_el;
				}
			}
			else if ($previous){
				if($combination2){
					$combination = $combination2.' & #1 '.$wd.' #'.$second_el;
				}
				else if ($snd_prev){
					$combination = ' & #1 '.$wd.' #'.$second_el;
				}			
			}
		}
		
	}
	$corp=0;
	if($_POST['text_id']){
			$corp = $_POST['text_id'];
			$corp = preg_replace('/, */', '|', $corp);
			$corp = preg_replace('/ */', '', $corp);
			$corp = 'meta::_author_id=/('.$corp.')/';
	}
	else if(isset($_POST['cur_corpus'])){
		if($_POST['cur_corpus']!="full" && $_POST['cur_corpus']!="DE"){
			$corp = 'meta::_author_id='.$_SESSION['corpora'][$_POST['cur_corpus']]['ids'];
		}
	}
	else if(array_key_exists('cur_corpus_single', $_POST)){

		if($_POST['cur_corpus_single']!="full"){
		$corp = 'meta::_author_id='.$_SESSION['corpora'][$_POST['cur_corpus_single']]['ids'];
		}
		
	}
			
	if($previous>1){
		if($corp){
			return $elements.$combination." & ".$corp;	
		}
		else{
			return $elements.$combination;
		}
	}
	else if($previous){
		if($second_el){
			if($corp){
				return $elements.$combination." & ".$corp;	
			}
			else{
				return $elements.$combination;
			}
		}
		else{
			if($corp){
				return $elements." & ".$corp;	
			}
			else{
				return $elements;
			}
		
		}
	}
	else{
		if($corp){
			return $corp." & tok";	
		}
		else{
			echo "ERROR";
		}		
	}
}


function get_corp(){

	if(isset($_POST)){
		if($_POST['lang'] == 'German'){
			//$corpus = '2014-07-29-german-gold-ea1-pilot';
			$corpus = 'MERLIN_German';
		}
		else if($_POST['lang'] == 'Italian'){
			$corpus = 'MERLIN_Italian';
		}
		else if($_POST['lang'] == 'Czech'){
			$corpus = 'MERLIN_Czech';
		}
		else{		
			$corpus = $_POST['lang'];
		}
	}
	return $corpus;
}


?>
<script>
    function previewUrl(url,target){
        //use timeout coz mousehover fires several times
        clearTimeout(window.ht);
        window.ht = setTimeout(function(){
            var div = document.getElementById(target);
            div.innerHTML = '<iframe style="width:885px; height:800px" frameborder="0" src="' + url + '" />';
        },20);      
    }   
</script>
	
	
<?php
function openANNIS($qu, $corp, $a_server){
	$q = base64_encode($qu);
	if($corp == "all"){
		// all corpora: 2014-07-29-italian-gold-pilot,2014-07-29-german-gold-pilot,2014-07-29-czech-gold-pilot
		//$c="MjAxNC0wNy0yOS1pdGFsaWFuLWdvbGQtcGlsb3QsMjAxNC0wNy0yOS1nZXJtYW4tZ29sZC1waWxvdCwyMDE0LTA3LTI5LWN6ZWNoLWdvbGQtcGlsb3Q=";
		
		// all final corpora: MERLIN_Czech,MERLIN_Italian,MERLIN_German
		$c="TUVSTElOX0N6ZWNoLE1FUkxJTl9JdGFsaWFuLE1FUkxJTl9HZXJtYW4=";
		
	}
	else{
		$c = base64_encode($corp);
	}

	//http://samos.sfs.uni-tuebingen.de:8080/annis/
	$link = "<script>previewUrl('".$a_server."#_q=".$q."&_c=".$c."&cl=5&cr=5&s=0&l=10&_seg=bGVhcm5lcg','div1')</script>";
	echo $link;
}
	// button: modify previous query
	echo '<button onclick="history.go(-2);">'.$trans['modify'][$_SESSION['lang']].'</button>';
	
	// button: about the search output
	echo '<form class="inline" action="../php/index.php" method="post" target="_blank">';
	echo '<input type="hidden" name="curscr" value="help-annis.php">';
	echo '<button type="submit" name="new" value="">'.$trans['about_out'][$_SESSION['lang']].'</button>';
	echo '</form>';
	
	// button: abbreviations (glossary)
	echo '<form class="inline" action="../php/index.php" method="post" target="_blank">';
	echo '<input type="hidden" name="curscr" value="help-annis-glossary.php">';
	echo '<button type="submit" name="new" value="">'.$trans['gloss'][$_SESSION['lang']].'</button>';
	echo '</form>';
	
?>
	
<div id="div1"></div>

<?php

// Ablauf::
// 1. - ANNIS query aus POST-Daten bauen (64urlEncode)
// 2. - Query zu ANNIS posten
// 3. - ANNIS-Anzeige im MERLIN Fenster (_self) (erweitert, ohne linke Taskleiste)

if(!empty($_POST["featq"])){
	openANNIS($_POST["featq"], "all", $annis_server);
}
else{
	$aql = process_post();
	$corpus = get_corp();
	openANNIS($aql, $corpus, $annis_server);
}
?>

<!--### IFRAME PART END-->  
</div>		  
</div>
</div>
<!--MAIN PART END-->
</div>