﻿
function y2k(number)
{return (number<1000) ? number+1900 : number;}
 
function checkDateExist(day, month, year)
{
 var today=new Date();
 year=((!year) ? y2k(today.getYear()):year);
 month=((!month) ? today.getMonth():month-1);
 
 if(!day){return false;}
 var test=new Date(year,month,day);
 if((y2k(test.getYear()) == year) && (month == test.getMonth()) && (day == test.getDate()))
 {return true;}
  else
 {return false;}
}
//判斷日期 1981/12/02 
function checkDate()
{
	if (checkDate.arguments.length==1){
		myDate=document.getElementById(checkDate.arguments[0]).value;
	}else{
		myDate=document.getElementById(checkDate.arguments[0]).value;
		for (i=1; i<3; i++){
			if (document.getElementById(checkDate.arguments[i]).value.length==1){
				myDate=myDate+"/0"+document.getElementById(checkDate.arguments[i]).value;
			}else{
				myDate=myDate+"/"+document.getElementById(checkDate.arguments[i]).value;
			}
		}
	}
	var datePattern=/^\d{4}\/(0[1-9]|1[0-2])\/(3[0-1]|[0-2][0-9])$/;
	if(myDate.match(datePattern))
	{
		if(checkDateExist(myDate.substring(8, 10), myDate.substring(5, 7), myDate.substring(0, 4))==true){return 0;}
		else{alert("日期格式有誤!");return 1;}//日期錯
	}
	else//格式錯
		{alert("日期格式有誤!");return 2;}
}

//判斷有沒有輸入
function checkIn(){
	for (i=0; i<checkIn.arguments.length; i++){
		if (document.getElementById(checkIn.arguments[i]).value==""){
			alert('請'+document.getElementById(checkIn.arguments[i]).title+'!!');
			document.getElementById(checkIn.arguments[i]).focus();
			return 1;
		}
	}
	return 0;
}

//判斷檔案格式
function checkFile(fname){
	filename=document.getElementById(fname).value;
	txt = filename.substring(filename.length-4,filename.length);
	if (txt!='.jpg'&&txt!='.JPG'&&txt!='.doc'&&txt!='.DOC'&&txt!='docx'&&txt!='DOCX'&&txt!='.xls'&&txt!='.XLS'&&txt!='xlsx'&&txt!='XLSX'&&txt!='.ppt'&&txt!='.PPT'&&txt!='pptx'&&txt!='PPTX'&&txt!='.pdf'&&txt!='.PDF'){
		alert("不支援上傳此檔案格式!");
		return 1;
	}
	return 0;
}

//判斷檔案PIC
function checkPic(){
	for (i=0; i<checkPic.arguments.length; i++){
		filename=document.getElementById(checkPic.arguments[i]).value;
		txt = filename.substring(filename.length-3,filename.length);
		if (txt!='jpg'&&txt!='JPG'&&txt!='gif'&&txt!='GIF'){
			alert("圖片格式需為 'JPG' 或 'GIF' !");
			return 1;
		}
	}	
	return 0;
}


//判斷有沒有一樣
function checkSame(fname1,fname2,fmsg){
	if (document.getElementById(fname1).value != document.getElementById(fname2).value){
		alert("輸入的"+fmsg+"不同,請重新輸入!!");
		document.getElementById(fname1).value="";
		document.getElementById(fname2).value="";
		document.getElementById(fname1).focus();
		return 1;
	}
	return 0;
}

//判斷是不是Mail
function checkMail(){
	for (i=0; i<checkMail.arguments.length; i++){
		eml = document.getElementById(checkMail.arguments[i]).value;
		if (eml.match(/@.*@/) || !eml.match(/@{1}/)){
			alert('E-mail格式有誤,請重新輸入!!');
			document.getElementById(checkMail.arguments[i]).value="";
			document.getElementById(checkMail.arguments[i]).focus();
			return 1;
		}
	}
	return 0;
}

//判斷是不是數字
function checkNum(){
	for (i=0; i<checkNum.arguments.length; i++){
		mData = document.getElementById(checkNum.arguments[i]).value;
		if (isNaN(mData)){
			alert('格式有誤,請重新輸入!!\n請'+document.getElementById(checkNum.arguments[i]).title+'!!');
			document.getElementById(checkNum.arguments[i]).value="";
			document.getElementById(checkNum.arguments[i]).focus();
			return 1;
		}
	}
	return 0;
}

//判斷點選哪一個
function getChecked(tName,num){
	j=0;
	for (i=1;i<=num;i++){
		if (document.getElementById(tName+i).checked==true){j=i;break;}
	}
	return j;
}

//value to Html 多個
function VInHM(){
	for (i=0; i<VInHM.arguments.length; i++){
		document.getElementById('see'+VInHM.arguments[i]).innerHTML=document.getElementById(VInHM.arguments[i]).value;
	}
}

//value to Html 單個
function VInHO(fName,tName){
		document.getElementById(tName).innerHTML=document.getElementById(fName).value;
}

//value to value 多個
function VInVM(){
	for (i=0; i<VInVM.arguments.length; i++){
		document.getElementById('see'+VInVM.arguments[i]).value=document.getElementById(VInVM.arguments[i]).value;
	}
}

//value to value 單個
function VInVO(fName,tName){
	document.getElementById(tName).value=document.getElementById(fName).value;
}

//看不見
function disNone(fName){
	document.getElementById(fName).style.display="none";
}

//看得見
function disShow(fName){
	document.getElementById(fName).style.display="";
}

//變換看見
function changeDis(fName){
	if(document.getElementById(fName).style.display==""){
		disNone(fName);
	}else{
		disShow(fName);
	}
}

//上架或下架
function on_changeAble(id,type,thref,tmsg){
	var check = confirm('您確定要'+tmsg+'!');
    if(check) {
		location.href= ('changeAble.asp?type='+type+'&tname='+thref+'&id='+id);
	}
}

//字串取代
function ReplaceAll(strSource, strFind, strRepl) {
    var str5 = new String(strSource);
    while (str5.indexOf(strFind) != -1) {
          str5=str5.replace(strFind, strRepl);
    }
   return str5;
} 

//取亂數4碼 0~9
function get_rnd(){
	var code;
	code = "";
	for (i=0; i<4; i++){
		var x,y;
		x=Math.random()*9;
		y=Math.floor(x);//無條件捨去後面的小數
		code += y ;
	}
	return code;
}

















