function srGetAjaxObject(){
  var ajaxObj = null;
  
  try{
    ajaxObj = new XMLHttpRequest();
  } catch(e){
  
    try{
      ajaxObj = new ActiveXObject("Msxml2.XMLHTTP");
    
    }catch(e){
     
      try{
         ajaxObj = new ActiveXObject("Microsoft.XMLHTTP");
       }catch(e){
       
         
           try{
             
             ajaxObj = new window.createRequest();
           
           }catch(e){
             ;
           }
         
       }
    
    }
  }
  return ajaxObj;
  
}

function frameRedirect(url) {
	  var elm = document.getElementById("frGeneral");
	  if(!elm) {
	  	return;
	  }
	  elm.src = url;
	  		
}
function popupWindow(url, winTitle){
 if(typeof(winTitle) != 'string') {
   winTitle = "Geodata";
 }
 winTitle = winTitle.replace(/[^a-zA-Z0-9_]+/g, "");
 metadataWin = window.open(url , winTitle, "width=766,toolbar=no,menubar=no,location=no,directories=no,status=no,resizable=yes,scrollbars=yes");
 metadataWin.focus();
}


//Y position of html object
function getY(obj)
{
   var yPos = 0;
   yPos = obj.offsetTop;
   obj = obj.offsetParent;
   
   while(obj){
     yPos = yPos + obj.offsetTop;
     obj = obj.offsetParent;
   }
   return yPos;
}

//X position of html object
function getX(obj)
{
   var xPos = 0;
   xPos = obj.offsetLeft;
   obj = obj.offsetParent;
   
   while(obj){
     xPos = xPos + obj.offsetLeft;
     obj = obj.offsetParent;
   }
   return xPos;
}

function closeContentTypeMenu(link){
  if (!link) return;
  if (!link.parentNode) return;
  link.parentNode.style.visibility = "hidden";
}

function hideIconLink(idMenuItem) {
  var menuItems = document.getElementById(idMenuItem);
  if (menuItems) menuItems.style.visibility = "hidden";
}

function mouseOverContentTypeMenu(menuItems) {
  if (!menuItems.timer) return;
  clearTimeout(menuItems.timer);
}

function onMouseOutIcon(iconDiv,idMenuItem) {
  var menuItems = document.getElementById(idMenuItem);
  if (!menuItems) return;
  menuItems.timer = window.setTimeout("hideIconLink('"+  idMenuItem + "')", 800)
}

function onMouseOutContentTypeMenu(menuItems, e){
  if (!e) e = window.event;  

  var rtg = (e.relatedTarget)? e.relatedTarget : e.toElement;

  for (var i=0;i<3;i++) {

    if (rtg == null) break;

    if (rtg == menuItems) return;

    rtg = rtg.parentNode;

  }

  menuItems.style.visibility = "hidden";


}

function onMouseOverIcon(icon, idMenuItem, leftSpace, prefix){
  var menuItems = document.getElementById(idMenuItem);
  //var icon  = document.getElementById(idIcon);
  var div = document.getElementById("resultDiv");
  positionSlidingMenu(menuItems, icon, leftSpace, div.scrollTop);
  
  if (!menuItems.timer) {
  } else clearTimeout(menuItems.timer);
 
  //Turn off every menuItem
  var i = 0;
  var popup = document.getElementById(prefix + i);
  
  while(popup){
    if(popup.style){
       popup.style.visibility = "hidden";
    }
    popup = document.getElementById(prefix + i);
    i++;
  }
  
  menuItems.style.visibility = "visible";
  menuItems.style.zIndex = 10;
}

function positionSlidingMenu(slidingDiv, iconDiv, leftSpace, top){

 
 if(!slidingDiv){
    if(window.status)
      window.status("slidingDiv not valid in positionSlidingMenu");
    return;
 }
 
 if(!iconDiv){
    if(window.status)
       window.status("iconDiv not valid in positionSlidingMenu");
    return;
 }
 
 if(!leftSpace){
    leftSpace = 0;
 }
 
 if(!top){
    top = 0;
 }
  //var slidingDiv = document.getElementById(slidingDiv);
  
  var y = parseInt(getY(iconDiv)) - parseInt(top);
  var x = parseInt(getX(iconDiv)) + parseInt(leftSpace);
  
  //slidingDiv.style.position = "absolute";
  slidingDiv.style.top = y + "px";
  slidingDiv.style.left = x + "px";
}

function addToMap(server, service, isWMS, serviceParam, uuid) {
  new sendEFWStatistic(uuid, sendEFWStatistic.ADD_TO_MAP );
  addToArcExplorerWeb(server, service, isWMS, uuid);
}


function highLightIcon(element, color, guid){
   //element.origBGColor = element.style.backgroundColor;
   element.parentNode.parentNode.parentNode.style.backgroundColor = color;
   if (self.mgc_mapHighlightResult) self.mgc_mapHighlightResult(guid);
}

function unHighLightIcon(element, guid){
  element.parentNode.parentNode.parentNode.style.backgroundColor = "#FFFFFF";
  if (self.mgc_mapHighlightResult) self.mgc_mapHighlightResult();
}

function highLight(index, color, guid) {
  setRowColor(index,"#FFFFCC");
  if (self.mgc_mapHighlightResult) self.mgc_mapHighlightResult(guid);
}

function unHighLight(index, guid){
  
  var sColor = "#FFFFFF";
  if(typeof(iconMode) == 'undefined' || iconMode == null)
    return;
  if ((typeof(iconMode) != 'undefined' && iconMode != null) && index % 2 == 0 && iconMode == false) { 
    sColor = "#E8F2FE";
  }
  setRowColor(index,sColor);
  if (self.mgc_mapHighlightResult) self.mgc_mapHighlightResult();
}

/*function unHighLight(index, guid){

   unHighLight(index, guid, true);
}*/

function setRowColor(index, color) {
  var iconRow = "iconRow_" + index;
  var textRow = "textRow_" + index;
  var words = "results:_metaViewLink_" + index + "_linkText";
  var textOrgname = "textOrgName_" + index;
  var cellIconView = "cell4IconView_" + index;
  iconRow = document.getElementById(iconRow);
  textRow = document.getElementById(textRow);
  words = document.getElementById(words);
  textOrgname = document.getElementById(textOrgname);
  cellIconView = document.getElementById(cellIconView);
  if (iconRow && textRow && words && textOrgname) {
    iconRow.style.backgroundColor = color;
    textRow.style.backgroundColor = color;
    textOrgname.style.backgroundColor = color;
  }
  
  if(cellIconView){
  
     cellIconView.style.backgroundColor = color;
  
  }
}

function srOpenUnknownService(guid, sServer) {
	 if(typeof(sServer) == 'string' && sServer.length >= 1) {
     var nw=window.open(sServer, "ViewSite","width=800,height=600,scrollbars");
     if(nw.focus) {
       nw.focus();
     }
     return;
  } 
}



function zoomTo(index) {
  if (self.mgc_mapZoomToResult) self.mgc_mapZoomToResult(index);
  return false;
}

function mds_onMapHover(nIndex,bHighlight) {
  var elIconRow = document.getElementById("iconRow_"+nIndex);
  var elIconView = document.getElementById("results:_icon4metadatalist_"+nIndex);
  if (elIconRow) {
    if (bHighlight) highLight(nIndex);
    else unHighLight(nIndex);
  } else if (elIconView) {
    var elHigh = elIconView;
    for (var i=1;i<=3;i++) {
      if (elHigh == null) break;
      elHigh = elHigh.parentNode;
    }
    if (elHigh) {
      var sColor = "#FFFFCC";
      if (!bHighlight) sColor = "#FFFFFF";
      elHigh.style.backgroundColor = sColor;
    }
  }
}

/**
 * Generic function to add
 * function on load
 */
function srAddLoadEvent(eventFunc) {
  var loadFunction = window.onload;
  
  window.onload = function() {
    if(typeof loadFunction == 'function') {
       loadFunction();
    }
    if(typeof eventFunc == 'function') {
       eventFunc();
    }
  }
}

/*
 * Check the service by gathering all guid's
 * needed & dispatching them to the service checker
 */
function srServiceChecker() {
	if(!recordsJson) {
		return;
	}
	var resultItems = recordsJson;
	if(!resultItems || !resultItems.length) {
		return; 
	}
	var guids = "";
	var hasLiveData = false;
	var guidToIndex = new Array();
	var indexToGuid = new Array();
	for(var i = 0; i < resultItems.length; i++) {
		
		if(resultItems[i].type && resultItems[i].type == '1') {
			 
			 guidToIndex[resultItems[i].id.toString()] = i;
			 indexToGuid[i] = resultItems[i].id.toString();
			 
			 guids = guids + resultItems[i].id;
			 hasLiveData = true;
			 if(i < resultItems.length - 1) {
			 	guids = guids + ",";
			 	
			 }
		}
		
	}
	if(hasLiveData == false) {
		return;
	}
	var url = "/E-FW/statuschecker?ids=" + guids;
	var ids = "ids=" + escape(guids);
	var ajaxObj = srGetAjaxObject();
	ajaxObj.onreadystatechange = function() {
		 var rState = ajaxObj.readyState;
		 if(rState == 4) {
	 
	    srServiceStatus(ajaxObj.responseText, guidToIndex, indexToGuid);
		 }
	}
	try {
   ajaxObj.open("POST", url, true);
   ajaxObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   ajaxObj.setRequestHeader("Content-length", ids.length);
   ajaxObj.setRequestHeader("Connection", "close");
   ajaxObj.send(ids);
 }catch(err) {   
  
 }

}
/*
 * Will get the response and print out the appropriate image
 */
function srServiceStatus(response, guidToIndex, indexToGuid) {
	  var empty = false;
	  if(!response) {
	  	 response="";
	  }
   var status = eval(response);
   if(!status || status.length <= 0 ) {
   	 status = new Array();
   	 for(var i = 0; i < indexToGuid.length; i++) {
   	 	 status[i] = new Object();
   	 	 status[i].id = indexToGuid[i];
   	 	 status[i].status = "EsriNoConection";
   	 }
   }
   var root4img = "/Portal/images/serviceChecker";
   
   for(var i = 0; i < status.length; i++) {
   	  var sId = status[i].id;
   	  var sStatus = status[i].status;
   	  var iStatus = parseInt(status[i].status);
   	  var index = guidToIndex[sId];
   	  
   	  var elTitle = 
   	    document.getElementById("results:_metaViewLink_" + index);

   	  if(!elTitle) {
   	  	continue;
   	  }
   	 
   	  var info = iStatus + "/100";
   	  var imgSrc = root4img;
   	        	  
   	  if(sStatus == "EsriNoConnection") {
   	  	 imgSrc = imgSrc + "NoInformation16.png";
        info = "N/A"
   	  } else if(sStatus == "NoData") {
   	  	 imgSrc = imgSrc + "Unknown16.png";
   	  	 info = "Unknown";
   	  } else if(sStatus == "NoFoundId") {
   	  	 imgSrc = imgSrc + "Unknown16.png";
        info = "Unknown";
   	  } else if(iStatus == NaN){
   	  	 imgSrc = imgSrc + "Unknown16.png";
        info = "Unknown";
   	  } else if(iStatus < 0) {
   	  	 imgSrc = imgSrc + "Unknown16.png";
        info = "Unknown";
   	  } else if(iStatus <= 25) {
   	  	 imgSrc = imgSrc + "VeryBad16.png";
   	  } else if(iStatus <= 50 ) {
   	    imgSrc = imgSrc + "Bad16.png";
   	  } else if(iStatus <= 75 ) {
   	  	 imgSrc = imgSrc + "Good16.png";
   	  } else if(iStatus > 75 && iStatus <= 100) {
   	  	 imgSrc = imgSrc + "Excellent16.png";
   	  } else {
   	    imgSrc = imgSrc + "Unknown16.png";
        info = "Unknown";
   	 	}
   	 
   	  var elImg = document.createElement("img");
   	  elImg.src = imgSrc;
   	  elImg.setAttribute("src", imgSrc);
   	  elImg.alt = "Service Availability: "+info;
      elImg.setAttribute("alt", "Service Availability: "+ info);
      elImg.title = "Service Availability: "+ info;
      elImg.setAttribute("title", "Service Availability: "+ info);
      elImg.onclick = function(){srServiceAvailabilityHelp();}
      //elImg.setAttribute("onclick", "srServiceAvailabilityHelp()");
      elImg.style.cursor = "pointer";
      //elImg.setAttribute("style", "cursor: pointer;");
   	  elTitle.parentNode.insertBefore(elImg, elTitle.parentNode.firstChild)
   	  
   }

}

/*
 * Help page showing service availability
 */
function srServiceAvailabilityHelp() {
	  // TODO: Change to help page url
	 var url = "/gos/help/Search.htm#serviceCheckerHelp";
	 var win = window.open(url, "ServiceAvailabilitySite", 
    "toolbar=yes,location=no,scrollbars=yes,resizable=yes,width=800,height=564,left=5,top=5");
	 if(win.focus) {
     win.focus();
     return false;
  }
}
/*
 * Given a guid, a page will be generated with service info
 * @guid Guid for the service
 */
function srCheckServiceAvailability(guid) {
	 if(!guid) {
	 	 alert("Could not get the id of the service");
	 	 return;
	 }
	 if(typeof(serviceCheckerURL) == 'undefined') {
	 	 alert("Service checker url not defined");
	 	 return;
	 }
	 serviceCheckURL = serviceCheckerURL + "?ClientId=" + guid;
	 var win = window.open(serviceCheckURL, "ServiceAvailabilitySite", 
	   "toolbar=yes,location=no,scrollbars=yes,resizable=yes,width=800,height=564,left=5,top=5");
  if(win.focus) {
     win.focus();
  }
  return false;

}

