/*
 * makeSublist - jQuery controlled Dependent (or Cascading) Select List
 * Copyright (c) 2007 Anis uddin Ahmad
 * http://www.ajaxray.com/blog/2007/11/08/jquery-controlled-dependent-or-cascading-select-list-2/
 * Modified by Samuel Jolibois for max-the-elephant.com
 */
function makeSublist(parent,child,isSubselectOptional,childVal)
{
  $("body").append("<select style='display:none' id='"+parent+child+"'></select>");
  $('#'+parent+child).html($("#"+child+" option"));

    var parentValue = $('#'+parent).attr('value');
    $('#'+child).html($("#"+parent+child+" .sub_"+parentValue).clone());

  childVal = (typeof childVal == "undefined")? "" : childVal ;
  $("#"+child+' option[@value="'+ childVal +'"]').attr('selected','selected');

  $('#'+parent).change(
    function()
    {
      var parentValue = $('#'+parent).attr('value');
      $('#'+child).html($("#"+parent+child+" .sub_"+parentValue).clone());
      if(isSubselectOptional) $('#'+child).prepend("<option value='none'> -- Select -- </option>");
      $('#'+child).trigger("change");
                        $('#'+child).focus();
    }
  );
}


/*
* Really Simple Live Comment Preview
* Copyright (c) Nov 19, 2006 Karl Swedberg
* http://www.learningjquery.com/2006/11/really-simple-live-comment-preview
* Modified by Samuel Jolibois for max-the-elephant.com
*/

function selectInvitationMsg() {
$('#contact_email').one('submit',function() {
$('#contact_email').parent().after('<input type="hidden" name="contact_message" id="contact_message" value="" />');
});
$('#guest_email').one('submit',function() {
$('#guest_email').parent().after('<input type="hidden" name="guest_message" id="guest_message" value="" />');
});
var $invitation = '';
  $('#invitation').keyup(function() {
  $invitation = $(this).val();
  // For html view only
  //$invitation = $invitation.replace(/\n/g, "<br />").replace(/\n\n+/g, '<br /><br />').replace(/(<\/?)script/g,"$1noscript");
  $invitation = $invitation.replace(/(<\/?)script/g,"$1noscript");
  $('#contact_message').attr("value", $invitation );
  $('#guest_message').attr("value", $invitation );
  });
}


/*
* selectSenderEmail()
* Copyright (c) Dec 2007 Samuel Jolibois
* http://www.max-the-elephant.com
*/

function selectSenderEmail() {
var $email = '';
  $('#user_email').change(function() {
  $email = $(this).val();
  $('#contact_sender_email').attr("value", $email );
  $('#guest_sender_email').attr("value", $email );
  });
}


  $(document).ready(function()
  {
    selectSenderEmail();
    makeSublist('contact_id','contact_email', false, '1');
    selectInvitationMsg();
    //makeSublist('child','grandsun', true, '');
    //makeSublist('parent','child', false, '1');
  });
