function getEditForm(){
    //gets the object representing the form that contains the textarea for editing.
    var forms = document.getElementById("wikiedit").getElementsByTagName("form");
    if(forms.length != 1){
        alert("form not found or too many")
        return null;
    }
    
    return forms[0];
}

function amazonButton() {
    if (!(document.getElementById && document.getElementsByTagName)) 
        //doing some ObjectDetection
        return;
 
    var editDiv = document.getElementById("wikiedit");
    if(!editDiv)
        return;                 //not editing a wikipage? -> return! and don't do anything.
    
    var form = getEditForm();   //get the edit-form
    if(!form)
        return;
        
    var input = document.createElement('input');    //creating the input-element
    z = document.createAttribute('value');          //adding a value-attribute
    z.value = 'Link Amazon';
    input.setAttributeNode(z)

    z = document.createAttribute('type');           //adding a type-attribute
    z.value = 'submit';
    input.setAttributeNode(z)

    z = document.createAttribute('id');             //adding an id-attribute (mainly for styling via CSS)
    z.value = 'amazonButton';
    input.setAttributeNode(z)
    

    input.onclick = function(){                     //an anonymous function providing the main functionality
        if (document.selection) {
            form = getEditForm();
            form.focus();
            sel = document.selection.createRange();
            isbn = prompt('ASIN of media?', '');
            if(isbn)
                sel.text = '[[http://www.amazon.de/exec/obidos/ASIN/'+isbn+'/gungfude-21 ]] by ';
            form.focus();
        }
        
        return false; //return false so that the button-click does not reload the page.
    }
    
    form.appendChild(input);
}

addLoadEvent(amazonButton);
