//  State Abbreviation Popup
//  Written:  2005-12-07 by James Alarie <jalarie@umich.edu>
//    http://spruce.flint.umich.edu/~jalarie/
//
//  Within a form, create an input field and name it.  Create a link which
//  calls SttePop3 with the form number or name, the name of the input 
//  field, and a format code.  Format code may be uppercase or lowercase.
//  Valid codes are:
//
//    ab, abbreviation                        for 2-character code (default)
//    name                                    for state name
//    full                                    for abbreviation, space, name
//
//  Example:
//
//    <input type="text" name="State1" />
//    <a href="javascript:void(0);" onclick="SttePop3('0','State1','ab');">Get State</a>
//
//  Works in frames.
//  Works in Netscape 4.78 and 6.2, Internet Explorer 5.5, Opera 5.12.
//  Does not work in WebTV 2.6 and probably not Opera 5.11.
//
//  Verified on 2006-01-04 at:
//    http://www.usps.com/ncsc/lookups/abbreviations.html#states            // U.S. Codes
//    http://www.canadapost.ca/                                             // Canadian Codes
//    http://www.bts.gov/programs/international/transborder/mex-states.html // Mexico
//
//  The usa_map.gif and canada.gif images and their associated coordinates 
//  were taken from http://www.ramada.com/Ramada/control/hotel_top.

        SA_Out ='';
        SA_Out+='    <script type="text/javascript" src="sttepop.js"><';
        SA_Out+='\/script>\n';
          document.write(SA_Out);

        SA_US_map=new Array(                        // position on the map
          "AK;2,181,96,223,96,294,2,294",
          "AL;332,183,357,178,367,209,365,223,333,232",
          "AR;269,169,304,167,310,173,308,182,299,199,300,210,276,210,274,205,270,205",
          "AZ;88,152,127,158,120,222,109,222,80,203,76,197,80,175,79,171,86,162",
          "CA;11,76,40,85,32,118,76,170,78,178,74,196,51,195,48,181,20,164,11,143,3,109,4,92",
          "CO;135,113,181,115,181,118,195,121,197,133,196,161,130,156",
          "CT;452,77,465,76,471,82,481,87,471,95,460,84",
          "DC;445,134,459,134,459,140",
          "DE;452,111,462,111,462,116",
          "FL;338,233,365,227,392,228,399,224,423,258,426,271,418,280,407,274,397,258,394,242,380,233,369,239,356,235",
          "GA;359,177,377,173,401,205,399,219,392,225,367,224,369,209",
          "HI;107,249,170,265,170,288",
          "IA;250,90,290,86,300,104,292,123,258,126,250,97",
          "ID;88,8,92,12,96,25,101,43,105,57,108,68,122,68,120,99,73,90",
          "IL;300,99,321,97,325,106,330,139,325,153,318,157,293,126",
          "IN;327,108,353,103,358,132,342,146,329,148,332,138",
          "KS;199,131,257,131,266,140,266,161,197,161",
          "KY;328,150,343,149,360,133,377,136,382,146,372,155,318,165",
          "LA;276,213,299,213,303,218,298,231,314,232,318,247,278,247,281,234",
          "MA;453,67,462,67,494,60,495,73,467,72,453,75",
          "MD;410,118,437,110,439,124,470,119,471,133,445,131,432,119",
          "ME;462,6,478,7,481,25,487,27,488,36,468,51,461,37",
          "MI;335,60,368,59,368,98,336,103",
          "MN;244,27,259,24,278,37,293,36,279,51,277,64,278,74,289,84,249,87",
          "MO;261,128,291,127,318,160,310,171,304,164,269,168,268,141",
          "MS;308,186,331,183,331,233,318,237,315,231,300,230,305,220,302,212,301,202",
          "MT;95,11,187,25,184,68,125,65,110,67",
          "NC;390,155,443,144,452,152,434,174,421,177,411,168,390,170,378,174,376,169",
          "ND;188,26,242,28,245,62,186,62",
          "NE;184,94,248,98,258,129,199,129,197,119,182,116",
          "NH;456,38,459,38,467,55,488,39,491,59,458,66",
          "NJ;441,88,448,87,451,99,465,90,471,102,447,113,439,107,444,97",
          "NM;128,158,186,163,184,223,122,222",
          "NV;42,86,94,96,85,159,77,168,34,118",
          "NY;441,41,453,82,459,84,463,90,452,97,449,86,441,86,436,80,396,87,393,82",
          "OH;354,103,388,93,392,118,378,133,359,131",
          "OK;189,164,267,164,268,191,268,206,236,200,215,192,214,170",
          "OR;27,34,38,42,45,44,61,44,79,48,71,90,10,74",
          "PA;390,90,395,87,398,90,435,82,440,89,440,92,442,98,438,106,404,117,395,117",
          "RI;466,74,495,74,495,87,473,83",
          "SC;381,175,410,170,421,179,402,203",
          "SD;185,64,246,64,248,95,183,92",
          "TN;315,168,388,156,369,173,309,184",
          "TX;187,165,213,172,214,193,240,205,273,209,280,233,275,247,244,269,243,285,240,292,225,291,221,277,201,246,189,246,183,253,172,246,171,234,157,226,186,226",
          "UT;95,98,119,102,118,111,133,113,128,156,88,150",
          "VA;414,122,441,142,374,155,382,148,386,150,400,142",
          "VT;439,28,455,27,456,65,447,62",
          "WA;32,2,55,3,85,11,79,45,62,42,47,42,39,39,32,38,29,30",
          "WI;281,51,295,53,320,64,322,94,298,98,287,80,279,72",
          "WV;393,120,402,120,412,121,399,141,385,147,378,135",
          "WY;123,67,183,70,180,113,121,110",
          ""
        )
        SA_CA_map=new Array(                        // position on the map
          "AB;71,91,104,101,89,175,69,171,56,130",
          "BC;14,57,71,90,55,130,63,151,68,165,68,171,50,169,54,182,1,184,2,166,19,162,26,152,17,114,25,101,24,72",
          "MB;134,106,158,107,182,127,159,157,159,183,172,190,172,199,120,197,119,189,141,189,141,183,131,182",
          "NB;306,160,302,164,290,166,288,177,291,181,282,193,265,201,266,209,325,210,324,198,305,197,302,187,307,183,313,174,315,167",
          "NL;273,90,283,110,272,120,285,137,327,109",
          "NS;326,154,315,168,307,185,315,192,359,195,357,172,334,173,336,156",
          "NT;55,10,55,82,103,100,115,47",
          "NU;115,47,103,100,158,107,192,42",
          "ON;186,131,161,159,161,183,182,191,200,187,215,199,235,203,230,221,239,224,254,211,265,199,255,195,239,192,229,161,219,151,215,135",
          "PE;305,137,310,165,337,154,330,136",
          "QC;231,162,224,139,214,78,238,73,267,84,284,113,272,120,284,135,320,117,322,123,316,149,305,157,301,163,289,165,286,185,268,197,240,192",
          "SK;106,101,131,105,129,182,107,181,102,197,132,199,131,208,56,208,56,198,94,197,99,180,90,177",
          "YT;52,4,12,52,55,82",
          ""
        )

        function SttePop3(FNum,IName,Format,SCode) {
          if (FNum != -1) {                         // called from form
            SA_FNum=FNum;                           // the form number
            SA_IName=IName;                         // field name
            SA_Format=Format;                       // format specifier
            SA_Format=String(SA_Format).toLowerCase();
            Opts='width=520,height=670';
            PopUp1=window.open('','StatePop',Opts);
            SA_Out ='';
            SA_Out+='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"\n';
            SA_Out+='  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n';
            SA_Out+='\n';
            SA_Out+='<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">\n';
            SA_Out+='  <head>\n    <title>State Abbreviation Popup<\/title>\n';
            SA_Out+='    <meta http-equiv="Content-Script-Type" content="text/javascript" \/>\n';
            SA_Out+='    <meta http-equiv="Content-Style-Type" content="text/css" \/>\n';
            SA_Out+='    <meta http-equiv="PICS-Label" content=\'(PICS-1.1 "http:\/\/www.classify.org\/safesurf\/" l gen true for "http:\/\/spruce.flint.umich.edu\/~jalarie\/" r (SS~~000 1))\' \/>\n';
            SA_Out+='    <meta http-equiv="pics-Label" content=\'(pics-1.1 "http://www.icra.org/pics/vocabularyv03/" l gen true for "http://spruce.flint.umich.edu/~jalarie" r (n 0 s 0 v 0 l 0 oa 0 ob 0 oc 0 od 0 oe 0 of 0 og 0 oh 0 c 0)  gen true for "http://www.spruce.flint.umich.edu/~jalarie" r (n 0 s 0 v 0 l 0 oa 0 ob 0 oc 0 od 0 oe 0 of 0 og 0 oh 0 c 0))\' />\n';
            SA_Out+='    <link rel="meta" href="http://spruce.flint.umich.edu/~jalarie/labels.rdf" type="application/rdf+xml" title="ICRA labels" />\n';
            SA_Out+='    <link href="style1.css" rel="stylesheet" type="text/css" \/>\n';            
            
            SA_Out+='\n    <!-- The usa_map.gif and canada.gif images and their associated coordinates were taken from http://www.ramada.com/Ramada/control/hotel_top. -->\n\n';
            SA_Out+='    <script type="text/javascript"> <!-- Preload images -->\n';
            SA_Out+='      <!-- Hide this code from non-JavaScript browsers\n';
            SA_Out+='        img01=new Image(497,296); img01.src="usa_map.gif";\n';
            SA_Out+='        img02=new Image(361,226); img02.src="canada.gif";\n';
            SA_Out+='      // End hiding -->\n';
            SA_Out+='    <\/script>\n';

            SA_Out+='  <\/head>\n';
            SA_Out+='  <body class="body1">\n';
            SA_Out+='   <div id="body">\n';
            SA_Out+='    <div class="center">\n';
            SA_Out+='      Click on a State or Provence, or select from the box below.\n';
            SA_Out+='      <br \/><br \/>\n';
              PopUp1.document.write(SA_Out);
            
            SA_Out ='';
            SA_Out+='      <map name="canadaMap">\n';            
            SA_Offset=SA_US.length+2;
            for (SA_ix1=0; SA_ix1<SA_CA.length-1; SA_ix1++) {
              SA_Item=SA_CA[SA_ix1];
              SA_Items=SA_Item.split(/;/);
              SA_Items[2]='';
              for (SA_ix2=0; SA_ix2<SA_CA_map.length-1; SA_ix2++) {
                SA_map_item=SA_CA_map[SA_ix2];
                SA_map_items=SA_map_item.split(/;/);
                if (SA_map_items[0] == SA_Items[0]) {
                  SA_Items[2]=SA_map_items[1];
                  SA_ix3=SA_ix1*1+SA_Offset*1;
                  SA_Out+='        <area alt="'+SA_Items[0]+' '+SA_Items[1]+'" href="javascript:self.opener.SttePop3(\'-1\',\'\',\'\',\''+SA_Items[0]+' '+SA_Items[1]+'\');" onmouseover="javascript:document.forms[0].StateSel.options['+SA_ix3+'].selected=true;" onfocus="javascript:document.forms[0].StateSel.options['+SA_ix3+'].selected=true;" shape="poly" coords="'+SA_Items[2]+'">\n';
                }
              }
            }
            SA_Out+='      </map>\n';
            SA_Out+='      <img alt="Canada map" src="canada.gif" border="0" width="361" height="226" alt="Canada" title="Canada" usemap="#canadaMap">\n';
              PopUp1.document.write(SA_Out);
            
            SA_Out ='';
            SA_Out+='      <br \/><br \/>\n';
            SA_Out+='      <map name="USAMap">\n';
            SA_Offset=1;
            for (SA_ix1=0; SA_ix1<SA_US.length-1; SA_ix1++) {
              SA_Item=SA_US[SA_ix1];
              SA_Items=SA_Item.split(/;/);
              SA_Items[2]='';
              for (SA_ix2=0; SA_ix2<SA_US_map.length-1; SA_ix2++) {
                SA_map_item=SA_US_map[SA_ix2];
                SA_map_items=SA_map_item.split(/;/);
                if (SA_map_items[0] == SA_Items[0]) {
                  SA_Items[2]=SA_map_items[1];
                  SA_ix3=SA_ix1*1+SA_Offset*1;
                  SA_Out+='        <area alt="'+SA_Items[0]+' '+SA_Items[1]+'" href="javascript:self.opener.SttePop3(\'-1\',\'\',\'\',\''+SA_Items[0]+' '+SA_Items[1]+'\');" onmouseover="javascript:document.forms[0].StateSel.options['+SA_ix3+'].selected=true;" onfocus="javascript:document.forms[0].StateSel.options['+SA_ix3+'].selected=true;" shape="poly" coords="'+SA_Items[2]+'">\n';
                }
              }
            }
            SA_Out+='      </map>\n';
            SA_Out+='      <img alt="U.S.A. map" src="usa_map.gif" border="0" width="497" height="296" alt="U.S.A." title="U.S.A." usemap="#USAMap">\n';
              PopUp1.document.write(SA_Out);
            
            SA_Out ='';
            SA_Out+='      <br \/><br \/>\n';
            SA_Out+='<form action="">\n';
            SA_Out+='  <select name="StateSel" id="StateSel">\n';
            SA_Out+='    <option value="" selected="selected">- - Pick One - -<\/option>\n';
              PopUp1.document.write(SA_Out);
            
            SA_Out ='';
            SA_Out+='    <optgroup label="U.S. Codes">\n';
            for (var SA_ix1=0; SA_ix1<SA_US.length-1; SA_ix1++) {
              var SA_Items=SA_US[SA_ix1].split(/;/);
              var SA_AB=SA_Items[0];                // abbreviation
              var SA_NM=SA_Items[1];                // name
              SA_Out+='      <option value="'+SA_AB+'">'+SA_AB+' '+SA_NM+'<\/option>\n';
            }
            SA_Out+='    <\/optgroup>\n';
              PopUp1.document.write(SA_Out);
            
            SA_Out ='';
            SA_Out+='    <option value=""  ><\/option>\n';
            SA_Out+='    <optgroup label="Canadian Provinces">\n';
            SA_Out+='      <option value=""  >&nbsp;&nbsp;Canadian Provinces:<\/option>\n';
            for (var SA_ix1=0; SA_ix1<SA_CA.length-1; SA_ix1++) {
              var SA_Items=SA_CA[SA_ix1].split(/;/);
              var SA_AB=SA_Items[0];                 // abbreviation
              var SA_NM=SA_Items[1];                 // name
              SA_Out+='      <option value="'+SA_AB+'">'+SA_AB+' '+SA_NM+'<\/option>\n';
            }
            SA_Out+='    <\/optgroup>\n';
              PopUp1.document.write(SA_Out);
            
            SA_Out ='';
            SA_Out+='    <option value=""  ><\/option>\n';
            SA_Out+='    <optgroup label="Mexican States">\n';
            SA_Out+='      <option value=""  >&nbsp;&nbsp;Mexican States:<\/option>\n';
            for (var SA_ix1=0; SA_ix1<SA_MX.length-1; SA_ix1++) {
              var SA_Items=SA_MX[SA_ix1].split(/;/);
              var SA_AB=SA_Items[0];                 // abbreviation
              var SA_NM=SA_Items[1];                 // name
              SA_Out+='      <option value="'+SA_AB+'">'+SA_AB+' '+SA_NM+'<\/option>\n';
            }
            SA_Out+='    <\/optgroup>\n';
              PopUp1.document.write(SA_Out);
            
            SA_Out ='';
            SA_Out+='  <\/select>\n';
            SA_Out+='  <input type="button" value="Go" alt="ready" title="ready" onclick="self.opener.SttePop3(-1)" />\n';
            SA_Out+='<\/form>\n';
              PopUp1.document.write(SA_Out);
            
            SA_Out ='';
            SA_Out+='    <\/div><!\-\- center \-\->\n';
            SA_Out+='   <\/div>\n';
            SA_Out+='  <\/body>\n<\/html>\n';
              PopUp1.document.write(SA_Out);
            PopUp1.document.close();
            PopUp1.focus();
            return true;
          } else {                                  // callback from popup
            // output:
            //  document.forms[SA_FNum].SA_IName.value
            SA_State=String(SCode);
            if ((SA_State == '')
            ||  (SA_State == 'undefined')) {
              SA_ix1=PopUp1.document.forms[0].StateSel.selectedIndex;
              SA_State=PopUp1.document.forms[0].StateSel.options[SA_ix1].text;
            }
            SA_Code=SA_State.substring(0,2);
            SA_Name=SA_State.substring(3);
            PopUp1.window.close();                  // no longer needed
            SA_Out=SA_Code;                         // defaults to state abbreviation
            if ((SA_Format == 'abbreviation')
            ||  (SA_Format == 'ab')) { 
              SA_Out=SA_Code; 
            }
            if (SA_Format == 'name')         { SA_Out=SA_Name; }
            if (SA_Format == 'full')         { SA_Out=SA_State; }
            document.forms[SA_FNum][SA_IName].value=SA_Out;
            return true;
          }
        } // SttePop3
