﻿
function formreset(form1) {
	form1.reset();
	return false;
}

//round to 2 decimal places
function round2decimalplaces(n) {

  ans = n * 1000
  ans = Math.round(ans /10) + ""
  while (ans.length < 3) {ans = "0" + ans}
  len = ans.length
  ans = ans.substring(0,len-2) + "." + ans.substring(len-2,len)
  return ans
} 

//get working day
function workingday(coverfrom,coverto) {
	var cday;
	if ( coverto.getTime() >= coverfrom.getTime() ) {
		cday=coverto.getTime() - coverfrom.getTime();
		cday=Math.floor(cday / (1000 *60*60*24));
		cday++;
	}
	else {
		cday=-1;
	}
	return cday;
}

//check age
function genage(birth,to) {

	var dday=0;
	var age=0;
	age =to.getFullYear()-birth.getFullYear();
	var now=new Date();
	if ( to.getTime() <= birth.getTime() ) {
		dday = -1;
	}
	else {
		if(to.getMonth() < birth.getMonth()) {
			age=age-1;
		}
		if((to.getMonth() == birth.getMonth()) && (to.getDate() < birth.getDate())) {
			age=age-1;
		}
		dday=age;
	}
	return dday;
}

// Pay within a day
function onceaday(tincome) {

	var tmpcont=new Array();
	var tmpercont=0;
	var tmpeecont=0;
	if(tincome < 130) {
		tmpercont=7.5;
		tmpeecont=0;
	}
	if((tincome >= 130) && (tincome < 260)) {
		tmpercont=7.5;
		tmpeecont=7.5;
	}
	if((tincome >= 260) && (tincome < 390)) {
		tmpercont=15;
		tmpeecont=15;
	}
	if((tincome >= 390) && (tincome < 520)) {
		tmpercont=22.5;
		tmpeecont=22.5;
	}
	if((tincome >= 520) && (tincome < 650)) {
		tmpercont=30;
		tmpeecont=30;
	}
	if(tincome >= 650) {
		tmpercont=30;
		tmpeecont=30;
	}

	tmpcont=[tmpercont,tmpeecont];
	return(tmpcont);
}


// Pay within a day (after 1 Feb 2003)
function onceaday2(tincome) {

	var tmpcont=new Array();
	var tmpercont=0;
	var tmpeecont=0;
	if(tincome < 160) {
		tmpercont=7.5;
		tmpeecont=0;
	}
	if((tincome >= 160) && (tincome < 260)) {
		tmpercont=7.5;
		tmpeecont=7.5;
	}
	if((tincome >= 260) && (tincome < 390)) {
		tmpercont=15;
		tmpeecont=15;
	}
	if((tincome >= 390) && (tincome < 520)) {
		tmpercont=22.5;
		tmpeecont=22.5;
	}
	if((tincome >= 520) && (tincome < 650)) {
		tmpercont=30;
		tmpeecont=30;
	}
	if(tincome >= 650) {
		tmpercont=30;
		tmpeecont=30;
	}

	tmpcont=[tmpercont,tmpeecont];
	return(tmpcont);
}

// Pay within a day (after 1 Nov 2011)
function onceaday3(tincome) {

	var tmpcont=new Array();
	var tmpercont=0;
	var tmpeecont=0;
	if(tincome < 250) {
		tmpercont=7.5;
		tmpeecont=0;
	}
	if((tincome >= 250) && (tincome < 260)) {
		tmpercont=13;
		tmpeecont=13;
	}
	if((tincome >= 260) && (tincome < 390)) {
		tmpercont=15;
		tmpeecont=15;
	}
	if((tincome >= 390) && (tincome < 520)) {
		tmpercont=22.5;
		tmpeecont=22.5;
	}
	if((tincome >= 520) && (tincome < 650)) {
		tmpercont=30;
		tmpeecont=30;
	}
	if(tincome >= 650) {
		tmpercont=30;
		tmpeecont=30;
	}

	tmpcont=[tmpercont,tmpeecont];
	return(tmpcont);
}


// Pay not within a day
function moreday(cday,dincome,cont) {
	var tmpcont=new Array();

	var tmpercont=0;
	var tmpeecont=0;



	if(dincome < 130) {
		tmpercont=dincome*0.05*cday;
		tmpeecont=0;
	}
	if((dincome >= 130) && (dincome < 650)) {
		tmpercont=dincome*0.05*cday;
		tmpeecont=dincome*0.05*cday;
	}
	if(dincome >= 650) {
		tmpercont=cday*32.5;
		tmpeecont=cday*32.5;
	}
	tmpercont=cont[0]+tmpercont;
	tmpeecont=cont[1]+tmpeecont;
	tmpcont=[tmpercont,tmpeecont]
	return(tmpcont);
}


// Pay not within a day (after 1 Feb 2003)
function moreday2(cday,dincome,cont) {
	var tmpcont=new Array();

	var tmpercont=0;
	var tmpeecont=0;



	if(dincome < 160) {
		tmpercont=dincome*0.05*cday;
		tmpeecont=0;
	}
	if((dincome >= 160) && (dincome < 650)) {
		tmpercont=dincome*0.05*cday;
		tmpeecont=dincome*0.05*cday;
	}
	if(dincome >= 650) {
		tmpercont=cday*32.5;
		tmpeecont=cday*32.5;
	}
	tmpercont=cont[0]+tmpercont;
	tmpeecont=cont[1]+tmpeecont;
	tmpcont=[tmpercont,tmpeecont]
	return(tmpcont);
}

// Pay not within a day (after 1 Nov 2011)
function moreday3(cday,dincome,cont) {
	var tmpcont=new Array();

	var tmpercont=0;
	var tmpeecont=0;



	if(dincome < 250) {
		tmpercont=dincome*0.05*cday;
		tmpeecont=0;
	}
	if((dincome >= 250) && (dincome < 650)) {
		tmpercont=dincome*0.05*cday;
		tmpeecont=dincome*0.05*cday;
	}
	if(dincome >= 650) {
		tmpercont=cday*32.5;
		tmpeecont=cday*32.5;
	}
	tmpercont=cont[0]+tmpercont;
	tmpeecont=cont[1]+tmpeecont;
	tmpcont=[tmpercont,tmpeecont]
	return(tmpcont);
}


function checknum(val){
	var num = parseFloat(val);
	if (val != ''+num) {
		return true;
	}
}

function checkday(val,mth,year) {
	var num = parseFloat(val);
	var mthnum = parseFloat(mth);
	var yearnum = parseFloat(year);
	if(val.length > 2) {
		return true;
	}
	else {
		if(val.length == 2) {
			if(mthnum == 4 || mthnum == 6 || mthnum == 9 || mthnum == 11) {
				if(val < "01" || val > "30") {
					return true;
				}
			}
			else {
			  if(mthnum == 2) {
			  	mod = yearnum % 4;
				if(mod == 0) {
					if(val < "01" || val > "29") {
						return true;
					}
				}
				else {
					if(val < "01" || val > "28") {
						return true;
					}
			  	}
			  }
			  else {
				if(val < "01" || val > "31") {
					return true;
				}
			  }
			 }
		}
		else {
			if((val != ''+num) || (num<1) || (num >31)) {
				return true;
			}
		}
	}
}

function checkmth(val) {

	if( val != null )
	{
	var num = parseFloat(val);
	if((val.length > 2)) {
		return true;
	}
	else {
		if(val.length == 2) {
			if(val < "01" || val > "12") {
				return true;
			}
		}
		else {
		if((val != ''+num) || (num<1) || (num >12)) {
				return true;
			}
		}
	}
}

}

function checkyear(val) {
	var num = parseFloat(val);
	if((val.length != 4) || (val != ''+num)) {
		return true;
	}
}

// Check whether string s is empty.

function isEmpty(s) {
	return ((s == null) || (s.length == 0))
}

// Returns true if string s is empty or
// whitespace characters only.
function isWhitespace(s) {
    var i;
    var whitespace = " \t\n\r";

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

// *.??
function pad(val){
	var newval=val*100;
	if((newval - parseInt(newval)) >= 0.5) {
		newval=newval+1;
	}
	newval=parseInt(newval)/100;
	return newval;
}

function minusmonth(val)
{
	var returnval = 0;
	
	if( val == "" )
		return val - 1;
	if( val.substring(0,1) == 0 )
	{
		returnval = ( val.substring(1,2)*1 - 1 );
		return "0" + returnval;
	}else
	{
		returnval = val*1 - 1;
		return "" + returnval;
	}

}

function checkform(form1,cday,aday,age) {
	var msg="";
	
	if(isWhitespace(form1.dateofbirth.value) || isWhitespace(form1.coverfrom.value) || isWhitespace(form1.coverto.value) || isWhitespace(form1.tincome.value)) {
		msg = msg+'- 所有資料必須填寫\n';
	}
	if(checknum(form1.tincome.value)) {
		msg = msg+'- 必須填寫數字\n';
	}
	
	var dateofbirthorg = "";
	var coverfromorg = "";
	var covertoorg = "";
	var paydayorg = "";
	
	dateofbirthorg = form1.dateofbirth.value == "" ? "--" : form1.dateofbirth.value;
	coverfromorg = form1.coverfrom.value == "" ? "--" : form1.coverfrom.value;
	covertoorg = form1.coverto.value == "" ? "--" : form1.coverto.value;
	paydayorg = form1.payday.value == "" ? "--" : form1.payday.value;
	
	var dateofbirth=dateofbirthorg.split("-");
	var coverfrom=coverfromorg.split("-");
	var coverto=covertoorg.split("-");
	var payday=paydayorg.split("-");
	
	var birthday=dateofbirth[0];
	var birthmth=dateofbirth[1];
	var birthyear=dateofbirth[2];

	var cfromday=coverfrom[0];
	var cfrommth=coverfrom[1];
	var cfromyear=coverfrom[2];

	var ctoday=coverto[0];
	var ctomth=coverto[1];
	var ctoyear=coverto[2];

	var paidday=payday[0];
	var paidmth=payday[1];
	var paidyear=payday[2];
	
    		if(checkday(birthday,birthmth,birthyear)||checkday(cfromday,cfrommth,cfromyear)|| checkday(ctoday,ctomth,ctoyear)|| checkday(paidday,paidmth,paidyear) ) {
		msg = msg+'- 日期不正確\n';
	}
	if(checkmth(birthmth)|| checkmth(cfrommth)|| checkmth(ctomth)|| checkmth(paidmth)) {
		msg = msg+'- 月份不正確\n';
	}
	if(checkyear(birthyear)|| checkyear(cfromyear)|| checkyear(cfromyear)|| checkyear(paidyear)) {
		msg = msg+'- 年份不正確\n';
	}
	if(cfromyear < 2000) {
		msg = msg+'- 蓋括日期必須於2000年12月1日之後\n';
	}
	else {
		if((cfromyear == 2000) && (cfrommth.value < 12)) {
			msg = msg+'- 蓋括日期必須於2000年12月1日之後\n';
		}
	}
	if(form1.tincome.value <0) {
		msg = msg+'- 有關入息總數必須大於零\n';
	}

	if(cday == -1) {
		msg = msg+'- 蓋括日期由必須早於蓋括日期至\n';
	}
	if(age==-1) {
		msg=msg+'- 出生日期必須早於蓋括日期\n';
	}
	/*
	if((age >= 65) || (age < 18) || (aday <= 0)) {
		msg=msg+'- Age should be < 65 and >=18.\n';
	}
	*/

	return msg;
}

function reset(form)
{
	form.dateofbirth.value="";
	form.coverfrom.value="";
	form.coverto.value="";
	form.payday.value="";
	form.tincome.value="";
	
	document.all.coverday.innerHTML = "0";
	document.all.employercon.innerHTML = "0";
	document.all.employeecon.innerHTML = "0";
}


function cont_cal(form1) {
	var msg="";
	
	var dateofbirthorg = "";
	var coverfromorg = "";
	var covertoorg = "";
	var paydayorg = "";
	
	dateofbirthorg = form1.dateofbirth.value == "" ? "--" : form1.dateofbirth.value;
	coverfromorg = form1.coverfrom.value == "" ? "--" : form1.coverfrom.value;
	covertoorg = form1.coverto.value == "" ? "--" : form1.coverto.value;
	paydayorg = form1.payday.value == "" ? "--" : form1.payday.value;
	
	var dateofbirth=dateofbirthorg.split("-");
	var coverfrom=coverfromorg.split("-");
	var coverto=covertoorg.split("-");
	var payday=paydayorg.split("-");
	
	if (dateofbirth.length != 3) {
		msg = msg+'- 出生日期不正確\n';
    }
	if (coverfrom.length != 3) {
		msg = msg+'-涵蓋開始日期不正確\n';
    }
	if (coverto.length != 3) {
		msg = msg+'-涵蓋結束日期不正確\n';
    }
	if (payday.length != 3) {
		msg = msg+'- 支薪日期不正確\n';
    }
    
	if (msg == "") {
		// Input value from form1
		var birthday=dateofbirth[0];
		//var birthmth=dateofbirth[1];
		var birthmth=minusmonth(dateofbirth[1]);
		var birthyear=dateofbirth[2];
		
		var cfromday=coverfrom[0];
		//var cfrommth=coverfrom[1];
		var cfrommth=minusmonth(coverfrom[1]);
		var cfromyear=coverfrom[2];
		
		var ctoday=coverto[0];
		//var ctomth=coverto[1];
		var ctomth=minusmonth(coverto[1]);
		var ctoyear=coverto[2];
		
		var paidday=payday[0];
		//var paidmth=payday[1];
		var paidmth=minusmonth(payday[1]);
		var paidyear=payday[2];
	
		var tincome=form1.tincome.value;
	
		//Set Date
		var dateofbirth=new Date(birthyear,birthmth,birthday,0,0,0);
		var coverfrom=new Date(cfromyear,cfrommth,cfromday,0,0,0);
		var coverto=new Date(ctoyear,ctomth,ctoday,0,0,0);
		var paiddate=new Date(paidyear,paidmth,paidday,0,0,0);
		
		//new 20040813
		var pday = workingday(coverfrom,coverto);
		var cday1 = 0;
		var cday2 = workingday(coverfrom,coverto);
		
	
		//Age
		var age;
		var minage=18;
		var maxage=65;
	
		//begin calculate
		var cont = new Array;
		cont=[0,0];
	
	
		var allday = cday2;
		var cday = cday2;
		var coverfromage = genage(dateofbirth,coverfrom);
		var covertoage = genage(dateofbirth,coverto);
		var age = genage(dateofbirth,coverto);
		var dincome = 0;
		//covertoage < 18 || coverfromage > 65
		if(covertoage < minage || coverfromage >= maxage) {
			cont=[0,0];
		}
		else {
			//one day
			if(cday == 1) {							
				//before 01-Feb-2003
				if (coverfrom < new Date(2003,1,1,0,0,0)) {					
					cont = onceaday(tincome);
				}								
				//from 01-Feb-2003 and before 01-Nov-2011
				else if ((coverfrom >= new Date(2003,1,1,0,0,0)) && (coverfrom < new Date(2011,10,1,0,0,0))) {					
					cont = onceaday2(tincome);
				}
				//from 01-Nov-2011 
				else {					
					cont = onceaday3(tincome);
				}			
			}
			//more than one cover day
			else {		
				//before 01-Feb-2003
				if (coverfrom < new Date(2003,1,1,0,0,0)) {										
		  			dincome = tincome / cday;
					cont = moreday(cday,dincome,cont);										
				}								
				//from 01-Feb-2003 and before 01-Nov-2011
				else if ((coverfrom >= new Date(2003,1,1,0,0,0)) && (coverfrom < new Date(2011,10,1,0,0,0))) {					
		  			dincome = tincome / cday;
		  			cont = moreday2(cday,dincome,cont);
				}
				//from 01-Nov-2011 
				else {					
		  			dincome = tincome / cday;
					cont = moreday3(cday,dincome,cont);
				}
			}
		}
		//check form
		msg=checkform(form1,cday,allday,age);
	}
	if(msg != "") {
		alert(msg);
		document.all.coverday.innerHTML = "";
		document.all.employercon.innerHTML = "";
		document.all.employeecon.innerHTML = "";
//		return false;
	}else
	{
	//Error : set all value be 0
		if((cont[0] == 0) && (cont[1] == 0)) {
			if(msg != "") document.all.coverday.innerHTML = 0;
			else document.all.coverday.innerHTML = cday;
			document.all.employercon.innerHTML = 0;
			document.all.employeecon.innerHTML = 0;
	//	return false;
		}
		else {
			cday=allday;
			document.all.coverday.innerHTML = cday;
			document.all.employercon.innerHTML = round2decimalplaces(cont[0]);
			document.all.employeecon.innerHTML = round2decimalplaces(cont[1]);
		}
	}
	//return false;
}




