///////////////////////////
//
//  prototype.jsを使用が前提
//
///////////////////////////

var inputLow  = null;
var inputHigh = null;
var idPLLow = null;
var idPLHigh = null;

// 入力値チェック
function inputCheck(){
	//changeDispMsg( null, false );
	
	idPLLow = $('id_price_limit_low');
	idPLHigh = $('id_price_limit_high');
	if( idPLLow != null && idPLLow.value != null )   var pLow = idPLLow.value;
	if( idPLHigh != null && idPLHigh.value != null ) var pHigh = idPLHigh.value;
	
	inputLow  = numericCheck( pLow );
	inputHigh = numericCheck( pHigh );
	
	var inputSubmit = $('inputSubmit');
	
	if( $('id_price_limit_low').value.length == 0 && $('id_price_limit_high').value.length == 0 ){
		// 両方の入力欄が空白
		//inputSubmit.disabled = true;
		submitDisable( true );
		changeDispMsg( null, false );
		
	} else if( isLeftZero( pLow ) || isLeftZero( pHigh ) ){
		// 入力値の先頭が０だった場合。
		changeDispMsg( 'leftZero', true );
		
	} else if( inputLow == null || inputHigh == null ){
		// 以上 or 以下 に数値以外が入っていた場合。先頭が０だった場合。
		changeDispMsg( 'input', true );
		//inputSubmit.disabled = true;
		submitDisable( true );
		
	} else if( inputLow != null && inputHigh != null && !relationCheck(inputLow, inputHigh) ){
		// 以上より以下が小さい場合
		changeDispMsg( 'relation', true );
		//inputSubmit.disabled = true;
		submitDisable( true );
		
	} else {
		$('inputMsg').innerHTML = "";
		changeDispMsg( null, false );
		//inputSubmit.disabled = false;
		submitDisable( false );
	}
}

function submitDisable( bool ){
	var inputSubmit = $('inputSubmit');
	if( bool ){
		inputSubmit.disabled = true;
		inputSubmit.src = inputSubmit.src.replace( /_on./, '_off.' );
		
	} else {
		inputSubmit.disabled = false;
		inputSubmit.src = inputSubmit.src.replace( /_off./, '_on.' );
	}
	return;
}

// 入力値に対するメッセージを表示
// param:1 = メッセージタイプ    param2: = 入力エラーかどうか
function changeDispMsg( type, bool ){
	// 表示メッセージ
	var dispMsg = "半角数値を入力して下さい";
	if( type == 'relation' ){
		dispMsg = "以上・以下が不正です";
	} else if( type == 'leftZero' ){
		dispMsg = "先頭が「0」です";
	}
	
	// 表示メッセージサイズ
	//var dispMsgSize = "12px";
	
	// 通常時のメッセージフォントカラー
	var normalMsgColor = "gray";
	// 入力値エラー時のメッセージフォントカラー
	var errorMsgColor  = "red";
	
	var checkMsg = $('inputMsg');
	if( checkMsg != null ){
		// 既に要素がある場合は、まず消去
		while( checkMsg.firstChild != null ){
			checkMsg.removeChild( checkMsg.firstChild );
		}
		
		var msgColor = '';
		// メッセージカラー決定
		// true:エラー    false:通常時
		( bool ) ? msgColor = errorMsgColor : msgColor = normalMsgColor;
		var checkElem = document.createElement( 'span' );
		checkElem.style.color    = msgColor;
		//checkElem.style.fontSize = dispMsgSize;
		//checkElem.setAttribute( 'style', 'color:'+msgColor );
		var checkMsgText = document.createTextNode( dispMsg );
		checkElem.appendChild( checkMsgText )
		checkMsg.appendChild( checkElem );
	}
}

// 数値チェック
function numericCheck( inputStr ){
	// 「,」のみ許可
	inputStr = inputStr.replace(/,/g, '');
	// 半角数値かどうか
	if( inputStr.match(/\D/) ){
		// 入力が不正
		return null;
	} else {
		return inputStr;
	}
}
// 入力値の先頭が０で始まっているか
function isLeftZero( inputStr ){
	if( inputStr != 0 && parseInt(inputStr.substring(0,1)) == 0 ){
		// 入力値の先頭が０
		return true;
	} else {
		return false;
	}
}
// 入力欄：以上、以下の関係チェック
function relationCheck( low, high ){
	// 以下の方が小さければ
	if( parseInt(high) < parseInt(low) ){
		// 大小関係が不正
		return false;
	} else {
		return true;
	}
}
$j(function(){
	changeDispMsg( null, false );
	inputCheck();
});

function goSort(){
	idPLLow.value  = inputLow;
	idPLHigh.value = inputHigh;
}

/*
function toggleSort( key, val ){
	var manager = new CookieManager({shelfLife:30});
	manager.setCookie(key, val);
	alert("EEE" + manager.getCookie(key));
}
*/