
function backToTop() {
    var x1 = x2 = x3 = 0;
    var y1 = y2 = y3 = 0;

    if (document.documentElement) {
        x1 = document.documentElement.scrollLeft || 0;
        y1 = document.documentElement.scrollTop || 0;
    }

    if (document.body) {
        x2 = document.body.scrollLeft || 0;
        y2 = document.body.scrollTop || 0;
    }

    x3 = window.scrollX || 0;
    y3 = window.scrollY || 0;

    var x = Math.max(x1, Math.max(x2, x3));
    var y = Math.max(y1, Math.max(y2, y3));

    window.scrollTo(Math.floor(x / 2), Math.floor(y / 2));

    if (x > 0 || y > 0) {
        window.setTimeout("backToTop()", 25);
    }
}

function frmValidate()
{
   //declarations
   var x; var y; var strErrors; var strURL;

   //the HTML DIV where the errors will show up.
   document.getElementById('subscribebox_errors').innerHTML="";

   //function call....returns how many checkboxes have actually been checked. 
   var x = numberChecked();
   
   //function call....uses regular expressions to validate if the value in the email form box is
   //indeed a valid email.  Please note, I had to add an actual ID tag to the form, not just use
   //the HTML name tag.

   var y = validateEmail(document.getElementById('ml_email').value);
   
   //if neither a checkbox is checked, or it's not a valid email.
   if( (!(x>=1)) || (!y) )

   {
      //build the error text/html string
      strErrors='<ul class="error_text">Errors found:';
      
      if(!(x>=1))
      {
         strErrors = strErrors + '<li>Please select at least one newsletter.<\/li>';
      }
         
      if(!y)
      {
         strErrors = strErrors + '<li>Please input a valid e-mail address.<\/li>';
      }
   
      strErrors = strErrors + '<\/ul>';

      //write the error HTML to the appropriate error area.
      document.getElementById("subscribebox_errors").innerHTML=" " + strErrors;
   }
   
   else if( (x>=1) && (y=1) )
   {
      //its all good!  Begin the AJAX post procedure.
      ajaxSendSubscribeInfo();
   }
   else
   {
      //if you get here, something went really wrong.
      alert('Error!');
   }
}

function ajaxSendSubscribeInfo()
{
   //declarations
   var urlStr; var urlParams; var returnResponse;
   var sForm;
   var xmlhttp;


   //standard AJAX object creation
   if (window.XMLHttpRequest)
   {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
   }
   else
   {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }

   //place your PHPList URL here.  IMPORTANT! Be sure to add ?p=subscribe and the &id= if you are using these functions.
   
   urlStr = 'http://www.isabellondono.info/lists/?p=subscribe&id=2';
   
   //declare your AJAX form
   sForm = document.forms["subscribeform"];
   
   //prepping POST post string.  Should probably use a proper URL encoding.  Haven't got there yet.
   urlParams = urlParams + '&email=' + escape(sForm.elements["email"].value);

   //iterate through all the checkboxes.  IMPORTANT side note regarding PHPLIST.  The list names in your form must be in this format.
   // list[1]=signup list[2]=signup etc.  Be sure to use the word "signup" (all lower case, no spaces) as the value for each list, or
   // phplist won't recognize it.

   for(var i=0;i<sForm.elements.length;i++)
   {
      if( (sForm.elements[i].type=="checkbox") && (sForm.elements[i].checked) )
         {   
               urlParams=urlParams + '&' + escape(sForm.elements[i].name) + '=' + escape(sForm.elements[i].value);      
         }
   }
   
   //must add the subscribe variable to the POST parameters. 

   urlParams = urlParams + '&subscribe=hitme';

   //Start POSTING via AJAX.
   xmlhttp.open("POST",urlStr,true);

   //Send the proper header information along with the request
   xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   xmlhttp.setRequestHeader("Content-length", urlParams.length);
   xmlhttp.setRequestHeader("Connection", "close");

 
   xmlhttp.onreadystatechange = function()
   {
   alert("Status is "+xmlhttp.status)
      if (xmlhttp.readyState == 4)
      {
         if (xmlhttp.status == 200)
         { // only if "OK"
   	window.open(urlStr);
         
            var returnResponse = String(xmlhttp.responseText);
            document.getElementById('subscribebox').innerHTML="Thank you for subscribing.  Please check your email for information about confirming your subscription.";
         }
      
         else
         {
               document.getElementById('subscribebox').innerHTML="An Error has occured while submitting your request.  Please contact the webmaster.";
         }
      }
alert("URL doesn't Exist!")
   }

   xmlhttp.send(urlParams);
}

function validateEmail(elementValue)
{
   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
   return emailPattern.test(elementValue);
}


function numberChecked()
{
   var numChecked = 0;
   
   var sForm = document.forms["subscribeform"];

   for(var i=0;i<sForm.elements.length;i++)
   {   
      if( (sForm.elements[i].type=="checkbox") && (sForm.elements[i].checked) )
      {   
            numChecked++;      
      }
   }
   
   return numChecked;
}
