﻿// JScript File

function buildMultiPropertyForm(result)
{
  document.getElementById('locatorPanel').style.visibility='hidden';
  $find('locatorMPE').hide();
  
  var formhtml; 
  
  formhtml = "<p class='formText'>Multiple locations were found that match search criteria</p>";
  formhtml += "<p class='formText'>Please select from the list below</p>";
  formhtml += "<table width='320px' border='0'>";
  formhtml += "<tr>"
  formhtml += "<td class='formText'><strong>Address</strong></td>";
  formhtml += "</tr>";
  formhtml += "</table>";
  
  formhtml += "<table width='320px' border='0' cellpadding='3' cellspacing='3'>";
  var rootElem = result.getElementsByTagName("Address");
  var i = 0;
  for (i; i < rootElem.length; i++) 
  {
    //alert(rootElem[i].getElementsByTagName("str_no").item(0).text);
    formhtml += "<tr>";
    //formhtml += "<td align='center'>"+rootElem[i].getElementsByTagName("str_no").item(0).text + " " + rootElem[i].getElementsByTagName("str_name").item(0).text + " " + rootElem[i].getElementsByTagName("mailing_city").item(0).text + " " + rootElem[i].getElementsByTagName("post_code").item(0).text+"</td>";
    formhtml += "<td class='formText'><a href=\"javascript:processMultiProperty("+rootElem[i].getElementsByTagName("strno")[0].childNodes[0].nodeValue+",'"+rootElem[i].getElementsByTagName("strname")[0].childNodes[0].nodeValue+"','"+rootElem[i].getElementsByTagName("muniname")[0].childNodes[0].nodeValue+"','"+rootElem[i].getElementsByTagName("postcode")[0].childNodes[0].nodeValue+"');\">"+rootElem[i].getElementsByTagName("strno")[0].childNodes[0].nodeValue + " " + rootElem[i].getElementsByTagName("strname")[0].childNodes[0].nodeValue + " " + rootElem[i].getElementsByTagName("mailingcity")[0].childNodes[0].nodeValue + " " + rootElem[i].getElementsByTagName("postcode")[0].childNodes[0].nodeValue+"</a></td>";
    
    formhtml += "</tr>";
    
  document.getElementById("divActionBodyText").innerHTML = formhtml;
  document.getElementById('actionPanel').style.visibility='visible';
  $find('actionMPE').show();
  }
}  

function buildClosestPropertyForm(result)
{
  document.getElementById('locatorPanel').style.visibility='hidden';
  $find('locatorMPE').hide();
  
  //var formheaderhtml;
  var formbodyhtml; 
  
  formbodyhtml = "<p class='formTextHeader'>An exact match could not be found.</p>";
  formbodyhtml += "<p class='formText'>Listed are the closest matches to your search criteria</p>";
  formbodyhtml += "<table width='320px' border='0'>";
  formbodyhtml += "<tr>"
  formbodyhtml += "<td class='formText'>Please select a location</td>";
  formbodyhtml += "</tr>";
  formbodyhtml += "</table>";
  formbodyhtml += "<br/>";
  formbodyhtml += "<table width='320px' border='0' cellpadding='3' cellspacing='3'>";
  var rootElem = result.getElementsByTagName("Address");
  var i = 0;
  for (i; i < rootElem.length; i++) 
  {
    //alert(rootElem[i].getElementsByTagName("str_no").item(0).text);
    formbodyhtml += "<tr>";
    //formhtml += "<td align='center'>"+rootElem[i].getElementsByTagName("str_no").item(0).text + " " + rootElem[i].getElementsByTagName("str_name").item(0).text + " " + rootElem[i].getElementsByTagName("mailing_city").item(0).text + " " + rootElem[i].getElementsByTagName("post_code").item(0).text+"</td>";
    formbodyhtml += "<td class='formText' style='text-align: left'>- <a href=\"javascript:processMultiProperty("+rootElem[i].getElementsByTagName("str_no")[0].childNodes[0].nodeValue+",'"+rootElem[i].getElementsByTagName("str_name")[0].childNodes[0].nodeValue+"','"+rootElem[i].getElementsByTagName("muni_name")[0].childNodes[0].nodeValue+"','"+rootElem[i].getElementsByTagName("post_code")[0].childNodes[0].nodeValue+"');\">"+rootElem[i].getElementsByTagName("str_no")[0].childNodes[0].nodeValue + " " + rootElem[i].getElementsByTagName("str_name")[0].childNodes[0].nodeValue + " " + rootElem[i].getElementsByTagName("mailing_city")[0].childNodes[0].nodeValue + " " + rootElem[i].getElementsByTagName("post_code")[0].childNodes[0].nodeValue+"</a></td>";
    
    formbodyhtml += "</tr>";
  }
  
  formbodyhtml += "</table>";
  document.getElementById("divActionBodyText").innerHTML = formbodyhtml;
  document.getElementById('actionPanel').style.visibility='visible';
  $find('actionMPE').show();
}

function buildMultiPropertyUnitForm(result)
{
  //alert("Got here!");
  
  document.getElementById('locatorPanel').style.visibility='hidden';
  $find('locatorMPE').hide();

  var formhtml; 
  var gid = "";
  var addr = "";
  var stnum;
  var stname;
  var muniname;
  var postcode;
  
  var rootElem = result.getElementsByTagName("Records");

  addrgid = rootElem[0].getAttribute("addrgid");
  stnum = rootElem[0].getAttribute("strno");
  stname = rootElem[0].getAttribute("strname");
  muniname = rootElem[0].getAttribute("muniname");
  postcode = rootElem[0].getAttribute("postcode");
  
  //alert("Juan");
  formhtml = "<p class='addressText'>This location is associated with multiple units</p>";
  formhtml += "<p class='formText'>Please select one of the following options</p>";
  formhtml += "<table width='350px' border='0'>";
  formhtml += "<tr>";
  formhtml += "<td class='formText'><input id='radMultiPropertyUnit' name='radMultiPropertyUnit' type='radio' onclick='javascript:checkRadMultiPropertyUnit(this.value);' value='PRIMARY' checked>proceed to the primary address</td>";
  formhtml += "</tr>";
  formhtml += "<tr>";
  formhtml += "<td class='formText'> &nbsp; &nbsp; &nbsp;<strong> " + stnum + " " + stname + "<br/>" + muniname + " " + postcode+"</strong></td>";
  formhtml += "</tr>";
  formhtml += "<tr>";
  formhtml += "<td>&nbsp;</td>";
  formhtml += "</tr>";
  formhtml += "<tr>";
  formhtml += "<td class='formText'><input id='radMultiPropertyUnit' name='radMultiPropertyUnit' type='radio' value='SECONDARY' onclick='javascript:checkRadMultiPropertyUnit(this.value);'>"; 
  formhtml += "&nbsp;select from list of units</td>";
  formhtml += "</tr>";
  formhtml += "<tr>";
  formhtml += "<td class='formText'> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  formhtml += "<select name='selUnits' disabled=true id='selUnits' class='formText'>";
  var addElem = result.getElementsByTagName("Address");
  var i = 0;
  
  gid = addrgid;
  addr = stnum + " " + stname;
  for (i; i < addElem.length; i++) 
  {  
    //if (addElem[i].getElementsByTagName("unit")[0].childNodes[0].nodeValue == "0") 
    //{gid = addElem[i].getElementsByTagName("addrgid")[0].childNodes[0].nodeValue;}
    //else {formhtml += "<option value='"+addElem[i].getElementsByTagName("addrgid")[0].childNodes[0].nodeValue+"'>"+addElem[i].getElementsByTagName("unit")[0].childNodes[0].nodeValue+"</option>";} 

    if (addElem[i].getElementsByTagName("unit")[0].childNodes[0] == null)
      {
        formhtml += "<option value='"+addElem[i].getElementsByTagName("addrgid")[0].childNodes[0].nodeValue+"'>NULL</option>";
      }
    else 
      { 
        formhtml += "<option value='"+addElem[i].getElementsByTagName("addrgid")[0].childNodes[0].nodeValue+"'>"+addElem[i].getElementsByTagName("unit")[0].childNodes[0].nodeValue+"</option>";
      } 
  }


  formhtml += "</select>";
  formhtml += "</td>";
  formhtml += "</tr>";
  formhtml += "<tr>";
  formhtml += "<td><input name='pGID' type='hidden' id='pGID' value='"+gid+"'></td>";
  formhtml += "</tr>";
  formhtml += "<tr>";
  formhtml += "<td>&nbsp;<input name='pAddress' type='hidden' id='pAddress' value='"+addr+"'></td>";
  formhtml += "</tr>";
  formhtml += "<tr>";
  formhtml += "<td>&nbsp;&nbsp;<input type='button' name='Button' value='Go to Location' class='style1' onClick=\"javascript:getPropertyUnitLocation();\"></td>";
  formhtml += "</tr>";
  formhtml += "<tr>";
  formhtml += "</tr>";
  formhtml += "</table>";

  document.getElementById("divActionBodyText").innerHTML = formhtml;
  document.getElementById('actionPanel').style.visibility='visible';
  $find('actionMPE').show();
}


function checkRadMultiPropertyUnit(unittype)
{
  var f_selUnits = document.getElementById("selUnits");
  
  if (unittype == "PRIMARY")
  {
    f_selUnits.disabled=true;
  }
  else 
  {
    f_selUnits.disabled=false;
  }
}

function getPropertyUnitLocation()
{
  //alert("Got here");
  //alert(document.forms[0].radMultiUnit.length);
  var f_radMultiPropertyUnit = document.forms[0].radMultiPropertyUnit;
  var f_selUnits = document.forms[0].selUnits;
  //alert([1].value);
    for (var i=0;i<f_radMultiPropertyUnit.length;i++)
    {
      if (f_radMultiPropertyUnit[i].checked == true)
      {
         if (f_radMultiPropertyUnit[i].value == "PRIMARY")
         {
           //alert("Primary selected: " + document.getElementById("pPinNum").value);
           findPropertyUnit(document.getElementById("pGID").value,document.getElementById("pAddress").value);
         }
         else if (f_radMultiPropertyUnit[i].value == "SECONDARY")
         {
           //alert("Secondary selected: " + f_selUnits.options[f_selUnits.selectedIndex].value);
           findPropertyUnit(f_selUnits.options[f_selUnits.selectedIndex].value,"");
         }
       }    
    }        
 }
