/**
 * @author alec.hill
 * 
 * allows the content of the tests results to be extracted and added to the form
 */

// override the standard postResults of unittest.js as this is called when finished
// so instead of automatically submitting results , it will display the form and add the results and any failure info to that
Test.Unit.Runner.prototype.postResults = function(){
    
    // passed will be true until proven otherwise
    var passed = true;
    
    // get elements
    var resultsNotification = $('resultsNotification');
    var resultsForm = $('resultsForm');
    var passedField = $('passedField');
    var results = $('results');
    var failures = $('failures');
    var logLines = $$('#loglines tr');
    
    // loop through the loglines
    logLines.each( function(line){
        // if it is a failure or has errors
        if( line.hasClassName('failed') || line.hasClassName('error') ){
            // get whether an error or a failure
            var failureType = line.hasClassName('failed') ? 'Failure' : 'Error';
            // set passed to false
            passed = false;
            // set passedField to no
            
            var tds = line.immediateDescendants();
            // get the name of the test that has gone wrong
            var testName = tds[1].firstChild.nodeValue;
            //get the name of the test that has gone wrong
            var assertions = tds[2].firstChild.nodeValue; 
            
            // get the brs, a reason will be after each of these
            var brs = tds[2].getElementsBySelector('br');
            var reasons = '';
            // get each reason and add to the reasons string
            var i = 1;
            brs.each( function(br){
                reasons += '[' + i + '] - ' + br.nextSibling.nodeValue + '  -----  ';
                i++;
            });
             
            // now whack this all into the form
            var input = document.createElement('input');
            input.name = testName;
            input.type = 'hidden';
            input.value = failureType + '  -----  assertions ----- ' + reasons;
            failures.appendChild(input);
        }
    });  
    
    // add a message to the notification element
    var message = "All of the tests have passed, wooo! If your browser was not on the list of those known to be supported, then please submit the results...";
    if(!passed) message = "Not all of the tests have passed, booo! Please submit the form below to send these results...";	
	resultsNotification.appendChild(document.createTextNode(message));
    
	// set passed field if failed
	if(!passed) passedField.value = 'No';
	
    //show the notification and the form
    results.removeClassName('hidden');
    
    // if the test passed then LiveValidation will work, so add it to the form
    if(passed) window.setupFormValidation();
}

// this will setup the form to be validated with LiveValidation
setupFormValidation = function(){
    // make live validation fields
    var osField = new LiveValidation('osField', { wait: 500 });
    osField.add(Validate.Presence);
    var browserField = new LiveValidation('browserField', { wait: 500 });
    browserField.add(Validate.Presence);
}

Site.getInstance().addLoadEvent(runTests);
