/**
 * Title:        TerraMapTool - Map Navigation JS
 * Description:  Javascript file for the Maptool JSP file. This JS file is used to for the rubber banding
 * Copyright:    Copyright (c) 2004
 * Company:      LISAsoft
 * @author       Alexandre Djioev, Benjamin Schiavone
 */

/* 
 * function that is called on page load
 */
function onPageLoad(state) {
	states = new Array('NSW', 'VIC', 'QLD', 'SA', 'TAS', 'ACT', 'NT', 'WA');
	for (i=0; i < states.length; i++) {
		document.getElementById(states[i]).style.visibility = "visible";
	}
	document.getElementById(state).style.display = "none";
}


/* 
 * Check if hiven string is a number
 */
function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   }


// -------------------------------------------------------------------------
 //  Name: SelectOptionInList
 //  Abstract: Given a select list and an ID search the list for the option with
 //               the matching ID and select it.
 // -------------------------------------------------------------------------
 function SelectOptionInList( lstSelectList, intID )
 {
      try {
         var intIndex = 0;
         for( intIndex = 0; intIndex < lstSelectList.options.length; intIndex++ )
         {
            if( lstSelectList.options[intIndex].value == intID )
            {
                 lstSelectList.selectedIndex = intIndex;
                 break;
            }
         }
      }
      catch( expError ) {
           alert( "ClientUtilities1.js::SelectOptionInList( ).\n" +
                     "Error:" + expError.number + ", " + expError.description );
      }
 }

 
function trim(str) {
   return str.replace(/^\s*|\s*$/g,"");
}

 
function getOffsets (evt) {
  var target = evt.target;
  if (typeof target.offsetLeft == 'undefined') {
    target = target.parentNode;
  }
  var pageCoords = getPageCoords(target);
  var eventCoords = { 
    x: window.pageXOffset + evt.clientX,
    y: window.pageYOffset + evt.clientY
  };
  var offsets = {
    offsetX: eventCoords.x - pageCoords.x,
    offsetY: eventCoords.y - pageCoords.y
  }
  return offsets;
}

function getPageCoords (element) {
  var coords = {x : 0, y : 0};
  while (element) {
    coords.x += element.offsetLeft;
    coords.y += element.offsetTop;
    element = element.offsetParent;
  }
  return coords;
}


function mapEvent (evt, operation) {
	document.MapServImage.operation.value = operation;
  if (typeof evt.offsetX == 'undefined') {
    var evtOffsets = getOffsets(evt);
    document.MapServImage.panOffsetX.value  = evtOffsets.offsetX;
    document.MapServImage.panOffsetY.value = evtOffsets.offsetY;
  } else {
    document.MapServImage.panOffsetX.value = evt.offsetX;
    document.MapServImage.panOffsetY.value = evt.offsetY;
  }
  
  document.MapServImage.submit();	
}
  


  
function load(file,target) {
    if (target != '')
        target.window.location.href = file;
    else
        window.location.href = file;
}
   	  
/*
  Check if 'Enter' button is pressed. If it is
  submit the state and suburb to the main form
  on map.jsp.
*/   	  
function noenter(code,operation) {
 if (code && code == 13){
 	doSearch(operation);
 	return false;
 } else {
 	return true;
 }
} 

function hideHelp() {
	document.getElementById('startupmessage').style.display = "none";
} 
  
function doSearch(operation) {
  document.MapServImage.operation.value = operation;
  document.MapServImage.state.value  = document.LocationSearchForm.state.value;
  document.MapServImage.suburb.value = document.LocationSearchForm.search.value;
  document.MapServImage.postcode.value = document.LocationSearchForm.postcode.value;
  document.MapServImage.submit();
}


function changeState(state) {
	states = new Array('NSW', 'VIC', 'QLD', 'SA', 'TAS', 'ACT', 'NT', 'WA');
	clearSuburb();
	document.LocationSearchForm.state.value = state;
	document.LocationSearchForm.search.value = "";
	for (i=0; i < states.length; i++) {
		document.getElementById(states[i]).style.display = "block";
	}
	document.getElementById(state).style.display = "none";
}


function showImage() {
	document.getElementById('shadow_map').style.visibility="hidden";
	document.getElementById('loading_image').style.visibility="hidden";	
	document.getElementById('ysearchinput').disabled = false;
}


function imageLoading(){
	document.getElementById('shadow_map').style.visibility="visible";
	document.getElementById('loading_image').style.visibility="visible";
}

 
function direct(operation, direction) {
 document.MapServImage.operation.value =  operation;
 document.MapServImage.panDirection.value = direction;
 document.MapServImage.submit();
}


function lightup(imageobject, opacity){
    if (navigator.appName.indexOf("Netscape")!=-1&&parseInt(navigator.appVersion)>=5)
    imageobject.style.MozOpacity=opacity/100;
    else if (navigator.appName.indexOf("Microsoft")!=-1&&parseInt(navigator.appVersion)>=4)
    imageobject.filters.alpha.opacity=opacity;
}



function zoomchange(operation, level) {
 //imageLoading();
 document.MapServImage.zoomLevel.value = level;
 document.MapServImage.operation.value = operation;
 document.MapServImage.submit();
}

function zoombutton(change) {
 //imageLoading();
 if (change == 'in') {
  var zoomIncrease = document.MapServImage.zoomLevel.value;
  if (zoomIncrease > 1) {
   zoomIncrease--;
   document.MapServImage.zoomLevel.value = zoomIncrease;
   document.MapServImage.operation.value = "Zoom";
   document.MapServImage.submit();
  }
 } else if (change == 'out') {
   var zoomDecrease = document.MapServImage.zoomLevel.value;

   if (zoomDecrease < zoomLength) {
       zoomDecrease++;
       document.MapServImage.zoomLevel.value = zoomDecrease;
       if (zoomDecrease >= '3') {
    		document.MapServImage.aerial.value = "hide";
       }
       if (zoomDecrease >= '2') {
    		document.MapServImage.dimensions.value = "hide";
       }
       document.MapServImage.operation.value = "Zoom";
  }

  document.MapServImage.submit();
 }
}



function recentre(operation, x, y) {
 //imageLoading();
 document.MapServImage.operation.value = operation;
 document.MapServImage.centreX.value = x;
 document.MapServImage.centreY.value = y;
 document.MapServImage.submit();
}


function refocus(operation) {
 //imageLoading();
 document.MapServImage.operation.value = operation;
 document.MapServImage.submit();
}

function restart(operation, level) {
 //imageLoading();
 document.MapServImage.zoomLevel.value = level;
 document.MapServImage.operation.value = operation;
 document.MapServImage.submit();
}

function imgClickHandler (evt, img) {
			if (!evt) {
				var evt = window.event;
			}
			if (typeof evt.offsetX == 'undefined') {
				var evtOffsets = getOffsets(evt);
				document.MapServ.pixelX.value = evtOffsets.offsetX;
				document.MapServ.pixelY.value = evtOffsets.offsetY;
		    } else {
			    document.MapServ.pixelX.value = evt.offsetX;
				document.MapServ.pixelY.value = evt.offsetY;
			}
			document.forms[0].operation.value = "Recenter";
			document.forms[0].submit();
		}     
		
function getOffsets (evt) {
  var target = evt.target;
  if (typeof target.offsetLeft == 'undefined') {
    target = target.parentNode;
  }
  var pageCoords = getPageCoords(target);
  var eventCoords = { 
    x: window.pageXOffset + evt.clientX,
    y: window.pageYOffset + evt.clientY
  };
  var offsets = {
    offsetX: eventCoords.x - pageCoords.x,
    offsetY: eventCoords.y - pageCoords.y
  }
  return offsets;
}		



function getPageCoords (element) {
  var coords = {x : 0, y : 0};
  while (element) {
    coords.x += element.offsetLeft;
    coords.y += element.offsetTop;
    element = element.offsetParent;
  }
  return coords;
}


