﻿// This wonderful script from here:
// http://gavin.panicus.org/downloads/gmail_file_attachment.html

// Gmail File Attachment Clone
// (c) 2005, Gavin Lynch

//number of forms currently in < span id="content" > tree
var form_count = 0;

// max number of allowed forms
var max_form_count = 0;

//add file attachment form and associated elements
function AddRow()
{
    // IAS: create new < div > element
    var new_div = document.createElement('div');
    new_div.setAttribute('class','child_attach_row');
    new_div.setAttribute('id','child_attachment_row_' + form_count);
    
	//create new < input > element
	var new_attachment = document.createElement('input');
	new_attachment.setAttribute('id', 'child_attachment_' + form_count);
	new_attachment.setAttribute('name', 'child_attachment_' + form_count);
	new_attachment.setAttribute('type', 'file');
	new_attachment.setAttribute('class', 'child_attach_input');
	new_attachment.setAttribute('size', '48');
	
    new_div.appendChild(new_attachment);

    // Cannot remove item 0
    if( form_count > 0 ){
	    //create new < span > element
	    var new_text = document.createElement('span');
	    new_text.setAttribute('id','child_attachment_text_' + form_count);
	    new_text.innerHTML = '&nbsp;<span class="remove" onclick="RemoveRow(' + form_count + ');">Remove</span>';
        new_div.appendChild(new_text);
    }
    
	document.getElementById('pagecontent').appendChild(new_div);
   
	//increase the form count
	form_count++;

    check_footer();
 } 

//remove file attachment form and associated elements
function RemoveRow(remove_form_num) {

	//decrease the form count
	form_count--;

	if($('child_attachment_row_'+ remove_form_num) ){
	    //remove < input > element attachment
	    if( $('child_attachment_' + remove_form_num) )
	        $('child_attachment_row_'+ remove_form_num).removeChild($('child_attachment_' + remove_form_num));
	        
	    //remove < span > element text
	    if(document.getElementById('child_attachment_text_' + remove_form_num))
	        document.getElementById('child_attachment_row_'+ remove_form_num).removeChild(document.getElementById('child_attachment_text_' + remove_form_num));

	    //remove < img > element image
	    if(document.getElementById('child_attachment_img_' + remove_form_num))
	        document.getElementById('child_attachment_row_'+ remove_form_num).removeChild(document.getElementById('child_attachment_img_' + remove_form_num));

	    //remove < div > element attachment
	    document.getElementById('pagecontent').removeChild(document.getElementById('child_attachment_row_' + remove_form_num));
    }

    check_footer();

	return false;
}

// A document type has been selected. 
// Check how many pages are allowed.
function documentTypeSelected(){
    var typeselector = document.getElementById('documentType');
    var sel_id = GetSelectorId();
        
    if ( sel_id < 1 ) {
        max_form_count = 0;
    } else if( sel_id == 2 || sel_id == 5 || sel_id == 6 || sel_id == 35 ) {
        max_form_count = 1;
    } else if( sel_id == 1 || sel_id == 7 || sel_id == 8 || sel_id == 11 || sel_id == 12 || sel_id == 15 ) {
        max_form_count = 3;
    } else if( sel_id == 4 ) {
        max_form_count = 4;
    } else if( sel_id == 13 || sel_id == 14 ) {
        max_form_count = 8;
    } else {
        max_form_count = 2;
    }
    
	if( $('photolink') ) {
		$('photolink').hide();
		$('portraitlink').hide();
		$('vidlink').hide();
		$('doclink').hide();
	}

	var showWhat = '';

    if ( sel_id == 5 || sel_id == 6 || sel_id == 0 ) {
        $('scanning').hide();
        $('video').hide();
        $('fileType').innerHTML = "Image";
        
        if( sel_id == 5 ) {
			showWhat = 'portraitlink';
        } else {
			if( sel_id == 6 ) {
				showWhat = 'photolink';
		    }        
        }
    } else if ( sel_id == 35 ) {
        $('scanning').hide();
        $('video').show();
        $('fileType').innerHTML = "Video";
        showWhat = 'vidlink';
    } else {
        $('scanning').show();
        $('video').hide();
        $('fileType').innerHTML = "File";
        showWhat = 'doclink';
    }
    
    if( $(showWhat) ){
		$(showWhat).show();
    }
    
    var i = 0;
    var editMode = ($('Upload').value == "Update");
    
    // Remove all but the allowed number of extra rows!
    if( form_count > max_form_count ){
        for(i = form_count; i > max_form_count; i--){
            RemoveRow( i-1 );
        }
    } else {
        if (editMode) {
            //form_count = $$("div.existing_page").length;
            form_count = max_form_count;
        } else if( form_count == 0 ) {
            // usability; let's always have one ready for the user
            AddRow();
        }
    }
    
    // Oh dear. Change the label against the textarea depending upon what type of document is selected
    if( sel_id == 7 || sel_id == 8 || sel_id == 9 || sel_id == 10 || sel_id == 11 || sel_id == 12 || sel_id == 17 || sel_id == 18 || sel_id == 34 ) {
        $('refereename_row').show();
        $('refereeName').focus();
        $('notes_row').hide();
    } else {
        $('refereename_row').hide();
        // portrait photo
        if( sel_id != 5 && sel_id != 0 ) {
		    $('notes_row').show();
	        $('summary').focus();
        } else {
			$('notes_row').hide();
		}
    }
	if( max_form_count > 0 ) {
		$('image_row').show();
	} else { 
		$('notes_row').hide();
        $('image_row').hide();
	}
	
	// JVDM: For mandatory notes, add the star, else remove it
    if ( sel_id == 6 || sel_id == 16 || sel_id == 30 || sel_id == 31 || sel_id == 35 ) {
        $$('#notes_row td.labelcol').each(function(element) { element.addClassName("mand"); });
	} else {
        $$('#notes_row td.labelcol').each(function(element) { element.removeClassName("mand"); });
	}
	
	check_footer();
}

function check_footer(){
    //if an attachment has been added, change text to "Attach another file"
    if( form_count < max_form_count ) {
	    //if all forms are removed, change text back 
	    if (form_count == 0) {
   	  	    $('more').innerHTML = 'Add a page to this document';
	    }else{
	        $('more').innerHTML = 'Add another page to this document';
	    }
	} else {
	    $('more').innerHTML = '';
	}
}

function ValidateUploadForm(){
	if( $F('documentType') == 0 ) { $('documentType').focus(); alert('Please select the item type that best matches what you are uploading.'); return false;
	} else {
		if( $('refereename_row').visible() ) { 
			if( $F('refereeName').length == 0 ){ $('refereeName').focus(); alert('Please enter the name of the referee who wrote this reference.'); return false; }
		} else {
			if( $('notes_row').visible() && $F('summary').length == 0 ) {
			    // JVDM: Only certain document types have the description mandatory
                var selId = GetSelectorId();
                if ( selId == 6 || selId == 16 || selId == 30 || selId == 31 || selId == 35 ) {
			        $('summary').focus(); 
			        alert('Please enter some notes regarding this upload.'); 
			        return false; 
			    }
			}
		}
		var ct = 0;
		while( ct < form_count ) {
			if( $F('child_attachment_' + ct).length == 0 ){ $('child_attachment_' + ct).focus(); alert('Please select which file you are going to upload.'); return false; }
			ct++;
		}
	}
	return true;
}

function GetSelectorId() {

    var result = 0;
    var typeSelector = document.getElementById('documentType');

    if (typeSelector == null) {
        typeSelector = document.getElementById('documenttypeid');
        result = typeSelector.value;
    } else {
        result = typeSelector.options[typeSelector.selectedIndex].value;
    }

    return result;
}

// Disbales Add Document button.
// #3577 (JK: 02/12/2011)
function checkEnableSubmit()
{
    var buttonselector = $('Proceed');
    if (buttonselector != null)
    {
        buttonselector.disabled = true;
        return;
    }
}


