﻿//************adjustMapSize************
//// Declare variables that will be used across functions
//var reloadTimer;
//var webMapAppMapDisplay = null;
//var initialStartUp = true;
//var mapHeight;
//var mapWidth;
//    
//function setPageElementSizes() {
//    // Get the div containing the map
//    webMapAppMapDisplay = document.getElementById("Map_Panel");  
//    
//    // Hide the scrollbar and set the body of the page to occupy the entire
//    // height of the browser window.  The logical branch is required because
//    // of differences in browser impelementations.              
//    if (document.documentElement) {
//        document.documentElement.style.overflow = "hidden";
//        document.documentElement.style.height = "100%"; 
//    } else {
//        document.body.style.overflow = "hidden";
//        document.body.style.height = "100%";
//    }  
//        
//    // Use pre-packaged ADF JavaScript functions to get the browser 
//    // window's width and height         
//    var sWidth = getWinWidth();
//    var sHeight = getWinHeight();
//    //sHeight  =sHeight - 85; //modefied by zizo
//    // Set the height and width of the map div to that of the browser window
//    webMapAppMapDisplay.style.width =  sWidth + "px";
//    webMapAppMapDisplay.style.height = sHeight  + "px";
//       
//    // Store map width and height upon initial load.  These variables will
//    // be used to compare current map width to requested map width, and thus
//    // eliminate erroneous resizing upon load when the map container does 
//    // not use absolute positioning.
//    mapWidth = sWidth;
//    mapHeight = sHeight;
//}

//function AdjustMapSizeHandler(e) {
//    // Get the client-side map object
//    var map = $find('Map1');
//    
//    // Use pre-packaged ADF JavaScript functions to get the window's width and height         
//    var sWidth = getWinWidth();
//    var sHeight = getWinHeight();
//    
//    // Set the size of the div containing the map.  The variable referencing the div
//    // was initialized in setPageElementSizes.
//    webMapAppMapDisplay.style.width =  sWidth + "px";
//    webMapAppMapDisplay.style.left =  "0px";
//    webMapAppMapDisplay.style.height = sHeight + "px";

//    // Use an ADF JavaScript function to get the position of the map div
//    var box = calcElementPosition("Map_Panel"); 
//    // Adjust properties on the map object to reflect any change in the map div's position
//    map.containerLeft = box.left;
//	map.containerTop = box.top;
//}

//function resetMapHistory() {
//   var map = $find('Map1');
//    var toolbar = $find("Toolbar1"); 
//	map._currentExtentHistory = 0;
//	var tbElem = Toolbars[toolbar._uniqueID];
//    var backButton = tbElem.items[tbElem.btnMapBack];
//    if (backButton) { backButton.disabled = true; }
//    var forwardButton = tbElem.items[tbElem.btnMapForward];
//    if (forwardButton) { forwardButton.disabled = true; }
//    tbElem.refreshCommands();
//}

var myHeightTemp;
var myWidthTemp;
var LoadFirstTime=true;
function adjustMapSize() {
var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    document.documentElement.style.overflow = "hidden";
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    document.body.style.overflow = "hidden";
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
   //change height
   if(myHeightTemp!=myHeight || myWidthTemp!= myWidth)
   {
        document.getElementById('Map_Td').style.height = (myHeight - 112 ) + 'px';
        //document.getElementById('Map_Td').style.height = "100%";
        
        document.getElementById('Map1').style.height = (myHeight - 112 ) + 'px';
         
     myHeightTemp = myHeight;
     myWidthTemp = myWidth;
     
     if(LoadFirstTime)
     {
         var map = $find('Map1');
         if(map!=null)
         {
//         xmin, ymin, xmax, ymax

            //var newEnvelope = new ESRI.ADF.Geometries.Envelope(639722.1535857655,2714516.063659304,708976.9795954175,2744943.2078469256);
            var newEnvelope = new ESRI.ADF.Geometries.Envelope(622123.867565,2698379.381756,725576.157803,2774976.406397);
            map.set_extent(newEnvelope);
            map.refresh();
         }
     }
     LoadFirstTime =false;
       
   }
  
  
 }
//*************Magnifier****************
function toggleMagnifier() {
    var mag = $get("Magnifier1");
    if (mag!=null) { 
            toggleFloatingPanelVisibility('Magnifier1'); 
    } else 
        alert("Magnifier is not available"); 
    
}
//*****************General********************
    //*************right menu*************
var currenPanel = 'SS';
var lastPanel = '';

function switchControl(ID)
{   
    
    lastPanel = currenPanel;
    
    currenPanel = ID;
    var panel = document.getElementById(ID);
    
    if(panel == null)
        return false;
    panel.style.display = 'block';

    if(ID=="SS")
    {
        document.getElementById("lblSS").style.fontWeight = "bold";
        document.getElementById("lblSS").style.fontSize = "11px"
        
        document.getElementById("lblDD").style.fontSize = "12px"
        document.getElementById("lblDD").style.fontWeight = "";
        
        HideControl("DD");
    }
    if(ID == "DD")
    {
        document.getElementById("lblDD").style.fontWeight = "bold";
        document.getElementById("lblDD").style.fontSize = "11px"
        
        document.getElementById("lblSS").style.fontSize = "12px"
        document.getElementById("lblSS").style.fontWeight = "";
        
        
        HideControl("SS");

    }
    
}
function HideControl(ID)
{ 
    var panel = document.getElementById(ID);

    if(panel == null)
        return false;
    panel.style.display = 'none';
}
function ShowControl(ID)
{ 
    var panel = document.getElementById(ID);

    if(panel == null)
        return false;
    panel.style.display = 'block';
}


 function popUp(URL) 
    {
        day = new Date();
        id = day.getTime();
        eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=600,left = 212,top = 84');");
    }
    function AddNewUser(URL) 
    {
        day = new Date();
        id = day.getTime();
        eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=320,height=380,left = 212,top = 84');");
    }
    
    function AddNewService()
    {
        if(document.getElementById("txtIsLogin").value == "1")
        {
            ShowControl("fpAddService");
             var selObj = document.getElementById("fpAddService_AddNewService1_cmbAddServiceType");
        selObj.value = "0106";
        }
        else
        {
            ShowControl("fpLogin");
        }
    }
    
    function Addistraha()
    {
       
        AddNewService();
        var selObj = document.getElementById("fpAddService_AddNewService1_cmbAddServiceType");
        selObj.value = "0408";

    }
   
//    function printDiv(DivName)
//    {
//        
//       var divToPrint=document.getElementById(DivName);
//       var mapImage=document.getElementById('map_td');
//      newWin= window.open("");
//      //newWin.document.write(document.documentElement.innerHTML);
//      
//      //newWin.document.write("<html><body>" + divToPrint  +"</body></html>"+"<script>window.print();</script>");
//      //newWin.document.write("<html xmlns:v='VML'> <style>v\\:*{behavior:url(#default#VML);position:absolute}</style><body>"+ mapImage.innerHTML +  divToPrint.innerHTML +"</body></html>"+"<script>window.print();</script>");
//      
//      
//      newWin.location.reload(true);

//    
//      //newWin.print();
//      newWin.close();

//    }

    function set_mouse_pointer(obj)
    {
        obj.style.cursor='pointer';
    }
    function PrintMap()
    {
    
//        var mapImage=document.getElementById('map_td');
//        newWin= window.open("");
//        newWin.document.write("<html><body>" + mapImage.innerHTML  +"</body></html>"+"<script>window.print();</script>");
        
       var mapImage=document.getElementById('Map_Td');
       newWin= window.open('Print.aspx');
       
      while(true)
       {

            if(newWin.document.getElementById('imageDiv')== null)
            {
                 
            }
            else
            {
                newWin.document.getElementById('imageDiv').innerHTML = mapImage.innerHTML;
                break;
            }
       }
    }
    
     function PrintDrivinDirection()
    {   
        var divToPrint=document.getElementById('DivDD');
       var mapImage=document.getElementById('Map_Td');
       newWin= window.open('PrintDrivingDirection.aspx');
       //alert(newWin.document.getElementById('imageDiv').innerHTML);
      while(true)
       {
        
            if(newWin.document.getElementById('imageDiv') ==null || newWin.document.getElementById('DivDirection')==null)
            {
                
            }
            else
            {
                newWin.document.getElementById('imageDiv').innerHTML = mapImage.innerHTML;
                newWin.document.getElementById('DivDirection').innerHTML = divToPrint.innerHTML;
                break;
            }
       }
    }
    
    
    
    var CurrentTD = "td2";
     var ArrTDs =new Array ("td2","td3","td4","td5","td6","td7");
    function AddServiceWizerd(btn,e)
    {
//        alert(e);
        var indexShowTD;
        if(btn=='next')
        {
             for(var x=0; x<ArrTDs.length; x++)
             {
                 if(ArrTDs[x]==CurrentTD)
                    {
                      indexShowTD= x + 1; 
	                }
             }
        }
        if(btn=='prv')
        {
            for(var x=0; x<ArrTDs.length; x++)
             {
                 if(ArrTDs[x]==CurrentTD)
                    {
                      indexShowTD= x - 1;  
	                }
             }
        }
        
          ShowControl(ArrTDs[indexShowTD]);
          HideControl(CurrentTD);
          CurrentTD =  ArrTDs[indexShowTD];

           ShowControl("btnServicePerv");
           ShowControl("btnServiceNext");
           HideControl("DivBtnSaveServices");
           
          if(CurrentTD=="td2")
          {
            HideControl("btnServicePerv");
           
             if(_pointS != null)
             {
                    
                    var tbObject = $find('Toolbar1');                    
			        if (tbObject!=null) {
                       var handler = tbObject.get_events().getHandler('onToolSelected');
                       if(handler) 
                            handler(tbObject,{"name": "Toolbar1MapZoomIn1", "tool": document.getElementById("Toolbar1MapZoomIn1")});
			        }
  
                  map = $find('Map1');
                  map.removeGraphic(_pointS); 
                  document.getElementById('fpAddService_AddNewService1_txtHpointService').value ="";
             }
          }
//           if(CurrentTD=="td3")
//          {
//            var tbObject = $find('Toolbar4');                    
//			        if (tbObject!=null) {
//                       var handler = tbObject.get_events().getHandler('onToolSelected');
//                       if(handler) 
//                            handler(tbObject,{"name": "Toolbar4Tool", "tool": document.getElementById("Toolbar4Tool")});
//                            }
//          }
           if(CurrentTD=="td4")
          {
            if(document.getElementById('fpAddService_AddNewService1_txtHpointService').value=="")
            {
                 CurrentTD="td3";
                 ShowControl("td3");
                    HideControl("td4");
                    alert("حدد مكان الخدمة أولا.");
            
            }
            else
            {  
                HideControl("btnServiceNext");
                ShowControl("DivBtnSaveServices");
            }
          }
          
          if(CurrentTD=="td7")
          {
            HideControl("btnServiceNext");
          }
         
    }
    
function ServiceResult(message,resCode,ID)
{

    if(resCode==0)
    {
    //alert(CurrentTD);
        
            HideControl(CurrentTD);
            ShowControl("td2"); 
            CurrentTD = "td2";
            var tbObject = $find('Toolbar1');                    
			        if (tbObject!=null) {
                       var handler = tbObject.get_events().getHandler('onToolSelected');
                       if(handler) 
                            handler(tbObject,{"name": "Toolbar1MapZoomIn1", "tool": document.getElementById("Toolbar1MapZoomIn1")});
			        }
  
                  map = $find('Map1');
                  map.removeGraphic(_pointS); 
                  document.getElementById('fpAddService_AddNewService1_txtHpointService').value =""; 
                  
                  ShowControl("btnServiceNext");
                  HideControl("btnServicePerv");
                  HideControl("DivBtnSaveServices");
                  HideControl("fpAddService");
    }

    if(resCode==1)
    {  
        
          
            CurrentTD = "td2";
            HideControl("td4");
            ShowControl("td2"); 
            var tbObject = $find('Toolbar1');                    
			        if (tbObject!=null) {
                       var handler = tbObject.get_events().getHandler('onToolSelected');
                       if(handler) 
                            handler(tbObject,{"name": "Toolbar1MapZoomIn1", "tool": document.getElementById("Toolbar1MapZoomIn1")});
			        }
  
                  map = $find('Map1');
                  map.removeGraphic(_pointS); 
                  document.getElementById('fpAddService_AddNewService1_txtHpointService').value =""; 
                  HideControl("fpAddService");
                  //open this window for images
                    window.open("AddServicePicture.aspx?id=" + ID , "imageWin","width=800,height=600,scrollbars,menubar");
        //alert(message);
    }
    if(resCode==2)
    {
         alert(message);
    }
}

    
    //********************Driving Direction************

function DrivingPoint(x,y,Direction,txtBox,StrValue)
{
    switchControl("DD"); // to set focus on driving direction control
    var txtbox = document.getElementById(txtBox);
         if(txtbox != null)
         {
            txtbox.value = StrValue;
          }
    if(Direction=="From")
    {
        document.getElementById("DrivingDirection1_txtHPointA").value = x + ";" + y;
    }
    if(Direction=="To")
    {
        document.getElementById("DrivingDirection1_txtHPointB").value = x + ";" + y;
    }
}
//*************map functions*******************
function zoomToPoint(x,y)
{    
    var zoomFactror = 100;
    var map = $find('Map1');
    var p = new  ESRI.ADF.Geometries.Point(x,y,map.get_spatialReference());
    //alert(p);
    var env = p.getEnvelope();
       
     env.set_xmax(env.get_xmax() + zoomFactror);
     env.set_ymax(env.get_ymax() + zoomFactror);
     env.set_xmin(env.get_xmin() - zoomFactror);
     env.set_ymin(env.get_ymin() - zoomFactror);

    map.zoomToBox(env,true);
}

function ZoomTOEnvelop(xmax,xmin,ymax,ymin)
{
    var newEnvelope = new ESRI.ADF.Geometries.Envelope(xmin, ymin, xmax, ymax);
    //    env.set_xmax(xmax);
    //    env.set_xmin(xmin);
    //    env.set_ymax(ymax);
    //    env.set_ymin(ymin);
 
    map.set_extent(newEnvelope);
    
}

function ZoomToDistrict()
{
     var value =  document.getElementById("cbHay").value;
     if(value!=0)
     {
        env  =  value.split(';')      
        
        ZoomTOEnvelop(parseFloat(env[0]),parseFloat(env[1]),parseFloat(env[2]),parseFloat(env[3]));
     }
}

function refreshMap()
{
    map.refresh();
}

   function PrintVersion() {
        var f = document.forms[0];
        
        __doPostBack('Toolbar1', '');
        //var win1 = window.open("PrintPage.aspx", "PrintWin","width=800,height=600,scrollbars,menubar");
    }

// this event in tool bar items changed
function OnToolSelectHandler(sender, args) {
    if (args.name) 
    {
        var mode = args.name;
        if (mode!="Measure") closeMeasureToolbarTool();
        
     }    
}


//***********************singleballon*******************
var _PointData=null;
var _PointEventData=null;
var _httpPath =null;
var _called =false;

function SetSingleballon(pointsData,httpPath)
{
    _PointData =pointsData;
    _httpPath =httpPath;
    _called = false;
}
function SetEventballon(pointsData,httpPath)
{
	
    _PointEventData =pointsData;
    _httpPath =httpPath;
  
}
function DrawEventballon()
{
	
    if(_PointEventData==null || _httpPath==null)
    return;
	
	DrawBallons(_PointEventData,_httpPath);
	
}
function DrawSingleballon(_PointData,_httpPath)
{
	
    if(_called || _PointData==null || _httpPath==null)
    return;
	
	
   try
    {
    
       arr= _PointData.split(";");
	
       for(var z=0;z<arr.length-6;z=z+6)
        {    
 
             
             var TitleContent = "<font color='#000000' style='font-size:10px;font-weight:normal;font-family:Tahoma;'>"+arr[z+3]+" </font>";
             
             var DescContent ="";
             if(arr[z+6]!="") // if no image dont concatenate image tag
             {    
                 DescContent = DescContent + " <div align=center><img src='" + _httpPath + arr[z+6] +"' style='border: solid 1px gray;padding:3px;width:120px;height:80px;padding-top:2px'></div>";
                 
             }
              DescContent = DescContent + "<div dir='rtl' style='padding:3px;text-align:justify' align=right><font color='#444444' style='font-size:10px;font-weight:bold;font-family:Tahoma;'>"+arr[z+3]+"</font><br>";
              if(arr[z+4]!="") // if no phone 
             { 
               DescContent = DescContent + "<font color='#444444' style='font-size:10px;font-weight:;font-family:Tahoma;'><b> هاتف : </b>" +arr[z+4] + "</font>";
		 
               
             }
             
             DescContent = DescContent + "<br><font color='#444444' style='font-size:10px;font-weight:;font-family:Tahoma;'>"+arr[z+5]+"</font></div> " ;
           
             var map = $find('Map1');
             
           
             

	
    		map = $find('Map1');
		//alert(map);
			var point = new ESRI.ADF.Geometries.Point(arr[z+1],arr[z+2],map.get_spatialReference());
            
             var imagUrl = "images/Flages/0.png";
//         if(arr[z] <= 50)
//         {
//            imagUrl = "images/Flages/1.png"
//         }
         
         var adfMarkerSymbol = new ESRI.ADF.Graphics.MarkerSymbol(imagUrl, 12, 12, "hand");

         
         //adfMarkerSymbol.set_centerX(20); // need to set to djust ballon
        // adfMarkerSymbol.set_centerY(23);
          var attributes = new Object;
          attributes.title= TitleContent;
          attributes.content = DescContent;
          attributes.id=z;
          graphicFeature = new ESRI.ADF.Graphics.GraphicFeature(point, adfMarkerSymbol, attributes); 
          //graphicFeature.id = z;
          
         map.addGraphic(graphicFeature);

         _oldpoints[_oldpoints.length]= graphicFeature;
         graphicFeature.add_mouseOver(graphicMouseOverHandler);
         graphicFeature.add_mouseOut(graphicMouseOutHandler);
            graphicFeature.add_click(graphicClickHandler);
            
            _called=true;
            zoomToPoint(arr[z+1],arr[z+2]);
            
             
        } 
    }
    catch(ee)
    {
        //alert(arr[i])
    }
 
    
}

function ativaOptionsDisabled(){
    var sels = document.getElementsByTagName('select');
    for(var i=0; i < sels.length; i++){
    if(sels[i].id == "fpAddService_AddNewService1_cmbAddServiceType")
    {
        sels[i].onchange= function(){ //pra se mudar pro desabilitado
            if(this.options[this.selectedIndex].disabled){
                if(this.options.length<=1){
                    this.selectedIndex = -1;
                }else if(this.selectedIndex < this.options.length - 1){
                    this.selectedIndex++;
                }else{
                    this.selectedIndex--;
                }
            }
        }
        if(sels[i].options[sels[i].selectedIndex].disabled){
            //se o selecionado atual é desabilitado chamo o onchange
            sels[i].onchange();
        }    
        for(var j=0; j < sels[i].options.length; j++){ //colocando o estilo
            if(sels[i].options[j].disabled){
                sels[i].options[j].style.color = '#990000';
                
                
            }
        }
      }
    }
}
