function savePortfolio(){
    if($("savePortfolioButton") != null){
        $("savePortfolioButton").disabled = true;
    }
    var userId = $("userId").value;
    var portfolioName = encodeURIComponent( $("portfolioName").value );
    var thisId = $("thisId").value;
	if(document.getElementsByName("newOrExist")[0].checked == true){
	   	var newOrExist = document.getElementsByName("newOrExist")[0].value;
	}else{
		var newOrExist = document.getElementsByName("newOrExist")[1].value;
 	}

    var type = $("saveType").value;
    var u = document.URL;
        if( u.indexOf('/tm/') != -1 || u.indexOf('type=tm') != -1 ){
                rightType = 'tm';
        }else if( u.indexOf('/pt/') != -1 || u.indexOf('type=pt') != -1  ){
                rightType = 'pt';
        }else if( u.indexOf('/ds/') != -1 || u.indexOf('type=ds') != -1  ){
                rightType = 'ds';
        }else if( u.indexOf('/uc/') != -1 || u.indexOf('type=uc') != -1  ){
                rightType = 'uc';
        }else if( u.indexOf('/um/') != -1 || u.indexOf('type=um') != -1  ){
                rightType = 'um';
        }else if( u.indexOf('/cr/') != -1 || u.indexOf('type=cr') != -1  ){
                rightType = 'cr';
        }else if( u.indexOf('type=etc') != -1 || u.indexOf('type=etc') != -1  ){
                rightType = 'etc';
        }


    var parameter = "cmd=saveDetail&userId=" + userId +
                    "&portfolioName=" + portfolioName +
                    "&thisId=" + thisId +
                    "&newOrExist=" + newOrExist +
                    "&rightType=" + rightType +
                    "&type=" + type +
                    "&t=" + new Date();
    if($("hash") != null){
        var hash = encodeURIComponent( $("hash").value )
        parameter = parameter + "&hash=" + hash;
    }

	if(newOrExist == 'exist'){
		var portfolioId = $('portfolioId').value;
		parameter = parameter + "&portfolioId=" + portfolioId;
	}

	if(newOrExist == 'new' && $("portfolioName").value.match(/^[ 　]*$/) ){
		alert('新規ポートフォリオに追加する場合は、ポートフォリオ名を入力してください。');
		$('savePortfolioAlert').innerHTML = '';
        if($("savePortfolioButton") != null){
            $("savePortfolioButton").disabled = false;
        }
		return;
	}

    new Ajax.Request('/portfolio/save/DetailSave.do',
        {
            method: 'get',
            asynchronous: true,
            parameters: parameter,
            onSuccess: function(httpObj){
                    var result = httpObj.responseText;
                    $('savePortfolioAlert').innerHTML = result;
                    if($("savePortfolioButton") != null){
                        $("savePortfolioButton").disabled = false;
                    }
                },
            onFailure: function(req, header){
                    $('savePortfolioAlert').innerHTML = '保存に失敗しました。';
                    if($("savePortfolioButton") != null){
                        $("savePortfolioButton").disabled = false;
                    }
                }
        }
    );
}

function newChecked(){
    $('portfolioName').disabled = false;
    $('portfolioId').disabled = true;
}

function existChecked(){
    if($("userId") != null){
       var userId = $("userId").value;
	   var url = '/portfolio/save/ExistPortfolioSelectBox.do';
	   var parameter = "userId=" + userId +
                    "&t=" + new Date();

	   var myAjax = new Ajax.Updater(
		  'portfolioSelect', 
    		url, 
    		{
		 	    method: 'get',
    		    parameters: parameter
    		});
    }
    document.getElementById('portfolioName').disabled = true;
    document.getElementById('portfolioId').disabled = false;
}


function savePortfolioFromList(){
    if($("savePortfolioButton") != null){
        $("savePortfolioButton").disabled = true;
    }
    var userId = $("userId").value;
    var portfolioName = encodeURIComponent( $("portfolioName").value );
    var thisId = '';
    var rightType = '';
    var checkFlag = true;
    for(i=1; i<=30; i++){
    	var checkID = "check"+i;
    	
    	if($(checkID) == null){
            break;
    	}
    	if($(checkID).checked == true){
    		thisId = thisId + $("thisId"+i).value  + ',';
    		rightType = rightType + $("rightType"+i).value  + ',';
    		checkFlag = false;
    	}
    }
    if(checkFlag){
   	    alert('チェックボックスを選択してください。');
   		$('savePortfolioAlert').innerHTML = '';
        if($("savePortfolioButton") != null){
            $("savePortfolioButton").disabled = false;
        }
   		return;
   	}

	if(document.getElementsByName("newOrExist")[0].checked == true){
	   	var newOrExist = document.getElementsByName("newOrExist")[0].value;
	}else{
		var newOrExist = document.getElementsByName("newOrExist")[1].value;
 	}
	var type = $("saveType").value;

    var parameter = "cmd=saveList&userId=" + userId +
                    "&portfolioName=" + portfolioName +
                    "&thisId=" + thisId +
                    "&newOrExist=" + newOrExist +
                    "&rightType=" + rightType +
                    "&type=" + type +
                    "&t=" + new Date();

    if($("hash") != null){
        var hash = encodeURIComponent( $("hash").value );
        parameter = parameter + "&hash=" + hash;
    }

	if(newOrExist == 'exist'){
		var portfolioId = $('portfolioId').value;
		parameter = parameter + "&portfolioId=" + portfolioId;
	}

	if(newOrExist == 'new' && $("portfolioName").value.match(/^[ 　]*$/) ){
		alert('新規ポートフォリオに追加する場合は、ポートフォリオ名を入力してください。');
		$('savePortfolioAlert').innerHTML = '';
        if($("savePortfolioButton") != null){
            $("savePortfolioButton").disabled = false;
        }
		return;
	}

    new Ajax.Request('/portfolio/save/DetailSave.do',
        {
            method: 'get',
            asynchronous: true,
            parameters: parameter,
            onSuccess: function(httpObj){
                    var result = httpObj.responseText;
                    $('savePortfolioAlert').innerHTML = result;
                    if($("savePortfolioButton") != null){
                        $("savePortfolioButton").disabled = false;
                    }
                },
            onFailure: function(req, header){
                    $('savePortfolioAlert').innerHTML = '保存に失敗しました。';
                    if($("savePortfolioButton") != null){
                        $("savePortfolioButton").disabled = false;
                    }
                }
        }
    );
}

function dispOnlyMemberForSave(){
    $('savePortfolioAlert').innerHTML = 'この機能はパテントビューロIDに登録後、<br><a href="http://www.patentbureau.co.jp/login/login/Login.do">ログイン</a>した状態でご利用いただけます。<br>' + 
        'パテントビューロIDは<a href="http://www.patentbureau.co.jp/login/signUp/SignUp.do">こちら</a>からご登録下さい。';
}


