﻿var map;

function Search(){
    var rdoValue = GetRadioValue('rdoSearch');
    var LatLong;
    
    if(rdoValue == "STREET"){
        var txtStreet = document.getElementById("txtStreet");
        if(txtStreet.value == ''){ alert('No Street Address was entered'); return false; }
        
            map.Find(null, txtStreet.value);
        
    }
    else if(rdoValue == "POI"){
        var txtPOI = document.getElementById("txtPoI");
        if(txtPOI.value == ''){ alert('No Point of Interest Address was entered'); return false; }
        map.Find(null, txtPOI.value);
    }
    else if(rdoValue == "LL"){
    var txtLatSearch = document.getElementById("txtLatSearch");
    var txtLongSearch = document.getElementById("txtLongSearch");
      if(txtLatSearch.value == ''){ alert('No latitude was entered'); return false; }
      if(txtLongSearch.value == ''){ alert('No Longitude was entered'); return false; }
       
      LatLong = new VELatLong(txtLatSearch.value, txtLongSearch.value); 
      map.SetCenter(LatLong);
    }
    var divSearch = document.getElementById("Search");
    divSearch.style.display = 'none'; 
}

function ValChanged(type, searchPage){
    document.getElementById(type).checked = true;
    if(searchPage){ GreyInputs(type); }
}

function GreyInputs(type){
    var Enable = '#FFF';
    var Disable = '#DEDEDE';
    var valAddress; var valLoc; var valLL;
    
    if(type == 'rdoLL')
    {
        valAddress = Disable;
        valLoc = Disable;
        valLL = Enable;
    }
    if(type == 'rdoLocation')
    {
        valAddress = Disable;
        valLoc = Enable;
        valLL = Disable;
    }
    if(type == 'rdoStreet'){
        valAddress = Enable;
        valLoc = Disable;
        valLL = Disable;
    }
    
    
        document.getElementById("txtAddStreet").style.backgroundColor = valAddress;
        document.getElementById("txtAddCity").style.backgroundColor = valAddress;
        document.getElementById("ctl00_Main_ddlStateList").style.backgroundColor = valAddress;
    
        document.getElementById("ctl00_Main_ddlState").style.backgroundColor = valLoc;
        document.getElementById("ctl00_Main_ddlCity").style.backgroundColor = valLoc;
        
        document.getElementById("txtLatitude").style.backgroundColor = valLL;
        document.getElementById("txtLongitude").style.backgroundColor = valLL;

   
}
function EnterKey(){ if(window.event && window.event.keyCode == 13) { Search();} }

function DisplaySearch(){
var divSearch = document.getElementById("Search");
    if(divSearch.style.display == 'block'){ divSearch.style.display = 'none'; }
    else {
         posY = getScreenCenterY();  
         posX = getScreenCenterX();  
           
         var divSearch = document.getElementById("Search");
         divSearch.style.cssText = "z-index:999;position:fixed;top:"+(posY - 150)+"px;left:"+(posX - 300)+"px;"; 
         
     
         document.getElementById("txtStreet").value = "";
         
         document.getElementById("txtPoI").value = "";
         document.getElementById("txtLatSearch").value = "";
         document.getElementById("txtLongSearch").value = "";
         
         divSearch.style.display = 'block';
         document.getElementById("txtStreet").focus();
         document.getElementById("rdoStreet").checked = true;
    }
}
 
 function getScreenCenterY() {  
 var y = 0;  
   
 y = getScrollOffset()+(getInnerHeight()/2);  
   
 return(y);  
 }  
   
 function getScreenCenterX() {  
    return(document.body.clientWidth/2);  
 }  
   
 function getInnerHeight() {  
     var y;  
     if (self.innerHeight) // all except Explorer  
     {  
     y = self.innerHeight;  
     }  
     else if (document.documentElement && document.documentElement.clientHeight)  
     // Explorer 6 Strict Mode  
     {  
     y = document.documentElement.clientHeight;  
     }  
 else if (document.body) // other Explorers  
 {  
 y = document.body.clientHeight;  
 }  
 return(y);  
 }  
  
 function getScrollOffset() {  
    var y;  
     if (self.pageYOffset) // all except Explorer  
     {  
     y = self.pageYOffset;  
     }  
     else if (document.documentElement && document.documentElement.scrollTop)  
     // Explorer 6 Strict  
     {  
     y = document.documentElement.scrollTop;  
     }  
     else if (document.body) // all other Explorers  
     {  
     y = document.body.scrollTop;  
     }  
    return(y);   
 }  
 
 
 //Search page functions
 var map;
 var layer = new VEShapeLayer();
 var results;

 
  function GetHiddenMap(){
            if(navigator.userAgent.indexOf("Firefox")!=-1){
                var ffv = 0;
                var ffn = "Firefox/"
                var ffp = navigator.userAgent.indexOf(ffn);
                if (ffp != -1) ffv = parseFloat(navigator.userAgent.substring(ffp + ffn.length));
                
                if (ffv >= 1.5) {
                    Msn.Drawing.Graphic.CreateGraphic=function(f,b) { return new Msn.Drawing.SVGGraphic(f,b) }
                }
            }
            map = new VEMap('Map');
    
            map.LoadMap();
        }
 function FindResults(btnPress){
    var rdoValue = GetRadioValue('rdoProjSearch');
    var City;
        var Address;
        var StateDD;
        var Latitude;
        var Longitude;
    switch(rdoValue){  
        case 'ADD':
            City = document.getElementById("txtAddCity");
            if(btnPress){ if(City.value == ""){ alert('A city name is required for searching'); return false; } }
            else { if(City.value == "") { return false; } }
            
            StateDD = document.getElementById("ctl00_Main_ddlStateList");
            if(btnPress) { if(StateDD[StateDD.selectedIndex].value == 'Select '){ alert('A state selection is required for searching'); return false; } }
            else { if(StateDD[StateDD.selectedIndex].value == 'Select ') { return false; } }
            
            Address = document.getElementById("txtAddStreet");
   
            results = map.Find('addr', Address.value + " " + City.value + ", " + StateDD[StateDD.selectedIndex].value, null, layer, 0, 10, true, true, false, true, GetResults);
            //var div = document.getElementById("Map").style.display = "block";
        break;
       
        case 'LOC':
            City = document.getElementById("ctl00_Main_ddlCity");
            //City is optional
            StateDD = document.getElementById("ctl00_Main_ddlState");
            if(btnPress) { if(StateDD[StateDD.selectedIndex].value == 'Select'){ alert('A State must be selected for searching'); return false; } }
            else { if(StateDD[StateDD.selectedIndex].value == 'Select') { return false; } }
            KeyView.KASDefaultAjax.GetResultsByLocation(City[City.selectedIndex].value, StateDD[StateDD.selectedIndex].value, ResultsEval);
        break;
        
        case 'LL':
            Latitude = document.getElementById("txtLatitude");
            if(IsNumeric(Latitude.value) == false) { return false; }
            if(btnPress) { if(Latitude.value == '') { alert('A Latitude must be entered for searching.'); return false; } }
            else { if(Latitude.value == '') { return false; } }
            
            Longitude = document.getElementById("txtLongitude");
             if(IsNumeric(Longitude.value) == false) { return false; }
            if(btnPress) { if(Longitude.value == '') { alert('A Longitude must be entered for searching.'); return false; } }
            else { if(Longitude.value == '') { return false; } }
            var aryLatLong = new Array();
            aryLatLong.push(Latitude.value);
            aryLatLong.push(Longitude.value);
            KeyView.KASDefaultAjax.GetResultsByLatLong(aryLatLong, ResultsEval);
        break;
        default:
            return false;
        break;
    }
 }
 
 function GetResults(layer, resultsArray, places, hasMore, veErrorMessage)
 {
     var aryResults = new Array();
  if(places.length > 0){
        for (x = 0; x < places.length; x++) 
        {
            aryResults.push(places[x].LatLong.Latitude);
            aryResults.push(places[x].LatLong.Longitude);
        }
    KeyView.KASDefaultAjax.GetResultsByLatLong(aryResults, ResultsEval);
    
  }
 }
 
 function ResultsEval(arg){
    var div = document.getElementById("divResults");
    div.innerHTML = arg;
    div.style.display = 'block';
    
    if(arg.indexOf('No results found.',0) > -1){ document.getElementById('divView').style.display = 'none'; }
    else { document.getElementById('divView').style.display = 'block'; }
 }
 
function GetRadioValue(rdoName){
var radioObj = document.forms[0].elements[rdoName];
//if(blnSelection==true){ radioObj = document.forms[0].elements['rdoSearch']; }
//else {  radioObj = document.forms[0].elements['rdoSelection']; }
    
    if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
};

function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;
   if(strString.length == 1 && strString == "-") return false;
    
   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }
   
function PrepareView(){
    var rdoValue = GetRadioValue("rdoProjSel");
    document.getElementById("ctl00_Main_hdnView").value = rdoValue;
    if(rdoValue != undefined && rdoValue != ''){ return true; }
    else { return false; }
}