/*
 *   Class: FileUpload
 *   Dependencies: s1JS.Jobs, jQuery
 */

s1JS.Jobs.FileUpload = Base.extend({
    constructor: function(options)
    {
        this.options = options;
        $('#' + this.options.form).bind("submit", {this_f: this}, this.submitHandler);
    },
    submitHandler: function(e)
    {
        var this_f = e.data.this_f;    
        
        if(this_f.options.validator)
        {
            validator = this_f.options.validator;
            validator.validate();
            if(validator.hasErrors())
            {
                validator.applyErrors();
                return false;
            }
        }
    
        if(this_f.options.loader)
        {
            $('#' + this_f.options.loader).show();
        }
        var $form = $(this);
        var id = ("target" + (new Date()).getTime());
        var $frame = $( "<iframe name=\"" + id + "\" src=\"about:blank\" />" ).css("display", "none");               

        $frame.load(function(e) {
            var response = window.frames[id ].document.getElementsByTagName( "body" )[0];	    
            var data;

            // Load fires prematurely in Opera and Webkit
            if(!response || !response.innerHTML || response.innerHTML == 0) { return false; }

            var responseText = response.innerHTML;            
            responseText = responseText.replace(/[\f\n\r\t]/g, ' '); // Firefox 1.0.x fix
            // strip all HTML.. most of it has been added by browser plugins and it breaks the JSON            
            responseText = responseText.replace(/<[^>]+>/ig, "");

            try 
            {                
                data = eval('(' + responseText + ')');
            } 
            catch(e)
            {
                if(responseText.match('application has been sent'))
                {
                    data = {template_file: '/apply/apply-submission-successful.tmpl'};
                    if(s1JS.Jobs.loggedIn())
                    {
                        data.status_not_s1_member = 1;
                    }
                    else
                    {
                        data.status_member_everything_fine = 1;
                    }
                }
                else if(responseText.match('already applied for this job'))
                {
                    data = {template_file: '/apply/apply-followup-form.tmpl'};
                }
                else
                {           
                    var message = responseText;
                    var vacancy_id = page_data.vacancy_id;
                    var email = document.getElementById('apply-form')['email'].value;
                    var member = '';
                    var user_agent = navigator.userAgent;
                    var browser = navigator.appName + '(' + navigator.appCodeName + ')';
                    var operating_system = navigator.appVersion;
                    
                    if(s1JS.Jobs.loggedIn())
                    {
                        member = 'Yes';
                    }
                
                    $.post('/apply/catch-errors.cgi', {error: message, vacancy_id: vacancy_id, email: email, logged_in: member, user_agent: user_agent, browser: browser, os: operating_system});            
              
                    $('<div class="flora"><p>Sorry, we have encountered a problem confirming your application.  It\'s likely that your application has been sent successfully, but we have been unable to confirm this.</p><p>If you have an account you can confirm that applications have been sent in the <a href="/myaccount/jobs-applied/">Jobs Applied For</a> section.  Alternatively, you can choose the &quot;Email me a copy...&quot; option on future applications.</p><p>If this problem persists, please <a href="mailto:servicedesk@s1jobs.com">contact customer services</a> and quote the error <strong>ASJS01</strong></div>')
                        .appendTo('#content')
                                .dialog({modal:true, width:400, title:"Application Confirmation Error",  overlay: { opacity: 0.5, background: 'black' }, open: s1JS.Jobs.fastAds.hideAds, close: s1JS.Jobs.fastAds.showAds});
                }
            }
            this_f.options.callback(data);     
            
            if(this_f.options.loader)
            {
                $('#' + this_f.options.loader).hide();
            }       
            // $frame.remove();            
        });       

        // Attach to body.
        $("body:first").append( $frame );
        $form.attr( "target", id );
        
    }
});

