//
//	JavaScript Business Functions 1
//
//	Copyright 1996, by Hiro Nakamura.  All rights Reserved.  <<nhiro@itechlabo.com>>
//	It is prohibited to reproduction or to use this script without the authorization.
//	著作権は全て中村 裕明に帰属します。このプログラムの複製及び無断使用を禁止します。

// ====== 	Function:	numeric check	only the numeric values are OK
//		Parm:		p_val		Check value
//		Return value:	If numeric --> True,  Not numeric --> False
//
function numericCheck(p_val){
	if (p_val == ""){return(false);}
	for(i=0; i<p_val.length; i++){
		if (p_val.substring(i, i+1)<"0" || p_val.substring(i, i+1)>"9"){
			return(false);
		}
	}
	return(true);
}

// ====== 	Function:	numeric check 2		numeric values and \/-/,/. are OK
//		Parm:		p_val			Check value
//		Return value:	If numeric --> True,  Not numeric --> False
//
function numericCheck2(p_val){
	if (p_val == ""){return(false);}
	for(i=0; i<p_val.length; i++){
		if ((p_val.substring(i, i+1)>="0" && p_val.substring(i, i+1)<="9") || (p_val.substring(i, i+1)=="\\") || (p_val.substring(i, i+1)==",") || (p_val.substring(i, i+1)=="-") || (p_val.substring(i, i+1)==".")){
			continue;
		} else {
			return(false);
		}
	}
	return(true);
}

// ====== 	Function:	check date
//		Parm:		p_val		Check value (YYMMDD/YYYYMMDD)
//		Return value:	valid date--> YYYYMMDD,  invalid date --> False
//				YY > 50, 1900	YY <= 50, 2000	
//
function checkDate(p_val){
	var v_yyyy;
	var v_mm;
	var v_dd;

	if (p_val.length == 8){				//YYYYMMDD --> YYMMDD
		p_val = p_val.substring(2, 8);
	}
	if (p_val.length == 7){				//YYYMMDD --> YYMMDD
		p_val = p_val.substring(1, 7);
	}

	if (numericCheck(p_val) == false){return(false);}	// not numeric
	v_yyyy = parseInt(p_val.substring(0, 2),10);
	if (v_yyyy > 50){
		v_yyyy = v_yyyy + 1900;
	} else {
		v_yyyy = v_yyyy + 2000;
	}
	v_mm = parseInt(p_val.substring(2, 4),10);
	v_dd = parseInt(p_val.substring(4, 6),10);
	if ((isNaN(v_mm)) || (isNaN(v_dd))){return(false);}
	if ((v_mm < 1) || (v_mm > 12)){return(false);}		// invalid month
	if ((v_mm == 1) || (v_mm == 3) || (v_mm == 5) || (v_mm == 7) || (v_mm == 8) || (v_mm == 10) || (v_mm == 12)){
		if ((v_dd < 1) || (v_dd > 31)){return(false);}	// invalid date
	} else {
		if ((v_dd < 1) || (v_dd > 30)){return(false);}	// invalid date
	}
	if (v_mm == 2){						// check leap year
		if ((v_yyyy % 400 == 0) || ((v_yyyy % 4 == 0) && (v_yyyy % 100 != 0))){
			if (v_dd > 29){return(false);}		// invalid date, leap year
		} else {
			if (v_dd > 28){return(false);}		// invalid date, not leap year
		}
	}	
	return(v_yyyy + p_val.substring(2, 4) + p_val.substring(4, 6));
}

// ====== 	Function:	Compare Numbers		This function is used by sort method of array.
//		Parm:		none
//		Return value:	none
//
function compareAsce(a, b){			// for ascending sort
	return a - b;
}

function compareDesc(a, b){			// for descending sort
	return b - a;
}

// ====== 	Function:	editNumber	edit number based on the format parm
//		Parm:		p_format	1:-123,456.78  2:123,456.78-  3:\-123,456.78  4:\123,456.78-
//				p_number	number value -123456.78
//		Return value:	result
//
function editNumber(p_format, p_number){	
	var v_result = "";
	var v_offset = 0;	// 0:>=0, 1:<0
	var v_point;		// decimal point

	p_number = "" + parseFloat(p_number);		// eliminate spaces
	if (p_number < 0){v_offset = 1;}		// check if negative 
	v_point = p_number.indexOf(".", 0);		// search decimal point

	if (v_point >=0){
		v_result = p_number.substring(v_point, p_number.length);
		v_point = v_point - 1;
	} else {
		v_point = p_number.length - 1;
	}
	
	for (var i=v_point; i>=v_offset;){
		for (var j=0; j<3; j++){
			v_result = p_number.charAt(i--) + v_result;
			if (i < v_offset){break;}
		}
		if (j == 3){v_result = "," + v_result;}
	}

	if (p_format == 1){				// -123,456
		if (v_offset == 1){v_result = "-" + v_result;}
	}
	if (p_format == 2){				// 123,456-
		if (v_offset == 1){v_result = v_result + "-";}
	}
	if (p_format == 3){				// \-123,456
		if (v_offset == 1){v_result = "-" + v_result;}
		v_result = "\\" + v_result;
	}
	if (p_format == 4){				// \123,456-
		if (v_offset == 1){v_result = v_result + "-";}
		v_result = "\\" + v_result;
	}
	return(v_result);
}

// ====== 	Function:	toNumber	convert edited number to number with format check -1,234.56, 1,234.56-
//						0 - 9 \ - , .
//		Parm:		p_string	edited number
//		Return value:	number: if the input value is OK	false: not a number
//
function toNumber(p_string){	
	var v_result = "";
	var v_offset;
	var v_point;		// decimal point

	if (p_string.charAt(0) == "\\"){p_string = p_string.substring(1, p_string.length);}// delete \ sign from the top
	v_offset = p_string.length;
	if (p_string.charAt(p_string.length - 1) == "-"){v_offset = p_string.length - 1;}// check if back sign 
	v_point = p_string.lastIndexOf(".", v_offset);		// search decimal point

	if (v_point >=0){
		v_result = p_string.substring(v_point + 1, v_offset);
		if (numericCheck(v_result) == true){
			v_result = "." + v_result;
			v_point = v_point - 1;
		} else {
			return(false)
		}
	} else {
		v_point = v_offset - 1;
	}
	
	for (var i=v_point; i>=0;){
		for (var j=0; j<3; j++){
			if ((numericCheck(p_string.charAt(i)) == true) || ((p_string.charAt(i) == "-") && (i == 0))){
				v_result = p_string.charAt(i--) + v_result;
			} else{
				return(false);			// illegal value
			}
			if (i < 0){break;}
		}
		if ((j == 3) && (p_string.charAt(i) == ",")){i = i - 1;}
	}
	if (v_offset == p_string.length - 1){		// back sign
		if (v_result.charAt(0) != "-"){
			v_result = "-" + v_result;
		} else {
			return(false);
		}
	}
	v_result = parseFloat(v_result);
	return(v_result);
}

// ====== 	Function:	editDate	edit date based on the format parm
//		Parm:		p_format	1:YY/MM/DD  2:YY年MM月DD日  3:YYYY/MM/DD  4:YYYY年MM月DD日
//				p_date		YYYYMMDD
//		Return value:	result
//
function editDate(p_format, p_date){	
	var v_result = "";

	if (p_format == 1){
		v_result = p_date.substring(2, 4) + "/" + p_date.substring(4, 6) + "/" + p_date.substring(6, 8);
	}
	if (p_format == 2){
		v_result = p_date.substring(2, 4) + "年" + p_date.substring(4, 6) + "月" + p_date.substring(6, 8) + "日";
	}
	if (p_format == 3){
		v_result = p_date.substring(0, 4) + "/" + p_date.substring(4, 6) + "/" + p_date.substring(6, 8);
	}
	if (p_format == 4){
		v_result = p_date.substring(0, 4) + "年" + p_date.substring(4, 6) + "月" + p_date.substring(6, 8) + "日";
	}
	return(v_result);
}

// ====== 	Function:	calcDate1	to date = from date + days   and day of the week
//		Parm:		p_from_date	from date	YYYYMMDD
//				p_day		number of days
//		Return value:	result		to date	+ day	YYYYMMDDd  d: 0 - 6 / sun - sat
//
function calcDate1(p_from_date, p_day){
	var v_yyyy;
	var v_mm;
	var v_dd;
	var v_day;
	var v_from_date = new Date(parseInt(p_from_date.substring(2,4),10), parseInt(p_from_date.substring(4,6),10) - 1, parseInt(p_from_date.substring(6,8),10));
	var v_to_date = new Date();

	v_to_date.setTime(v_from_date.getTime() + (parseInt(p_day,10) * 24 * 60 * 60 * 1000));

	if (v_to_date.getYear() < 2000){
		v_yyyy = v_to_date.getYear() + 1900;
	}
	v_mm = v_to_date.getMonth() + 1;
	if (v_mm < 10){v_mm = "0" + v_mm;}
	v_dd = v_to_date.getDate();
	if (v_dd < 10){v_dd = "0" + v_dd;}
	v_day = v_to_date.getDay();
	v_result  = "" + v_yyyy + v_mm + v_dd + v_day;
	return(v_result);
}

// ====== 	Function:	calcDate2	to date - from date = days
//		Parm:		p_from_date	from date	YYYYMMDD
//				p_to_date	to date		YYYYMMDD
//		Return value:	result		days
//
function calcDate2(p_from_date, p_to_date){
	var v_result;
	var v_from_date = new Date(parseInt(p_from_date.substring(0,4),10) - 1900, parseInt(p_from_date.substring(4,6),10) - 1, parseInt(p_from_date.substring(6,8),10));
	var v_to_date = new Date(parseInt(p_to_date.substring(0,4),10) - 1900, parseInt(p_to_date.substring(4,6),10) - 1, parseInt(p_to_date.substring(6,8),10));

	v_result = v_to_date.getTime() - v_from_date.getTime();
	v_result = parseInt(v_result / 24 / 60 / 60 / 1000, 10);
	return(v_result);
}

//
// ======	Function	unstring
//		Parm		unstring value, array for store the result, delimiter
//
function f_unstring(p_val, p_array, p_del){
	var v_start = 0;
	var v_end = 0;
	
	if (p_val == ""){
		p_array[0] ="";
		p_array.length = 1;
		return;
	}

	for(var i=0; ; i++){
		v_end = p_val.indexOf(p_del, v_start);
		if (v_end == -1){v_end = p_val.length;}
		p_array[i] = p_val.substring(v_start, v_end);
		v_start = v_end + p_del.length;
		if (v_end == p_val.length){
			p_array.length = i+1;
			break;
		}
	}
}

//
// ====== 	Function:	adjust decimal point
//		Parm:		p_val  : input value
//				p_point: decimal point
//
function decimal_point(p_val, p_point){
	var v_start;
	var v_ret_val;

	p_val = "" + p_val;
	v_start = p_val.indexOf(".", 0);
	if (v_start == -1){
		v_start = p_val.length;
	}
	v_ret_val = p_val.substring(0, v_start) + "."	// get integaer portion
	v_start += 1;

	for (var i=0; i<p_point; i++){
		if (v_start < p_val.length){
			v_number = p_val.substring(v_start, v_start+1);
		} else {
			v_number = 0;
		}
		v_ret_val += "" + v_number;
		v_start++
	}
	return v_ret_val;
}

//
// ======	Function	search_array
//		Parm		p_array:	array which will be searched
//				p_start:	index for array to search
//				p_string:	search string
//
function search_array(p_array, p_start, p_string){

	if (p_array == ""){return -1;}		// if no data return

	for(var i=p_start; i<p_array.length ; i++){
		if (p_array[i] == p_string){
			return i;
		}
	}
	return -1;
}

//
// ======	Function	replace_string
//		Parm		p_string:	string which will be searched
//				p_old:		search value
//				p_new:		replace value
//
function replace_string(p_string, p_old, p_new){
	var v_start = 0;
	var v_end = 0;
	var v_new_string = "";

	for(; v_end != p_string.length; ){
		v_end = p_string.indexOf(p_old, v_start);			// search old value
		if (v_end == -1){						// no more old value
			v_end = p_string.length;
			v_new_string += p_string.substring(v_start, v_end);	// move the rest
		}else{
			v_new_string += p_string.substring(v_start, v_end) + p_new;	// replace old to new
			v_start = v_end + p_old.length;
		}
	}
	return (v_new_string);
}

//
// ======	Function	check MAC	Macかどうか調べる。Macならtrueを返す。
//				Return:	true = MAC, false = others		
//
function check_mac(){
       if(navigator.userAgent.indexOf("Mac")!=-1){
		return true;
	}else{
		return false;
	}
}

// ====== 	Function:	check if some data was entered other than spaces
//		Parm:		p_val		Check value
//		Return value:	If entered --> True,  Not entered --> False
//
function check_required(p_val){
	if (p_val == ""){return(false);}
	for(i=0; i<p_val.length; i++){
		if (p_val.substring(i, i+1)!=" " && p_val.substring(i, i+1)!="　"){
			return(true);
		}
	}
	return(false);
}

// ======  Function Name:   NumToChar
//		Parameters:     p_item  : numeric value to convert
//						p_digit: digit
//      Return:   		charcter string 
//      Purpose:   		This function convert numeric to character with the leading 0s.
function numtochar(p_item, p_digit){
	var v_char
	var i

	p_item = "" + p_item;
	v_char = p_item;
	for (i=p_item.length; i<p_digit; i++){
		v_char = "0" + v_char;
	}
	return v_char;
}
