var baseURL = '';
var ajaxQ = new Array();
var ajaxQp = 0;
var ajaxLock = false;

function ajaxQ__start()
{
	document.getElementById( 'word_button' ).disabled = true;
	document.getElementById( 'def_button' ).disabled = true;
	
	var btn = document.getElementById( 'defnew_button' );
	
	if ( btn )
	{
		btn.disabled = true;
	}
	
	document.getElementById( 'ajax_ready' ).style.display = 'none';
	document.getElementById( 'ajax_loading' ).style.display = 'block';
	ajaxLock = true;
}
function ajaxQ__end()
{
	ajaxLock = false;
	document.getElementById( 'ajax_loading' ).style.display = 'none';
	document.getElementById( 'ajax_ready' ).style.display = 'block';
	document.getElementById( 'def_button' ).disabled = false;
	document.getElementById( 'word_button' ).disabled = false;

	var btn = document.getElementById( 'defnew_button' );
	
	if ( btn )
	{
		btn.disabled = false;
	}

	word_transport();
	def_transport();
	for( var i = 0; i <= 47; i++ )
		phonetic_transport( i );
}
function ajaxQInit()
{
	ajaxQ__start();
	ajaxQ = new Array();
	ajaxQp = -1;
}
function ajaxQAdd( url, container )
{
	var nocache = new Date();
	ajaxQ[ajaxQ.length] = new Array( url + '&nocache=' + nocache.getTime(), container );
}
function ajaxQRun()
{
	ajaxQp++;
	if( ajaxQ[ajaxQp] && ajaxQ.length > 0 )
	{
		ajaxStart( ajaxQ[ajaxQp][0] );
	}
	else
		ajaxQ__end();
}
function ajaxLoad()
{
	httpReq = false;
	if( window.XMLHttpRequest )
	{
		try
		{
			httpReq = new XMLHttpRequest();
		}
		catch( e )
		{
			httpReq = false;
		}
	}
	else if( window.ActiveXObject )
	{
		try
		{
			httpReq = new ActiveXObject( "Msxml2.XMLHTTP" );
		}
		catch( e )
		{
			try
			{
				httpReq = new ActiveXObject( "Microsoft.XMLHTTP" );
			}
			catch( e )
			{
				httpReq = false;
			}
		}
	}
	if( !httpReq ) alert( 'No XMLRPCHTTPRequest method found for this browser. AJAX can not run!' );
	return httpReq;
}
function ajaxStart( url )
{
	httpReq = ajaxLoad();
	if( httpReq && url )
	{
		httpReq.onreadystatechange = ajaxProcess;
		url = baseURL + url;
		httpReq.open( "GET", url, true );
		httpReq.send( "" );
	}
}
function ajaxProcess()
{
	if( httpReq.readyState == 4 )
	{
		if( httpReq.status == 200 )
		{
			// If the action has () then call that JS function, and pass the results.
			// If the action exists as an element, then fill that with the results.
			if( ajaxQ[ajaxQp][1].match( /\(\)/g ) ){
//				alert( ajaxQ[ajaxQp][1].replace( /\(\)/g, '' ) + '( ' + httpReq.responseText + ' );' );
				eval( ajaxQ[ajaxQp][1].replace( /\(\)/g, '' ) + '( ' + httpReq.responseText + ' );' );
			}
			else if( document.getElementById( ajaxQ[ajaxQp][1] ) )
				document.getElementById( ajaxQ[ajaxQp][1] ).innerHTML = httpReq.responseText;
			ajaxQRun();
		}
		else
		{
			alert( "There was a problem retrieving the XML data:\n" + httpReq.statusText );
			ajaxQ__end();
		}
	}
}

function split_tags( delim, str )
{
	var delim_expr = '';
	for( var d in delim )
	{
		if( delim_expr.length > 0 )
			delim_expr += '|';
		delim_expr += delim[d].replace( /([^A-Za-z_0-9])/ig, '\\$1' );
	}

	str = str.replace( /^\s+|\s+$/g, '' );

	var tags = new Array();
	var matches = new Array();
	var tag;

	while( str && str.length > 0 )
	{
		matches = eval( 'str.match( /^(((\\[(.*?)\\])|(\{(.*?)\})|(\{(.*?)\})|(.*?))((' + delim_expr + '\\s*)|$))/ );' );
		
		if( matches )
		{
			if( matches[4] )
				tag = matches[4];
			else if( matches[6] )
				tag = matches[6];
			else if( matches[8] )
				tag = matches[8];
			else
				tag = matches[9];

			tag = tag.replace( /^\s+|\s+$/g, '' );
			tag = tag.replace( /\s+/g, ' ' );
			if( tag.length > 0 )
				tags[ tags.length ] = tag;
			
			str = str.substring( matches[1].length );
		}
		else break;
	}
	
	return tags;
}


var phonetic_store = new Array();
var phonetic_in = new Array();
var phonetic_status = new Array();

function phonetic_verify( phonetic )
{
	var o = '';
	if( phonetic[0] && phonetic[0].length )
	{
		o+= phonetic[0] + '<br><br>';
		if( phonetic[1] )
			o+= 'Phonetic syntax is correct. Please continue.';
		else
			o+= 'There is something wrong with your phonetic syntax.';
		phonetic_status[ (phonetic[2]) ] = ( phonetic[1] ? 'phonetic_good' : 'phonetic_bad' );
	}
	document.getElementById( 'phonetic_out' + phonetic[2] ).innerHTML = o;
	status_update();
}

function phonetic_transport( c )
{
	if( !c ) c = 0;

	if( !document.getElementById( 'phonetic_in' + c ) )
		return false;

	phonetic_in[c] = document.getElementById( 'phonetic_in' + c ).value;

	if( phonetic_in[c] == phonetic_store[c] )
		return false;

	if( ajaxLock )
		return false;

	phonetic_store[c] = phonetic_in[c];

	ajaxQInit();
	ajaxQAdd( '/modules.php?name=Dictionary&file=add_natural_verify&phonetic=' + escape( phonetic_in[c] ) + '&c=' + c, 'phonetic_verify()' );
	ajaxQRun();
}
phonetic_transport();

var word_store = '';
var word_in = '';
var word_mid = '';
var word_status = '';

function word_verify( word )
{
	var o = '';
	if( word[0] && word[0].length )
	{
		o+= '<span style="font-size: 14px; color: #' + ( word[1] ? '0077DD' : 'DD7700' ) + ';">' + word[0] + '</span><br><br>';
		if( word[1] )
			o+= 'This word already exists in the dictionary.<br>CONTINUE and you can add an additional definition.';
		else
			o+= 'This is a new word in our dictionary.<br>-Check your spelling and CONTINUE if you still want to ADD this word.<br> -If you do NOT want to add this word, correct the spelling or press RESTART to begin over.';
		word_status = ( word[1] ? 'word_exists' : 'word_new' );
	}
	document.getElementById( 'word_out' ).innerHTML = o;
	status_update();
}

function word_transport()
{
//	word_in = document.getElementById( 'word_in' ).value;
	word_in = document.getElementById( 'word_in' ).value;

//	word_mid = word_in.toLowerCase().replace( /[^a-z' ]/ig, '' );

	word_mid = word_in.toLowerCase().replace( /[1-9\[\]]/ig, '' );

	document.getElementById( 'word_in' ).value = word_mid;
	
	if( word_mid == word_store )
		return false;

	if( ajaxLock )
		return false;

	word_store = word_mid;

	document.getElementById( 'word_mid' ).value = word_mid;

	ajaxQInit();
//	ajaxQAdd( '/modules/Dictionary/add_natural_verify.php?word=' + escape( word_mid ), 'word_verify()' );
	ajaxQAdd( '/modules.php?name=Dictionary&file=add_natural_verify&word=' + escape( word_mid ), 'word_verify()' );//simik
	ajaxQRun();
}
document.getElementById( 'word_in' ).disabled = false;
word_transport();

var def_store = '';
var def_words = new Array();
var def_status = '';

function def_verify( words )
{
	def_words = words;
	var o = '';
	o+= '<span style="font-size: 14px; line-height: 21px;">';
	var flag = false;
	
	if( def_words )
	{
		for( w = 0; w < def_words.length; w+=2 )
		{
			if( def_words[w+1] )
			{
				o+= '<span style="color: black; border-bottom: 1px solid #0077DD;">';
			}
			else
			{
				o+= '<span style="color: white; background-color: #DD7700;">';
				flag = true;
			}
			o+= def_words[w];
			o+= '</span>&nbsp; ';
		}
	}
	o+= '</span><br><br>';	

	if( flag )
	{
		if( def_words.length == 2 )
		{
			if( def_words[0] == "" )
			{
				o+= 'Please fill at least one word for definizione.';
				def_status = '';
			}
			else
			{
				o+= 'At least one of the words is not on our dictionary.<br>You will need to create them below.';
				def_status = 'def_new';
			}
		}
		else
		{
			o+= 'At least one of the words is not on our dictionary.<br>You will need to create them below.';
			def_status = 'def_new';
		}
	}
	else
	{
		o+= 'All of the words are already in our dictionary.<br>You can now add your new word.';
		def_status = 'def_exists';
	}

	document.getElementById( 'def_out' ).innerHTML = o;
	status_update();
}

function def_transport()
{
	var def_in = document.getElementById( 'def_in' ).value;

	if( def_in == def_store )
		return false;

	if( ajaxLock )
		return false;

	def_store = def_in;
//	def_in = def_in.toLowerCase().replace( /[^a-z'\[\]\{\}\., ]/ig, ' ' );
	def_in = def_in.toLowerCase().replace( /[0-9`\\\-\=~!@#$%\^&*()_+\|;:\"\/?><]/ig, ' ');
	
	// 1. Split tags by SPACE
	// 2. Join array by |
	// 3. Wrap , & . with PIPE's
	// 4. Get rid of multiple PIPE's
	// 5. Get rid of initial or final PIPE's
	// 6. Ger rid of bogus characters (again).
//	var def_mid = split_tags( new Array( ' ' ), def_in ).join( '|' ).replace( /(,|\.)/g, '|$1|' ).replace( /\|+/g, '|' ).replace( /^\||\|$/g, '' ).replace( /[^a-z'\.,\| ]/ig, '' );


	var def_mid = split_tags( new Array( ' ' ), def_in ).join( '|' ).replace( /(,|\.)/g, '|$1|' ).replace( /\|+/g, '|' ).replace( /^\||\|$/g, '' ).replace( /[0-9`\\\-\=~!@#$%\^&*()_+;:\"\/?><\[\]\{\}]/ig, '' );


	document.getElementById( 'def_mid' ).value = def_mid;
	
	ajaxQInit();
//	ajaxQAdd( '/modules/Dictionary/add_natural_verify.php?def=' + escape( def_mid ), 'def_verify()' );
	ajaxQAdd( '/modules.php?name=Dictionary&file=add_natural_verify&def=' + escape( def_mid ), 'def_verify()' );//simik
	ajaxQRun();
}
document.getElementById( 'def_in' ).disabled = false;
def_transport();

function status_update()
{
	document.getElementById( 'status' ).value = word_status + '|' + def_status;
}

var ajax_loading_s = -1;
function ajax_loading()
{
	ajax_loading_s++;
	if( ajax_loading_s > 3 ) ajax_loading_s = -1;

	var o = '';
	o+= '<center>';
	for( i = 1; i <= ajax_loading_s; i++ )
	{
		o+= '&bull;&nbsp;';
	}
	o+= 'CHECKING WORDS';
	for( i = 1; i <= ajax_loading_s; i++ )
	{
		o+= '&nbsp;&bull;';
	}
	o+= '</center>';

	document.getElementById( 'ajax_loading' ).innerHTML = o;
	setTimeout( 'ajax_loading();', 500 );
}
ajax_loading();

function word_continue()
{
	if( !word_mid || word_mid.length < 1 )
	{
		alert( 'Please enter a word before continuing.' );
		return false;
	}
	if( word_status == 'word_new' )
		document.getElementById( 'details_table' ).style.display = 'inline';
	document.getElementById( 'def_table' ).style.display = 'inline';
	document.getElementById( 'word_button' ).style.display = 'none';
	document.getElementById( 'word_in' ).disabled = true;
	document.getElementById( 'word_clear' ).style.display = 'none';
	document.getElementById( 'def_out' ).innerHTML = '';
	
	//simik
	document.getElementById( 'disearch' ).value = word_mid;
}

function def_continue()
{
	if( word_status == 'word_new' )
	{
		if( !document.getElementById( 'phonetic_in0' ).value || document.getElementById( 'phonetic_in0' ).value.length < 1 || !document.getElementById( 'english' ).value || document.getElementById( 'english' ).value.length < 1 || !document.getElementById( 'def_mid' ).value || document.getElementById( 'def_mid' ).value.length < 1 )
		{
			alert( 'Either the phonetic description, english meaning, or the definition is missing. Please try again.' );
			return false;
		}
		if( phonetic_status[0] == 'phonetic_bad' )
		{
			alert( 'The phonetic description you entered of the word seems to be incorrect. Please try again.' );
			return false;
		}
	}
	if( def_status == 'def_new' )
	{
		document.getElementById( 'defnew_table' ).style.display = 'inline';
		document.getElementById( 'def_button' ).style.display = 'none';
		document.getElementById( 'def_clear' ).style.display = 'none';
		document.getElementById( 'def_button_back' ).style.display = 'none';
		document.getElementById( 'def_in' ).disabled = true;
		var o = '';
		o+= '<table width="100%" border="0">';

		var c = 1;
		for( w = 0; w < def_words.length; w+=2 )
		{
			if( !def_words[w+1] )
			{
				o+= '<tr>';
				o+= '<td><b>New Definition Word #' + c + '</b></td>';
				o+= '<td>';
					o+= '<span style="font-family: Verdana, sans-serif; font-size: 14px; font-weight: bold; color: #DD7700;">' + def_words[w] + '</span>';
					o+= '<input type="hidden" name="defnew_worddisplay_' + c + '" value="' + def_words[w] + '">';
				o+= '</td>';
				o+= '</tr>';
				o+= '<tr>';
				o+= '<td><b>Phonetic&nbsp;&nbsp;*</b></td>';
				o+= '<td>';
					o+= '<input type="text" name="defnew_word_' + c + '" id="phonetic_in' + c + '" style="width: 347px;" onKeyUp="phonetic_transport( ' + c + ' );" onMouseUp="phonetic_transport( ' + c + ' );" onChange="phonetic_transport( ' + c + ' );" onBlur="phonetic_transport( ' + c + ' );">';
				o+= '</td>';
				o+= '</tr>';
				o+= '<tr>';
					o+= '<td></td>';
					o+= '<td>';
						o+= '<div style="width: 347px; font-family: Verdana, sans-serif; font-size: 9px; font-weight: bold; border: 1px dashed black; line-height: 14px;"><div id="phonetic_out' + c + '" style="padding: 10px;"></div></div>';
					o+= '</td>';
				o+= '</tr>';
				o+= '<tr>';
				o+= '<td><b>English&nbsp;&nbsp;*</b></td>';
				o+= '<td>';
					o+= '<input type="text" name="defnew_eng_' + c + '" id="defnew_eng_' + c + '" style="width: 347px;"><br>';
				o+= '</td>';
				o+= '</tr>';
				o+= '<tr>';
				o+= '<td><b>Type&nbsp;&nbsp;*</b></td>';
				o+= '<td>';
					o+= '<select name="defnew_type_' + c + '" id="defnew_type_' + c + '" style="width: 347px;">';
					o+= document.getElementById( 'type' ).innerHTML.replace( /\[\[/g, '<' ).replace( /\]\]/g, '>' );
					o+= '</select>';
				o+= '</td>';
				o+= '</tr>';
				o+= '<tr><td width="50%"></td><td width="50%">&nbsp;</td></tr>';
				
				c++;
			}
		}
		o+= '<tr><td colspan="2" align="center"><input type="button" id="defnew_button_back" value="  &lt; BACK  " onClick="defnew_back();"><input type="button" id="defnew_clear" value="  RESTART  " onClick="document.location.href = request_uri;"><input type="button" id="defnew_button" value="  CONTINUE &gt;  " onClick="defnew_continue();"></td></tr>';
		o+= '</table>';
		document.getElementById( 'defnew_table' ).innerHTML = o;
	}
	else if ( ( def_status != '' ) && ( ( document.getElementById( 'itext' ).value != '' ) && ( document.getElementById( 'engtext' ).value == '' ) ) || ( ( document.getElementById( 'itext' ).value == '' ) && ( document.getElementById( 'engtext' ).value != '') ) )
	{
		alert("If you fill Italian Text, please fill English Text too");
	}
	else if ( def_status != '' )
	{
		form_submit();
	}else 
	{
		alert("Please fill the Definition first");
	}

}

function defnew_continue()
{
	var c = 1;
	var o = '';
	for( w = 0; w < def_words.length; w+=2 )
	{
		if( !def_words[w+1] )
		{
			if( document.getElementById( 'phonetic_in' + c ).value.length < 1 )
				o+= '"' + def_words[w] + '" > Phonetic Description' + "\n";
			if( document.getElementById( 'defnew_eng_' + c ).value.length < 1 )
				o+= '"' + def_words[w] + '" > English Meaning' + "\n";
			if( document.getElementById( 'defnew_type_' + c ).value.length < 1 )
				o+= '"' + def_words[w] + '" > Word Type' + "\n";
			c++;
		}
	}

	if( o.length > 0 )
	{
		o = 'The following new word fields are missing:' + "\n" + o;
		alert( o );
	}
	else
	{
		form_submit();
	}

}

function def_back()
{
	document.getElementById( 'def_button' ).disabled = false;
	document.getElementById( 'def_in' ).value = '';
	def_transport();
	if( document.getElementById( 'sound' ) ) document.getElementById( 'sound' ).value = '';
	if( document.getElementById( 'image' ) ) document.getElementById( 'image' ).value = '';
	if( document.getElementById( 'deftype_normale' ) ) document.getElementById( 'deftype_normale' ).checked = true;
	if( document.getElementById( 'english' ) ) document.getElementById( 'english' ).value = '';
	if( document.getElementById( 'phonetic_in' ) ) document.getElementById( 'phonetic_in' ).value = '';
	document.getElementById( 'word_in' ).disabled = false;
	document.getElementById( 'word_button' ).style.display = 'inline';
	document.getElementById( 'word_clear' ).style.display = 'inline';
	document.getElementById( 'details_table' ).style.display = 'none';
	document.getElementById( 'def_table' ).style.display = 'none';
	word_transport();
}

function defnew_back()
{
	document.getElementById( 'defnew_table' ).innerHTML = '';
	document.getElementById( 'defnew_table' ).style.display = 'none';
	document.getElementById( 'def_in' ).disabled = false;
	def_transport();
	document.getElementById( 'def_button' ).style.display = 'inline';
	document.getElementById( 'def_clear' ).style.display = 'inline';
	document.getElementById( 'def_button_back' ).style.display = 'inline';
}

function form_submit()
{

	document.getElementById( 'word_button' ).disabled = true;
	document.getElementById( 'def_button' ).disabled = true;
	
	var btn = document.getElementById( 'defnew_button' );
	
	if ( btn )
	{
		btn.disabled = true;
	}

	if (!document.getElementById('radio_dimage').checked) {
		document.getElementById('def_image').disabled = true;
	}

	document.getElementById( 'add_natural' ).submit();	
}


//simik
//search for a suiting image for definition
function search_image()
{
	var q = document.getElementById('disearch').value;
	if (!q) return;
	document.getElementById('tr_imagesearch_results').style.display = 'none';

	if( ajaxLock )
		return false;

	ajaxQInit();
	ajaxQAdd( '/modules.php?name=Dictionary&file=add_natural_searchimage&q=' + escape(q), 'searchimage_result()' );//simik
	ajaxQRun();
}

function searchimage_result(result)
{
	var q = document.getElementById('disearch').value;
	document.getElementById('em_imagesearch_q').innerHTML = q;

	var div = document.getElementById('div_imagesearch_results');

	var s = '<ul>';

	if (result && result.length) {
		for (i = 0; i < result.length; i++) {
			s += '<li><input type="radio" name="dimage_id" value="' + result[i].id + '" /> <a href="/modules/Dictionary/' + result[i].location + '" onclick="return openDicImage(' + result[i].id + ')">' + result[i].filename + '</a>'
		}
		s += '</ul>';
		div.innerHTML = s;
	} else {
		div.innerHTML = '<p>No images found</p>';
	}

	document.getElementById('tr_imagesearch_results').style.display = '';
}

function openBrWindow(theURL,winName,features) { 
 window.open(theURL,winName,features);
  return false;
}

function openDicImage(id) {
	openBrWindow('/modules.php?name=Dictionary&file=def_image&dfid=' + id, 'definition_img_' + id,'width=500, height=400');
	return false;
}
