/*
* 指定されたURLにサブミットする
*
* @param arguments[0] form（必須）
* @param arguments[1] サブミット先URL（必須）
* @param arguments[2] オープン先ウィンドウ（オプション）
* @param arguments[3] オープン元ウィンドウに付ける名前（オプション）
*/
function subProc() {
    arguments[0].action = arguments[1];
    if (2 < arguments.length) {
        arguments[0].target = arguments[2];
    }
    if (3 < arguments.length) {
        window.name = arguments[3];
    }
    arguments[0].submit();
}

/*
* アクションクラスを書き換える
*
* @param arguments[0] form（必須）
* @param arguments[1] サブミット先URL（必須）
*/
function changeAction() {
    arguments[0].action = arguments[1];
}

/*
* 指定したチェックボックスを切り替えます。
*
* @param argument[0] form (必須)
* @param argument[1] チェックボックス名 (必須)
* @param argument[2] チェックボックスの状態 true/false
*/
function switchCheckBox(form, checkBoxElement, flg){
    if(!form.elements[checkBoxElement]){
        return;
    }
    cnt = form.elements[checkBoxElement].length
    if(cnt){
        for(i=0; i<cnt; i++){
            if(flg
            && !form.elements[checkBoxElement][i].checked){
                form.elements[checkBoxElement][i].click();
            }
            if(!flg
            && form.elements[checkBoxElement][i].checked){
                form.elements[checkBoxElement][i].click();
            }
        }
    }else{
        if(flg
        && !form.elements[checkBoxElement].checked){
            form.elements[checkBoxElement].click();
        }
        if(!flg
        && form.elements[checkBoxElement].checked){
            form.elements[checkBoxElement].click();
        }
    }
}

/*
* 画像の読み込みの完了を感知するタイマーです。
*
*/
var popupWin;    // ポップアップウィンドウ
var timerId;   // タイマーID  
var img;       // 画像オブジェクト
function fitimg() {
	if (img.complete == undefined || img.complete) { 
		clearInterval(timerId);
		
		// 画像サイズ
		imgWidth = img.width;
		imgHeight = img.height;
		
		// 画像の最大サイズ
		maxWidth = 800 * 0.90;
		maxHeight = 800 * 0.95;
	
		// 縦幅・横幅共に最大値を超えている場合
	    if (imgWidth > maxWidth && imgHeight > maxHeight) {
	    	// 縦幅・横幅の大きい方を最大値に合わせ縮小
	    	if (imgWidth > imgHeight) {
	    		imgHeight = imgHeight * maxWidth / imgWidth;
	    		imgWidth = maxWidth;
	    	} else 	if (imgWidth < imgHeight) {
	    		imgWidth = imgWidth * maxHeight / imgHeight;
	    		imgHeight = maxHeight;
	    	} else {
	    		// 正方形の画像の場合
	            // 最大幅と最大高さが同じならそのまま使用
	            if (maxWidth == maxHeight) {
	                imgWidth = maxWidth;
	                imgHeight = maxHeight;
	            } else {
	                // 最大値が異なる場合、正方形の縦横比を崩さないように、小さい方の値を両方に設定する。
	                imgWidth = Math.min(maxWidth, maxHeight);
	                imgHeight = Math.min(maxWidth, maxHeight);
	            }
	    	}
	    } else if (imgWidth > maxWidth) { // 横幅の最大値を超えていたら最大値に合わせ縮小
			imgHeight = imgHeight * maxWidth / imgWidth;
			imgWidth = maxWidth;
	    } else if (imgHeight > maxHeight) { // 縦幅の最大値を超えていたら最大値に合わせ縮小
			imgWidth = imgWidth * maxHeight / imgHeight;
			imgHeight = maxHeight;
	    }
	    popupWin.document.open();
		popupWin.document.write("<html><head><title>" + img.src +  "</title></head>");
		popupWin.document.write("<body style=’margin:0px; padding:0px;’><center><table border='0' cellspacing='0' cellpadding='0'><tr><td align='center'>");
		popupWin.document.write("<img width='" + imgWidth + "' height='" + imgHeight + "' src='" + img.src + "' name='img'></td></tr></table></center></body></html>");
		popupWin.document.close();
	}
}

/*
* 指定したURLの画像をポップアップ表示します。
*
* @param url 画像のURL (必須)
*/
function popup(url){
	img = new Image();
	img.src = url;
    
	var options = "width=" + 800 + ",height=" + 800 + ",scrollbars=yes,resizable=yes";
	popupWin = window.open("about:blank", "", options);
	
	timerId = setInterval("fitimg()", 100);

}
