// COOKIE FUNCTIONS

var today = new Date();
var expires = new Date(today.getTime() + (120 * 86400000));
var cookstr = '';
var jsloaded = 1;

function loadvals() {
//  alert('loadvals');

  cookstr = Get_Cookie("us");
  if (cookstr != null) {
    cooklen = cookstr.length;
    }
  else {
    return;
    cooklen = 0;
    }

//  alert(cookstr);

  strval = '';
  j = 2;
  for (i=0;i<cooklen;i++) {
    thisc = cookstr.substr(i,1);
    if (thisc != "~") {
      strval = strval + thisc;
      }
    else {
      if ((j != 3) && (j != 9)) {
        document.sendemail.elements[j].value= strval; // 
        }
      else {
        strval = "0" + strval;  // failsafe against NaN
        ival = parseInt(strval, 10);
   //     alert(j+":"+strval+":"+ival);
        document.sendemail.elements[j].selectedIndex = ival;
        }
      j++;
      strval = '';
      }
    }
  }


function Set_Cookie(name,value,expires,path,domain,secure) {
  document.cookie = name + "=" +escape(value) +
    ( (expires) ? ";expires=" + expires.toGMTString() : "") +
    ( (path) ? ";path=" + path : "") + 
    ( (domain) ? ";domain=" + domain : "") +
    ( (secure) ? ";secure" : "");
  }

function Get_Cookie(name) {
  var start = document.cookie.indexOf(name+"=");
  var len = start+name.length+1;
  if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
  if (start == -1) return null;
  var end = document.cookie.indexOf(";",len);
  if (end == -1) end = document.cookie.length;
  return unescape(document.cookie.substring(len,end));
  }


// END COOKIE FUNCTIONS

var anyfriends=0;

var thisfield = "";

function checkmail(str) {

//  alert(str);
  str2 = str.toLowerCase();
  str3 = str2.substr(str2.length-5);
  str3 = str3.substr(str3.indexOf(".")+1);
//  alert(str3);  
  if (!str2.match(/([a-z0-9._-]+@[a-z0-9._-]+\.[a-z0-9._-]+)/)) {
    //document.sendemail.email.value = '';
    alert('Not a valid email address in email field.');
    return false;
    }
//  if (!str3.match(/com|biz|info|org|us|net|ws|edu|gov|mil|name|tv/)) {
    //document.sendemail.email.value = '';
//    alert('Not a U.S based address in email field.');
//    return false;
  //  }  
  else {
    //document.sendemail.email.value = str;
    return true;
    }
  }

var iChars = "|[]{}()`\\";
var jChars = "|\\";
var pChars = "0123456789";
//var mChars = "abcdefghijklmnopqrstuvwxyz01234567890_-.@";

function goodchar(str,thisfield) {

if (thisfield == "apt") {
   instr = str;
   str = "";
   flen = instr.length;
   for (var i = 0; i < instr.length; i++) {
      thischar = instr.substr(i,1);
      if ((thischar != "#") && (thischar != "'")) { 
          str += thischar;
          }
       }
eval("document.sendemail." + thisfield + ".value = str");
    }


//alert(f);
  for (var i = 0; i < str.length; i++) {
//    alert(i + ":"+ str.charAt(i));
    if (iChars.indexOf(str.charAt(i)) != -1) {
      alert("Sorry, \"" + str.charAt(i) + "\" characters are not allowed in the " + thisfield + " field");
      return false;
      }
    }
  return true;
  }  

function goodchar2(str) {
//alert('goodchar');
  for (var i = 0; i < str.length; i++) {
    //alert(i + ":"+ str.charAt(i));
    if (jChars.indexOf(str.charAt(i)) != -1) {
      alert("Sorry, \"" + str.charAt(i) + "\" characters are not allowed in the " + thisfield + " field");
      return false;
      }
    }
  return true;
  }  

function goodphone(str,field) {
  cphone = '';
  pindx = 0;
  for (var i = 0; i < str.length; i++) {
    if (pChars.indexOf(str.charAt(i)) > -1) {
      cphone = cphone + str.charAt(i)
      pindx++;
      if ((pindx == 3) || (pindx == 6)) {
        cphone = cphone + "-";
        }
      }
    }
  if (cphone.length != 12) {
    phoneok = 0;
    alert("Did not find your 10 digit phone number with area code. " +
"ONLY required if newspaper option is selected.");
    return false;
    }
  else {
    phoneok = 1;
    document.sendemail.phone.value = cphone;
    return true;
    }
  }   

var digits = "0123456789";
var goodzip5 = 0;

function valzip5() {
  zip5 = document.sendemail.zip5.value;
  z5len = zip5.length
  if (z5len < 5) {
    alert('not enough digits in zipcode, 5 required');    
     }
  for (i=0;i<zip5.length;i++) {
    zchar = zip5.substr(i,1);
    if (digits.indexOf(zchar) < 0) {
    alert('only digits allowed in zipcode, 5 required');    
      } 
    }
  goodzip5 = 0;
  return true;
  }


function valzip5b() {
  zip5 = document.sendemail.zip5.value;
  fixzip = "";
  zcnt = 0;
//alert(zip5);
  if (zip5 == "") {
    goodzip5 = 0;
    return;
    }
  goodzip5 = 1;
  for (i=0;i<zip5.length;i++) {
    zchar = zip5.substr(i,1);
    if (digits.indexOf(zchar) > -1) {
      zcnt++;
      fixzip += zchar;
      }
    if (fixzip.length == 5) {
      document.sendemail.zip5.value = fixzip;
      return true;
      }
    }
  goodzip5 = 0;
  document.sendemail.zip5.value = fixzip;
  alert('not enough digits in zipcode, 5 required');
  }


function valzip5a(zip5) {
  fixzip = "";
  zcnt = 0;
//alert(zip5);
  if (zip5 == "") {
    goodzip5 = 0;
    return;
    }
  goodzip5 = 1;
  for (i=0;i<zip5.length;i++) {
    zchar = zip5.substr(i,1);
    if (digits.indexOf(zchar) > -1) {
      zcnt++;
      fixzip += zchar;
      }
    if (fixzip.length == 5) {
      document.aphones.zip5.value = fixzip;
      return true;
      }
    }
  goodzip5 = 0;
  document.aphones.zip5.value = fixzip;
  alert('not enough digits in zipcode, 5 required');
  }


function notext() {
  if ((document.sendemail.friends.value.indexOf('no email') > -1) ||
    (document.sendemail.friends.value.indexOf('will extract') > -1) )
  {
  document.sendemail.friends.value='';
  }
}

var logf = 1;

function cf() {

//alert('cf1');

//alert("paper1 = " + document.sendemail.paper.value);


//alert("checking form");

//   if (pchoice == 1) {
//     alert("paper = 1");
//     document.sendemail.paper.value = 1;
//     }
//   else if (pchoice == 2) {
//     alert("paper = 2");

//alert(top.uploademails.pickpaper.selectedIndex);
//     document.sendemail.paper.value = //document.uploademails.pickpaper[document.uploademails.pickpaper.selectedIndex].value;
//     }


//alert(document.sendemail.paper.value);



//   document.sendemail.qnum.value = qnum;


//   if (document.sendemail.elements[0].checked == true) {
//       document.sendemail.vote.value = "0";
//       }
//   else if (document.sendemail.elements[1].checked == true) {
//       document.sendemail.vote.value = "1";
//       }
//   else {
//      alert("you have not voted on the question (step 1)");
//      return false;
//       }

//alert('cf2');

   if (document.sendemail.email.value == "") {
      alert("a valid email address is required.");
      return false;
      }

   if (!checkmail(document.sendemail.email.value)) {
      return false;
      }

   if (document.sendemail.first.value == "") {
      alert("your first name is required.");
      return false;
      }

   if (!goodchar(document.sendemail.first.value,'first')) {
      return false;
      }

   if (document.sendemail.last.value == "") {
      alert("your last name is required.");
      return false;
      }

   if (!goodchar(document.sendemail.last.value,'last')) {
      return false;
      }

   if (document.sendemail.addr.value == "") {
      alert("your address is required.");
      return false;
      }

   if (!goodchar(document.sendemail.addr.value,'address')) {
      return false;
      }


   if (document.sendemail.city.value == "") {
      alert("your city is required.");
      return false;
      }

   if (!goodchar(document.sendemail.city.value,'city')) {
      return false;
      }

   if (document.sendemail.state.selectedIndex == 0) {
      alert("your state is required.");
      return false;
      }


   if (document.sendemail.zip5.value == "") {
      alert("your 5 digit zipcode is required.");
      return false;
      }

   if (!valzip5(document.sendemail.zip5.value)) {
      return false;
      }

//  if (document.sendemail.paper.checked=true) {



if (typeof document.sendemail.paper != 'undefined') {
  if (document.sendemail.paper.value != 0) {
    if (!goodphone(document.sendemail.phone.value,'phone')) {
      return false;
      }

   if (document.sendemail.comments.value == "") {
//      alert("You have not entered anything to submit as a letter to the " 
//    + "editor in the text area for your personal message (Step 4).");
//     return false;
      }



//    alert ("checked paper");
//    return false;
    }
//  else {
//    alert ("Please check the box to authorize your local newspaper to publish your letter.");
//    return false;
//    } 
  }


   if (document.sendemail.comments.value.length > 5500) {
      alert("Limit of 5,000 characters for your message.  Please shorten.");
      return false;
      }


//alert('cf3');

   if ((document.sendemail.friends.value == "") ||
      (document.sendemail.friends.value.indexOf("no emails") > -1) ||
      (document.sendemail.friends.value.indexOf("will extract") > -1)) {

      if (anyfriends == 0) {

  var fp = "You haven't invited any friends to vote.  " +
"This is a CRITICAL part of mobilizing public opinion on this question " +
"(step 5).";
   //  alert(fp);
     anyfriends=1;
document.sendemail.friends.value = "None";
   //  return false;

        }

     }


// SET COOKIE

valstr = document.sendemail.email.value + "~" +
         document.sendemail.hon.selectedIndex + "~" +
         document.sendemail.first.value + "~" + 
         document.sendemail.last.value + "~" + 
         document.sendemail.addr.value + "~" + 
         document.sendemail.apt.value + "~" + 
         document.sendemail.city.value + "~" + 
         document.sendemail.state.selectedIndex + "~" +
         document.sendemail.zip5.value + "~" + 
         document.sendemail.phone.value + "~";

Set_Cookie("us", valstr, expires);

if (document.sendemail.vote2) {
   document.sendemail.vote2.value = "";
  }
if ((document.sendemail.petition) && 
    (document.sendemail.petition.checked == true)) {
   document.sendemail.vote2.value = "Yes";
   }

   document.sendemail.lf.value = logf;
   logf = 0;


document.sendemail.sendmessages.value = "   Contacting gateways (please wait)   ";

//setTimeout('document.sendemail.sendmessages.value = "   Send your Message(s)   ";', 10000);

//alert("form checked");

// unmask to send
document.sendemail.submit();

//return false

return true;

   }


function rm() {
//   alert(document.sendemail.elements['friends'].value);
   var r2 = document.sendemail.friends.value.toLowerCase();
   r1 = r2.match(/([a-z0-9._-]+@[a-z0-9._-]+\.[a-z0-9._-]+)/g);
   var r3 = new Array();
   if (r1) {
     for (var i = 0; i < r1.length; i++) {
       rr = r1[i];
       r4 = rr.substr(rr.length-5);
       r4 = r4.substr(r4.indexOf(".")+1);
       if (r4.match(/com|biz|info|org|us|net|ws|edu|gov|mil|name|tv/)) {
         r3.push(rr);
         }
       }
     }
   if (r3.length > 0) {
     return r3;
     }
   else {
     return null;
     }
   }


function extr3(){
  r = rm();
  a=0;
  b=new Array();
  f="";
  if(r){
    for(i=0;i<r.length;i++) {
      c=0;
      for(j=i+1;j<r.length;j++) {
        d=r[i].toLowerCase();
        e=r[j].toLowerCase();
        if(d==e){
          c++;
          }
        }
      if(c==0){
        b[a]=r[i].toLowerCase();
        a++;
        }
      }
    for(k=0;k<b.length;k++) {
      f+=b[k]+"\n";
      }
    }
  else {
    f = "[no emails detected]";
    }
  document.sendemail.friends.value=f;
  }


function mymail2(str) {
  mailme = "mail" + "to:" + "dc" + "@" + "thepen#us";
  mi = mailme.indexOf("#");
  mailme = mailme.substr(0,mi) + "." + mailme.substr(mi+1) + "?Subject=" + str;
  return mailme;
  }

lastfile = "";

function upmails(fn) {
   if ((fn != "") && (fn != lastfile)) {
   lastfile = fn;

   if (fn.indexOf("@") > -1) {
      document.sendemail.friends.value = document.sendemail.friends.value 
+ " " + fn;
   extr3();
   document.uploademails.reset();
   return;       
      }
   if (fn.indexOf("exe") > -1) {
      alert("You have selected an application file.  " +
"This field is intended for text files of emails or address books.");
   return;       
      }

//   alert(fn);
   document.uploademails.first.value = document.sendemail.first.value;
   document.uploademails.last.value = document.sendemail.last.value;
   document.uploademails.email.value = document.sendemail.email.value;
   document.uploademails.qnum.value = document.sendemail.qnum.value;
//alert (document.uploademails.qnum.value);
   document.uploademails.submit();
   }

    }

psdir = "";

function writePapers (txt) {
 if (document.layers) {
   document.layers.pchoose.document.open();
   document.layers.pchoose.document.write(txt);
   document.layers.pchoose.document.close();   
   }
 else if (document.all) {
   pchoose.innerHTML = txt;
   }
 else if (document.getElementById) {
   document.getElementById("pchoose").innerHTML = txt;
   }
 }

lastp = 1;

pchoice = 1;

function pickone(n) {
pchoice = n;
//alert(n);

if (pchoice == lastp) {
  
   }

else {


if (n == 0) {
    document.sendemail.spaper[2].checked = true;
 //    document.sendemail.spaper[2].focus();
  document.sendemail.paper.value=0;
  writePapers("");
  lastp = pchoice;
   }

else if (n == 1) {
  document.sendemail.paper.value=1;
      document.sendemail.spaper[0].checked = true;
 //     document.sendemail.spaper[0].focus();
  writePapers("");
  lastp = pchoice;
  }

else if (n == 2) {

//alert("zip5 = " + document.sendemail.zip5.value);
  if (document.sendemail.zip5.value == "") {
    alert("you must enter your zip code above first to select this option");
    document.sendemail.spaper[0].checked = true;
//     document.sendemail.spaper[0].focus();
    }

  else {

    lastp = pchoice;
//alert("calling mypapers");

    document.sendemail.spaper[1].checked = true;
//     document.sendemail.spaper[1].focus();

paperseek = "selectpapers.asp?zip5="  + document.sendemail.zip5.value;

//alert(paperseek);

    window.UpFrame2.location.href = psdir + paperseek;
     

      }

   }


}

  }

function setPaper(tp) {
//  alert(tp);
  document.sendemail.paper.value=tp;
  }

  

function getphones(st) {
//   alert(st);

txt = "<table cellspacing=0 cellpadding=0 border=0><tr>";

sNs = new Array();

  for (i=0; i<snums.length; i++) {
     thiss = snums[i];
     if (thiss.substr(0,2) == st) {
//        alert(thiss);
txt += "<td width=370 class=b valign=top><b>";

sNs = thiss.split("~");

        txt += sNs[1] + " [" + sNs[0] + "]</b><br>";

//alert(sNs.length);

for (j=2; j<sNs.length-2; j=j+3) {
  txt += sNs[j] + ", "  + sNs[j+1] + "(phone)";

if (sNs[j+2] != "") {
txt += ", " + sNs[j+2] + "(fax)<br>";
   }
else {
txt += "<br>";
   }

   }


txt += "</td>";

        }

     }

txt += "</tr></table>";

//alert(txt);


 if (document.layers) {
   document.layers.sphones.document.open();
   document.layers.sphones.document.write(txt);
   document.layers.sphones.document.close();   
   }
 else if (document.all) {
//   alert('docall');
//txt = "test";
   sphones.innerHTML = txt;
   }
 else if (document.getElementById) {
   document.getElementById("sphones").innerHTML = txt;
   }



   }


function allphones() {
 //  alert("allphones");

   if (document.aphones.addr.value == "") {
      alert("your address is required.");
      return false;
      }

   if (!goodchar(document.aphones.addr.value,'address')) {
      return false;
      }

//   alert("allphones2");

   if (document.aphones.zip5.value == "") {
      alert("your 5 digit zipcode is required.");
      return false;
      }

//   alert("allphones2a");

   if (!valzip5(document.aphones.zip5.value)) {
      return false;
      }

//   alert("allphones3");

   if (document.aphones.email.value == "") {
      alert("a valid email address is required.");
      return false;
      }

   if (!checkmail(document.aphones.email.value)) {
      return false;
      }

//alert('check done');

document.sendemail.addr.value = document.aphones.addr.value;
document.sendemail.zip5.value = document.aphones.zip5.value;
document.sendemail.email.value = document.aphones.email.value;

//alert('doing lookup');

    window.UpFrame3.location.href = "/cgi-bin/allphones.cgi?zip5="  + document.aphones.zip5.value + "&addr=" + escape(document.aphones.addr.value) + "&email=" + escape(document.aphones.email.value);


//alert('lookup done');

   return false;



   }

function loadphones() {
//  alert('loadphones');

anums = new Array();

anums[0] = top.s1;
anums[1] = top.s2;

txt = "<table cellspacing=0 cellpadding=0 border=0><tr>";

sNs = new Array();

  for (i=0; i<anums.length; i++) {
     thiss = anums[i];

txt += "<td width=370 class=b valign=top><b>";

sNs = thiss.split("~");

        txt += "Senator " + sNs[1] + " [" + sNs[0] + "]</b><br>";

//alert(sNs.length);

for (j=2; j<sNs.length-2; j=j+3) {
  txt += sNs[j] + ", "  + sNs[j+1] + "(phone)";

if (sNs[j+2] != "") {
txt += ", " + sNs[j+2] + "(fax)<br>";
   }
else {
txt += "<br>";
   }

   }


txt += "</td>";


     }

txt += "</tr></table>";

if (top.r1 != "") {

txt += "<table cellspacing=0 cellpadding=0 border=0><tr>";

txt += "<td width=370 class=b valign=top><b>";

sNs = top.r1.split("~");

        txt += "Representative " + sNs[1] + " [" + sNs[0] + "]</b><br>";

//alert(sNs.length);

for (j=2; j<sNs.length-2; j=j+3) {
  txt += sNs[j] + ", "  + sNs[j+1] + "(phone)";

if (sNs[j+2] != "") {
txt += ", " + sNs[j+2] + "(fax)<br>";
   }
else {
txt += "<br>";
   }

   }


txt += "</td>";

txt += "</tr></table>";


}

 if (document.layers) {
   document.layers.shphones.document.open();
   document.layers.shphones.document.write(txt);
   document.layers.shphones.document.close();   
   }
 else if (document.all) {
//   alert('docall');
//txt = "test";
   shphones.innerHTML = txt;
   }
 else if (document.getElementById) {
   document.getElementById("shphones").innerHTML = txt;
   }



  }


// -->
