//window.onload = initialise; //won't work with ()

$(document).ready(function () {
    if (document.getElementById('copyrightform') != null) {
        initialise(); //works fine with ()
    }
})

    function initialise() {
        // Ensure we're working with a relatively standards compliant user agent
        if (!document.getElementById || !document.createElement || !document.createTextNode)
            return;

        // Add an event handler for the number form
        var objForm = document.getElementById('copyrightform');
        if (objForm != null) { 
                objForm.onsubmit = function() { return validateForm(objForm);
            }
        };
    }

    function validateForm(objForm) {
        // Test whether fields are valid
        var bBusiness = document.getElementById('txtBusiness').value;        
        var bAddress = document.getElementById('txtAddress').value;
        var bContactName = document.getElementById('txtContactName').value;
        var bTelephone = document.getElementById('txtTelephone').value;
        var bEmailAddress = document.getElementById('txtEmailAddress').value;
        var bTitle = document.getElementById('txtTitle').value;
        var bAuthor = document.getElementById('txtAuthor').value;
        var bPublicationYr = document.getElementById('txtPublicationYr').value;
        var bISBN = document.getElementById('txtISBN').value;
        var bURL = document.getElementById('txtURL').value;
        var bPageNo = document.getElementById('txtPageNo').value;        
        var bDesc = document.getElementById('txtDesc').value;
        var bIntendedPurpose = document.getElementById('txtIntendedPurpose').value;

        // If not valid, display errors
        if ((bBusiness.length == 0) || (bAddress.length == 0) || (bContactName.length == 0) || (bTelephone.length == 0) || (bEmailAddress.length == 0) || (bTitle.length == 0) || (bAuthor.length == 0) || (bPublicationYr.length == 0) || (bISBN.length == 0) || (bURL.length == 0) || (bPageNo.length == 0) || (bDesc.length == 0) || (bIntendedPurpose.length == 0)) {
            var objExisting = document.getElementById('validationerrors');
            var objNew = document.createElement('div');
            var objTitle = document.createElement('h2');
            var objParagraph = document.createElement('p');
            var objList = document.createElement('ol');
            var objAnchor = document.createElement('a');
            var strID = 'firsterror';
            var strError;

            // The heading element will contain a link so that screen readers
            // can use it to place focus - the destination for the link is 
            // the first error contained in a list
            objAnchor.appendChild(document.createTextNode('Errors in Submission'));
            objAnchor.setAttribute('href', '#firsterror');
            objTitle.appendChild(objAnchor);
            objParagraph.appendChild(document.createTextNode('Please review the following'));
            objNew.setAttribute('id', 'validationerrors');
            objNew.appendChild(objTitle);
            objNew.appendChild(objParagraph);

            // Add each error found to the list of errors
            if (bBusiness.length == 0) {
                strError = 'Please provide your Business/Organisation';
                objList.appendChild(addError(strError, '#txtBusiness', objForm, strID));
                strID = '';
            }         
        
            if (bAddress.length == 0) {
                strError = 'Please provide your Address';
                objList.appendChild(addError(strError, '#txtAddress', objForm, strID));
                strID = '';
            }

            if (bContactName.length == 0) {
                strError = 'Please provide your Contact Name';
                objList.appendChild(addError(strError, '#txtContactName', objForm, strID));
                strID = '';
            }

            if (bTelephone.length == 0) {
                strError = 'Please provide your Phone Number';
                objList.appendChild(addError(strError, '#txtTelephone', objForm, strID));
                strID = '';
            }

            if (bEmailAddress.length == 0) {
                strError = 'Please provide your Email Address';
                objList.appendChild(addError(strError, '#txtEmailAddress', objForm, strID));
                strID = '';
            }

            if (bTitle.length == 0) {
                strError = 'Please provide your Title';
                objList.appendChild(addError(strError, '#txtTitle', objForm, strID));
                strID = '';
            }
            
            if (bAuthor.length == 0) {
                strError = 'Please provide an Author';
                objList.appendChild(addError(strError, '#txtAuthor', objForm, strID));
                strID = '';
            }            

            if (bPublicationYr.length == 0) {
                strError = 'Please provide a Year of Publication';
                objList.appendChild(addError(strError, '#txtPublicationYr', objForm, strID));
                strID = '';
            }
            
            if (bISBN.length == 0) {
                strError = 'Please provide an ISBN, ISSN or Cat No';
                objList.appendChild(addError(strError, '#txtISBN', objForm, strID));
                strID = '';
            }
            
            if (bURL.length == 0) {
                strError = 'Please provide a Complete URL';
                objList.appendChild(addError(strError, '#txtURL', objForm, strID));
                strID = '';
            }
            
            if (bPageNo.length == 0) {
                strError = 'Please provide Page Numbers of relevant material';
                objList.appendChild(addError(strError, '#txtPageNo', objForm, strID));
                strID = '';
            }                                       

            if (bDesc.length == 0) {
                strError = 'Please provide a Description of graphs, tables, photos or drawings to be used';
                objList.appendChild(addError(strError, '#txtDesc', objForm, strID));
                strID = '';
            }  

            if (bIntendedPurpose.length == 0) {
                strError = 'Please provide an Intended purpose of reproduction';
                objList.appendChild(addError(strError, '#txtIntendedPurpose', objForm, strID));
                strID = '';
            }             
                                                                   
            // Add the list to the error information
            objNew.appendChild(objList);

            // If there were existing errors, replace them with the new lot,
            // otherwise add the new errors to the start of the form
            if (objExisting)
                objExisting.parentNode.replaceChild(objNew, objExisting);
            else {
                var objPosition = objForm.firstChild;
                objForm.insertBefore(objNew, objPosition);
            }

            // Place focus on the anchor in the heading to alert
            // screen readers that the submission is in error
            objAnchor.focus();

            // Do not submit the form
            objForm.submitAllowed = false;

            return false;
        }
        return true;
    }

    // Function to create a list item containing a link describing the error
    // that points to the appropriate form field
    function addError(strError, strFragment, objForm, strID) {
        var objAnchor = document.createElement('a');
        var objListItem = document.createElement('li');

        objAnchor.appendChild(document.createTextNode(strError));
        objAnchor.setAttribute('href', strFragment);
        objAnchor.setAttribute('title', strError);
        objAnchor.onclick = function(event) { return focusFormField(this, event, objForm); };
        objAnchor.onkeypress = function(event) { return focusFormField(this, event, objForm); };

        // If strID has a value, this is the first error in the list
        if (strID.length > 0)
            objAnchor.setAttribute('id', strID);
        objListItem.appendChild(objAnchor);

        return objListItem;
    }

    // Function to place focus to the form field in error
    function focusFormField(objAnchor, objEvent, objForm) {
        // Allow keyboard navigation over links
        if (objEvent && objEvent.type == 'keypress')
            if (objEvent.keyCode != 13 && objEvent.keyCode != 32)
            return true;

        // set focus to the form control
        var strFormField = objAnchor.href.match(/[^#]\w*$/);
        
        objForm[strFormField].focus();
        return false;
    }    
