var jInputObjs = null;
var jVdCdtnObjs = null;
var jMustObjs = null;

$(function(){
	jInputObjs = $("input[type=text],input[type=radio],input[type=checkbox],textarea,select");
	jVdCdtnObjs = jInputObjs.filter( ".vdcdtn" );
	jMustObjs = $("input[type=hidden][name=h_must_must]");

	jInputObjs.filter("input[type=radio],input[type=checkbox]").click( function(){
		checkOther( $(this) );
		checkVdcdtn( $(this) );
	});

	$.each( jInputObjs.filter("input[type=radio],input[type=checkbox]"), function( idx ) {
		checkOther( $(this) );
		checkVdcdtn( $(this) );
	});
});

function checkOther( jThis ) {
	var nm = jThis.attr("name");
	var jIns = $("input[name="+nm+"]")
	var jOther = jIns.filter(".other");
	var jOtherText = jIns.filter(".otherText");

	if( !jOther || jOther.length == 0 || !jOtherText || jOtherText.length == 0 ) {
		return;
	}

	if( jOther.attr("checked") ) {
		jOtherText.removeAttr("disabled");
		jOtherText.css("background-color","");
	}
	else {
		jOtherText.val("");
		jOtherText.attr("disabled","true");
		jOtherText.css("background-color","lightgrey");
	}
}

function checkVdcdtn( jThis ) {
	$.each( jVdCdtnObjs, function( idx ) {
		var cdtns = $(this).attr( "vdcdtn" );
		if( cdtns ) {
			var keyValArr = cdtns.split("=");
			if( keyValArr.length > 1 ) {
				var key = keyValArr[0];
				var valArr = keyValArr[1].split(",");
				if( valArr ) {
					var isValid = false;
					for( var i=0; i<valArr.length; i++ ) {
						if( jInputObjs.filter("[rscode="+key+"][rkcode="+valArr[i]+"]").attr("checked") ) {
							isValid = true;
							break;
						}
					}

					if( isValid ) {
						$(this).removeAttr("disabled");
						$(this).css("background-color","");
						jMustObjs.filter("[value="+$(this).attr("name")+"]").attr("name","h_must_must");
					}
					else {
						$(this).attr("disabled","true");
						$(this).css("background-color","lightgrey");
						jMustObjs.filter("[value="+$(this).attr("name")+"]").attr("name","h_must_must_");
					}
				}
			}
		}
	});
}
