var num_of_figure;
function OnLoad(x) {
    num_of_figure = x;
}

function SwapFigure(figure_number) {
    var temp;

  //knowledge_figure
    temp = document.getElementById("knowledge_figure_"+ figure_number +"_figure_caption").value;
    document.getElementById("knowledge_figure_"+ figure_number +"_figure_caption").value = document.getElementById("knowledge_figure_"+ (figure_number + 1) +"_figure_caption").value;
    document.getElementById("knowledge_figure_"+ (figure_number + 1) +"_figure_caption").value = temp;

  //file_name and figure_path and label and png
    split1 = document.getElementById("knowledge_figure_"+ figure_number +"_input").name.split("]");
    split2 = document.getElementById("knowledge_figure_"+ (figure_number + 1) +"_input").name.split("]");
    document.getElementById("knowledge_figure_"+ figure_number +"_input").name = split1[0] + "]" + split2[1] + "]";
    document.getElementById("knowledge_figure_"+ (figure_number + 1) +"_input").name = split2[0] + "]" + split1[1] + "]";

    temp = document.getElementById("knowledge_figure_"+ figure_number +"_input").value;
    document.getElementById("knowledge_figure_"+ figure_number +"_input").value = document.getElementById("knowledge_figure_"+ (figure_number + 1) +"_input").value;
    document.getElementById("knowledge_figure_"+ (figure_number + 1) +"_input").value = temp;

    temp = document.getElementById("knowledge_figure_"+ figure_number +"_input").type;
    document.getElementById("knowledge_figure_"+ figure_number +"_input").type = document.getElementById("knowledge_figure_"+ (figure_number + 1) +"_input").type;
    document.getElementById("knowledge_figure_"+ (figure_number + 1) +"_input").type = temp;
    
    temp = document.getElementById("label_"+ figure_number).firstChild.nodeValue;
    document.getElementById("label_"+ figure_number).firstChild.nodeValue = document.getElementById("label_"+ (figure_number + 1)).firstChild.nodeValue;
    document.getElementById("label_"+ (figure_number+1)).firstChild.nodeValue = temp;

    temp = document.getElementById("png_"+ figure_number).firstChild.nodeValue;
    document.getElementById("png_"+ figure_number).firstChild.nodeValue = document.getElementById("png_"+ (figure_number + 1)).firstChild.nodeValue;
    document.getElementById("png_"+ (figure_number+1)).firstChild.nodeValue = temp;

    temp = document.getElementById("knowledge_figure_"+ figure_number +"_figure_path").value;
    temp2 = document.getElementById("knowledge_figure_"+ (figure_number + 1) +"_figure_path").value;
    document.getElementById("knowledge_figure_"+ figure_number +"_figure_path").value = document.getElementById("knowledge_figure_"+ (figure_number + 1) +"_figure_path").value;
    document.getElementById("knowledge_figure_"+ (figure_number + 1) +"_figure_path").value = temp;

  //diagram_id
    temp = document.getElementById("knowledge_figure_"+ figure_number +"_diagram_id").value;
    document.getElementById("knowledge_figure_"+ figure_number +"_diagram_id").value = document.getElementById("knowledge_figure_"+ (figure_number + 1) +"_diagram_id").value;
    document.getElementById("knowledge_figure_"+ (figure_number + 1) +"_diagram_id").value = temp;

  //image
    temp = document.getElementById("img_"+ figure_number).src;
    document.getElementById("img_"+ figure_number).src = document.getElementById("img_"+ (figure_number + 1)).src;
    document.getElementById("img_"+ (figure_number + 1)).src = temp;

  //link to image
    temp = document.getElementById("link_to_img_"+ figure_number).href;
    document.getElementById("link_to_img_"+ figure_number).href = document.getElementById("link_to_img_"+ (figure_number + 1)).href;
    document.getElementById("link_to_img_"+ (figure_number + 1)).href = temp;

    temp = document.getElementById("link_to_img_"+ figure_number).firstChild.nodeValue;
    document.getElementById("link_to_img_"+ figure_number).firstChild.nodeValue = document.getElementById("link_to_img_"+ (figure_number + 1)).firstChild.nodeValue;
    document.getElementById("link_to_img_"+ (figure_number + 1)).firstChild.nodeValue = temp;

  //view image ...
    temp = document.getElementById("view_image_"+ figure_number).firstChild.nodeValue;
    document.getElementById("view_image_"+ figure_number).firstChild.nodeValue = document.getElementById("view_image_"+ (figure_number + 1)).firstChild.nodeValue;
    document.getElementById("view_image_"+ (figure_number+1)).firstChild.nodeValue = temp;
}

function DeleteFigure(figure_number) {
    for (var i = figure_number; i < num_of_figure; i++) {
	document.getElementById("knowledge_figure_"+ i +"_figure_caption").value = document.getElementById("knowledge_figure_"+ (i+1) +"_figure_caption").value;
	document.getElementById("knowledge_figure_"+ i +"_input").value = document.getElementById("knowledge_figure_"+ (i+1) +"_input").value;//figure_path
	//document.getElementById("knowledge_figure_"+ i +"_input").name = document.getElementById("knowledge_figure_"+ (i+1) +"_input").name;
	document.getElementById("img_"+ i).src = document.getElementById("img_"+ (i+1)).src;
	document.getElementById("link_to_img_"+ i).src = document.getElementById("link_to_img_"+ (i+1)).src;
    }
    var delete_field;
    if (delete_field = document.getElementById("figure_field_"+ num_of_figure)){
	delete_field.parentNode.removeChild(delete_field);
    }
    num_of_figure--;
    var last_down_button;
    if (last_down_button = document.getElementById("down_"+ num_of_figure)){
        // delete down.png
	var last_down_button_parent = last_down_button.parentNode;
	last_down_button_parent.removeChild(last_down_button);
	// add white.png
	var down_img = document.createElement("img");
	down_img.setAttribute("id", "down_alternative_"+ num_of_figure);
	down_img.setAttribute("alt", "Down");
	down_img.setAttribute("border", "0");
	down_img.setAttribute("src", "/images/white.bmp");
	down_img.setAttribute("height", "50");
	down_img.setAttribute("width", "50");
	last_down_button_parent.appendChild(down_img);
	}
}

function MoreFigure() {
    if (num_of_figure > 0) {
	var down_swap_alternative = document.getElementById("down_alternative_"+ num_of_figure);
	var down_swap_parent = down_swap_alternative.parentNode;
	var down_swap = document.createElement("a");
	down_swap.setAttribute("href", "#");
	down_swap.setAttribute("id", "down_"+ num_of_figure);
	if (navigator.appVersion.indexOf("MSIE") != -1) {
	    down_swap.setAttribute("onclick", new Function("SwapFigure("+ num_of_figure + "); location.href='#knowledge_figure_form'; return false;"));//for Internet Explorer
	} else {
	    down_swap.setAttribute("onclick", "SwapFigure("+ num_of_figure + "); location.href='#knowledge_figure_form'; return false;");// for Firefox
	}
	var down_img = document.createElement("img");
	down_img.setAttribute("alt", "Down");
	down_img.setAttribute("border", "0");
	down_img.setAttribute("src", "/images/down.png");
	down_img.setAttribute("height", "50");
	down_img.setAttribute("width", "50");

	down_swap_parent.removeChild(down_swap_alternative);
	down_swap_parent.appendChild(down_swap);
	down_swap.appendChild(down_img);
    }
    num_of_figure++;

    var outer_tr = document.createElement("tr");
    var outer_td = document.createElement("td");
    var fieldset = document.createElement("fieldset");
    fieldset.setAttribute("id", "figure_field_"+ num_of_figure);
    if (navigator.appVersion.indexOf("MSIE") != -1) {
	fieldset.setAttribute("className", "form_fieldset");//for Internet Explorer
    } else {
	fieldset.setAttribute("class", "form_fieldset");//for Firefox
    }
    var legend = document.createElement("legend");
    var legend_text = document.createTextNode("Figure "+ num_of_figure);
    legend.appendChild(legend_text);
    var table = document.createElement("table");
    var tbody = document.createElement("tbody");
    var tr = document.createElement("tr");
    tr.setAttribute("valign", "top");

    var td1 = document.createElement("td");
    var up_swap = document.createElement("a");
    up_swap.setAttribute("href", "#");
    up_swap.setAttribute("id", "up_"+ num_of_figure);
    if (navigator.appVersion.indexOf("MSIE") != -1) {
	up_swap.setAttribute("onclick", new Function("SwapFigure("+ (num_of_figure - 1) + "); location.href='#knowledge_figure_form'; return false;"));//for Internet Explorer
    } else {
	up_swap.setAttribute("onclick", "SwapFigure("+ (num_of_figure - 1) + "); location.href='#knowledge_figure_form'; return false;");// for Firefox
    }
    var up_img = document.createElement("img");
    up_img.setAttribute("id", "up_img_"+ num_of_figure);
    up_img.setAttribute("alt", "Up");
    up_img.setAttribute("border", "0");
    up_img.setAttribute("src", "/images/up.png");
    up_img.setAttribute("height", "50");
    up_img.setAttribute("width", "50");
    /*
    var down_swap = document.createElement("a");
    down_swap.setAttribute("href", "#");
    down_swap.setAttribute("id", "down_"+ num_of_figure);
    if (navigator.appVersion.indexOf("MSIE") != -1) {
	down_swap.setAttribute("onclick", new Function("SwapFigure("+ num_of_figure + "); location.href='#knowledge_figure_form'; return false;"));//for Internet Explorer
    } else {
	down_swap.setAttribute("onclick", "SwapFigure("+ num_of_figure + "); location.href='#knowledge_figure_form'; return false;");// for Firefox
    }
    */
    var down_img = document.createElement("img");
    down_img.setAttribute("id", "down_alternative_"+ num_of_figure);
    down_img.setAttribute("alt", "Down");
    down_img.setAttribute("border", "0");
    down_img.setAttribute("src", "/images/white.bmp");
    down_img.setAttribute("height", "50");
    down_img.setAttribute("width", "50");

    var td2 = document.createElement("td");
    var caption_label = document.createTextNode("Caption:");
    var caption = document.createElement("textarea");
    caption.setAttribute("id", "knowledge_figure_"+ num_of_figure +"_figure_caption");
    caption.setAttribute("name", "knowledge_figure["+ num_of_figure +"][figure_caption]");
    caption.setAttribute("cols", "50");
    caption.setAttribute("rows", "4");
    var label = document.createElement("div");
    label.setAttribute("id", "label_"+ num_of_figure);
    var label_text = document.createTextNode("Figure Path:");
    var figure_path = document.createElement("input");
    figure_path.setAttribute("type", "text");
    if (navigator.appVersion.indexOf("MSIE") != -1) {
	figure_path.setAttribute("className", "textarea_size60");//for Internet Explorer
    } else {
	figure_path.setAttribute("class", "textarea_size60");//for Firefox
    }
    figure_path.setAttribute("id", "knowledge_figure_"+ num_of_figure +"_input");
    figure_path.setAttribute("name", "knowledge_figure["+ num_of_figure +"][figure_path]");
    figure_path.setAttribute("value", "");
    var png_span = document.createElement("span");
    png_span.setAttribute("id", "png_"+ num_of_figure);
    var png = document.createTextNode(".png");
    var diagram_id = document.createElement("input");
    diagram_id.setAttribute("id", "knowledge_figure_"+ num_of_figure +"_diagram_id");
    diagram_id.setAttribute("name", "knowledge_figure[<%= i %>][diagram_id]");
    diagram_id.setAttribute("type", "hidden");
    diagram_id.setAttribute("value", "");
    var diagram_path = document.createElement("input");
    diagram_path.setAttribute("id", "knowledge_figure_"+ num_of_figure +"_figure_path");
    diagram_path.setAttribute("name", "knowledge_figure[<%= i %>][diagram_path]");
    diagram_path.setAttribute("type", "hidden");
    diagram_path.setAttribute("value", "");

    var td3 = document.createElement("td");
    var figure_img = document.createElement("img");
    figure_img.setAttribute("src", "/images/white.bmp");
    figure_img.setAttribute("id", "img_"+ num_of_figure);
    figure_img.setAttribute("name", "img_"+ num_of_figure);
    figure_img.setAttribute("width", "150");
    figure_img.setAttribute("height", "150");
    var link_to_img = document.createElement("a");
    link_to_img.setAttribute("href", "'#knowledge_figure_form'");
    link_to_img.setAttribute("id", "link_to_img_"+ num_of_figure);
    link_to_img.setAttribute("name", "link_to_img_"+ num_of_figure);
    var view_image = document.createElement("span");
    view_image.setAttribute("id", "view_image_"+ num_of_figure);
    var space = document.createTextNode("");
    var td4 = document.createElement("td");
    var delete_button = document.createElement("a");
    delete_button.setAttribute("href", "#");
    delete_button.setAttribute("id", "delete_figure_"+ num_of_figure);
    if (navigator.appVersion.indexOf("MSIE") != -1) {
	delete_button.setAttribute("onclick", new Function("DeleteFigure("+ num_of_figure +");location.href='#knowledge_figure_form';return false;"));//for Internet Explorer
    } else {
	delete_button.setAttribute("onclick", "DeleteFigure("+ num_of_figure +");location.href='#knowledge_figure_form';return false;");//for Firefox
    }
    var delete_img = document.createElement("img");
    delete_img.setAttribute("alt", "X");
    delete_img.setAttribute("border", "0");
    delete_img.setAttribute("src", "/images/x.gif");

    var knowledge_figure_tbody = document.getElementById("knowledge_figure_tbody");
    knowledge_figure_tbody.appendChild(outer_tr);
    outer_tr.appendChild(outer_td);
    outer_td.appendChild(fieldset);
    fieldset.appendChild(legend);
    fieldset.appendChild(table);
    table.appendChild(tbody);
    tbody.appendChild(tr);
    tr.appendChild(td1);
    td1.appendChild(document.createElement("br"));
    td1.appendChild(up_swap);
    up_swap.appendChild(up_img);
    td1.appendChild(document.createElement("br"));
    td1.appendChild(document.createElement("br"));
    td1.appendChild(down_img);
    tr.appendChild(td2);
    td2.appendChild(caption_label);
    td2.appendChild(document.createElement("br"));
    td2.appendChild(caption);
    td2.appendChild(document.createElement("br"));
    td2.appendChild(label)
    label.appendChild(label_text);
    td2.appendChild(figure_path);
    td2.appendChild(png_span);
    png_span.appendChild(png);
    td2.appendChild(diagram_id);
    td2.appendChild(diagram_path);
    td2.appendChild(document.createElement("br"));
    tr.appendChild(td3);
    td3.appendChild(figure_img);
    td3.appendChild(document.createElement("br"));
    td3.appendChild(link_to_img);
    link_to_img.appendChild(view_image);
    view_image.appendChild(space);
    tr.appendChild(td4);
    td4.appendChild(delete_button);
    delete_button.appendChild(delete_img);

    var first_up_button;
    if (first_up_button = document.getElementById("up_1")){
	first_up_button.parentNode.removeChild(first_up_button);
    }
}

