﻿/*****************************************************************************************************
[Description]
下拉式選單日期js模組(第四版).


------------------------------------------------------------------
[example]

*****************************************************************************************************/
function selDate_v4(D){
	//D.style.backgroundColor = "#FFCCAA";


	this.Create = function(CurrentDate,cha){
		if(CurrentDate == '1900-01-01' || CurrentDate == '2100-01-01'){
			if(idShow_Unlimited) CreateUnlimited('false');
			//
			var date = new Date;
			if(isShow_Year) CreateYear(date.getFullYear());
			if(isShow_Month) CreateMonth(date.getMonth()+1);
			if(isShow_Day) CreateDay(date.getDate());
		}else{
			if(idShow_Unlimited) CreateUnlimited('true');
			//
			var arrCurrentDate = CurrentDate.split(cha);
			if(isShow_Year) CreateYear(arrCurrentDate[0]);
			if(isShow_Month) CreateMonth(arrCurrentDate[1]);
			if(isShow_Day) CreateDay(arrCurrentDate[2]);
		}
		
	}
	
	this.setShow = function(arg1,arg2,arg3,arg4){
		idShow_Unlimited = arg1
		isShow_Year = arg2;
		isShow_Month = arg3;
		isShow_Day = arg4;
	}
	
	this.setTE = function(arg){
		TE = arg;	
	}
	
	this.setLanguage = function(arg){
		Language = arg;
	}
	
	this.setBeginEndDate = function(arg1,arg2){
		if(arg1<=arg2){
			BeginDate = arg1;
			EndDate = arg2;
		}
	}
	
	
	var idShow_Unlimited = true;
	var isShow_Year = true;
	var isShow_Month = true;
	var isShow_Day = true;
	var TE = 'E';
	var Language = 'T';
	var BeginDate = 2000;
	var EndDate = 2010;
	
	
	var CreateUnlimited = function(isDate){
		var is_ie = ((navigator.userAgent.toLowerCase().indexOf("msie") != -1) && (navigator.userAgent.toLowerCase().indexOf("opera") == -1));
		
		if(is_ie){
			if(isDate == 'false'){
				var newOption = document.createElement('<input type="radio" name="'+D.id+'_IsDate" value="0" checked />');
			}else{
				var newOption = document.createElement('<input type="radio" name="'+D.id+'_IsDate" value="0" />');
			}
			
		}else{
			var newOption = document.createElement('input');
			newOption.setAttribute("type","radio");
			newOption.setAttribute("name",D.id + "_IsDate");
			newOption.setAttribute("value","0");
			if(isDate == 'false'){
				newOption.setAttribute("checked",isDate);
			}else{
				//
			}
		}
		D.appendChild(newOption);
		
		if(Language == 'T'){
			D.appendChild(document.createTextNode("無期限 "));
		}else{
			D.appendChild(document.createTextNode("Unlimited "));
		}
		
		if(is_ie){
			if(isDate == 'false'){
				var newOption = document.createElement('<input type="radio" name="'+D.id+'_IsDate" value="1" />');
			}else{
				var newOption = document.createElement('<input type="radio" name="'+D.id+'_IsDate" value="1" checked />');
			}
		}else{
			var newOption = document.createElement('input');
			newOption.setAttribute("type","radio");
			newOption.setAttribute("name",D.id + "_IsDate");
			newOption.setAttribute("value","1");
			if(isDate == 'false'){
				//
			}else{
				newOption.setAttribute("checked",isDate);
			}
		}
		D.appendChild(newOption);
		
	}
	
	
	var CreateYear = function(CurrentDate_Year){		
		var newOption = document.createElement('select');
		newOption.setAttribute("id",D.id + "_Year");
		newOption.setAttribute("name",D.id + "_Year");

		newOption[0] = new Option("-","0000");
		for(var i=0;i<=(EndDate-BeginDate);i++){
			if(TE == 'E'){
				newOption[i+1] = new Option(BeginDate+i,BeginDate+i);
			}else{
				newOption[i+1] = new Option((BeginDate+i)-1911,BeginDate+i);
			}
			if( BeginDate+i == CurrentDate_Year){
				newOption[i+1].selected = true;
			}
		}
		
		addUserListener(newOption,'change',Tchange);
		if(idShow_Unlimited) addUserListener(newOption,'change',setUnlimited);
		
		D.appendChild(newOption);
		if(Language == 'T'){
			D.appendChild(document.createTextNode("年 "));
		}else{
			D.appendChild(document.createTextNode("Year "));
		}
		
	}
	
	var CreateMonth = function(CurrentDate_Month){		
		var newOption = document.createElement('select');
		newOption.setAttribute("id",D.id + "_Month");
		newOption.setAttribute("name",D.id + "_Month");
		
		newOption[0] = new Option("-","00");
		for(var i=1;i<=12;i++){
			newOption[i] = new Option(i,i);
			if( i == CurrentDate_Month){
				newOption[i].selected = true;
			}
		}

		addUserListener(newOption,'change',Tchange);
		if(idShow_Unlimited) addUserListener(newOption,'change',setUnlimited);
		
		D.appendChild(newOption);
		if(Language == 'T'){
			D.appendChild(document.createTextNode("月 "));
		}else{
			D.appendChild(document.createTextNode("Month "));
		}
	}
	
	var CreateDay = function(CurrentDate_Day){		
		var iYear = document.getElementById(D.id + "_Year").value;
		var iMonth = document.getElementById(D.id + "_Month").value;
		
		var arrMonth = Array(31,28,31,30,31,30,31,31,30,31,30,31);
		if(((iYear % 4 == 0) && (iYear % 100 != 0))||(iYear % 400 == 0)){
			arrMonth[1] = 29;
		}

		var newOption = document.createElement('select');
		newOption.setAttribute("id",D.id + "_Day");
		newOption.setAttribute("name",D.id + "_Day");
		
		newOption[0] = new Option("-","00");
		for(var i=1;i<=arrMonth[iMonth-1];i++){
			newOption[i] = new Option(i,i);
			if( i == CurrentDate_Day){
				newOption[i].selected = true;
			}
		}
		
		if(idShow_Unlimited) addUserListener(newOption,'change',setUnlimited);
		
		D.appendChild(newOption);
		if(Language == 'T'){
			D.appendChild(document.createTextNode("日 "));
		}else{
			D.appendChild(document.createTextNode("Day "));
		}
	}
	
	var UpDateDay = function(CurrentDate_Day){		
		var iYear = document.getElementById(D.id + "_Year").value;
		var iMonth = document.getElementById(D.id + "_Month").value;
		var arrMonth = Array(31,28,31,30,31,30,31,31,30,31,30,31);
		if(((iYear % 4 == 0) && (iYear % 100 != 0))||(iYear % 400 == 0)){
			arrMonth[1] = 29;
		}

		var newOption = document.createElement('select');
		newOption.setAttribute("id",D.id + "_Day");
		newOption.setAttribute("name",D.id + "_Day");

		newOption[0] = new Option("-","00");
		for(var i=1;i<=arrMonth[iMonth-1];i++){
			newOption[i] = new Option(i,i);
			if( i == CurrentDate_Day){
				newOption[i].selected = true;
			}
		}
		
		if(idShow_Unlimited) addUserListener(newOption,'change',setUnlimited);

		D.replaceChild(newOption,document.getElementById(D.id + "_Day"));
	}
	
	//-------------------------------- 事件之Function ---------------------------------
	var Tchange = function (e){
		var is_ie = ((navigator.userAgent.toLowerCase().indexOf("msie") != -1) && (navigator.userAgent.toLowerCase().indexOf("opera") == -1));
		if(is_ie){
			//alert("["+ e.srcElement.value +"]=IE");
		}else{
			//alert("["+ e.target.value +"]=NOIE");
		}
		if(isShow_Day) UpDateDay('1');
	}
	
	var setUnlimited = function (e){
		document.getElementsByName(D.id + "_IsDate")[1].checked = "checked";
	}
	
	
	var addUserListener = function (eventObject,eventName,eventFunc){
		var is_ie = ((navigator.userAgent.toLowerCase().indexOf("msie") != -1) && (navigator.userAgent.toLowerCase().indexOf("opera") == -1));
		
		if(is_ie){
			eventObject.attachEvent('on'+eventName,eventFunc);
		}else{
			eventObject.addEventListener(eventName,eventFunc,false);
		}
	}
	
}