//--------------------
// Ahmed Saad Functions
//--------------------


function SetCommand(Cmdname)
	{ 
		top.main.MapFrame.MapForm.Command.value = Cmdname;
	}


function TogImg(theElm,TheMode){
		if (TheMode == 'over') {
			theElm.src=theElm.src.replace("_out","_on")
		}
		if (TheMode == 'out'){
			theElm.src=theElm.src.replace("_on","_out")
		}
}


function ShowTip(TheElem,TheMode){
	//alert(TheElem + TheMode)
		if (TheMode == "On")
		{
			document.getElementById(TheElem).style.backgroundColor = '#E1EEF4'
			//document.getElementById(TheElem).style.display = "block"
		}else {
			document.getElementById(TheElem).style.backgroundColor = '#FFFFFF'
			//document.getElementById(TheElem).style.display = "none"
		}
}

//------ Ahmed Get Coordinates clicked ----- not used now ... to check if you likde to use it latter
function alertCoord(e) {
  if( !e ) {
    if( window.event ) {
      //Internet Explorer
      e = window.event;
    } else {
      //total failure, we have no way of referencing the event
      return;
    }
  }
  if( typeof( e.pageX ) == 'number' ) {
    //most browsers
    var xcoord = e.pageX;
    var ycoord = e.pageY;
  } else if( typeof( e.clientX ) == 'number' ) {
    //Internet Explorer and older browsers
    //other browsers provide this, but follow the pageX/Y branch
    var xcoord = e.clientX;
    var ycoord = e.clientY;
    var badOldBrowser = ( window.navigator.userAgent.indexOf( 'Opera' ) + 1 ) ||
     ( window.ScriptEngine && ScriptEngine().indexOf( 'InScript' ) + 1 ) ||
     ( navigator.vendor == 'KDE' )
    if( !badOldBrowser ) {
      if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        //IE 4, 5 & 6 (in non-standards compliant mode)
        xcoord += document.body.scrollLeft;
        ycoord += document.body.scrollTop;
      } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        //IE 6 (in standards compliant mode)
        xcoord += document.documentElement.scrollLeft;
        ycoord += document.documentElement.scrollTop;
      }
    }
  } else {
    //total failure, we have no way of obtaining the mouse coordinates
    return;
  }
  window.alert('Mouse coordinates are ('+xcoord+','+ycoord+')');
}
//-------------------------------------------------------- End Coordinates tool

function TheStop() {
	return false 
}


function DynamicZoom(TheParam,CountNow){
	var TheMap = top.main.MapFrame.MapForm.mapimage
		//TheMap.onclick = TheStop 
		//alert(event.wheelDelta)
	if (event.wheelDelta >= 120 || TheParam == 'In')
	{
		var OldWidth = TheMap.width  // For asp recentring before zoming 
		var OldHeight = TheMap.height 
		document.getElementById('mapimage').style.cursor = "url(/buttons/newtools/ZIN.cur)" //"move" 
		top.main.MapFrame.MapForm.Command.value = "ZoomInWheel"
		WFactor = 1.25
		TheMap.height = TheMap.height * WFactor
		TheMap.width = TheMap.width * WFactor
	} else{
		if (event.wheelDelta <= -120 || TheParam == 'Out')
		{document.getElementById('mapimage').style.cursor = "url(/buttons/newtools/ZOUT.cur)" //"move" 
		var OldWidth = TheMap.width
		var OldHeight = TheMap.height
		//alert(OldWidth + "-" + OldHeight) 

		WFactor = 0.75
		top.main.MapFrame.MapForm.Command.value = "ZoomOutWheel"
		TheMap.height = TheMap.height * WFactor
		TheMap.width = TheMap.width * WFactor
		}
	}
		//Reposition(WFactor,OldWidth,OldHeight);
		RepositionSamecenter(WFactor,OldWidth,OldHeight);
		setTimeout("submitDynamicZoom("+CountNow+");" , 0.1)
		//top.main.MapFrame.MapForm.submit();
	
	//setTimeout("top.main.MapFrame.MapForm.submit()",1000) 
	
}

function ZoomWheel(TheParam) {
	top.main.MapFrame.MapForm.WheelTimer.value = top.main.MapFrame.MapForm.WheelTimer.value + 1
    //setTimeout("top.main.MapFrame.MapForm.WheelTimer.value = top.main.MapFrame.MapForm.WheelTimer.value + 1",100)
	//alert(TheParam,top.main.MapFrame.MapForm.WheelTimer.value )
	DynamicZoom(TheParam,top.main.MapFrame.MapForm.WheelTimer.value)
}

function submitDynamicZoom(CountNow){
	if (top.main.MapFrame.MapForm.WheelTimer.value <= CountNow){
	 top.main.MapFrame.MapForm.submit();
	//} else {
	//submitDynamicZoom(TheParam)
	}
	
}

function Reposition(TheFactor,TheWidth,TheHeight){
	// This is not working any more .....  to review latter, now using RepositionSamecenter()  Ahmed
	var TheMap = document.getElementById('mapimage')
	var ClickedX = event.clientX  
	var ClickedY = event.clientY
	var ImgOrigX = MyGetImgX(TheMap)
	var ImgOrigY = MyGetImgY(TheMap)
	var MovedXF = (ClickedX - ImgOrigX ) * TheFactor
	var MovedYF = (ClickedY - ImgOrigY) * TheFactor
	var MovedX = MovedXF + ImgOrigX - ClickedX
	var MovedY = MovedYF + ImgOrigY - ClickedY
	
	TheMap.style.pixelLeft = TheMap.style.pixelLeft - MovedX 
	TheMap.style.pixelTop = TheMap.style.pixelTop - MovedY 

	// Recenter Before Zooming in ASP
	//var NewX = (  ( ImgOrigX + (TheWidth/ 2)) - MovedX ) / TheFactor
	//var NewY =  (  ( ImgOrigY + (TheHeight/ 2)) - MovedY ) / TheFactor
	var NewX = (ClickedX - ImgOrigX ) // ( MovedXF + ( TheWidth / 2) ) / TheFactor  // OKKKKK (ClickedX - ImgOrigX ) 
	var NewY = (ClickedY - ImgOrigY ) // ( MovedYF + ( TheHeight / 2) ) / TheFactor // OKKKKK (ClickedY - ImgOrigY ) 

	//alert(TheWidth + "-" + NewX + "-" + NewY)
	top.main.MapFrame.MapForm.NewCenterX.value = NewX
	top.main.MapFrame.MapForm.NewCenterY.value = NewY
	//top.main.MapFrame.MapForm.Recenter.value = "Yes"
}

function RepositionSamecenter(TheFactor,TheWidth,TheHeight){
	// Reposition image to zoomed location after scalling the image for Zoom
	var TheMap = document.getElementById('mapimage')
	var MovedX = ((TheWidth * TheFactor) - TheWidth) /2 
	var MovedY = ((TheHeight * TheFactor) - TheHeight) /2 
	
	TheMap.style.pixelLeft = TheMap.style.pixelLeft - MovedX 
	TheMap.style.pixelTop = TheMap.style.pixelTop - MovedY 

}

function MyGetImgX( oElement )
	// ===Get absolute X-coordinates of the Image ... Ahmed
	{
	var iReturnValue = 0;
	while( oElement != null ) {
	iReturnValue += oElement.offsetLeft;
	oElement = oElement.offsetParent;
	}
	return iReturnValue;
}
function MyGetImgY( oElement )
// ===Get absolute Y-coordinates of the Image >>> Ahmed
	{
	var iReturnValue = 0;
	while( oElement != null ) {
	iReturnValue += oElement.offsetTop;
	oElement = oElement.offsetParent;
	}
	return iReturnValue;
}

function SetFixedWidth(TheWidth){
	// ---- Set Fixed width for the Iframe
	var IFR = top.main.document.getElementById('SideFrame')
	 IFR.style.width = TheWidth + "px"
}

function winmin()
{
	top.main.document.getElementById('SideFrame').style.height=30 + "px";
	//alert(top.main.document.getElementById('TheWorking').name) //.style.scroll='no'
	//top.main.document.SideFrame.body.style.overflow='hidden';
	//top.main.document.getElementById('SideFrame').style.overflow-x =hidden;
}
function winmax()
{
	top.main.document.getElementById('SideFrame').style.height=1000 + "px";
	//top.main.document.getElementById('SideFrame').style.['overflow-y']  =auto;
	//top.main.document.getElementById('SideFrame').style.overflow-x =auto;
}

function changeWidth(TheFlg) {
	// --- Change IFrame Width
	var IFR = top.main.document.getElementById('SideFrame')
	newWidth = IFR.style.width
	newWidth= newWidth.replace("px", "") 
	if (TheFlg == "In")
	{		
		if (newWidth <= 1000)
		{newWidth = Number(newWidth) + 50
		}
	}else
	{	
		if (newWidth >=250)
		{newWidth = Number(newWidth) - 50
		}
	}
	top.main.document.getElementById('SideFrame').style.width=newWidth + "px";
}


function ShowSideFun(TheVal) {
	//---- Show Side Functions -----
	//alert(TheVal)
	top.main.SideFrame.SideForm.FunName.value= TheVal
	top.main.SideFrame.SideForm.submit()
	//top.main.MapFrame.MapForm.Command.value='SideFunction'
	//top.main.MapFrame.MapForm.SideFunName.value="/inc/" + TheVal + ".asp"
	//top.main.MapFrame.MapForm.submit();
}


// Show Hide Items 
var state = 'hidden';
var state1 = 'none';
function showhide(layer_ref) {
		if (state == 'visible') {
		state = 'hidden';
		state1= 'none';
		}
		else {
		state = 'visible';
		state1 = 'block';
		}
		if (document.all) { //IS IE 4 or 5 (or 6 beta)
		eval( "document.all." + layer_ref + ".style.visibility = state");
		eval( "document.all." + layer_ref + ".style.display = state1");
		
		}
		if (document.layers) { //IS NETSCAPE 4 or below
		document.layers[layer_ref].visibility = state;
		document.layers[layer_ref].display = state1;

		}
		if (document.getElementById && !document.all) {
		maxwell_smart = document.getElementById(layer_ref);
		maxwell_smart.style.visibility = state;
		maxwell_smart.style.display = state1;
		}

		}

//----For Working area
function showhideWorking(CurState) {
		if (CurState == 'none') {
		CurState= 'block';
		}
		else {
		CurState = 'none';
		}
		if (document.all) { //IS IE 4 or 5 (or 6 beta)
			eval( "document.all.Working.style.display = CurState");
			}
		if (document.layers) { //IS NETSCAPE 4 or below
		document.layers["Working"].display = CurState;
		}
		if (document.getElementById && !document.all) {
		maxWorking = document.getElementById("Working");
		maxWorking.style.display = CurState;
		}
		}

function showhideInfor(TheElem) { 
	// --------Show Hide Div By Sending The Name ; Ahmed Saad------
		if (document.all) { //IS IE 4 or 5 (or 6 beta)
			if (document.all(TheElem).style.display == "none"){
				eval( "document.all(TheElem).style.display = 'block'");
			} else {
				eval( "document.all(TheElem).style.display = 'none'");
			}
		}
		if (document.layers) { //IS NETSCAPE 4 or below
			if (document.layers[TheElem].display == "none"){
				document.layers[TheElem].display = "block";
			} else {
				document.layers[TheElem].display = "none";
			}
		}
		if (document.getElementById && !document.all) {
			maxWorking = document.getElementById(TheElem);
			if (maxWorking.style.display == "none"){
				maxWorking.style.display = "blcok";
				} else {
				maxWorking.style.display = "none";
			}
		}
	}

function SetPosition(){
	// ---setting the position of the Splash in Map Center , Ahmed
	MapLeft = top.main.MapFrame.MapForm.mapimage.offsetLeft
	MapWidth = top.main.MapFrame.MapForm.mapimage.offsetWidth
	MapTop = top.main.MapFrame.MapForm.mapimage.offsetTop
	MapHeight = top.main.MapFrame.MapForm.mapimage.offsetHeight
		// MapLeft = document.all('mapimage').offsetLeft
		// MapWidth = document.all('mapimage').offsetWidth
		// MapTop = document.all('mapimage').offsetTop
		// MapHeight = document.all('mapimage').offsetHeight

		top.main.MapFrame.MapForm.SplashIMG.style.left  = MapLeft + (MapWidth/2) - 100
		top.main.MapFrame.MapForm.SplashIMG.style.top  = MapTop + (MapHeight/2) - 50
}

function ShowRemoteSplash(){
		SetPosition()
		top.main.MapFrame.MapForm.SplashIMG.style.display = 'block'
}

function ShowRemoteSplash_srch(){
	SetPosition()
	parent.window.opener.top.main.MapFrame.MapForm.SplashIMG.style.display = 'block'
}


//Navigation rollover
function NavIn(menItem) {
	menItem.style.cursor= 'hand';
}

function NavOut(menItem) {
	menItem.style.cursor= 'default';
}

function NavCat(menItem) {
	menItem.style.borderColor='#FFFFFF'; 
	menItem.style.backgroundColor='#FFFFFF';
}


function getFindIT(Val)
{ 
		ShowRemoteSplash()
		//window.location ="/LMaps.asp?Command=Find&MapSizeO=" + top.main.MapFrame.MapForm.MapSize.value + "&FLayer=km_lmark&Expr=" + Val;
		top.main.MapFrame.MapForm.Command.value = "Find"
		top.main.MapFrame.MapForm.FLayer.value = "km_lmark"
		top.main.MapFrame.MapForm.Expr.value = Val
		top.main.MapFrame.MapForm.submit();

		//top.main.location ="/LMaps.asp?Command=Find&MapSizeO=" + top.main.MapFrame.MapForm.MapSize.value + "&FLayer=km_lmark&Expr=" + Val;
}


function onPointXY(x, y)
	{}

function onZoomXY(x, y)
	{
		document.MapForm.map_x.value = x;
		document.MapForm.map_y.value = y;
		//document.MapForm.submit();

		// Shbould be qualified only for ZoomIn, ZoomOut, and CenterAt
		//alert("AA")
		//ShowSplash();
	}

function onZoomBox(left, top, right, bottom)
	{
		document.MapForm.map_x.value = left;
		document.MapForm.map_y.value = top;
		document.MapForm.map_x2.value = right;
		document.MapForm.map_y2.value = bottom;
		document.MapForm.submit();
		ShowSplash();
	}

function OnButton(ButtonName)
	{	
		document.MapForm.Command.value = ButtonName;
		document.MapForm.mapimage.style.cursor = 'crosshair'

		document.ZoomIn.src = "/Buttons/NewTools/zoomin_out.gif";
		document.ZoomOut.src = "/Buttons/NewTools/zoomout_out.gif";
		//document.Pan.src = "/Buttons/NewTools/center_out.gif";
		document.Id.src = "/Buttons/NewTools/info_out.gif";
		document.Map.src = "/Buttons/NewTools/zoomall_out.gif";
		document.WPan.src = "/Buttons/NewTools/WPan_out.gif";
		//document.Add.src = "/buttons/Add_up.gif";  // --- Temp


		if (ButtonName == "ZoomIn")
		{	document.ZoomIn.src = "/Buttons/NewTools/zoomin_on.gif";
		}
		else if (ButtonName == "ZoomOut")
		{	document.ZoomOut.src = "/Buttons/NewTools/zoomout_on.gif";
		}
		//else if (ButtonName == "Pan")
		//{	document.Pan.src = "/Buttons/NewTools/center_on.gif";
		//}
		else if (ButtonName == "Id")
		{	document.Id.src = "/Buttons/NewTools/info_on.gif";
			document.MapForm.mapimage.style.cursor="url(/buttons/newtools/ID_Cur.cur)";
		}
		else if (ButtonName == "WPan")
		{	document.WPan.src = "/Buttons/NewTools/wPan_on.gif";
			document.MapForm.mapimage.style.cursor="url(/buttons/newtools/Moving_Final.cur)";
			//window.status="AA"
		}
		//else if (ButtonName == "StrPnt")
		//{	//document.StrPnt.src = "/buttons/Start_dn.gif";
		//}
		//else if (ButtonName == "EndPnt")
		//{	//document.EndPnt.src = "/buttons/End_dn.gif";
		//}
		else if (ButtonName == "Add")
		{	document.Add.src = "/buttons/Add_dn.gif";
			//parent.frames.contents.location="/inc/AddLoc1.asp";
			//parent.frames.contents.location ="Add_pnt.asp?MOD=ADD"			
		}

		else if (ButtonName == "Buffer")
		{	//document.Buffer.src = "/buttons/" + parent.frames.main.MapForm.evar.value + "Buffer_dn.gif";
			//parent.frames.contents.location="/inc/Buffer.asp";
		}
		else if (ButtonName == "Find")
		{	//document.Find.src = "/buttons/Find_dn.gif";
			//document.MapForm.submit();
			//document.redirect = "LM_Search.asp"
		}
		else if (ButtonName == "Key")
		{	//document.Key.src = "/buttons/" + parent.frames.main.MapForm.evar.value + "Key_dn.gif";
			//showhide('LegendIMG');
			//parent.frames.contents.location="/inc/Legend.asp";
			
			document.MapForm.Command.value= "GETLEGEND"
			document.MapForm.submit();
		}
		else if (ButtonName == "Search")
		{	//document.Search.src = "/buttons/" + parent.frames.main.MapForm.evar.value + "Find_dn.gif";
			//top.contents.location ="/inc/Search.asp?MOD=MAP";
			//---document.ZoomIn.src = "/buttons/in_dn.gif";document.MapForm.Command.value = "ZoomIn";
			//document.MapForm.submit();
		}
		else if (ButtonName == "Short")
		{	//document.Short.src = "/buttons/" + parent.frames.main.MapForm.evar.value + "Short_dn.gif";
			//parent.frames.contents.location ="/inc/Short.asp";
			document.ZoomIn.src = "/Buttons/NewTools/zoomin_on.gif";document.MapForm.Command.value = "ZoomIn";
			//showhide('ShortPath');
			//document.MapForm.submit();
		}	
		else if (ButtonName == "Help")
		{	//document.Help.src = "/buttons/Help_dn.gif";
			popup('/help/help.html','Help');
			document.ZoomIn.src = "/Buttons/NewTools/zoomin_on.gif";document.MapForm.Command.value = "ZoomIn";
			//document.MapForm.submit();
		}
		else if (ButtonName == "Print")
		{	//document.Print.src = "/buttons/Print_dn.gif";
			//parent.frames.contents.location="/inc/Print.asp?IMG=" + parent.frames.main.MapForm.mapimage.src + "&MSize=" + parent.frames.main.MapForm.MapSize.value;
			//bPrintSelected
		}
		else if (ButtonName == "Measure")
		{	//document.Measure.src = "/buttons/Measure_dn.gif";
			//parent.frames.contents.location="/inc/Measure.asp";
		}

		else if (ButtonName == "Map")
		{
			document.MapForm.submit();
		}

	}


function popup(mylink, windowname,W,H) // PopU Window Function
{ 
	if (! window.focus)return true;
	var href;
	if (typeof(mylink) == 'string')
	href=mylink;
	else
	href=mylink.href;
	
	window.open(href, windowname, 'resize=yes width=' + W + ',height=' + H + ',scrollbars=no');
	//return false; 
}

function popupa(mylink,Tit,W,H) // PopU Window Function
{ 
	if (! window.focus)return true;
	var href;
	if (typeof(mylink) == 'string')
	href=mylink;
	else
	href=mylink.href;
	//href = href.replace( "/",  "Tt-");
	//alert(Tit)
	window.open("/inc/ShowPic.asp?Pic=" + mylink + "&Tit=" + Tit , "صورة", 'resize=no width=' + W + ',height=' + H + ',scrollbars=no' );
	//return false; 
}


function OnPop(CC)
{	
	return popup(CC, 'Search')
}


// Toggel Icons ----
	function ToggelOver(TheItem, Mmod)
	{
		if (Mmod == "over")
		{
			TheItem.src=TheItem.src.replace('_up','_ov');
			//TheItem.src=TheItem.src.replace('dn','ov');
		}
		else if (Mmod == "out")
		{
			TheItem.src=TheItem.src.replace('_ov','_up');
		}
		else if (Mmod == "down")
		{
			TheItem.src=TheItem.src.replace('_ov','_dn');
		}
	}

//---- Full Screen Opener ----- 
function fullwin(targeturl){
window.open(targeturl,"","fullscreen,scrollbars")
}


function ShowGoogle(){
	top.main.document.getElementById('GoogleConn').style.display = "block"
}
function HideGoogle(){
	top.main.document.getElementById('GoogleConn').style.display = "none"
}



// === Cookies Function ==== 
//======================
function AsetCookie(c_name,value,expiredays)
	{
	var exdate=new Date()
	exdate.setDate(exdate.getDate()+expiredays)
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+";path=/"
}

function AgetCookie(c_name)
{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=")
	  if (c_start!=-1)
		{ 
		c_start=c_start + c_name.length+1 
		c_end=document.cookie.indexOf(";",c_start)
		if (c_end==-1) c_end=document.cookie.length
		return unescape(document.cookie.substring(c_start,c_end))
		} 
	  }
	return ""
}

function AcheckCookie()
{ // Not Used 
	username=getCookie('username')
	if (username!=null && username!="")
	  {alert('Welcome again '+username+'!')}
	else 
	  {
	  username=prompt('Please enter your name:',"")
	  if (username!=null && username!="")
		{
		setCookie('username',username,365)
		}
	  }
}
// ======================


function SetFireCommand(Cmdname)
{ 
		top.main.MapFrame.MapForm.Command.value = Cmdname;
}