/******************************************************************************
 * 
 */

/******************************************************************************
 * パス
 */
var image_path = 'images/';



/******************************************************************************
 * チェックボックス input タグを生成する
 */
function check_box(object, method, options) {
	var result = '';
	
	result += '<input type="checkbox" ';
	result += name_attr(object, method);
	result += id_attr(object, method);
	
	if (options != null) {
	}
	
	var value = get_object_method(object, method);
	if (value != null) {
		if (value) {
			result += 'checked="checked" ';
		}
	}
	
	result += ' />';
	
	return result;
}

/******************************************************************************
 * ファイル input タグを生成する
 */
function file_field(object, method, options) {
	var result = '';
	
	result += '<input type="file" ';
	result += name_attr(object, method);
	result += id_attr(object, method);
	
	if (options != null) {
		if (options.size != null) {
			result += 'size="' + options.size + '" ';
		}
		
		if (options.maxlength != null) {
			result += 'maxlength="' + options.maxlength + '" ';
		}
	}
	
	result += ' />';
	
	return result;
}

/******************************************************************************
 * 隠し input タグを生成する
 */
function hidden_field(object, method, options) {
	var result = '';
	
	result += '<input type="hidden" ';
	result += name_attr(object, method);
	result += id_attr(object, method);
	
	if (options != null) {
		if (options.size != null) {
			result += 'size="' + options.size + '" ';
		}
		
		if (options.maxlength != null) {
			result += 'maxlength="' + options.maxlength + '" ';
		}
	}
	
	var value = get_object_method(object, method);
	if (value != null) {
		if (value) {
			result += 'value="' + value + '" ';
		}
	}
	
	result += ' />';
	
	return result;
}

/******************************************************************************
 * パスワード input タグを生成する
 */
function password_field(object, method, options) {
	var result = '';
	
	result += '<input type="password" ';
	result += name_attr(object, method);
	result += id_attr(object, method);
	
	if (options != null) {
		if (options.size != null) {
			result += 'size="' + options.size + '" ';
		}
		
		if (options.maxlength != null) {
			result += 'maxlength="' + options.maxlength + '" ';
		}
	}
	
	var value = get_object_method(object, method);
	if (value != null) {
		if (value) {
			result += 'value="' + value + '" ';
		}
	}
	
	result += ' />';
	
	return result;
}

/******************************************************************************
 * ラジオボタン button タグを生成する
 */
function radio_button(object, method, tag_value, options) {
	var result = '';
	
	result += '<input type="radio" ';
	result += name_attr(object, method);
	result += 'id="' + object + '_' + method + '_' + tag_value + '" ';
	
	result += 'value="' + tag_value + '"';
	
	if (options != null) {
	}
	
	var value = get_object_method(object, method);
	if (value != null) {
		if (value) {
			if (value == tag_value) {
				result += 'checked="checked" ';
			}
		}
	}
	
	result += ' />';
	
	return result;
}

/******************************************************************************
 * textarea タグを生成する
 */
function text_area(object, method, options) {
	var result = '';
	
	result += '<textarea ';
	result += name_attr(object, method);
	result += id_attr(object, method);
	
	if (options != null) {
		if (options.cols != null) {
			result += 'cols="' + options.cols + '" ';
		}
		
		if (options.rows != null) {
			result += 'rows="' + options.rows + '" ';
		}
	}
	
	result += '>';
	
	var value = get_object_method(object, method);
	if (value != null) {
		if (value) {
			result += value;
		}
	}
	
	result += '</textarea>';
	
	return result;
}

/******************************************************************************
 * テキスト input タグを生成する
 */
function text_field(object, method, options) {
	var result = '';
	
	result += '<input type="text" ';
	result += name_attr(object, method);
	result += id_attr(object, method);
	
	if (options != null) {
		if (options.size != null) {
			result += 'size="' + options.size + '" ';
		}
		
		if (options.maxlength != null) {
			result += 'maxlength="' + options.maxlength + '" ';
		}
	}
	
	var value = get_object_method(object, method);
	if (value != null) {
		if (value) {
			result += 'value="' + value + '" ';
		}
	}
	
	result += ' />';
	
	return result;
}



/******************************************************************************
 * ボタンを生成する
 */
function button_tag(value, options) {
	var result = '';
	
	result += '<button type="button" value="' + value + '"';
	
	if (options != null) {
		if (options.onclick != null) {
			result += 'onclick="' + options.onclick + '" ';
		}
	}
	
	result += '>';
	
	result += value;
	
	result += '</button>';
	
	return result;
}

/******************************************************************************
 * submit ボタンタグを生成する
 */
function submit_tag(value, options) {
	var result = '';
	
	result += '<input type="submit" value="' + value + '"';
	
	if (options != null) {
		if (options.onclick != null) {
			result += 'onclick="' + options.onclick + '" ';
		}
	}
	
	result += ' />';
	
	return result;
}



/******************************************************************************
 * id: と name: がある JSON 配列を select タグに生成する
 */
function select(object, method, choices, options, html_options) {
	var result = '';
	
	result += '<select ';
	result += name_attr(object, method);
	result += id_attr(object, method);
	result += '>';
	
	if (options != null) {
		if (options.include_blank != null) {
			if (options.include_blank) {
				result += '<option></option>';
			}
		}
	}
	
	var value = get_object_method(object, method);
	if (value == null) {
		value = 0;
	}
	
	choices.each(function(choice, i) {
		if (value == choice.id) {
			result += '<option value="' + choice.id + '" selected="selected">' + choice.name + '</option>';
		}
		else {
			result += '<option value="' + choice.id + '">' + choice.name + '</option>';
		}
	});
	
	result += '</select>';
	
	return result;
}

/******************************************************************************
 * 日付 select タグに生成する
 */
function date_select(object, method, options) {
	var result = '';
	
	result += select(object, method + '_year', [ { id: 2007, name: '2007年'}, { id: 2008, name: '2008年'} ]);
	result += select(object, method + '_month', [ { id: 1, name: '1月'}, { id: 2, name: '2月'}, { id: 3, name: '3月'}, { id: 4, name: '4月'}, { id: 5, name: '5月'}, { id: 6, name: '6月'}, { id: 7, name: '7月'}, { id: 8, name: '8月'}, { id: 9, name: '9月'}, { id: 10, name: '10月'}, { id: 11, name: '11月'}, { id: 12, name: '12月'} ]);
	result += select(object, method + '_day', [ 
		{ id: 1, name: '1日'}, { id: 2, name: '2日'} , { id: 3, name: '3日'} , { id: 4, name: '4日'} , { id: 5, name: '5日'} , { id: 6, name: '6日'} , { id: 7, name: '7日'} , { id: 8, name: '8日'} , { id: 9, name: '9日'} , { id: 10, name: '10日'}, 
		{ id: 11, name: '11日'}, { id: 12, name: '12日'} , { id: 13, name: '13日'} , { id: 14, name: '14日'} , { id: 15, name: '15日'} , { id: 16, name: '16日'} , { id: 17, name: '17日'} , { id: 18, name: '18日'} , { id: 19, name: '19日'} , { id: 20, name: '20日'}, 
		{ id: 21, name: '21日'}, { id: 22, name: '22日'} , { id: 23, name: '23日'} , { id: 24, name: '24日'} , { id: 25, name: '25日'} , { id: 26, name: '26日'} , { id: 27, name: '27日'} , { id: 28, name: '28日'} , { id: 29, name: '29日'} , { id: 30, name: '30日'}, 
		{ id: 31, name: '31日'} 
	]);
	
	return result;
}




/******************************************************************************
 * img タグを生成する
 */
function image_tag(source, options) {
	var result = '';
	
	result += '<img src="' + image_path + source + '" ';
	
	if (options != null) {
		if (options.alt != null) {
			result += 'alt="' + options.alt + '" ';
		}
		
		if (options.title != null) {
			result += 'title="' + options.title + '" ';
		}
		
		if (options.width != null) {
			result += 'width="' + options.width + '" ';
		}
		
		if (options.height != null) {
			result += 'height="' + options.height + '" ';
		}
	}
	
	result += '/>';
	
	return result;
}

/******************************************************************************
 * a タグを生成する
 */
function link_to(name, options, html_options) {
	var result = '';
	
	result += '<a href="';
	if (options != null) {
		result += options;
	}
	result += '" ';
	
	if (html_options != null) {
		if (html_options.target != null) {
			result += 'target="' + html_options.target + '" ';
		}
		if (html_options.id != null) {
			result += 'id="' + html_options.id + '" ';
		}
	}
	
	result += '>' + name + '</a>';
	
	return result;
}

/******************************************************************************
 * label タグを生成する
 */
function label_tag(object, method, caption) {
	var result = '';
	
	result += '<label ';
	result += 'for="' + object + '_' + method + '" ';
	
	result += '>';
	
	if (caption != null) {
		result += caption;
	}
	
	result += '</label>';
	
	return result;
}

/******************************************************************************
 * ラジオボタン用の label タグを生成する
 */
function radio_label_tag(object, method, tag_value, caption) {
	var result = '';
	
	result += '<label ';
	result += 'for="' + object + '_' + method + '_' + tag_value + '" ';
	
	result += '>';
	
	if (caption != null) {
		result += caption;
	}
	
	result += '</label>';
	
	return result;
}



/******************************************************************************
 * 金額
 */
function yen(value) {
	var result = '';
	
	if (value != null) {
		result = value.toString()
		var tmp = '';
		
		while (result != (tmp = result.replace(/^([+-]?\d+)(\d\d\d)/,"$1,$2"))) {
			result = tmp
		}
	}
	
	return result;
}



/******************************************************************************
 * name 属性を生成する
 */
function name_attr(object, method) {
	return 'name="' + object + '_' + method + '" ';
}

/******************************************************************************
 * id 属性を生成する
 */
function id_attr(object, method) {
	return 'id="' + object + '_' + method + '" ';
}

/******************************************************************************
 * object.method の値を得る
 */
function get_object_method(object, method) {
	var result = null;
	
	if (object == null || method == null) {
		return null;
	}
	
	try {
		if (eval(object) == null) {
			return null;
		}
		
		result = eval(object + '.' + method);
	}
	catch (e) {
	}
	
	return result;
}

/******************************************************************************
 * object.method の値を設定する
 */
function set_object_method(object, method, value) {
	var result = null;
	
	if (object == null || method == null) {
		return null;
	}
	
	try {
		if (eval(object) == null) {
			return null;
		}
		
		result = eval(object + '.' + method + ' = ' + value);
	}
	catch (e) {
	}
	
	return value;
}
