
var add_http_request = false;
var ctladdress,ctlcity,ctlprovince,ctlpostal,ctlcountry;

add_drawdiv();

function add_funccallback(url) {
        var add_http_request = false;
       // _callback_control=htmlcontrol;
        if (window.XMLHttpRequest) { 
            add_http_request = new XMLHttpRequest();
            if (add_http_request.overrideMimeType) {
                add_http_request.overrideMimeType('text/plain');
                // See note below about this line
            }
        } else if (window.ActiveXObject) { // IE
            try {
                add_http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    add_http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!add_http_request) {
            return false;
        }
        add_http_request.onreadystatechange = function() { add_ajaxcallback(add_http_request); };
        add_http_request.open('GET', url, true);
        add_http_request.send(null);

    }


function _checkaddress(entity,lksrc,_ctladd,_ctlcity,_ctlprov,_ctlcountry,_ctlpostal)
{ctladdress=_ctladd;
 ctlcity=_ctlcity;
 ctlprovince=_ctlprov;
 ctlpostal=_ctlpostal;
 ctlpostal.value=ctlpostal.value.replace(/\s/g,'');
 ctlcountry=_ctlcountry;
  document.getElementById('add_div').display='none';

 if (lksrc==null&&(ctlcountry!=null&&ctlcountry.value!='10'&&ctlcountry.value.toLowerCase()!='ca'&&ctlcountry.value.toLowerCase()!='canada'&&ctlcountry.value!=''&&ctlcountry.value.toLowerCase().indexOf('canada')<0))
  return;
if (lksrc==null)
  add_funccallback('/Ajax/aj_addresslookup.jsp?address='+encodeuri(ctladdress.value)+'&city='+encodeuri(ctlcity.value)+'&province='+encodeuri(ctlprovince.value)+'&postalcode='+encodeuri(ctlpostal.value));
else
 { var srcid="";
   if (lksrc.indexOf('contacts')>-1)
     { if(ctlcity.form.contacttxt ==null){
       srcid=ctlcity.form.eventid.value;
      lksrc='event';
        }else
        srcid=ctlcity.form.contact.value;
    }
   if (lksrc.indexOf('assigned')>-1)
        srcid+=","+ctlcity.form.assigned.value;

 if (lksrc.indexOf('owner')>-1)
        srcid+=","+ctlcity.form.ownerid.value;

  if (srcid==null)
    alert('Invalid lookup source.');
  else{
     if(srcid.charAt(0)==',')
           srcid=srcid.substring(1);
    add_funccallback('/Ajax/aj_addresslookup.jsp?address=&city=&province=&postalcode=&srcid='+srcid+'&src='+lksrc);
   }
    
 }
}

function encodeuri(value)
{ value=value.replace(/\,/g,"%2C");
  value=value.replace(/\s/g,"+");
 return value;
 }

function add_ajaxcallback(req)
{
 if (req.readyState == 4) {
           if (req.status == 200) {
             var mstr=req.responseText; 
             if(mstr.indexOf("OK::")>-1){
              mstr=mstr.substring(mstr.indexOf('OK::')+4);
               processreturn(mstr);  
          
           } else {
            alert(mstr);
           }
            

     }else
      alert(req.responseText);
}
}


function processreturn(s)
{if (s.length>5){ //valid result
 var headerspace=120;  //120 pixels for header
  var rowspace=16;  //16 pixels per row
  var html="<form name='_frmadd'><table cellpadding=2 cellspacing=0 border=0 class=Form width='100%' style='border-collapse:collapse'><tr><th align=left style='border-top:1px solid #000000;border-bottom:1px solid #000000;'>Building</th><th align=left style='border-top:1px solid #000000;border-bottom:1px solid #000000;'>From</th><th align=left style='border-top:1px solid #000000;border-bottom:1px solid #000000;'>To</th><th align=left style='border-top:1px solid #000000;border-bottom:1px solid #000000;'>Street</th>";
     html+="<th align=left style='border-top:1px solid #000000;border-bottom:1px solid #000000;'>Type</th><th align=left style='border-top:1px solid #000000;border-bottom:1px solid #000000;'>City</th><th align=left style='border-top:1px solid #000000;border-bottom:1px solid #000000;'>Province</th><th align=left style='border-top:1px solid #000000;border-bottom:1px solid #000000;'>Postal Code</th></tr>";
  if(s.length>0)
  {var adddiv=document.getElementById('add_div');
   var adddata=document.getElementById('add_data');
   
   var rows=s.split(";");
   adddiv.style.height=headerspace+(rows.length*rowspace) +'px';
   var top=((screen.height-(headerspace+(rows.length*rowspace)))/2) -200;
   adddiv.style.top=top+'px';
   var count=0;
   for (var i=0;i<rows.length;i++)
       if(rows[i].length>4)
                  { var cols=rows[i].split(/\|/);
                    html+='<tr onMouseOver="this.bgColor=\'yellow\'" onMouseOut="this.bgColor=\'\'"><td style="border-bottom:1px #DFDFDF dashed"><a href=javascript:popaddress('+count+',1)>'+ cols[0] +'</a></td>' ; //~adbuilding~|~adstreetfromnum~|~adstreetto~|~adstreet~|~adstreettype~|~admunname~|~adprov~|~adpostal~;       
                    html+='<td style="border-bottom:1px #DFDFDF dashed"><a href=javascript:popaddress('+count+',0)>'+ cols[1] +'</a></td><td>';
                   if(cols[2]!=cols[1])
                      html+='<a href=javascript:popaddress('+count+',0)>'+ cols[2] +'</a>';
                     else
                      html+='&nbsp;';

                    html+='</td><td style="border-bottom:1px #DFDFDF dashed"><a href=javascript:popaddress('+count+',0)>'+

                        cols[3] +'</a></td><td style="border-bottom:1px #DFDFDF dashed"><a href=javascript:popaddress('+count+',0)>'+ cols[4] +'</a></td><td style="border-bottom:1px #DFDFDF dashed"><a href=javascript:popaddress('+count+',0)>'+ cols[5] +'</a></td><td style="border-bottom:1px #DFDFDF dashed"><a href=javascript:popaddress('+count+',0)>'+
                         cols[6] +'</td><td style="border-bottom:1px #DFDFDF dashed"><a href=javascript:popaddress('+count+',0)>'+
                         cols[7]+'</a></td>'+
                         '<input type=hidden name=a0 value="'+cols[0]+'">'+
                         '<input type=hidden name=a1 value="'+cols[1]+'">'+
                         '<input type=hidden name=a2 value="'+cols[2]+'">'+
                         '<input type=hidden name=a3 value="'+cols[3]+'">'+
                         '<input type=hidden name=a4 value="'+cols[4]+'">'+
                         '<input type=hidden name=a5 value="'+cols[5]+'">'+
                         '<input type=hidden name=a6 value="'+cols[6]+'">'+
                         '<input type=hidden name=a7 value="'+cols[7]+'"></tr>';
                    count++;
                   }
   html+="</table></form>";
  adddata.innerHTML=html;
  adddiv.style.display="block"; 
 
 }
}

}
function add_drawdiv()
{ var divwidth=700;
  var divheight=400;
  var top=(screen.height-divheight)/2;
  var left=(screen.width-divwidth)/2;

document.write('<div id="add_div" style="z-index:200;display:none;position:absolute;left:'+left+'px;top:'+top+'px;width:'+divwidth+';height:120px;');
document.write('border:1px solid #000000;background-color:ffffff;opacity:0.9;filter:alpha(opacity=90);">');
document.write('<center><div style="font-weight:bold;font-size:18px;padding:8px">Addresses Found</div></center>'); 
document.write('<div id="add_data"></div><center><input type=button value="Close" class=ButtonSm');
document.write(' onclick="document.getElementById(\'add_div\').style.display=\'none\';"></center></div>'); 
 document.close();

}
function popaddress(idx,addbuilding)
{ var frm=document._frmadd;
var building, from,to, street,streettype, city, prov, postal, add;
  
  if(frm.a0.length==null)
{  building=frm.a0.value=='-'?'':frm.a0.value;
   from=frm.a1.value=='-'?'':frm.a1.value;
   to=frm.a2.value=='-'?'':frm.a2.value;
   street=frm.a3.value=='-'?'':frm.a3.value;
   streettype=frm.a4.value=='-'?'':frm.a4.value;
   city=frm.a5.value=='-'?'':frm.a5.value;
   prov=frm.a6.value=='-'?'':frm.a6.value;
   postal=frm.a7.value=='-'?'':frm.a7.value;
} else
  {
building=frm.a0[idx].value=='-'?'':frm.a0[idx].value;
   from=frm.a1[idx].value=='-'?'':frm.a1[idx].value;
   to=frm.a2[idx].value=='-'?'':frm.a2[idx].value;
   street=frm.a3[idx].value=='-'?'':frm.a3[idx].value;
   streettype=frm.a4[idx].value=='-'?'':frm.a4[idx].value;
   city=frm.a5[idx].value=='-'?'':frm.a5[idx].value;
   prov=frm.a6[idx].value=='-'?'':frm.a6[idx].value;
   postal=frm.a7[idx].value=='-'?'':frm.a7[idx].value;
/*  
   building=frm.a0[idx].value;
   from=frm.a1[idx].value;
   to=frm.a2[idx].value;
   street=frm.a3[idx].value;
   streettype=frm.a4[idx].value;
   city=frm.a5[idx].value;
   prov=frm.a6[idx].value;
   postal=frm.a7[idx].value;*/
  }
from=from.replace(/\,/,'');
if(addbuilding==1&&building.length>1)
   add=building+', '+from +' '+street +' ' +streettype;
else
  add=from +' '+street +' ' +streettype;
 //if(ctladdress.value=='')
      ctladdress.value=add;
 //else if (ctladdress.value.length<add.length)
   //  ctladdress.value=add;
 
 ctlcity.value=city
 if(ctlprovince.type=='select')
  matchcombo(ctlprovince,prov);
 else
  ctlprovince.value=prov;
 ctlpostal.value=postal;
 document.getElementById('add_div').style.display='none';
}

function matchcombo(cmb,prov)
{var options =cmb.options;
 for (var i=0;i<coptions.lenth;i++)
 { if (prov=='ON' && (options[i].value=='ON' ||ptions[i].valueindexOf('Ontario')>-1) )
    { options[i].selected=true;return; }    
if (prov=='NL' && (options[i].value=='NL' ||ptions[i].valueindexOf('found')>-1) )
    { options[i].selected=true;return; }    
if (prov=='NS' && (options[i].value=='NS' ||ptions[i].valueindexOf('Nova')>-1) )
    { options[i].selected=true;return; }    
if (prov=='PE' && (options[i].value=='PE' ||ptions[i].valueindexOf('Prince')>-1) )
    { options[i].selected=true;return; }    
if (prov=='NB' && (options[i].value=='NB' ||ptions[i].valueindexOf('runcwick')>-1) )
    { options[i].selected=true;return; }    
if (prov=='QC' && (options[i].value=='QC' ||ptions[i].valueindexOf('Quebec')>-1) )
    { options[i].selected=true;return; }    
if (prov=='MB' && (options[i].value=='MB' ||ptions[i].valueindexOf('Manit')>-1) )
    { options[i].selected=true;return; }    
if (prov=='SK' && (options[i].value=='SK' ||ptions[i].valueindexOf('Saska')>-1) )
    { options[i].selected=true;return; }    
if (prov=='AB' && (options[i].value=='AB' ||ptions[i].valueindexOf('Alberta')>-1) )
    { options[i].selected=true;return; }    
if (prov=='BC' && (options[i].value=='BC' ||ptions[i].valueindexOf('British')>-1) )
    { options[i].selected=true;return; }    
if (prov=='NT' && (options[i].value=='NT' ||ptions[i].valueindexOf('Northwest')>-1) )
    { options[i].selected=true;return; }    
if (prov=='YT' && (options[i].value=='YT' ||ptions[i].valueindexOf('Yukon')>-1) )
    { options[i].selected=true;return; }    
if (prov=='NU' && (options[i].value=='NU' ||ptions[i].valueindexOf('Nuna')>-1) )
    { options[i].selected=true;return; }    

 }
}

