 url = ""; // see init
//url = "http://acips.dynalias.com/sandbox/AcipsServlet";
// var url = "http://localhost:8080/acips/AcipsServlet";
var servletUrl = "acips-neu.html";



function callServlet(key, val)
{
 var pars = key+"="+val;
   new Ajax.Request(url+"?"+pars,
   {
   		method:'post',
   		/* Accept: 'text/html', */
   		onSuccess: processReqChange,
//   		parameters: pars ,
   		onFailure: function(){alert('something went wrong ...');}
	});
}


// global var for sketch window
var sketch;

// retrieve the current sketch image from the server
function updateSketch()
{

	try{
		if( !sketch.closed )
		{
	
			sketch.open(url+"?panel=sketch",'ACIPS Skizze',			'height=350,width=450,resizable=yes,scrollbars=yes,toolbar=no,status=no');	
	
			
	} // if
	} 
	catch( e )
	{
	}
	// update inline sketch

   new Ajax.Request(url + "?panel=sketchUrl",
   {
   		method:'post',
   		/* Accept: 'text/html', */
   		onSuccess: handleSketchURL,
   		onFailuer: function(){alert('something went wrong ...');}
	});

}




gPreBlg = ""; // global stete of lastfal combo
gPreBbg = ""; // global state of bbereich comb

// we have received a new panel
function processReqChange( req )
{
            hideProgress();
		    document.getElementById("mainpane").innerHTML = req.responseText;

		    document.getElementById( "warningPane"  ).innerHTML = "";
		    document.getElementById( "warningHeader"  ).innerHTML = "";
			
		    cpName = "main";
		    try{
		          cpName = document.getElementsByName( "curPanel")[0].value;
		    }
		    catch(e){}


		    updateGlobals();
		    
		    if( cpName != "userinfo" &&
			cpName != "main" )
		    {
				// update the sketch
				updateSketch();
		    }
		    else
		    {
				hideSketch();
		    }

		    
}


var validation;     // status flag;1 == validation in progress
var nextPanelName;  // panel will be loaded after successful validation
// handle response of validatePanel()
function processValidateChange( req )
{
	        if( req.responseText.length > 0 )
	        {
	
			document.getElementById( "warningPane"  ).innerHTML =
			"<div class='fehler'>" + req.responseText +"</div>\n";
			document.getElementById( "warningHeader"  ).innerHTML = 
			"<div class='Hfehler'> <h3>Es liegt ein Eingabefehler vor! Detaillierte Hinweise finden Sie am Ende der Seite.</h3></div>\n";

		        validation = 0;
			// scroll to top
			window.scroll(0,0);

			// force redraw to prevent IE layout issues
			// PROBLEM: This clears the text boxes!
			// document.getElementById("mainpane").innerHTML += "";

		}
		else
		{
		      validation = 1;
        		selectPanel( nextPanelName );
		}

}

// collect the form elemnts for a post request
function collectElements()
{
	call = "";
	cElements = document.mainForm.elements.length;
	for( i = 0; i < cElements; i++ )
	{
	    if( document.mainForm.elements[i].disabled == 0 )
	    {
		call += document.mainForm.elements[i].name + 
			"=" 
			+ escape(document.mainForm.elements[i].value)
			+"&";
	    }
	}
	return call;

}
// send commit current values and evaluate errors, 
function validatePanel()
{
	call = collectElements();
	call += "submit=1";
   new Ajax.Request(url + "?" + call,
   {
   		method:'post',
   		Accept: 'text/html',
   		onSuccess: processValidateChange,
   		onFailuer: function(){alert('something went wrong ...');}
	});


	validation = 0;
/* get version 

	req = initRequest();
	req.onreadystatechange = processValidateChange;
        myUrl = url + "?" + call;
	validation = 0;
	// alert(myUrl);
	req.open("GET", myUrl, true );	
	req.send(null);
*/
}

// select a new panel
function selectPanel( arg )
{
	callServlet("panel", arg );
	
}

// get the preview page 
function getResult( arg )
{
		
	showPreviewProgress("Ihre Eingaben werden gepr&uuml;ft ...");
	callServlet("result", arg );
}



function processCommitChange()
{
	selectPanel( document.mainForm.curPanel.value);
}


var mutexSelChange = 0;


gDBIdxBeul = "4"; // idx of beul entry
gLfIdxBeul = "10"; // id of beul entry
// event handler for comobo box changed events
function selChange( eName )
{
   if( mutexSelChange++ == 0 )
   {
		// check the constraints
		// randlagerung
		if(     eName == 'Br1' ||	
	            eName == 'Br2' ||
	            eName == 'Br3' ||
	            eName == 'Br4' )
		{
			Brlg = document.getElementsByName("Brlg")[0];		
			if( Brlg &&
			Brlg.checked )
			{
				val = document.getElementsByName(eName)[0].value;
			    try{
				document.getElementsByName("Br1")[0].value = val;
			    }
			    catch( e ){}
			    try{
				document.getElementsByName("Br2")[0].value = val;
			    }
			    catch( e ){}
			    try{
				document.getElementsByName("Br3")[0].value = val;
			    }
			    catch( e ){}
			    try{
				document.getElementsByName("Br4")[0].value = val;
			    }
			    catch( e ){}				
			}
		} // if rlb
            
	// collect current values and send to server
	// alert(eName);
		else if( eName == 'Bfg' ) // plattenform
		{
			call = collectElements();
			call += "commit=1";
			
   new Ajax.Request(url + "?" + call,
   {
   		method:'post',
   		Accept: 'text/html',
   		onSuccess: processReqChange,
   		onFailuer: function(){alert('something went wrong ...');}
	});
			
/* get
			// alert(call);
		        myUrl = url + "?" + call;
			req.open("GET", myUrl, true );	
			req.send(null);	
*/
		}
		else if( eName == 'Blg' ) // Lastfall
		{
			val = document.getElementsByName(eName)[0].value;
			if( val == gLfIdxBeul ) // beulen
			{
				document.getElementsByName('Bbg')[0].value = gDBIdxBeul;
				gPreBbg = gDBIdxBeul;;
			} 
			else if( gPreBlg == gLfIdxBeul && val != gLfIdxBeul) 
			{
				document.getElementsByName('Bbg')[0].value = '0';
				gPreBbg = '0';
			}
			gPreBlg = val;
		}
		else if( eName == 'Bbg' ) // Berechnungsbereich
		{
			val = document.getElementsByName(eName)[0].value;
			if( val == gDBIdxBeul ) // beulen
			{
				document.getElementsByName('Blg')[0].value = gLfIdxBeul;
				gPreBlg = gLfIdxBeul;
			} 
			else if( gPreBbg == gDBIdxBeul && val != gDBIdxBeul) 
			{
				document.getElementsByName('Blg')[0].value = '0';
				gPreBlg = '0';
			}
			gPreBbg = val;
		}
   }  
   mutexSelChange--;
}

var mutex = 0;
// text input has changed
function inputChange( ename )
{
   if( mutex++ == 0 )
   {
	if( ename == 'azn' )
	{
		document.getElementsByName( 'rzn'  )[0].value = "";
	}
	else if( ename == 'rzn' )
	{
		document.getElementsByName( 'azn'  )[0].value = "";
	}
/* xor not required, June 12, 2007
	else if( ename == 'mpx' )
	{
		document.getElementsByName( 'mpy'  )[0].value = "0";
	}
	else if( ename == 'mpy' )
	{
		document.getElementsByName( 'mpx'  )[0].value = "0";
	}
*/
	else if( ename == 'sa')
	{
           try{
			document.getElementsByName( 'ksh'  )[0].value = "";
	   }
	   catch(e){}
 	}
	else if( ename == 'ksh')
	{
           try{
			document.getElementsByName( 'sa'  )[0].value = "";
	   }
	   catch(e){}
 	}

   }
   mutex--;
}

function enableElement( state, eName )
{
	document.getElementsByName( eName  )[0].disabled = state;
}

function checkBoxChange( cbName )
{
	cb = document.getElementsByName( cbName  )[0];
	// change the send value of the chackbox
	if( cb.checked )
	{
		cb.value = "1";
	}
	else
	{
		cb.value = "0";
	}
	// check constraints
	if(cbName == 'p11' )
	{
		enableElement( 1 - cb.checked, 'p11x' );
		enableElement( 1 - cb.checked, 'p11y' );
	}
	else if( cbName == 'p12' )
	{
		enableElement( 1 - cb.checked, 'p12r' );
		enableElement( 1 - cb.checked, 'p12phi' );
	}
	else if( cbName == 'Brlg' ) // gleiche Randlagerung
	{
		// copy Br1 to all boxes
		selChange( 'Br1' );	
	}
	
}


// send values and receive errorMsg or switch to next Panel
function submitPanel( arg0 )
{
    nextPanelName = arg0;
    validatePanel();
}


function showSketch()
{
	
	sketch = window.open(url+"?panel=sketch","ACIPS Skizze",'height=350,width=450,resizable=yes,scrollbars=yes,toolbar=no,status=no');
	
	sketch.focus();

}

function hideSketch()
{
	document.getElementById('imgThumb').innerHTML = "<img src='http://www.softplate.com/images/nosketch.jpg'>";


}
function handleSketchURL( req )
{
		document.getElementById('imgThumb').innerHTML = req.responseText;

}
// update the global state parameters
function  updateGlobals()
{
	try{
		gPreBlg = document.getElementsByName('Blg')[0].value;
	}
	catch( e )
	{}
	try{
		gPreBbg = document.getElementsByName('Bbg')[0].value;
	}
	catch(e )
	{}
	
}

function showHelp( tpName )
{
    cpName = document.getElementsByName( "curPanel")[0].value;
    helpWindow = window.open("http://www.softplate.com/help/"+cpName+".html#"+tpName,
	"help",
   "scrollbars=yes,menubar=yes,height=400,width=500, left=400, resizable=yes,toolbar=no,location=yes,status=no" );
	if (helpWindow .focus) 
		{helpWindow .focus()}

}


function hideProgress()
{
    hideProgressBar();
    try 
    {
        document.getElementById( "progress").innerHTML = "";
    }
    catch( e )
	{}
    try 
    {
        document.getElementById( "prgCaption").innerHTML = "";
    }
    catch( e )
	{}
}

function showProgress()
{
    showProgressBar();
    try 
    {
        document.getElementById( "progress").innerHTML = "<p align=center><B>Das Ergebnis wird berechnet,<br>wir bitten um etwas Geduld ...</B></P>";
    }
    catch( e )
	{}
}

function showPreviewProgress(str)
{
    showProgressBar();
    try 
    {
        document.getElementById( "prgCaption").innerHTML = str;
    }
    catch( e )
	{}
}

function hideJavaScriptWarning()
{
	document.getElementById( "warningHeader" ).innerHTML = "";
	document.getElementById( "warningPane" ).innerHTML = "";
}

function showConnecting(eId)
{
	document.getElementById( eId ).innerHTML = 
	"<P align=center> Die Verbindung zu unserem Server wird aufgebaut ... </P>";
}


function showFinalResultPage( req2 )
{
 // only if req shows "complete"
 $('result').innerHTML = req2.responseText;
   setTimeout('window.focus()',100);
   alert("Die Berechnung ist beendet.");
 /*
	hideProgressBar();
	
	w = window.open();
	w.document.open("text/html", "replace");
	w.document.write( req2.responseText );
	w.document.close();
	*/
}

// perform the calculation
function doCalculation()
{
	// showProgress();
	
	key = "calculate";
	val = "1";

   new Ajax.Request(url + "?" +key + "=" + val,
   {
   		method:'post',
   		Accept: 'text/html',
   		onSuccess: showFinalResultPage,
   		onFailure: function(){alert('something went wrong ...');}
	});

}

function gotoMainPanel()
{
	window.open(servletUrl, true);
/*
		document.open(servletUrl);
		document.close();
		selectPanel("main");
*/
}

function init()
{
	var hUrl =  window.document.URL.toString();
	if( hUrl.indexOf("sandbox") > 0 )
	{
		urlLogin = "http://acips.dynalias.com/sandbox/AcipsLogin";
		url = "http://acips.dynalias.com/sandbox/AcipsServlet";	
	}    
	else
	{
		urlLogin = "http://acips.dynalias.com/acips/AcipsLogin";
		url = "http://acips.dynalias.com/acips/AcipsServlet";			
	}
	urlLogin = "http://../AcipsLogin";
	url = "http://../AcipsServlet";			
	
	
}
