var Join =
 {
  selectUserType: function(id)
   {
    types = [0, 1, 2, 10, 12];
    checkId = "usertype_" + id.substring(1);
    
    $("step2next").style.backgroundPosition = "0px 0px";
   
    for (enumTypes = 0; enumTypes < types.length; enumTypes++)
     {
      tryId = "usertype_" + types[enumTypes];
      element = $(tryId);
      
      if (element != null)
       {
        if (checkId == tryId)
         {
          if (element.checked)
           {
            $("u" + types[enumTypes]).style.backgroundPosition = "0px 0px";
            element.checked = false;
           }
           
          else
           {
            if (types[enumTypes] == 2 || types[enumTypes] == 12)
             {
              $("step2next").style.backgroundPosition = "0px -50px";
             }
           
            $("u" + types[enumTypes]).style.backgroundPosition = "0px -80px";
            element.checked = true;
           }
         }
        
        else
         {
          $("u" + types[enumTypes]).style.backgroundPosition = "0px 0px";
          element.checked = false;
         } 
       }
     }
   },
   
  displayCountries: function()
   {
    Notifier.create({persistent: true, closeable: true});
    Notifier.open("Select your country: <span style='text-decoration: underline; cursor: pointer;' onClick='document.location=\"http://au.ratemyteachers.com/Join.php\";'>Australia</span>, " +
                                        "<span style='text-decoration: underline; cursor: pointer;' onClick='document.location=\"http://ie.ratemyteachers.com/Join.php\";'>Ireland</span>, " +
                                        "<span style='text-decoration: underline; cursor: pointer;' onClick='document.location=\"http://nz.ratemyteachers.com/Join.php\";'>New Zealand</span>, " +
                                        "<span style='text-decoration: underline; cursor: pointer;' onClick='document.location=\"http://uk.ratemyteachers.com/Join.php\";'>United Kingdom</span>, or " +
                                        "<span style='text-decoration: underline; cursor: pointer;' onClick='document.location=\"http://www.ratemyteachers.com/Join.php\";'>United States</span>.");


   }
 };
 
var Inviter =
 {
  invite: function()
   {
    Inviter.reset();
    $("importresults").setStyle("display", "none");
    
    if ($("inviteremail").value == "")
     {
      Notifier.create({persistent: true, closeable: true});
      Notifier.open("Please enter an email address.");
      return;
     }
     
    if ($("inviterpassword").value == "")
     {
      Notifier.create({persistent: true, closeable: true});
      Notifier.open("Please enter a password.");
      return;
     } 
    
    $("inviteremail_hold").value = $("inviteremail").value;
    service = this.findService($("inviteremail").value);
    
    if (this.isValidService(service))
     {
      $("importinputs").setStyle("display", "none");
      $("ajaxloader").setStyle("display", "block");
      request = new Request({"method": "get",
                             "url": "/OpenInviter/pullcontacts.php",
                             onComplete: function(responseText, responseXML)
                                          {
                                           Inviter.createForm(JSON.decode(responseText));
                                          }});
      request.send("email=" + $("inviteremail").value + "&password=" + $("inviterpassword").value + "&service=" + service);
     }
     
    else
     {
      Notifier.create({persistent: true, closeable: true});
      Notifier.open("We cannot pull contacts from your email provider - we can only read your contacts from GMail, Yahoo, Hotmail, and AOL.");
     }
   },

  createForm: function(results)
   {
    $("ajaxloader").setStyle("display", "none");
   
    if (results.length > 1 && results[0].error == null)
     {
      $("importcontainer").setStyle("display", "block");
      $("importresults").setStyle("display", "block");
      $("numresults").value = results.length;
      
      Notifier.create({persistent: true, closeable: true});
      Notifier.open("You have <span style='text-decoration: underline;'>" + results.length + "</span> contacts in your address book.")
     
      for (enumResults = 0; enumResults < results.length; enumResults++)
       {
        liElement = new Element("li", {"class": ((enumResults % 2 == 0) ? "" : "gray")});
        divElement = new Element("div", {"html": results[enumResults].email});
        inputElement = new Element("input", {"type": "checkbox",
                                             "name": "emailinvites_" + enumResults,
                                             "value": results[enumResults].email,
                                             "checked": "checked"});
        
        divElement.inject(liElement);
        inputElement.inject(liElement);
        liElement.inject($("resultslist"));
       }
     }
     
    else
     {
      $("importinputs").setStyle("display", "block");
      Notifier.create({persistent: true, closeable: true});
      Notifier.open("There was an error importing your contacts. Perhaps you typed in your e-mail address/password incorrectly?");
     } 
   },
   
  reset: function()
   {
    liElements = $("resultslist").getElements("li");
    
    for (enumElements = 0; enumElements < liElements.length; enumElements++)
     {
      liElements[enumElements].dispose();
     }
    
    $("numresults").value = 0; 
   }, 
   
  toggle: function()
   {
    liElements = $("resultslist").getElements("li");
    
    for (enumElements = 0; enumElements < liElements.length; enumElements++)
     {
      inputElement = liElements[enumElements].getElement("input");
      
      if (inputElement.checked)
       {
        inputElement.checked = false;
       }
       
      else
       {
        inputElement.checked = true;
       } 
     }
   }, 
   
  findService: function(email)
   {
    startPos = email.indexOf("@");
    endPos = email.indexOf(".", startPos);

    return email.substring(startPos + 1, endPos).toLowerCase();
   },
   
  isValidService: function(service)
   {
    switch (service)
     {
      case "gmail":
      case "yahoo":
      case "hotmail":
      case "aol":
       return true;
     }
     
    return false; 
   },

  submit: function()
   {
    $("importresults").submit();
   }, 
   
  cancel: function()
   {
    $("importresults").setStyle("display", "none");
    Inviter.reset();
    $("importresults").submit();
   } 
 };