/* 
 * This file contains functions used by the Image Editor 
 */

//this function helps to show the form for getting a message
function mailer_showEmailUsForm(input)
{
    var delim =',';
    var div_email_us_box_id=getTokenAt(input,0,delim,2);
    var btn_email_us_box_id=getTokenAt(input,1,delim,2);

       var action_file_path = "mailer/index.php";
       var actioncmd = "?show_message_box_request=1&&"+
       "div_email_us_box_id="+div_email_us_box_id;

       var url = action_file_path+actioncmd;
       url=url+"&sid="+Math.random();

       $(function()
       {
		// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
		$( "#dialog:ui-dialog" ).dialog( "destroy" );

		$( "#"+div_email_us_box_id ).dialog({
			autoOpen: false,
			height: 475,
			width: 400,
			modal: true,
                        draggable: false,
                        resizable:false,
                        show:"fade"
		});

		$( "#"+btn_email_us_box_id )
                .click(function(e)
                {
                        e.preventDefault();
                        $( "#"+div_email_us_box_id ).dialog( "open" );

                      $.ajax({
                                method: "get",url:url,data: "page",
                                beforeSend: function()
                                {
                                  showSpinner(div_email_us_box_id);
                                },
                                complete: function()
                                {},
                                success: function(html)
                                {
                                  $("#"+div_email_us_box_id).html(html);
                                }
                     }); //close $.ajax()
                });
	});
}//mailer_showEmailUsForm()

//this function validates the message box email
///this function also helps in processing of captcha code and processes the data
function mailer_validateMessageBoxEmail(input)
{
     var delim =',';
     var missing_fields = "";
     var txt_name_id =getTokenAt(input,0,delim,7);
     var txt_email_id=getTokenAt(input,1,delim,7);
     var txt_subject_id=getTokenAt(input,2,delim,7);
     var txt_captcha_code_id=getTokenAt(input,3,delim,7);
     var text_editor_id=getTokenAt(input,4,delim,7);
     var text_editor_default_value=getTokenAt(input,5,delim,7);
     var div_email_us_box_id=getTokenAt(input,6,delim,7);

     //validation
    var msg_sender_name = $("#"+txt_name_id).val();
    if(($('#'+txt_name_id).hasClass('ui-state-error'))||(!msg_sender_name))
      {
          $("#"+txt_name_id).addClass( "ui-state-error" );
            missing_fields +=" Name";
      }

    var msg_sender_email = $("#"+txt_email_id).val();
    if(($('#'+txt_email_id).hasClass('ui-state-error'))||(!msg_sender_email))
      {
          $("#"+txt_email_id).addClass( "ui-state-error" );
            missing_fields += ", Email";
      }

    var msg_sender_subject = $("#"+txt_subject_id).val();
    if(($('#'+txt_subject_id).hasClass('ui-state-error'))||(!msg_sender_subject))
      {
          $("#"+txt_subject_id).addClass( "ui-state-error" );
            missing_fields += ", Subject";
      }

    var captcha_code_id = $("#"+txt_captcha_code_id).val();
    if(($('#'+txt_captcha_code_id).hasClass('ui-state-error'))||(!captcha_code_id))
      {
          $("#"+txt_captcha_code_id).addClass( "ui-state-error" );
            missing_fields += ", The Security Code";
      }

       //message
        var txt_editor_content =$("#"+text_editor_id).html();        
        // clear the default value if is in requirements
        // cut off <p> and </p> to comply with XHTML strict
        // these can't be part of the default_value
        var slen = txt_editor_content.length;
        txt_editor_content = txt_editor_content.substring(3,slen-4);
         if((text_editor_default_value==txt_editor_content)||(!txt_editor_content))
        {
            missing_fields += ", Your Message";
            $("#"+text_editor_id).html(text_editor_default_value);
        }

        if(missing_fields!="")
            {
                alert("Please provide "+missing_fields);
                return;
            }

           var action_file_path = "mailer/index.php";
           var actioncmd = "?message_box_submit=1&&"+
           "message_box_name="+msg_sender_name+"&&"+
           "message_box_email="+msg_sender_email+"&&"+
           "message_box_subject="+msg_sender_subject;

           var url = action_file_path+actioncmd;
           url=url+"&sid="+Math.random();

           $.post(url,
           {
               txt_editor_content: txt_editor_content,
               msg_sender_name: msg_sender_name,
               msg_sender_email: msg_sender_email,
               msg_sender_subject: msg_sender_subject
           },
           function()
           {
             alert("Thanks, We have received your Message");
             closeDialog(div_email_us_box_id);
           });

    
   }//validate_validateCarCaptchaCode()


