var windows;
var validate;
function turnOffVotes()
{
    setTimeout("windows.turnOff('rateWin');",1000);
}
function turnOffEmail()
{
    setTimeout("windows.turnOff('emailWin');",2000);
    setTimeout("$(\"emailResponse\").innerHTML = \"\";",2000);
    $("RecipientEmail").value = "";
    $("RecipientName").value = "";
    $("SenderEmail").value = "";
    $("SenderName").value = "";
    $("PersonalMessage").value = "";
}
function closePrintWin()
{
    setTimeout("windows.turnOff('printWin');",500);
}
Event.observe(window,"load",function()
{
  // assign events
  $("emailClick").onclick = function(e) { windows.turnOn("emailWin"); }
  $("printClick").onclick = function(e) { windows.turnOn("printWin"); }
  $("rateClick").onclick = function(e) { windows.turnOn("rateWin"); }
  $("closePrint").onclick = function(e) { windows.turnOff("printWin"); }
  $("closeEmail").onclick = function(e) { windows.turnOff("emailWin"); }
  $("closeRate").onclick = function(e) { windows.turnOff("rateWin"); }
  if($("r1")) $("r1").onclick = function(e) { windows.rate(1); }
  if($("r2")) $("r2").onclick = function(e) { windows.rate(2); }
  if($("r3")) $("r3").onclick = function(e) { windows.rate(3); }
  if($("r4")) $("r4").onclick = function(e) { windows.rate(4); }
  if($("r5")) $("r5").onclick = function(e) { windows.rate(5); }
  if($("r1")) $("r1").onmouseover = function(e) { windows.starLight(1); };
  if($("r2")) $("r2").onmouseover = function(e) { windows.starLight(2); };
  if($("r3")) $("r3").onmouseover = function(e) { windows.starLight(3); };
  if($("r4")) $("r4").onmouseover = function(e) { windows.starLight(4); };
  if($("r5")) $("r5").onmouseover = function(e) { windows.starLight(5); };
  if($("r1")) $("r1").onmouseout = function(e) { windows.starUnlight(); };
  if($("r2")) $("r2").onmouseout = function(e) { windows.starUnlight(); };
  if($("r3")) $("r3").onmouseout = function(e) { windows.starUnlight(); };
  if($("r4")) $("r4").onmouseout = function(e) { windows.starUnlight(); };
  if($("r5")) $("r5").onmouseout = function(e) { windows.starUnlight(); };
  // define objects
  validate = {
    errorList:{
      ids:[],
      messages:[]
    },
    validList:{
      ids:[]
    },

    errorFlag:false,
    init:function()
    {
      // this is bound to the structure of the form
      this.errorFlag=false;
      this.errorList.ids = new Array();
      this.errorList.messages = new Array();
      this.validList.ids = new Array();
      if(!this.isName($("SenderName").value))
      {
        this.errorList.ids.push("SenderName");
        this.errorList.messages.push("Invalid Name");
        this.errorFlag = true;
      }
      else
      {
        this.validList.ids.push("SenderName");
      }
      if(!this.isName($("RecipientName").value))
      {
        this.errorList.ids.push("RecipientName");
        this.errorList.messages.push("Invalid Name");
        this.errorFlag = true;
      }
      else
      {
        this.validList.ids.push("RecipientName");
      }
      if(!this.isEmail($("SenderEmail").value))
      {
        this.errorList.ids.push("SenderEmail");
        this.errorList.messages.push("Invalid Email");
        this.errorFlag = true;
      }
      else
      {
        this.validList.ids.push("SenderEmail");
      }
      if(!this.isEmail($("RecipientEmail").value))
      {
        this.errorList.ids.push("RecipientEmail");
        this.errorList.messages.push("Invalid Email");
        this.errorFlag = true;
      }
      else
      {
        this.validList.ids.push("RecipientEmail");
      }
      this.yield();
    },
    isName:function(arg)
    {
        if(arg.match(/[A-Za-z0-9]/))
        {
            return true;
        }
        else
        {
            return false;
        }
    },
    isEmail:function(arg)
    {
        if(arg.match(/^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/))
        {
            return true;
        }
        else
        {
            return false;
        }
    },
    yield:function()
    {
      if(!this.errorFlag)
      {
        windows.email();
      }
      else
      {
        this.clear();
        this.report();
      }
    },
    report:function()
    {
      for(var i=0;i<this.errorList.ids.length;i++)
      {
        $(this.errorList.ids[i]+"Label").style.color = "red";
      }
    },
    clear:function()
    {
      var color = "#6f481e";
      $("SenderNameLabel").style.color = color;
      $("SenderEmailLabel").style.color = color;
      $("RecipientNameLabel").style.color = color;
      $("RecipientEmailLabel").style.color = color;
    }
  }
  windows =
  {
    cur:"",
    //#chefWin,#rateWin,#emailWin,#printWin
    init:function()
    {

    },
    starLight:function(x)
    {
        for(var i=1;i<=x;i++)
        {
            eval("$('r"+i+"').src='/images/misc/star_on.gif'");
        }
        var msg = "";
        switch(x)
        {
            case 1:
                msg = "(Bad)";
            break;
            case 2:
                msg = "(Poor)";
            break;
            case 3:
                msg = "(Fair)";
            break;
            case 4:
                msg = "(Good)";
            break;
            case 5:
                msg = "(Excellent)";
            break;
            default:
                msg = "";
            break;
        }
        $("rateMsg").innerHTML = msg;
    },
    starUnlight:function()
    {
        for(var i=1;i<6;i++)
        {
            eval("$('r"+i+"').src='/images/misc/star_off.gif'");
        }
        $("rateMsg").innerHTML = "";
    },
    turnOn:function(id)
    {

      // switch pointer
      var last = this.cur;
      this.cur = id;
      // fade down last
      if($(last))
      {
        //new Effect.Fade(last,{queue:'front',scope:'panelScope',limit:2});
        $(last).style.display = "none";
      }
      // fade up cur
      //new Effect.Appear(this.cur,{queue:'end',scope:'panelScope',limit:2});
      $(this.cur).style.display = "block";
    },
    turnOff:function(id)
    {
      this.cur = "";
      //new Effect.Fade(id,{queue:'end',scope:'panelScope',limit:2});
      $(id).style.display = "none";
    },
    rate:function(rating)
    {
        this.callback("rate","Rating="+rating+"&RecipeId="+$("RecipeId").value);
        // /recipes/recipe_box/rate.php?RecipeId={$iRecipeId}&Rating=3
    },
    email:function()
    {
      this.callback("email","PersonalMessage="+$("PersonalMessage").value+"&RecipeId="+$("RecipeId").value+"&RecipientName="+$("RecipientName").value+"&RecipientEmail="+$("RecipientEmail").value+"&SenderName="+$("SenderName").value+"&SenderEmail="+$("SenderEmail").value);
    },
    callback:function(type,data)
    {
      switch(type)
      {
        case "email":
        url = "/recipes/recipe_box/mail.php";
        break;
        case "rate":
        url = "/recipes/recipe_box/rate.php";
        break;
      }
      var opt = {
        method: 'post',
        //postBody: escape(data),
        postBody: data,
        onSuccess: function(t) {
          switch(type)
          {
            case "email":
              $("emailResponse").innerHTML = "Thank you! The email has been sent."; // show success
              turnOffEmail();
            break;
            case "rate":
                $("voteResponse").innerHTML = "Thank you for voting!";
                turnOffVotes();
            break;
          }
        },
        // Handle 404
        on404: function(t) {
          alert('Error 404: location "' + t.statusText + '" was not found.');
        },
        // Handle other errors
        onFailure: function(t) {
          alert('Error ' + t.status + ' -- ' + t.statusText);
        }
      }
      new Ajax.Request(url,opt);
    }
  };
  var eventDispatch = {
    yieldContainer:"yield", // id of the html element that will display the error messages
    displayErrors:function(errorList)
    {
      var finalString = "";
      for(var i=0;i<errorList.ids.length;i++)
      {
        // highlight

        // make list of errors
        if(finalString=="")
        {
          finalString = finalString.concat(errorList.messages[i]);
        }
        else
        {
          finalString = finalString.concat("<br />",errorList.messages[i]);
        }
      }
      $("yield").innerHTML = finalString;
      // maybe an effect herre instead of just a pop
      $("yield").style.display = "block";
    }
  }
});