var defaultCountry = "united states";
var theList = null;
var theList_lc = new Object();






function addDetails(stl)
{

   var tr = document.getElementById("details").insertRow(document.getElementById("details").rows.length);
   tr.name = "rowCnt" + glblCnt;
   tr.id = "rowCnt" + glblCnt;

   var traa = document.getElementById("details").insertRow(document.getElementById("details").rows.length);
   var tdaa = traa.insertCell(0);
   tdaa.innerHTML = "&nbsp;";

   var td = tr.insertCell(0);
   td.colspan = 2;

   var tbl = document.createElement("table");
   tbl.className = "detailBrdr";
   tbl.width = "100%";
   tbl.border = "0";

   var inp = document.createElement("input");
   inp.type = 'hidden';
   inp.value = '0';
   inp.id = 'addDeleteCnt'+glblCnt;
   inp.name = 'addDeleteCnt[]';

   td.appendChild(inp);
   td.appendChild(tbl);

   var tr1 = tbl.insertRow(0);

   var td1 = tr1.insertCell(0);
   td1.align = "center";
   td1.vAlign = "top";
   td1.width = "50%";

   var td1aa = tr1.insertCell(1);
   td1aa.width = "50%";

   var tbl1 = document.createElement("table");
   tbl1.width = "100%";
   tbl1.border = "0";

   td1.appendChild(tbl1);

   var tr1a = tbl1.insertRow(0);

   var td1a = tr1a.insertCell(0);
   td1a.width = "50%";
   td1a.align = "right";
   td1a.className = "conten-text2";
   td1a.innerHTML = "Country:";

   var td2 = tr1a.insertCell(1);
   td2.id = "ccell1";
   td2.align = "left";

            var inp2 = document.createElement("input");
            inp2.type = "text";
            inp2.id = "lcountry"+glblCnt;
            inp2.name = "lcountry[]";
            inp2.maxLength = 65;
            inp2.size= 21;
            inp2.className = "auto-complete-country";

            var inp2a = document.createElement("ul");
            inp2a.id = "country"+glblCnt;
            inp2a.name = "countryl[]";
            inp2a.className = "auto-complete-ul";

            for (var i = 0; i < theList_lc.countries.length; i++)
            {
                 var inp2aa = document.createElement("li");
                 inp2aa.className="auto-complete-li";
                 inp2aa.innerHTML = theList.countries[i].name;
                 inp2a.appendChild(inp2aa);
            }
            var inp2b = document.createElement("input");
            inp2b.type = "hidden";
            inp2b.id = "currCountry"+glblCnt;
            inp2b.name = "cuurCountry[]";

            inp2.value = 'United States';
            td2.appendChild(inp2);
            td2.appendChild(inp2a);
            td2.appendChild(inp2b);


   var tr1b = tbl1.insertRow(1);

   var td3 = tr1b.insertCell(0);
   td3.align = "right";
   td3.className = "conten-text2";
   td3.innerHTML = "State:";

   var td4 = tr1b.insertCell(1);
   td4.align = "left";
   td4.id = "scell"+glblCnt;

            var inp4 = document.createElement("input");
            inp4.type = "text";
            inp4.id = "lstate"+glblCnt;
            inp4.name = "lstate[]";
            inp4.maxLength = 65;
            inp4.size= 21;
            inp4.className = "auto-complete-state";

            var inp4a = document.createElement("ul");
            inp4a.id = "statel"+glblCnt;
            inp4a.name = "statel[]";
            inp4a.className = "auto-complete-ul";

            var inp4b = document.createElement("input");
            inp4b.type = "hidden";
            inp4b.id = "currState"+glblCnt;
            inp4b.name = "cuurState[]";

   for (var i = 0; i < theList_lc.countries.length; i++)
   {
        if (theList_lc.countries[i].name == 'united states')
        {
            for (var j = 0; j < theList_lc.countries[i].states.length; j++)
            {
                 var inp4aa = document.createElement("li");
                 inp4aa.className="auto-complete-li";
                 inp4aa.innerHTML = theList.countries[i].states[j].name;
                 inp4a.appendChild(inp4aa);
            }
       }
   }

            td4.appendChild(inp4);
            td4.appendChild(inp4a);
            td4.appendChild(inp4b);


   var tr1c = tbl1.insertRow(2);

   var td5 = tr1c.insertCell(0);
   td5.align = "right";
   td5.className = "conten-text2";
   td5.innerHTML = "City:";

   var td6 = tr1c.insertCell(1);
   td6.align = "left";
   td6.id = "cell"+glblCnt;

            var inp6 = document.createElement("input");
            inp6.type = "text";
            inp6.id = "lcity"+glblCnt;
            inp6.name = "lcity[]";
            inp6.maxLength = 65;
            inp6.size= 21;
            inp6.className = "auto-complete-city";

            var inp6a = document.createElement("ul");
            inp6a.id = "cityl"+glblCnt;
            inp6a.name = "cityl[]";
            inp6a.className = "auto-complete-ul";

                 var inp4aa = document.createElement("li");
                 inp4aa.className="auto-complete-li";
                 inp4aa.innerHTML = "";
                 inp4a.appendChild(inp4aa);

            var inp6b = document.createElement("input");
            inp6b.type = "hidden";
            inp6b.id = "currCity"+glblCnt;
            inp6b.name = "cuurCity[]";

            td6.appendChild(inp6);
            td6.appendChild(inp6a);
            td6.appendChild(inp6b);


   var tr1d = tbl1.insertRow(3);

   var td1d = tr1d.insertCell(0);
   td1d.align = "center";
   td1d.colSpan = "2";
   td1d.innerHTML = "<div style=\"width:160px\"><div class=\"ContactAgentBtn\" > <a class=\"roundBig3\" href='#' onclick=\"document.getElementById('rowCnt"+glblCnt+"').style.display='none';document.getElementById('addDeleteCnt"+glblCnt+"').value='1';return false;\"><span>remove</span></a></div><input type=\"hidden\" name=\"addDelete[]\" id=\"addDeleteCnt"+glblCnt+"\" value=\"0\"/></div>";

   var tbl2 = document.createElement("table");
   tbl2.width = "100%";
   tbl2.border = "0";

   td1aa.appendChild(tbl2);

   var tr5 = tbl2.insertRow(0);

   var td7 = tr5.insertCell(0);
   td7.align = "right";
   td7.className = "conten-text2";
   td7.vAlign = "top";
   td7.innerHTML = "Details";

   var td8 = tr5.insertCell(1);
   td8.align = "left";
   td8.className = "conten-text2";
   td8.innerHTML = "<textarea style=\"width:320px;\" rows=8 id=\"addDesc"+glblCnt+"\" name=\"addDesc[]\" onKeyDown=\"textCounter(this.form.addDesc"+glblCnt+",this.form.fullCharRemainaddDesc"+glblCnt+",600,'fullCharUsedaddDesc"+glblCnt+"');\" onKeyUp=\"textCounter(this.form.addDesc"+glblCnt+",this.form.fullCharRemainaddDesc"+glblCnt+",600,'fullCharUsedaddDesc"+glblCnt+"');\"></textarea>";
 
   var tr6 = tbl2.insertRow(1);

   var td9 = tr6.insertCell(0);
   td9.align = "right";
   td9.className = "conten-text2";

   var td10 = tr6.insertCell(1);
   td10.className = "conten-text2";
   td10.bgColor = "#FFFFEE";
   td10.align = "left";
   td10.style.padding = "5px";
   td10.innerHTML = "This field has a character limit of 600 characters.<br /><strong>Character Used:</strong> <span style=\"background: #FFFFFF\"><input  type=\"text\" name=\"fullCharUsedaddDesc"+glblCnt+"\" id=\"fullCharUseddesc"+glblCnt+"\" readonly=\"readonly\" value=\"0\" size=\"5\" /></span><br /><strong>Remaining:</strong> <span style=\"background:#FFFFFF\"><input type=\"text\" name=\"fullCharRemainaddDesc"+glblCnt+"\" id=\"fullCharRemaindesc"+glblCnt+"\" readonly=\"readonly\" value=\"600\" size=\"5\" /></span>";

   glblCnt++;
   autoComplete();

   return false;
}


function getCityStateNdx(city)
{
     for (i = 0; i < cityList.length; i++)
     {
         if (cityList[i].toLowerCase() == city)
         {
            return i;
         }
     }
     return -1;
}

function submitForm(arg, ndx, preFix)
{
     if (static)
     {
         stateVal(ndx, preFix);
     }
//     if (document.getElementById(arg).submit)
//     {
//        document.getElementById(arg).removeChild(document.getElementById(arg).submit);
//     }
     var inp0 = document.createElement("input");
     inp0.type = "submit";
     inp0.style.display = 'none';
     inp0.name = "submit";
     inp0.id = "submit";
     document.getElementById(arg).appendChild(inp0);
     inp0.click();
}

function lcList()
{
   theList_lc.countries = new Array(theList.countries.length);
   for (var i = 0; i < theList_lc.countries.length; i++)
   {
       theList_lc.countries[i] = new Object();
       theList_lc.countries[i].name = theList.countries[i].name.toLowerCase();
       theList_lc.countries[i].id = theList.countries[i].id;
       theList_lc.countries[i].states = new Array(theList.countries[i].states.length);
       for (var j = 0; j < theList_lc.countries[i].states.length; j++)
       {
          theList_lc.countries[i].states[j] = new Object();
          theList_lc.countries[i].states[j].name = theList.countries[i].states[j].name.toLowerCase();
          theList_lc.countries[i].states[j].id = theList.countries[i].states[j].id;
/*
          theList_lc.countries[i].states[j].cities = new Array(theList.countries[i].states[j].cities.length);

          for (var k = 0; k < theList_lc.countries[i].states[j].cities.length; k++)
          {
             theList_lc.countries[i].states[j].cities[k] = new Object();
             theList_lc.countries[i].states[j].cities[k].name = theList.countries[i].states[j].cities[k].name.toLowerCase();
             theList_lc.countries[i].states[j].cities[k].id = theList.countries[i].states[j].cities[k].id;
          }
*/
       }
   }
}

function getCountryNdx(cntry)
{
   for (var i = 0; i < theList_lc.countries.length; i++)
   {
        if (theList_lc.countries[i].name == cntry)
        {
            return i;
       }
   }
   return -1;
}
function getStateNdx(cntry, st)
{
   for (var i = 0; i < theList_lc.countries.length; i++)
   {
        if (theList_lc.countries[i].name == cntry)
        {
            for (var j = 0; j < theList_lc.countries[i].states.length; j++)
            {
                if (theList_lc.countries[i].states[j].name == st)
                {
                   return j;
               }
            }
       }
   }
   return -1;
}
function getCityNdx(cntry, st, city)
{
   var br = 0;
   for (var i = 0; i < theList_lc.countries.length; i++)
   {
        if (theList_lc.countries[i].name == cntry)
        {
            for (var j = 0; j < theList_lc.countries[i].states.length; j++)
            {
                if (theList_lc.countries[i].states[j].name == st && theList_lc.countries[i].states[j].cities)
                {
                    for (var k = 0; k < theList_lc.countries[i].states[j].cities.length; k++)
                    {
                       if (theList_lc.countries[i].states[j].cities[k].name == city)
                       {
                           return k;
                       }
                    }
                }
           }
       }
   }
   return -1;
}









function getCountrySubNdx(cntry)
{
   for (var i = 0; i < theList_lc.countries.length; i++)
   {
        if (theList_lc.countries[i].name.substr(0,cntry.length) == cntry)
        {
            return i;
       }
   }
   return -1;
}
function getStateSubNdx(cntry, st)
{
   for (var i = 0; i < theList_lc.countries.length; i++)
   {
        if (theList_lc.countries[i].name == cntry)
        {
            for (var j = 0; j < theList_lc.countries[i].states.length; j++)
            {
                if (theList_lc.countries[i].states[j].name.substr(0,st.length) == st)
                {
                   return j;
               }
            }
       }
   }
   return -1;
}
function getCitySubNdx(cntry, st, city)
{
   var br = 0;
   for (var i = 0; i < theList_lc.countries.length; i++)
   {
        if (theList_lc.countries[i].name == cntry)
        {
            for (var j = 0; j < theList_lc.countries[i].states.length; j++)
            {
                if (theList_lc.countries[i].states[j].name == st && theList_lc.countries[i].states[j].cities)
                {
                    for (var k = 0; k < theList_lc.countries[i].states[j].cities.length; k++)
                    {
                       if (theList_lc.countries[i].states[j].cities[k].name.substr(0,city.length) == city)
                       {
                           return k;
                       }
                    }
                }
           }
       }
   }
   return -1;
}




















	/*==================================================================*\
	  coder: Michele Waldman
	\*==================================================================*/
function stateVal(ndx, preFix)
{
   lc = document.getElementById("lcountry"+ndx).value.toLowerCase();
   ls = document.getElementById("lstate"+ndx).value.toLowerCase();
   lct = document.getElementById("lcity"+ndx).value.toLowerCase();
   for (var i = 0; i < theList_lc.countries.length; i++)
   {
        if (theList_lc.countries[i].name == lc)
        {
            document.getElementById(preFix+"country").value = theList_lc.countries[i].id;
            for (var j = 0; j < theList_lc.countries[i].states.length; j++)
            {
                if (theList_lc.countries[i].states[j].name == ls)
                {
                    document.getElementById(preFix+"state").value = theList_lc.countries[i].states[j].id;
                     for (var k = 0; k < theList_lc.countries[i].states[j].cities.length; k++)
                     {
                          if (theList_lc.countries[i].states[j].cities[k].name == lct)
                          {
                              document.getElementById(preFix+"city").value = theList_lc.countries[i].states[j].cities[k].id;
                          }
                     }
               }
            }
       }
   }
}


function population()

  {

	  var hasCityVal=false;
      c1="";
      s1="";
      n1="";
      if(document.getElementById('lcity1').value!='' && document.getElementById('lstate1').value!='' && document.getElementById('lcountry1').value!='')
      {
	     c1 = document.getElementById('lcity1').value;
		 s1 = document.getElementById('lstate1').value;
		 n1 = document.getElementById('lcountry1').value;
         hasCityVal = true;
      }
      if (hasCityVal)
      {
	     for(i=2;i <= document.getElementById("addlocations").rows.length;i++)
	     {
           if(document.getElementById('lcity'+i).value!='' && document.getElementById('lstate'+i).value!='' && document.getElementById('lcountry'+i).value!='')
           {
				 c1 += ","+document.getElementById('lcity'+i).value;
				 s1 += ","+document.getElementById('lstate'+i).value;
				 n1 += ","+document.getElementById('lcountry'+i).value;
     	   }
           else
           {
              if (document.getElementById('lstate'+i).value != "" && document.getElementById('lcity'+i).value == "")
              {
                  hasCityVal = false;
              }
           }
        }
     }
	 if(hasCityVal)
     {
          for (i = 1; i <= document.getElementsByName("city[]").length; i++)
          {
             if (document.getElementById("city"+i))
             {
               document.getElementById('populationQuote').removeChild(document.getElementById("city"+i));
               document.getElementById('populationQuote').removeChild(document.getElementById("state"+i));
               document.getElementById('populationQuote').removeChild(document.getElementById("country"+i));
             }
          }
		var listingType=document.getElementById('listingType').value;
        var queryStr='cities='+c1+'&states='+s1+'&countries='+n1+'+&listingType='+listingType;
        fillComboOption('user/includes/code/ajax_city_population_quote.php',queryStr,'populationQuote');
        population12();

	  }
	  else
	  {
	   document.getElementById('msgTxt').innerHTML = 'Please select a minimum of one Service Area by selecting the Country, State, City for the service area you work in. If you are outside the United States all providences and states may not be available. If that is the case, you may select just the country or the closest matches to your service area.';
	  }
 }


function population2()

  {
    var c1 = '';
    var s1 = '';
    var n1 = '';
	     for(i=0;i < document.getElementsByName("addCountry_name[]").length;i++)
	     {
         if (document.getElementById('row'+i).style.display != 'none')
         {         if (c1 == '')
         {
				 c1 += document.getElementsByName('addCity_name[]')[i].value;
				 s1 += document.getElementsByName('addState_name[]')[i].value;
				 n1 += document.getElementsByName('addCountry_name[]')[i].value;
         }
         else
         {
				 c1 += ","+document.getElementsByName('addCity_name[]')[i].value;
				 s1 += ","+document.getElementsByName('addState_name[]')[i].value;
				 n1 += ","+document.getElementsByName('addCountry_name[]')[i].value;
         }
          }
                 }
	     for(i=orgCnt;i < glblCnt;i++)
	     {
         if (document.getElementById('rowCnt'+i).style.display != 'none')
         {
         if (document.getElementById('lcity'+i).value != '' &&
         document.getElementById('lstate'+i).value != '' &&
         document.getElementById('lcountry'+i).value != '')
         {
         if (c1 == '')
         {
				 c1 += document.getElementById('lcity'+i).value;
				 s1 += document.getElementById('lstate'+i).value;
				 n1 += document.getElementById('lcountry'+i).value;
}
else
{
				 c1 += ","+document.getElementById('lcity'+i).value;
				 s1 += ","+document.getElementById('lstate'+i).value;
				 n1 += ","+document.getElementById('lcountry'+i).value;
                 }
}
         }
}
		var queryStr='cities='+c1+'&states='+s1+'&countries='+n1;
if (document.getElementsByName("addCity_name[]").length || (glblCnt - orgCnt))
		fillComboOption('user/includes/code/ajax_city_population_quote_edit.php',queryStr,'populationQuote');
 }

 var currNm = "";

  function getXmlHttpObject()
 {
      var objXMLHttp=null;
      try
      {
          var objXMLHttp = new XMLHttpRequest();
          if (objXMLHttp.overrideMimeType)
          {
              objXMLHttp.overrideMimeType('text/xml');
          }
     }
     catch(e)
     {
         if (window.ActiveXObject)
         {
             var ieXmlHttpVersions = new Array();
             ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.7.0";
             ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.6.0";
             ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.5.0";
             ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.4.0";
             ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.3.0";
             ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp";
             ieXmlHttpVersions[ieXmlHttpVersions.length] = "Microsoft.XMLHttp";

             var cnt;
             for (cnt=0; cnt < ieXmlHttpVersions.length; cnt++)
             {
                 try
                 {
                    var objXMLHttp = new ActiveXObject(ieXmlHttpVersions[cnt]);
                    break;
                 }
                 catch (e)
                 {
                 }
            }
        }
     }
     return objXMLHttp;
}function verifyData(e)
{
   for (var cnt = document.getElementsByName("city[]").length - 1; cnt >= 0; cnt--)
   {
        if (!parseInt(document.getElementsByName('locDel[]')[cnt].value))
        {
           if (document.getElementsByName('city[]')[cnt].value == "")
           {
              document.getElementById('msgTxt').innerHTML ="City "+(cnt+1)+" required.";
              err = 1;
           }
           if (document.getElementsByName('state[]')[cnt].value == "")
           {
              document.getElementById('msgTxt').innerHTML ="State "+(cnt+1)+" required.";
              err = 1;
           }
           if (document.getElementsByName('country[]')[cnt].value == "")
           {
              document.getElementById('msgTxt').innerHTML ="Country "+(cnt+1)+" required.";
              err = 1;
           }
      }
   }
   if (chkDup())
   {
       err = 1;
   }
   if (err)
   {
      if (e && e.preventDefault)
      {
          e.preventDefault();
      }
      else
      {
          return false;
      }
   }
}

function chkDup()
{
   var err = 0;
   for (var cnt = 0; cnt < document.getElementsByName("city[]").length; cnt++)
   {
      for (var cnt1 = cnt+1; cnt1 < document.getElementsByName("city[]").length; cnt1++)
      {
        if (document.getElementsByName('city[]')[cnt].value == document.getElementsByName('city[]')[cnt1].value &&
            document.getElementsByName('state[]')[cnt].value == document.getElementsByName('state[]')[cnt1].value &&
            document.getElementsByName('country[]')[cnt].value == document.getElementsByName('country[]')[cnt1].value &&
            !parseInt(document.getElementsByName('locDel[]')[cnt].value) &&
            !parseInt(document.getElementsByName('locDel[]')[cnt1].value))
        {
           document.getElementById('msgTxt').innerHTML ="Location "+(cnt1 + 1)+" is a duplicate location.";
           err = 1;
        }
      }
   }
   return err;
}

function setEdited(e)
{
   var ndx = -1;
   if (e && e.target)
   {
      if (e.target.id.indexOf('city') != -1)
      {
          ndx = parseInt(e.target.id.substr(e.target.id.indexOf('city')+'city'.length));
      }
      if (e.target.id.indexOf('state') != -1)
      {
          ndx = parseInt(e.target.id.substr(e.target.id.indexOf('state')+'state'.length));
      }
      if (e.target.id.indexOf('country') != -1)
      {
          ndx = parseInt(e.target.id.substr(e.target.id.indexOf('country')+'country'.length));
      }
   }
   else
   {
      if (window.event.srcElement.id.indexOf('city') != -1)
      {
          ndx = parseInt(window.event.srcElement.id.substr(window.event.srcElement.id.indexOf('city')+'city'.length));
      }
      if (window.event.srcElement.id.indexOf('state') != -1)
      {
          ndx = parseInt(window.event.srcElement.id.substr(window.event.srcElement.id.indexOf('state')+'state'.length));
      }
      if (window.event.srcElement.id.indexOf('country') != -1)
      {
          ndx = parseInt(window.event.srcElement.id.substr(window.event.srcElement.id.indexOf('country')+'country'.length));
      }
   }
   document.getElementById('locEdit'+ndx).value = "1";
}

function removeLocation(e)
{
   if (e && e.target)
   {
      ndx = parseInt(e.target.id.substr(e.target.id.indexOf('remove')+'remove'.length));
   }
   else
   {
      ndx = parseInt(window.event.srcElement.id.substr(window.event.srcElement.id.indexOf('remove')+'remove'.length));
   }
       var del = ndx - 1;
       if (document.getElementById("city"+ ndx))
       {
          document.getElementById('populationQuote').removeChild(document.getElementById("city"+ ndx));
          document.getElementById('populationQuote').removeChild(document.getElementById("state"+ ndx));
          document.getElementById('populationQuote').removeChild(document.getElementById("country"+ ndx));
        }
       document.getElementById("addlocations").deleteRow(del);
       for (var cnt = (ndx + 1); cnt <= (document.getElementById("addlocations").rows.length + 1); cnt++)
       {
           rw = document.getElementById("row" + cnt);
           rm = document.getElementById("remove"+ cnt);
           lct = document.getElementById("lcity"+ cnt);
           lst = document.getElementById("lstate"+ cnt);
           lcn = document.getElementById("lcountry"+ cnt);
           ctl = document.getElementById("cityl"+ cnt);
           cl = document.getElementById("cell"+ cnt);
           cc1 = document.getElementById("currCity"+ cnt);
           cs = document.getElementById("currState"+ cnt);
           stl = document.getElementById("statel"+ cnt);
           sl = document.getElementById("scell"+ cnt);
           cc = document.getElementById("currCountry"+ cnt);
           rw.name = "row" + (cnt - 1);
           rw.id = "row" + (cnt - 1);
           rm.name = "remove" + (cnt - 1);
           rm.id = "remove" + (cnt - 1);
           lct.id = "lcity" + (cnt - 1);
           lcn.id = "lcountry" + (cnt - 1);
           lst.id = "lstate" + (cnt - 1);
           cs.id = "currState" + (cnt - 1);
           cc.id = "currCountry" + (cnt - 1);
           cc1.id = "currCity" + (cnt - 1);
           cl.id = "cell" + (cnt - 1);
           ctl.id = "cityl" + (cnt - 1);
           sl.id = "scell" + (cnt - 1);
           stl.id = "statel" + (cnt - 1);
           if (document.getElementById("city" + cnt))
           {
              ct = document.getElementById("city"+ cnt);
              st = document.getElementById("state"+ cnt);
              cn = document.getElementById("country"+ cnt);
              ct.id = "city" + (cnt - 1);
              cn.id = "state" + (cnt - 1);
              st.id = "country" + (cnt - 1);
           }
       }
     cnt = document.getElementById("addlocations").rows.length;
   population21();
   if (e && e.preventDefault)
   {
       e.preventDefault();
   }
   else
   {
       return false;
   }

}
function deleteChildren(node)
{

       var children = node.childNodes;
       for (var cnt = (children.length - 1); cnt >= 0; cnt--)
       {
            deleteChildren(children[cnt]);
            node.removeChild(children[cnt]);
       }
}
var refreshCity = 0;
function updateCity(cNdx)
{
  refreshCity = 1;
/*
   if (e && e.target)
   {
      cNdx = parseInt(e.target.id.substr(e.target.id.indexOf('lcity')+'lcity'.length));
   }
   else
   {
      cNdx = parseInt(window.event.srcElement.id.substr(window.event.srcElement.id.indexOf('lcity')+'lcity'.length));
   }
*/
   if (document.getElementById("lstate"+ cNdx).value == "")
   {
       document.getElementById("lstate"+ cNdx).focus();
   }
   else
   {
     if (document.getElementById("lcity"+ cNdx).value.toLowerCase() != document.getElementById("currCity"+ cNdx).value.toLowerCase() ||
         document.getElementById("lstate"+ cNdx).value.toLowerCase() != document.getElementById("currState"+ cNdx).value.toLowerCase()||
         document.getElementById("currCity"+ cNdx).value == "")
     {
         document.getElementById("currCity"+ cNdx).value = document.getElementById("lcity"+ cNdx).value;
         document.getElementById("currState"+ cNdx).value = document.getElementById("lstate"+ cNdx).value;

         lcn = document.getElementById("lcountry"+ cNdx).value.toLowerCase();
         ls = document.getElementById("lstate"+ cNdx).value.toLowerCase();
         lc = document.getElementById("lcity"+ cNdx).value.toLowerCase();
         theNdx = getStateNdx(lcn, ls);
         theCNdx = getCityNdx(lcn, ls, lc);

         if (theNdx != -1) // && theCNdx == -1)
         {
            deleteChildren(document.getElementById("cityl"+ cNdx));
            sz = document.getElementById("lcity"+ cNdx).size;
            document.getElementById('cell'+cNdx).removeChild(document.getElementById("cityl"+ cNdx));
            document.getElementById('cell'+cNdx).removeChild(document.getElementById("lcity"+ cNdx));

            var inp2 = document.createElement("input");
            inp2.type = "text";
            inp2.id = "lcity"+cNdx;
            inp2.name = "lcity[]";
            if (!static)
            {
               inp2.maxLength = 65;
               inp2.size= 21;
            }
            else
            {
               inp2.size = sz;
            }
            inp2.className = "auto-complete-city";

            var inp2a = document.createElement("ul");
            inp2a.id = "cityl"+cNdx;
            inp2a.name = "cityl[]";
            inp2a.className = "auto-complete-ul";

            cr = 0;
            for (var i = 0; i < theList_lc.countries.length; i++)
            {
                 if (theList_lc.countries[i].name == lcn)
                 {
                     for (var j = 0; j < theList_lc.countries[i].states.length; j++)
                     {
                        if (theList_lc.countries[i].states[j].name == ls)
                        {
                            for (var k = 0; k < theList_lc.countries[i].states[j].cities.length; k++)
                            {
                                if (theList_lc.countries[i].states[j].cities[k].name.substr(0, document.getElementById("currCity"+ cNdx).value.length) == lc || lc == "")
                                {
                                    var inp2aa = document.createElement("li");
                                    inp2aa.className="auto-complete-li";
                                    inp2aa.innerHTML = theList.countries[i].states[j].cities[k].name;
                                    inp2a.appendChild(inp2aa);
                                    cr = 1;
                                }
                            }
                            break;
                        }
                     }
                 }
            }
            if (!cr)
            {
               var inp2aa = document.createElement("li");
               inp2aa.className="auto-complete-li";
               inp2aa.innerHTML = "";
               inp2a.appendChild(inp2aa);
               document.getElementById("currCity"+ cNdx).value = document.getElementById("currCity"+ cNdx).value.substring(0, document.getElementById("currCity"+ cNdx).value.length -1);
            }
            inp2.value = document.getElementById("currCity"+ cNdx).value;
            document.getElementById('cell'+cNdx).appendChild(inp2);
            document.getElementById('cell'+cNdx).appendChild(inp2a);
            autoCompleteCity();

         }
     }
       if (document.createEvent)
       {
           var e = document.createEvent("HTMLEvents");
           e.initEvent("click", false, true);
           document.getElementById("lcity"+ cNdx).dispatchEvent(e);
       }
       else
       {
          document.getElementById("lcity"+ cNdx).fireEvent("onclick");
       }
   }
//   if (e && e.target)
//   {
//       e.preventDefault();
//   }
//   return false;
}




var refreshState = 0;
function updateState(cNdx)
{
   refreshState = 1;
/*
   if (static)
   {
      cNdx = 0;
   }
   else
   {
     if (e && e.target)
     {
        cNdx = parseInt(e.target.id.substr(e.target.id.indexOf('lstate')+'lstate'.length));
     }
     else
     {
        cNdx = parseInt(window.event.srcElement.id.substr(window.event.srcElement.id.indexOf('lstate')+'lstate'.length));
     }
   }
*/
   if (document.getElementById("lcountry"+ cNdx).value == "")
   {
       document.getElementById("lcountry"+ cNdx).focus();
   }
   else
   {
         document.getElementById("currState"+ cNdx).value = document.getElementById("lstate"+ cNdx).value;
         document.getElementById("currCountry"+ cNdx).value = document.getElementById("lcountry"+ cNdx).value;

         lcn = document.getElementById("lcountry"+ cNdx).value.toLowerCase();
         ls = document.getElementById("lstate"+ cNdx).value.toLowerCase();
         theNdx = getCountryNdx(lcn);
         theSNdx = getStateNdx(lcn, ls);

         if (theNdx != -1 && theSNdx == -1)
         {
            deleteChildren(document.getElementById("statel"+ cNdx));
            sz = document.getElementById("lstate"+ cNdx).size;
            document.getElementById('scell'+cNdx).removeChild(document.getElementById("statel"+ cNdx));
            document.getElementById('scell'+cNdx).removeChild(document.getElementById("lstate"+ cNdx));

            var inp2 = document.createElement("input");
            inp2.type = "text";
            inp2.id = "lstate"+cNdx;
            inp2.name = "lstate[]";

            if (!static)
            {
               inp2.maxLength = 65;
               inp2.size= 21;
            }
            else
            {
               inp2.size = sz;
            }
            inp2.className = "auto-complete-state";

            var inp2a = document.createElement("ul");
            inp2a.id = "statel"+cNdx;
            inp2a.name = "statel[]";
            inp2a.className = "auto-complete-ul";

            var cr = 0;
            for (var i = 0; i < theList_lc.countries.length; i++)
            {
                 if (theList_lc.countries[i].name == lcn)
                 {
                     for (var j = 0; j < theList_lc.countries[i].states.length; j++)
                     {
                          if (theList_lc.countries[i].states[j].name.substr(0, document.getElementById("currState"+ cNdx).value.length) == ls)
                          {
                             var inp2aa = document.createElement("li");
                             inp2aa.className="auto-complete-li";
                             inp2aa.innerHTML = theList.countries[i].states[j].name;
                             inp2a.appendChild(inp2aa);
                             cr = 1;
                          }
                     }
                     break;
                }
            }
            if (!cr)
            {
               var inp2aa = document.createElement("li");
               inp2aa.className="auto-complete-li";
               inp2aa.innerHTML = "";
               inp2a.appendChild(inp2aa);
               document.getElementById("currState"+ cNdx).value = document.getElementById("currState"+ cNdx).value.substring(0, document.getElementById("currState"+ cNdx).value.length -1);
            }
            inp2.value = document.getElementById("currState"+ cNdx).value;
            document.getElementById('scell'+cNdx).appendChild(inp2);
            document.getElementById('scell'+cNdx).appendChild(inp2a);
            autoCompleteState();

         }
       if (document.createEvent)
       {
           var e = document.createEvent("HTMLEvents");
           e.initEvent("click", false, true);
           document.getElementById("lstate"+ cNdx).dispatchEvent(e);
       }
       else
       {
          document.getElementById("lstate"+ cNdx).fireEvent("onclick");
       }
   }

   if (e && e.target)
   {
       e.preventDefault();
   }
   return false;
}
var refreshCityState = 0;
function updateStateCity()
{

            refreshCityState = 1;
            lc = document.getElementById("lstatecity0").value.toLowerCase();
            deleteChildren(document.getElementById("statecityl0"));
            document.getElementById('cell').removeChild(document.getElementById("statecityl0"));
            document.getElementById('cell').removeChild(document.getElementById("lstatecity0"));

            var inp2 = document.createElement("input");
            inp2.type = "text";
            inp2.id = "lstatecity0";
            inp2.name = "lstatecity[]";
            inp2.maxLength = 65;
            inp2.size= 21;
            inp2.value = lc;
            inp2.className = "auto-complete-state-city";

            var inp2a = document.createElement("ul");
            inp2a.id = "statecityl0";
            inp2.name = "statecityl[]";
            inp2a.className = "auto-complete-ul";

            cr = 0;
            for (i = 0; i < cityList.length; i++)
            {
              if (cityList[i].toLowerCase().substr(0, lc.length) == lc || lc == "")
              {
                var inp2aa = document.createElement("li");
                inp2aa.className="auto-complete-li";
                inp2aa.innerHTML = cityList[i];
                inp2a.appendChild(inp2aa);
                cr = 1;
              }
            }
            if (!cr)
            {
               var inp2aa = document.createElement("li");
               inp2aa.className="auto-complete-li";
               inp2aa.innerHTML = "";
               inp2a.appendChild(inp2aa);
            }
            document.getElementById('cell').appendChild(inp2);
            document.getElementById('cell').appendChild(inp2a);
            autoCompleteStateCity();
       if (document.createEvent)
       {
           var e = document.createEvent("HTMLEvents");
           e.initEvent("click", false, true);
           document.getElementById("lstatecity0").dispatchEvent(e);
       }
       else
       {
          document.getElementById("lstatecity0").fireEvent("onclick");
       }

}


function addLocation(tbl)
{
     cnt = document.getElementById(tbl).rows.length;

     if (tbl != "locations")
     {
        cnt++;
     }

     var tr0 = document.getElementById(tbl).insertRow(document.getElementById(tbl).rows.length);
     tr0.name = "row" + cnt;
     tr0.id = "row" + cnt;

     var td0 = tr0.insertCell(0);
     td0.align = "center";
     var inp0 = document.createElement("input");
     inp0.type = "text";
     inp0.id = "lcountry"+cnt;
     inp0.name = "lcountry[]";
     inp0.size = 21;
     inp0.value = "United States";
     inp0.className = "auto-complete-country";

     var inp0a = document.createElement("ul");
     inp0a.className = "auto-complete-ul";
      for (var i = 0; i < theList_lc.countries.length; i++)
      {
         var inp0aa = document.createElement("li");
         inp0aa.className="auto-complete-li";
         inp0aa.innerHTML = theList.countries[i].name;
         inp0a.appendChild(inp0aa);
     }

     td0.appendChild(inp0);
     td0.appendChild(inp0a);
/*
       try
       {
         inp0.addEventListener("blur", updateState, false);
       }
       catch (e)
       {
         inp0.attachEvent("onblur", updateState);
       }
*/
     var td1 = tr0.insertCell(1);
     td1.id = "scell"+cnt;
     td1.name = "scell[]";
     td1.align = "center";
     var inp1 = document.createElement("input");
     inp1.type = "text";
     inp1.id = "lstate"+cnt;
     inp1.name = "lstate[]";
     inp1.maxLength = 65;
     inp1.size= 21;
     inp1.className = "auto-complete-state";

     var inp1d = document.createElement("input");
     inp1d.type = "hidden";
     inp1d.id = "currCountry"+cnt;
     inp1d.name = "currCountry[]";
     inp1d.maxLength = 65;
     inp1d.value = "";
     inp1d.size= 21;

     var inp1b = document.createElement("input");
     inp1b.type = "hidden";
     inp1b.id = "currCity"+cnt;
     inp1b.name = "currCity[]";
     inp1b.maxLength = 65;
     inp1b.value = "";
     inp1b.size= 21;

     var inp1c = document.createElement("input");
     inp1c.type = "hidden";
     inp1c.id = "currState"+cnt;
     inp1c.name = "currState[]";
     inp1c.maxLength = 65;
     inp1c.value = "";
     inp1c.size= 21;

     var inp1a = document.createElement("ul");
     inp1a.id = "statel"+cnt;
     inp1a.name = "statel[]";
     inp1a.className = "auto-complete-ul";

     cr = 0;
     for (var i = 0; i < theList_lc.countries.length; i++)
     {
        if (theList_lc.countries[i].name == defaultCountry)
        {
            for (var j = 0; j < theList_lc.countries[i].states.length; j++)
            {
             var inp1aa = document.createElement("li");
             inp1aa.className="auto-complete-li";
             inp1aa.innerHTML = theList.countries[i].states[j].name;
             inp1a.appendChild(inp1aa);
             cr = 1;
            }
            break;
        }
     }
     if (!cr)
     {
         var inp1aa = document.createElement("li");
         inp1aa.className="auto-complete-li";
         inp1aa.innerHTML = "";
         inp1a.appendChild(inp1aa);
     }
     td1.appendChild(inp1d);
     td1.appendChild(inp1b);
     td1.appendChild(inp1c);
     td1.appendChild(inp1);
     td1.appendChild(inp1a);

     var td2 = tr0.insertCell(2);
     td2.id = "cell"+cnt;
     td2.name = "cell[]";
     td2.align = "center";
     var inp2 = document.createElement("input");
     inp2.type = "text";
     inp2.id = "lcity"+cnt;
     inp2.name = "lctiy[]";
     inp2.maxLength = 65;

     inp2.size= 21;
     inp2.className = "auto-complete-city";

     var inp2a = document.createElement("ul");
     inp2a.id = "cityl"+cnt;
     inp2a.name = "cityl[]";
     inp2a.className = "auto-complete-ul";

     var inp2aa = document.createElement("li");
     inp2aa.className="auto-complete-li";
     inp2aa.innerHTML = "";
     inp2a.appendChild(inp2aa);

     td2.appendChild(inp2);
     td2.appendChild(inp2a);

     if (tbl != "locations")
     {
         var td3 = tr0.insertCell(3);
         td3.align = "center";
         var a0 = document.createElement("a");
         a0.href = "#";
         a0.name = "remove" + cnt;
         a0.id = "remove" + cnt;
         a0.innerHTML = "remove";

         try
         {
              a0.addEventListener("click", removeLocation, false);
          }
          catch (e)
          {
              a0.attachEvent("onclick", removeLocation);
          }
          td3.appendChild(a0);
     }
}
function autoComplete()
{
      autoCompleteCity();
      autoCompleteState();
      autoCompleteCountry();
}
function addLocations(e)
{
   addLocation("locations");
   autoComplete();
   if (e && e.target)
   {
       e.preventDefault();
   }
   return false;
}
function addAddLocations(e)
{
   addLocation("addlocations");
   addLocation("addlocations");
   autoComplete();
   if (e && e.target)
   {
       e.preventDefault();
   }
   return false;
}

function addStaticLocation()
{
     var cr = 0;
     for (var i = 0; i < theList_lc.countries.length; i++)
     {
         var inp0aa = document.createElement("li");
         inp0aa.className="auto-complete-li";
         inp0aa.innerHTML = theList.countries[i].name;
         document.getElementById('countryl0').appendChild(inp0aa);
         if (theList_lc.countries[i].name == defaultCountry)
         {
            for (var j = 0; j < theList_lc.countries[i].states.length; j++)
            {
               var inp1aa = document.createElement("li");
               inp1aa.className="auto-complete-li";
               inp1aa.innerHTML = theList.countries[i].states[j].name;
               document.getElementById('statel0').appendChild(inp1aa);
               cr = 1;
            }
            break;
          }
     }

      if (!cr)
      {
         var inp1aa = document.createElement("li");
         inp1aa.className="auto-complete-li";
         inp1aa.innerHTML = "";
         document.getElementById('statel0').appendChild(inp1aa);
      }

      var inp2aa = document.createElement("li");
      inp2aa.className="auto-complete-li";
      inp2aa.innerHTML = "";
      document.getElementById('cityl0').appendChild(inp2aa);

}


function addDynamicLocation(cnt, numLists)
{
     var cr = 0;
     for (var i = 0; i < theList_lc.countries.length; i++)
     {
         var inp0aa = document.createElement("li");
         inp0aa.className="auto-complete-li";
         inp0aa.innerHTML = theList.countries[i].name;
         document.getElementById('countryl'+cnt).appendChild(inp0aa);
         for (k = (cnt + 1); k <= numLists; k++)
         {
             var tmp = inp0aa.cloneNode(true);
             document.getElementById('countryl'+k).appendChild(tmp);
         }
         if (theList_lc.countries[i].name == defaultCountry)
         {
            for (var j = 0; j < theList_lc.countries[i].states.length; j++)
            {
               var inp1aa = document.createElement("li");
               inp1aa.className="auto-complete-li";
               inp1aa.innerHTML = theList.countries[i].states[j].name;
               document.getElementById('statel'+cnt).appendChild(inp1aa);
               for (k = cnt; k < numLists; k++)
               {
                   var tmp = inp1aa.cloneNode(true);
                   document.getElementById('statel'+k).appendChild(tmp);
               }
               cr = 1;
            }
            break;
         }
     }
     if (!cr)
     {
        var inp1aa = document.createElement("li");
        inp1aa.className="auto-complete-li";
        inp1aa.innerHTML = "";
        document.getElementById('statel'+cnt).appendChild(inp1aa);
        for (k = cnt + 1; k <= numLists; k++)
        {
             var tmp = inp1aa.cloneNode(true);
             document.getElementById('statel'+k).appendChild(tmp);
        }
     }
      var inp2aa = document.createElement("li");
      inp2aa.className="auto-complete-li";
      inp2aa.innerHTML = "";
      document.getElementById('cityl'+cnt).appendChild(inp2aa);
      for (k = cnt + 1; k <= numLists; k++)
      {
           var tmp = inp2aa.cloneNode(true);
           document.getElementById('cityl'+k).appendChild(tmp);
      }
}


function population12()
{ 
} 

function population21()
{
}

function ldStatic()
{
      addStaticLocation();
      autoComplete();
}
function ldDynamic()
{
      addDynamicLocation(1, 4);
      autoComplete();
}

function getCities(cid, sid, cNdx, sNdx, ndx)
{
      var ajax = document.createElement('SCRIPT');
      ajax.type = 'text/javascript';
      ajax.src = "/includes/code/ajax_location_list.php?cid="+cid+"&sid="+sid+"&cndx="+cNdx+"&sndx="+sNdx+"&ndx="+ndx+"&callback=finishLd";
      if (document.getElementById('cell0'))
      {
          document.getElementById('cell0').appendChild (ajax);
      }
      else
      {
          document.getElementById('cell1').appendChild (ajax);
      }
}

function finishLd(cid, sid, ndx, json)
{
    theList.countries[cid].states[sid].cities = eval(json);
    lcCityList(cid, sid, ndx);
    if (document.getElementById('lcountry0'))
    {
       document.getElementById('lcountry0').disabled = false;
    }
    if (document.getElementById('lstate0'))
    {
       document.getElementById('lstate0').disabled = false;
    }
    if (document.getElementById('lcity0'))
    {
       document.getElementById('lcity0').disabled = false;
    }
}
/*

function getCities(cid, sid, cNdx, sNdx, ndx)
{
var xmlHttp;
try
  {
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.open("GET","/includes/code/ajax_location_list.php?cid="+cid+"&sid="+sid,false);
  xmlHttp.send(null);
  tmp = eval(xmlHttp.responseText);
  theList.countries[cNdx].states[sNdx].cities = eval(xmlHttp.responseText);
  theList.countries[cNdx].states[sNdx].cities = tmp;
  lcCityList(cNdx, sNdx, ndx);
}
*/
function lcCityList(cid, sid, ndx)
{
          theList_lc.countries[cid].states[sid].cities = new Array(theList.countries[cid].states[sid].cities.length);
          for (var k = 0; k < theList_lc.countries[cid].states[sid].cities.length; k++)
          {
             theList_lc.countries[cid].states[sid].cities[k] = new Object();
             theList_lc.countries[cid].states[sid].cities[k].name = theList.countries[cid].states[sid].cities[k].name.toLowerCase();
             theList_lc.countries[cid].states[sid].cities[k].id = theList.countries[cid].states[sid].cities[k].id;
          }
          if (document.getElementById("lcity"+ ndx).disabled)
          {
              document.getElementById("lcity"+ ndx).disabled = false;

              if (document.createEvent)
              {
                var e = document.createEvent("HTMLEvents");
                e.initEvent("click", false, true);
                document.getElementById("lcity"+ ndx).dispatchEvent(e);
             }
             else
             {
                document.getElementById("lcity"+ ndx).fireEvent("onclick");
             }
          }
}



