// readonly
var formsubmitted = false;
var remreturn = false;

/****************** Calendar Setup ********************/
var calNodeIds = new Array();

function addCalNode( id )
{
    calNodeIds.push( id );
}

function SetupCalendar()
{
    var i = 0;
    for( i = 0; i < calNodeIds.length; i++ )
    {
        var ip = calNodeIds[i];
	Calendar.setup(
	{
	    inputField  : "input_" + ip,         // ID of the input field
	    ifFormat    : "%d/%m/%Y",    // the date format
	    button      : "CalTrigger" + ip,       // ID of the button
	    weekNumbers : false
	});
    }
}


/****************** Calendar Setup ********************/

/****************** DateTime Setup ********************/
var dateTimeNodeIds = new Array();

function addDateTimeNode( idd )
{
	dateTimeNodeIds.push( idd );
}

function SetupDateTime()
{
    var j = 0;
    for( j = 0; j < dateTimeNodeIds.length; j++ )
    {
        var ipd = dateTimeNodeIds[j];
	Calendar.setup(
	{
	    inputField  : "input_" + ipd,         // ID of the input field
	    showsTime	: true,					//show time control
	    ifFormat    : "%d/%m/%Y %H:%M",    // the date format
	    button      : "CalTrigger" + ipd,       // ID of the button
	    weekNumbers : false
	});
    }
}

/***************** DateTime Setup Finish **********************/
/****************** GIS Functions ********************/
var polyWin;

function ShowBoundaryMap( xpoint, ypoint )
{		
	argv=arguments;
	argc=arguments.length;
	driver=(argc>2) ? argv[2] : 'Integraph';

	/*-- Integraph--*/
	if(driver=='Integraph')
	{
	polyWin = window.open( 'http://localhost/vroot/asp/parsolframeset.asp?MOVE=WIDTH&amp;width=250&amp;X1=' + xpoint + '&amp;Y1=' + ypoint + '&amp;X2=' + xpoint + '&amp;Y2=' + ypoint + '&amp;CENTREMARK=RED',
				'Geomedia', 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no,location=no,width=800,height=600' );
	}	
	/*-- Swift--*/
	else if(driver=='swift')
		polyWin = window.open( 'http://triton/SwiftMap2/swiftmap-simple-capture.asp?config-file=C:\\Program Files\\SwiftMapWeb\\Parsol\\capture-test-tk.xml&view='+xpoint+','+ypoint+',300&appDomain='+document.domain,null,'fullscreen=no,toolbar=no,menubar=no,scrollbars=no,status=yes,resizable=yes,location=no,width=800,height=600' );
	/*--GDC--*/
	else if(driver=='gdc')
	{
		polyWin = window.open( 'http://mir/tkflow/selectBoundaries.html?Easting='+xpoint+'&Northing='+ypoint,null,'fullscreen=no,toolbar=no,menubar=no,scrollbars=no,status=yes,resizable=yes,location=no,width=760,height=600' );
	}
}
function SetURL( url )
{
    document.all.input_1.value = url;
    //polyWin.close();
    document.forms['tkinputform'].submit();
}
function SetBoundary( coords )
{
    document.all.input_1.value = coords;
    polyWin.close();
    document.forms['tkinputform'].submit();
}
/****************** GIS Functions ********************/


/****************** BackButton Handler ****************/
var xx="1";
var isBack;

function handleBackButton()
{
	//alert("isBack is " + isBack + " and back flag is " + document.tkinputform._backflag.value);
	if(document.tkinputform != null)
	{
			isBack = (xx != document.tkinputform._backflag.value);
			document.tkinputform._backflag.value=2;
			document.tkinputform._backflag.defaultValue=2; 
	}
	//alert("isBack is " + isBack + " and back flag is " + document.tkinputform._backflag.value);
}

function isBackButtonUsed()
{
	//alert("reached isBackButtonUsed");
	if(isBack)
		alert('This page was accessed using the browsers back button.\nPlease avoid this as it may result in unpredictable behaviour!');
}

/****************** BackButton Handler ****************/

/***************** Pre-loading page ******************/
function clearPreloadPage() 
{ //DOM
	if (document.getElementById)
	{
		if(document.getElementById('prepage') != null)
			document.getElementById('prepage').style.visibility='hidden';
	}
	else
	{
		if (document.layers){ //NS4
			document.prepage.visibility = 'hidden';
		}
		else { //IE4
			document.all.prepage.style.visibility = 'hidden';
		}
	}
}
/***************** Pre-loading page ******************/


/****************** Others ****************/

function pageSetup()
{
//	alert("Reached pageSetup()");
	handleBackButton();
	clearPreloadPage(); //Not implemented
}

function FinishDialogue( refurl )
{
	if ( window.opener != null )
		window.close();
	else
		window.top.location= refurl;
}


function tktoggle(tki, tkb)
{
	document.images[tki].src=(tkb)?"./images/sel.gif":"./images/des.gif";
}

function SupMulti()
{
    if (!formsubmitted)
    {
	document.forms['tkinputform'].submit();
        formsubmitted=true;
        return true;
    }
    else
    {
	document.all.pleasewaitScreen.style.pixelTop = (document.body.scrollTop + 100);
	document.all.pleasewaitScreen.style.visibility="visible";

        window.status= "Please wait, Your submission is being processed.";
        return false;
    }
}
function setChoice(txt)
{
    var f = document.forms["tkinputform"];
    f.elements["choice"].value=txt;
}
function setNextNode(nid)
{ 
    var f = document.forms["tkinputform"];
    f.nextnode.value=nid;
}


// What we really need to do is allow us to close the log session if we have one
// open and then go to the URL. In order to do this we need to pass both to a 
// small proxy class where we can do these things.
function AreYouSureSameWindow(question,u)
{
/*    if (confirm(question)) window.location= "/tkflow/CloseSession.aspx?goto=" + decodeAmps(u);*/
    if (confirm(question)) window.location=decodeAmps(u);
}

function AreYouSure(question, u)
{
/*    if (confirm(question)) FinishDialogue("/tkflow/CloseSession.aspx?goto=" + decodeAmps(u) );*/
    if (confirm(question)) window.top.location=decodeAmps(u);
}

function decodeAmps(s) 
{
    return s.replace("&amp;amp;","&amp;");
}

/***********GRID STUFF**************/
var currentRow=-1;

function SelectRow(newRow)
{
   try
   {
	   if(currentRow == '')
		   currentRow = -1;
	   if(currentRow != -1 && currentRow == newRow)
	   {
		   var row=document.getElementById(newRow);
           var table=row;
		   while(table.nodeName != "TABLE")
				table = table.parentNode;

	       var ip=document.getElementById('input_'+table.id);
		   row.style.background='';
		   ip.value='';
		   return;
	   }
       var row=document.getElementById(newRow);
       var table=row;
       while(table.nodeName != "TABLE")
			table = table.parentNode;

	   var ip=document.getElementById('input_'+table.id);
       row.style.background='#dcdcdc';
       if(currentRow!=-1)
       {
           var row=document.getElementById(currentRow);
           row.style.background='';
		   ip.value=newRow;
       }
   }
   catch(exception)
   {
	   var row=document.getElementById(currentRow);
       row.style.background='';
       var table=row;
       while(table.nodeName != "TABLE")
			table = table.parentNode;
	   var ip=document.getElementById('input_'+table.id);
	   currentRow=newRow;
       ip.value='';
   }    
   //}

   currentRow=newRow;
   ip.value=currentRow;
}


function IsSelected()
{
   return (currentRow==-1 || currentRow=='')?false:true;
}

function GetSelectedRow()
{
   return currentRow;
}
/***********************************/