var loc_refresh;
var div_refresh;
var www_main = 'http://localhost:8080/my/wp4/';
var edit_notice = 'Please fill the fields in the right way and then click on the button ADD';

function test() { alert('test ok');}


function top_hover( target , status , base ) {
	
	obj = document.getElementById(target);
	img = base ;
	if(status == 0) img += '_n.png';
	if(status == 1) img += '_h.png';
	//alert(img);
	obj.style.background = 'url(' + img + ')';
	obj.style.backgroundRepeat = 'repeat-x';
}

function show_hide( container , state )  {

	var divCustomerInfo = document.getElementById( container );

	if(divCustomerInfo.style.display=='block')
		divCustomerInfo.style.display='none';
	else
		divCustomerInfo.style.display='block';

	//alert(container + ':'+ divCustomerInfo.style.display);
}

function displayContainer(sContainer , sText)
{
	var divCustomerInfo = document.getElementById(sContainer);
	divCustomerInfo.style.visibility='visible';
	if(sText.indexOf("<RAW>")>=0)
	{
		sText = sText.substring(0 , sText.indexOf("<!DOCTYPE"));
		sText=str_replace("<RAW>" , "" , sText);
		divCustomerInfo.innerHTML = sText;
	}
	else	
	{
		if(sText.indexOf("<!DOCTYPE")>0)
			divCustomerInfo.innerHTML = sText.substring(0,sText.indexOf("<!DOCTYPE")); //sText;
		else
		{
			//alert(sText);
			divCustomerInfo.innerHTML = sText;
		}
	}
}

function str_replace(search, replace, subject) 
{
    return subject.split(search).join(replace);
}	

function currentTimeStamp()
{
	var d = new Date();
	s = d.getUTCDate() + "_" + d.getUTCMonth() + "_" + d.getUTCFullYear() + "_" + d.getUTCHours() + "_" + d.getUTCMinutes() + "_" + d.getUTCSeconds() + "_" + d.getUTCMilliseconds();
	return s;	
}
		
function getData(Dest , sUrl  )
{	
	var sLoc;
	var b;
	var oXmlHttp = zXmlHttp.createRequest();
	
	//alert(Dest + ":" + sUrl );

	if(sUrl.indexOf(".html")>=0)
		sLoc  = sUrl;
	else
	{
		if(sUrl.indexOf("?")>=0)
			sLoc  = sUrl + "&TimeStamp=" + currentTimeStamp() ;
		else
			sLoc  = sUrl + "?TimeStamp=" + currentTimeStamp() ;
	}
	displayContainer( Dest , "<img border=0 src=" + www_main + "images/loading.gif>");		
	
	
	oXmlHttp.open("get", sLoc, true);
	//alert('step2:'+sLoc);
	oXmlHttp.onreadystatechange = function () 
	{
		if (oXmlHttp.readyState == 4) 
		{
			if (oXmlHttp.status == 200) 
			{
				b = 1 ;
				
				if(oXmlHttp.responseText.indexOf('<!--err-->') >=0)
				{
					
					b = 0 ; 
					toRefresh = 0;
					//alert('err' + b + ':' + toRefresh);
					
					err = str_replace( '#' , '<br>' , str_replace('<!--err-->' , '' , oXmlHttp.responseText ));
					//err = str_replace( '?and' , '&' , err );
					//alert(err);
					get_error();

				}
				else
				{
					if( toRefresh==1 )
					{
						toRefresh = 0 ;
						alert(toRefresh +':' + b + ':' + div_refresh + ':' + loc_refresh);
						getData( div_refresh , loc_refresh , 0 );
						get_clear();

					}
				
					if(b==1)
					{
						//alert(Dest + ':' + b + ':' + toRefresh);
						displayContainer( Dest , oXmlHttp.responseText);
					}
				}
			} 
			else 
			{
				displayContainer( Dest , "An error occurred: " + oXmlHttp.statusText); 
			}
		}            
	};
	oXmlHttp.send(null);
}

var pos;
var word;
var quest;
var direction;
var cat_id;
var err;
var toRefresh;

function get_clear()
{
	loc  = www_main + 'inc/class.matrix.php?err=' + edit_notice;
	loc += '&pos=' + pos;
	loc += '&direction=0';
	loc += '&word=';
	loc += '&quest=';
	loc += '&cat_id=' + cat_id;
	
	toRefresh = 0 ;
	//alert('sex');
	//alert('clr:' + toRefresh + ':' +loc)
	getData( 'matrix_edit' , loc );
}

function get_error()
{
	//alert('inn get_error');
	var loc = '';

	loc+= www_main + 'inc/class.matrix.php?';
	loc+= '&pos=' + pos;
	loc+= '&direction=' + direction;
	loc+= '&word=' + word;
	loc+= '&quest=' + quest;
	loc+= '&cat_id=' + cat_id;
	loc+= '&err=' + err;
	
	toRefresh = 0 ;
	//alert('err:' + toRefresh + ':' +loc)
	getData( 'matrix_edit' , loc , 1 );
}

function add_word()
{
	//alert('start add');

	loc =  www_main + 'action.php?op=1';
	
	po = document.getElementById("start");
	loc = loc + "&pos=" + po.value;

	toRefresh = 1 ;
	loc_refresh = www_main + 'inc/class.matrix.php?refresh=1&pos='+po.value;
	pos = po.value;

	po = document.getElementById("word");
	loc = loc + "&word=" + po.value;
	word = po.value;

	po = document.getElementById("quest");
	loc = loc + "&quest=" + po.value;
	quest = po.value;

	po = document.getElementById("direction");
	loc = loc + "&direction="+po.options[po.selectedIndex].value;
	direction  = po.options[po.selectedIndex].value;

	po = document.getElementById("cat_id");
	loc = loc + "&cat_id="+po.options[po.selectedIndex].value;
	cat_id = po.options[po.selectedIndex].value;

	div_refresh = 'matrix';

	getData( 'matrix_edit' , loc , 1 );
}
