// JavaScript Document

      //Set variables for formHide (hides entry area if no JS), emailText, sentText, and submitButton.
      //Reset the form.

      if (document.getElementById){
        document.getElementById('downloadForm').reset();
        var formHide = document.getElementById('formHide');
        var emailText = document.getElementById('emailText');
        var sentText = document.getElementById('sentText');
        var submitButton = document.getElementById('subButton');
      } else if (document.all){
        document.forms[1].reset();
        var formHide = document.all['formHide'];
        var emailText = document.all['emailText'];
        var sentText = document.all['sentText'];
        var submitButton = document.all['subButton'];
      }

      //Hide formHide, emailText and sentText divs
      formHide.style.display='none';
      emailText.style.display = 'none';
      sentText.style.display = 'none';
     
      //Set submitButton onclick to default - falseSubmit
      submitButton.onclick = falseSubmit;
      
      //Set submitButton's mouse functions
      submitButton.onmouseover = function() { submitButton.style.color = '#FFFF33'; };
      submitButton.onmousedown = function() { submitButton.style.color = '#CC0000'; };
      submitButton.onmouseout = function() { submitButton.style.color = '#00FF00'; };

     //Initialize jukebox
     var so = new SWFObject("playerMultipleList.swf", "mymovie", "295", "200", "5", "#FFFFFF");  
     so.addVariable("autoPlay","no")
     so.addVariable("playlistPath","playlist.xml")
     so.write("flashPlayer");

    

    

    //**Function emailReturnIE()**
    //Processes return from call to emailValid.php.  If emailValid.php returns at 1, address is valid.
    //If return is "", email is invalid and an error message div is displayed.
    function emailReturnIE(){
      if (request.readyState == 4){
            var emailText = document.all['emailText'];
            var submitButton = document.all['subButton'];
            if (request.responseText == '1'){
              emailText.style.display = 'none';
              submitButton.onclick = trueSubmit;	
            }else{
              emailText.innerHTML = "Email address is invalid. Please correct.<br />";
              emailText.style.display = 'block';
              submitButton.onclick = falseSubmit;
            }
      }
    }

    

    //**Function checkEmail()**
    //After emailText has been changed, this function delivers the new email address to 
    //emailValid.php.
    function checkEmail(){
      var email = document.getElementById('email');
      email = email.value;
      var emailText = document.getElementById('emailText');
      var sentText = document.getElementById('sentText');
      sentText.style.display = 'none';    

      if (request){
        var params = "email=" + email;
        var url = "emailValid.php";
        request.open("POST", url ,true);
      if (navigator.appName=="Microsoft Internet Explorer"){
          request.onreadystatechange = function(){emailReturnIE();};
        }else{
          request.onreadystatechange = function(){
            if (request.readyState == 4){
              if (request.responseText == '1'){
                emailText.style.display = 'none';
                submitButton = document.getElementById('subButton');
                submitButton.onclick = trueSubmit;
              }else{
                emailText.innerHTML = "Email address is invalid.  Please correct. <br />";
                emailText.style.display = 'block';
                submitButton = document.getElementById('subButton');
                submitButton.onclick = falseSubmit;
              }
            }
          }
        }
        request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        request.setRequestHeader("Content-length", params.length);
        request.setRequestHeader("Connection", "close");
        request.send(params);
      }
    }
    
    //**Function falseSubmit()**
    //Used for submitButton when email address is invalid.  When run, it displays
    //an error message.
    function falseSubmit(){
      alert('Please retype your entries in full. If you\'re still having trouble, email store(at)hemlockrecords.com for help');
    }

    //**Function submitReturnIE()**
    //Processes return from the complete submission to sendLink.php.  If 'sent' is returned,
    //a positive message is displayed.  If 'dup' is returned, a 'duplicate email received' message
    //is displayed.
    function submitReturnIE(){
      if (request.readyState == 4){
          if (request.responseText == 'sent'){
             var sentText = document.all['sentText'];
             var form = document.all['downloadForm'];
             sentText.innerHTML = 'You email has been sent!';
             sentText.style.display = 'block';
             form.reset();
             } else if (request.responseText == 'duplicate'){
              var sentText = document.all['sentText'];
              sentText.innerHTML = "That email address is already in our database.  Please enter another or email <a href='mailto:store@hemlockrecords.com'>store at hemlockrecords.com</a> with any questions.";
              sentText.style.display = 'block';
             } else {
              alert(request.responseText);
             }
      }
    }

  //**Function trueSubmit()**
  //If email is valid, trueSubmit is assigned to the submitButton.  This sends the 
  //information from the form to sendLink.php.
  function trueSubmit(){
    if (request){
        var url = "sendLink.php";
        request.open("POST", url ,true);
        if (navigator.appName=="Microsoft Internet Explorer"){
          request.onreadystatechange = function(){submitReturnIE();};
        }else{ 
          request.onreadystatechange=function(){
            if (request.readyState == 4){
                if (request.responseText == 'sent'){
                  sentText = document.getElementById('sentText');
                  form = document.getElementById('downloadForm');
                  sentText.innerHTML = 'Email was sent!';
                  sentText.style.display = 'block';
                  form.reset();
                }else if (request.responseText == 'duplicate'){
                  sentText = document.getElementById('sentText');
                  sentText.innerHTML = "That email address is already in our database.  Please enter another or email <a href='mailto:store@hemlockrecords.com'>store at hemlockrecords.com</a> with any questions.";
                  sentText.style.display = 'block';
                }else {
                  alert(request.responseText);
                }
            }
          }
        }  

        var email = document.getElementById('email').value;
        var name = document.getElementById('fullname').value;
        var favBands = document.getElementById('favBands').value;
        var zip = document.getElementById('zip').value;
        //var checked = document.getElementById('physBox').checked;
        /* force checked to false since we aren't using it */
        var checked = false;
        var params = "email=" + email + "&name=" + name + "&bands=" + favBands + "&zip=" + zip + "&check=" + checked;       
        request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        request.setRequestHeader("Content-length", params.length);
        request.setRequestHeader("Connection", "close");
        request.send(params);
      }
    }

     



  


