function returnForm(id) {
    var element = document.getElementById(id);
    element.mode.value = "return";
    element.submit();
}

function clearFormAll() {
    for (var i=0; i<document.forms.length; ++i) {
        clearForm(document.forms[i]);
    }
}
function clearForm(form) {
    for(var i=0; i<form.elements.length; ++i) {
        clearElement(form.elements[i]);
    }
}

function clearElement(element) {
    switch(element.type) {
        case "hidden":
        case "submit":
        case "reset":
        case "button":
        case "image":
            return;
        case "file":
            return;
        case "text":
        case "password":
        case "textarea":
            element.value = "";
            return;
        case "checkbox":
        case "radio":
            element.checked = false;
            return;
        case "select-one":
        case "select-multiple":
            element.selectedIndex = 0;
            return;
        default:
    }
}



function pasteWithQuotation(fromId, toId) {
    var toElement = document.getElementById(toId);
    toElement.value = ""; //clear
    toElement.value = quote(getTextDataByElementId(fromId));
}

function quote(message) {
	var LF = String.fromCharCode(10);
	var lineData = message.split(LF);
	var result = "";
	for (var i=0; i<lineData.length; i++) {
		result += '> ' + lineData[i] + LF;
	}
	return result;
}

function getTextDataByElementId(id) {
	var element = document.getElementById(id);
    var result = "";
    for(var i = 0; i < element.childNodes.length; ++i) {
    	if (element.childNodes[i].nodeType == 3) {
    		result += element.childNodes[i].data;
    	}
    }
    return result;
}

function htmlspecialchars(ch) {
    ch = ch.replace(/&/g,"&amp;");
    ch = ch.replace(/"/g,"&quot;");
    ch = ch.replace(/'/g,"&#039;");
    ch = ch.replace(/</g,"&lt;");
    ch = ch.replace(/>/g,"&gt;");
    return ch;
}

function convertImage(maxWidth, maxHeight, image) {
	var width = image.width;
	var height = image.height;
    if(width <= maxWidth && height <= maxHeight) {
        return image;
    }
    result_width = width;
    result_height = height;

    if(width < height) {
    	result_width = (maxHeight / height) * width;
    } else {
    	result_height = (maxWidth / width) * height;
    }
    if(result_width >= maxWidth) {
    	result_width = maxWidth;
    }
    if(result_height >= maxHeight) {
    	result_height = maxHeight;
    }
    image.width = result_width;
    image.height = result_height;
    
    return image;
}

