if(!PSINC) { var PSINC = {} };
if(!PSINC.util) { PSINC.util = {} };
(function(){
var d = document;

if(!PSINC.util.getDivElement) {
	// 指定されたidのdivを取得
	PSINC.util.getDivElement = function (id) {
		var e = d.getElementById(id);
		if(!e) {
			e = d.createElement('div');
			e.id = id;
			d.body.appendChild(e);
		}
		return e;
	}
}

if(!PSINC.util.parseMMDDYY) {
	// MM/DD/YY 形式のデータをパース
	PSINC.util.parseMMDDYY = function(str) {
		if(!YAHOO.lang.isString(str)) {
			return new Date();
		}
		var aDate = str.split('/');
		var oDate;
		if(aDate.length == 3) {
			oDate = YAHOO.widget.DateMath.getDate(2000+aDate[2]*1,aDate[0]-1,aDate[1]);
			//new Date(aDate[2]*1+2000,aDate[0]-1,aDate[1]);
		} else {
			oDate = new Date();
		}
		return oDate;
	}
}
if(!PSINC.util.parseYYYYMMDD) {
	// YYYY/MM/DD 形式のデータをパース
	PSINC.util.parseYYYYMMDD = function(str) {
		if(!YAHOO.lang.isString(str)) {
			return new Date();
		}
		var aDate = str.split('/');
		var oDate;
		if(aDate.length == 3) {
			oDate = YAHOO.widget.DateMath.getDate(aDate[0]*1,aDate[1]-1,aDate[2]);
			//new Date(aDate[2]*1+2000,aDate[0]-1,aDate[1]);
		} else {
			oDate = new Date();
		}
		return oDate;
	}
}
if(!PSINC.util.setUpCalendar) {
	//カレンダーをセットアップ
	PSINC.util.setUpCalendar = function (target,container,oConfig) {
		if(YAHOO.lang.isString(target)) {
			target = d.getElementById(target);
		}
		if(YAHOO.lang.isString(container)) {
			container = PSINC.util.getDivElement(container);
		}
		if(!YAHOO.lang.isObject(oConfig)) {
			oConfig = {};
		}
		oConfig.close = true;
		
		YAHOO.util.Dom.setStyle(container,'display','none');
		YAHOO.util.Dom.setStyle(container,'position','absolute');
		YAHOO.util.Dom.setStyle(container,'z-index','9000');
		
		var cal = new YAHOO.widget.CalendarGroup('',container,oConfig);
		if(target.value) {
			if(PSINC.locale != 'cn') {
				cal.select(PSINC.util.parseMMDDYY(target.value));
			} else {
				cal.select(PSINC.util.parseYYYYMMDD(target.value));
			}
		}
		cal.selectEvent.subscribe(function(type,args,obj){
			var dates = args[0];
			var date = dates[0];
			var year = date[0], month = date[1], day = date[2];
			if(PSINC.locale != 'cn') {
				year = ('0'+year).slice(-2);
				month = ('0'+month).slice(-2);
				day = ('0'+day).slice(-2);
		
				target.value = month + "/" + day + "/" + year;
			} else {
				target.value = year+"/"+month+"/"+day;
			}
			cal.hide();
		}, cal, true);
		
		/*
		YAHOO.util.Event.addListener(container,'blur',function(){
			cal.close();
		},container,true);
		*/
		
		YAHOO.util.Event.addListener(target,'click',function(event){
			var region = YAHOO.util.Region.getRegion(target);

			YAHOO.util.Dom.setStyle(container,'top',region.bottom+'px');
			YAHOO.util.Dom.setStyle(container,'left',region.left+'px');

			if(PSINC.locale != 'cn') {
				cal.select(PSINC.util.parseMMDDYY(target.value));
			} else {
				cal.select(PSINC.util.parseYYYYMMDD(target.value));
			}

			var selectedDates = cal.getSelectedDates();
			if (selectedDates.length > 0) {
				var firstDate = selectedDates[0];
				cal.cfg.setProperty("pagedate", (firstDate.getMonth()+1) + "/" + (firstDate.getFullYear()));
				cal.show();
				cal.render();
			} else {
				alert("Cannot select a date before 1/1/2006 or after 12/31/2008");
			}
		},target,true);

		cal.render();
		return cal;
	}
}

if(!PSINC.util.setFormElementValue) {
	// フォームエレメントの値をセットする
	PSINC.util.setFormElementValue = function (target,value) {
		if(typeof target == 'string') {
			target = d.getElementById(target);
		}
		if(target) {
			if(target.type == 'select-one') {
				for(var i = 0; i < target.options.length; ++ i) {
					if(target.options[i].value == value) {
						target.selectedIndex = i;
						break;
					}
				}
			} else if(target.type == 'text' || target.type == 'hidden'
					  || target.type == 'password' || target.type == 'textarea') {
				target.value = value;
			}
		}
	}
}

if(!PSINC.util.getFormElementValue) {
	// フォームエレメントの値をセットする
	PSINC.util.getFormElementValue = function (target) {
		if(typeof target == 'string') {
			target = d.getElementById(target);
		}
		var value = null;
		if(target) {
			if(target.type == 'select-one') {
				value = target.options[target.selectedIndex].value;
			} else if(target.type == 'text' || target.type == 'hidden'
					  || target.type == 'password' || target.type == 'textarea') {
				value = target.value;
			}
		}
		return value;
	}
}

if(!PSINC.util.createHiddenElement) {
	PSINC.util.createHiddenElement = function(name,value) {
		var el = d.createElement('input');
		el.type = 'hidden';
		el.name = name;
		el.value = value;
		return el;
	}
}

})();

