//design by ascent.
if(is_ie == undefined) {
	var agt = navigator.userAgent.toLowerCase();
	var is_ie = (agt.indexOf("msie") != -1);
	var is_ff = (agt.indexOf("firefox") != -1);
	var is_sf = (agt.indexOf("safari") != -1);
	var cssClassName = (is_ie) ? "className" : "class" ;
}

var now = new Date();
var now_date = now;
var year = (is_ie) ? now.getYear() : now.getUTCFullYear() ;

var month = now.getMonth()+1;
if(month <= 9) month = "0"+month;

var day = now.getDate();
if(day <= 9) day = "0"+day;

var hours = now.getHours();
if(hours <= 9) hours = "0"+hours;
var now_hours = hours;

var minutes = now.getMinutes();
if(minutes <= 9) minutes = "0"+minutes;

var seconds = now.getSeconds();
if(seconds <= 9) seconds = "0"+seconds;

if (is_ie) {
	var selector_year_start = now_date.getYear() - 5;
	var selector_year_stop = now_date.getYear() + 1;
	var now_year = now_date.getYear();
	var end_year = now_date.getYear() + 3;
} else {
	var selector_year_start = now_date.getUTCFullYear() - 5;
	var selector_year_stop = now_date.getUTCFullYear() + 1;
	var now_year = now_date.getUTCFullYear();
	var end_year = now_date.getUTCFullYear() + 1;
}

var now_month = now_date.getMonth()+1;
//if (now_month <= 9) {now_month = "0"+now_month;}
var now_day = now_date.getDate();
//if (now_day <= 9) now_day = "0"+now_day;

var upvalue = year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;

var YearStart = year;
var YearEnd = year+2;

//20070729 ascent
function getFutureDate(nDay) {
	var futudate = new Date();
	nDay = (nDay > 0) ? nDay : 0 ;
	futudate.setDate(futudate.getDate()+nDay);
	var fYear = (is_ie) ? futudate.getYear() : futudate.getUTCFullYear();
	var fMonth = futudate.getMonth()+1;
	var fDay = futudate.getDate();
	var fHour = futudate.getHours();
	var fMinutes = futudate.getMinutes();
	var fSec = futudate.getSeconds();
	//var op = [fYear,fMonth,fDay,fHour,fMinutes,fSec];
	var op = {"year":fYear,"month":fMonth,"day":fDay,"hours":fHour,"minutes":fMinutes,"seconds":fSec};
	return op;
}


function checkDateFormat(DateValue){
	var exmple = "\n\n格式範例：\n0000-00-00 00:00:00\n\n年－月－日　時：分：秒";
	var lend = DateValue.length;
	var msg = "";

	if(DateValue.charAt(4) != "-"){
		msg = "年與月中間需有－符號"+exmple;
	}else if(DateValue.charAt(7) != "-"){
		msg = "月與日中間需有－符號"+exmple;
	}else if(DateValue.charAt(10) != " "){
		msg = "日期與時間中間需有空格"+exmple;
	}else if(DateValue.charAt(13) != ":"){
		msg = "小時與分鐘中間需有：符號"+exmple;
	}else if(DateValue.charAt(16) != ":"){
		msg = "分鐘與秒鐘中間需有：符號"+exmple;
	}else{
		var isec = DateValue.substring(17,19);
		var imin = DateValue.substring(14,16);
		var ihour = DateValue.substring(11,13);
		var iday = DateValue.substring(8,10);
		var imonth = DateValue.substring(5,7);
		var iyear = DateValue.substring(0,4);

		if(isec.length == 2){
			if(checkIntFormat(1,isec)){msg = "秒數格式不對";}
		}else{
			msg = "秒數字數不對";
		}

		if(imin.length == 2){
			if(checkIntFormat(1,imin)){msg = "分數格式不對";}
		}else{
			msg = "分數字數不對";
		}

		if(ihour.length == 2){
			if(checkIntFormat(1,ihour)){msg = "時數格式不對";}
		}else{
			msg = "時數字數不對";
		}

		if(iday.length == 2){
			if(checkIntFormat(1,iday)){msg = "天數格式不對";}
		}else{
			msg = "天數字數不對";
		}

		if(imonth.length == 2){
			if(checkIntFormat(1,imonth)){msg = "月數格式不對";}
		}else{
			msg = "月數字數不對";
		}

		if(iyear.length == 4){
			if(checkIntFormat(3,iyear)){msg = "年數只能是數字";}
		}else{
			msg = "年數字數不對";
		}
	}

	if(msg != ""){
		return msg;
	}else{
		return false;
	}
}

function checkIntFormat(intTotal,iValue){
	for(var i=0;i<intTotal;i++){
		var ic = iValue.charAt(i)
		if(!(ic >= "0" && ic <= "9")){return true;}
	}
}

function CaricaFoto(img,pixURL){
	var img = pixURL+img;
  foto1 = new Image();
  foto1.src = (img);
  Controlla(img);
}

function Controlla(img){
  if((foto1.width!=0)&&(foto1.height!=0)){
    viewFoto(img);
  }else{
  	if(navigator.platform.indexOf('Win') > -1){
    	funzione = "Controlla('"+img+"')";
    	intervallo = setTimeout(funzione,20);
  	}else{
	  	foto1.width = 280;
	  	foto1.height = 280;
	  	viewFoto(img);
  	}
  }
}

function viewFoto(img){
	winfilter = ((navigator.platform.indexOf('Win') > -1) ? ",scrollbars=no,resizable=no" : ",scrollbars=yes,resizable=yes");
	var largh = foto1.width+20;
	var altez = foto1.height+20;
	var stringa = "width="+largh+",height="+altez+winfilter;
	finestra = window.open(img,"ViewImages",stringa);
	finestra.focus();
}

function editFontFilters(iFilter,iField){
	var iValue = document.editor[iField].value;

	switch(iFilter){
		case "allb":
			document.editor[iField].value = "[b]" + iValue + "[/b]";
			document.editor[iField].focus();		
			break;

		case "b":
			document.editor[iField].value = iValue + "[b][/b]";
			document.editor[iField].focus();		
			break;

		case "i":
			document.editor[iField].value = iValue + "[i][/i]";
			document.editor[iField].focus();		
			break;

		case "url":
			document.editor[iField].value = iValue + "[url=http://]相關連結[/url]";
			document.editor[iField].focus();		
			break;

		case "img":
			document.editor[iField].value = iValue + "[img]http://[/img]";
			document.editor[iField].focus();		
			break;
	}
}

function number_select_bar(Name,Start,Ends,Default){
	var bar = "<select name="+Name+">\n";
	var ed;
	for(i=Start;i<Ends;i++){
		if(i == Default){ed = " SELECTED";}else{ed = false;}
		if(i <= 9){i = "0"+i;}
		bar += "<option value=\""+i+"\""+ed+">"+i+"</option>\n"; 
	}
	bar += "</select>\n";
	document.write(bar);
}


//20070729 ascent
function makeSelectorNumbers(elem,sName,Start,Ends,sDefault,optionHead,optionFoot) {
	/* 20070729 by ascent */
	var d = document; var j = (optionHead == undefined) ? 0 : 1;
	var selector = d.createElement("select");
	selector.setAttribute("id",sName);
	selector.setAttribute("name",sName);
	if(j > 0) {
		selector.options[0] = new Option(optionHead[1], optionHead[0]);
		if(optionHead[2] == "selected"){selector.options[j].selected = true;}
	}
	for(var i=Start;i<Ends;i++){
		selector.options[j] = new Option(i, i);
		if(i == sDefault){selector.options[j].selected = true;}
		j++;
	}
	if(optionFoot != undefined) {
		selector.options[j] = new Option(optionFoot[1], optionFoot[0]);
		if(optionFoot[2] == "selected"){selector.options[j].selected = true;}
	}
	d.getElementById(elem).appendChild(selector);
}


function checkDateFormatII(editform){
	var thisdoor;
	var ad_year_start = editform.ad_year_start.value;
	var ad_year_end = editform.ad_year_end.value;
	
	var ad_month_start = editform.ad_month_start.value;
	var ad_month_end = editform.ad_month_end.value;
	
	var ad_day_start = editform.ad_day_start.value;
	var ad_day_end = editform.ad_day_end.value;
	
	var ad_hour_start = editform.ad_hour_start.value;
	var ad_hour_end = editform.ad_hour_end.value;
	
	var ad_min_start = editform.ad_min_start.value;
	var ad_min_end = editform.ad_min_end.value;

	if(ad_year_end == ad_year_start){
		if(ad_month_end == ad_month_start){
			if(ad_day_end == ad_day_start){
				if(ad_hour_end == ad_hour_start){
					if(ad_min_end == ad_min_start){
						thisdoor = "close";
						msg = _MSG_SAME;
						focusobj = "ad_min_end";			
					}else if(ad_min_end < ad_min_start){
						thisdoor = "close";
						msg = _MSG_MINUTE;	
						focusobj = "ad_min_end";			
					}
				}else if(ad_hour_end < ad_hour_start){
					thisdoor = "close";
					msg = _MSG_HOUR;	
					focusobj = "ad_hour_end";			
				}
			}else if(ad_day_end < ad_day_start){
				thisdoor = "close";
				msg = _MSG_DAY;	
				focusobj = "ad_day_end";			
			}
		}else if(ad_month_end < ad_month_start){
			thisdoor = "close";
			msg = _MSG_MONTH;	
			focusobj = "ad_month_end";			
		}
	}else if(ad_year_end < ad_year_start){
		thisdoor = "close";
		msg = _MSG_YEAR;	
		focusobj = "ad_year_end";
	}

	if(thisdoor == "close"){
		return msg;
	}else{
		return false;
  }
}

function isInt(elm){
	var pattern = /[^0-9]/;
	if(pattern.test(elm)){
		return false;
	}else{
		return true;
	}
}

function isAlpha(elm){
	var pattern = /[^a-zA-Z]/;
	if(pattern.test(elm)){
		return false;
	}else{
		return true;
	}
}

function isAlphaAndInt(elm){
	var pattern = /[^a-zA-Z0-9]/;
	if(pattern.test(elm)){
		return false;
	}else{
		return true;
	}
}

function isAlphaAndIntaAndPointLine(elm){
	var pattern = /[^a-zA-Z0-9\.\_]/;
	if(pattern.test(elm)){
		return false;
	}else{
		return true;
	}
}

function denyAccountWord(elm){
	var pattern = /[admin]/;
	if(pattern.test(elm)){
		return false;
	}else{
		return true;
	}
}

function checkIDN(elm){
	var pattern = /[^a-zA-Z]/;
	if ( pattern.test(elm.charAt(0)) ) {
		return false;
	} else {
		return true;
	}
}

function checkDateFormatIII(editform){
	var thisdoor;
	var year_start = editform.year_start.value;
	var year_end = editform.year_end.value;
	
	var month_start = editform.month_start.value;
	var month_end = editform.month_end.value;
	
	var day_start = editform.day_start.value;
	var day_end = editform.day_end.value;
	
	var hour_start = editform.hour_start.value;
	var hour_end = editform.hour_end.value;
	
	var min_start = editform.min_start.value;
	var min_end = editform.min_end.value;

	if(year_end == year_start){
		if(month_end == month_start){
			if(day_end == day_start){
				if(hour_end == hour_start){
					if(min_end == min_start){
						thisdoor = "close";
						msg = _MSG_SAME;
						focusobj = "min_end";			
					}else if(min_end < min_start){
						thisdoor = "close";
						msg = _MSG_MINUTE;	
						focusobj = "min_end";			
					}
				}else if(hour_end < hour_start){
					thisdoor = "close";
					msg = _MSG_HOUR;	
					focusobj = "hour_end";			
				}
			}else if(day_end < day_start){
				thisdoor = "close";
				msg = _MSG_DAY;	
				focusobj = "day_end";			
			}
		}else if(month_end < month_start){
			thisdoor = "close";
			msg = _MSG_MONTH;	
			focusobj = "month_end";			
		}
	}else if(year_end < year_start){
		thisdoor = "close";
		msg = _MSG_YEAR;	
		focusobj = "year_end";
	}

	if(thisdoor == "close"){
		return msg;
	}else{
		return false;
  }
}

function OnOpen(path){
	features = "toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=no,height=500,width=350";
	window.open(path,"Info",features);
}

function OnOpenS(path,ht,wh){
	features = "toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,height="+ht+",width="+wh;
	window.open(path,"Info",features);
}

//2002-12-18 by ascent
function acctionButton(thisform) {
	var buttonOutput;
	var action = thisform.action.value;
	var resetValue = (action == 'add') ? '清除欄位' : '回覆欄位';

	buttonOutput = '<input type="submit" name="Submit" value="送出"> ';
	buttonOutput += '<img src="0.gif" width=16 height=1 align="absmiddle"> ';
	buttonOutput += '<input name="reset" type="reset" value="'+resetValue+'"> ';
	buttonOutput += '<input type="button" name="back" value="回上一頁" onClick="window.history.go(-1);">';

	return buttonOutput;
}


function setCheckboxes(the_form, do_check)
{
    var elts      = (typeof(document.forms[the_form].elements['addid[]']) != 'undefined')
                  ? document.forms[the_form].elements['addid[]']
                  : (typeof(document.forms[the_form].elements['deleter[]']) != 'undefined')
          ? document.forms[the_form].elements['deleter[]']
          : document.forms[the_form].elements['addid[]'];

    var elts_cnt  = (typeof(elts.length) != 'undefined')
                  ? elts.length
                  : 0;

    if (elts_cnt) {
        for (var i = 0; i < elts_cnt; i++) {
            elts[i].checked = do_check;
        } // end for
    } else {
        elts.checked        = do_check;
    } // end if... else

    return true;
} // end of the 'setCheckboxes()' function


//20070801
function field_focus_alert (msg,who) {
	alert(msg);
	field_style_alert(who);
}

function field_style_alert (who) {
	who.style.backgroundColor = '#FFCC99';
	who.style.border = '1px solid #FF0000';
	who.style.padding = '2px';
	who.focus();
} 

function field_style_reset (who) {
	who.style.backgroundColor = '#ffffff';
	who.style.border = '1px solid #A5ACB2';
	who.style.padding = '2px';
}

//20070801
function getSelectedValue(k) {
	return k.options[k.selectedIndex].value;
}

function getCheckedValue(radioObj) {
	if(!radioObj) {return false;}
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		if(radioObj.checked) {
			return radioObj.value;
		} else {
			return false;
		}
	}
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return false;
}
