﻿/*========== String ==========*/

/*--------------------------------------------------------------------------*/
//	Function	Strip whitespace(1&2 byte) from the beginning and end of a string
//	Input		sValue	(string)	The string that will be trimmed. 
//	Return		The trimmed string.
/*--------------------------------------------------------------------------*/
function trim(sValue) {
  var re = new RegExp(/^[ \t]+|[ \t]+$/g);//normal space
  var re2 = new RegExp(/^[　\t]+|[　\t]+$/g);//japanese space
  var str = sValue.replace(re, '');
  return str.replace(re2, '');
}

//Japanese character

/*--------------------------------------------------------------------------*/
//	Function	Check string is Higarana or not (include space(2byte & 1byte))
//	Input		sValue	(string)	The string that will be checked. 
//	Return		true	is Higarana
//				false
/*--------------------------------------------------------------------------*/
function isHigarana(sValue){
  var re = new RegExp(/[^あ-んぁぃぅぇぉっゃゅょゎ　\s]+/ );
  return (!sValue.match(re) != !1)?1:0;
}

/*--------------------------------------------------------------------------*/
//	Function	Check string is Katakana(2byte) or not (include space(2byte & 1byte))
//	Input		sValue	(string)	The string that will be checked. 
//	Return		true	is Katakana(2byte)
//				false
/*--------------------------------------------------------------------------*/
function isZenKana(sValue){
  var re = new RegExp(/[^ア-ンーァィゥェォッャュョ　\s]+/ );
  return (!sValue.match(re) != !1)?1:0;
}

/*--------------------------------------------------------------------------*/
//	Function	Check string is Katakana(1byte) or not
//	Input		sValue	(string)	The string that will be checked. 
//	Return		true	is Katakana(1byte)
//				false
/*--------------------------------------------------------------------------*/
function isHanKana(sValue){
  var re = new RegExp(/[^ｱ-ﾝﾞﾟｰｧｨｩｪｫｯｬｭｮ \s]+/ );
  return (!sValue.match(re) != !1)?1:0;
}

/*--------------------------------------------------------------------------*/
//	Function	Count string by byte (Zenkaku 2 bytes, Hankaku 1 byte)
//	Input		sValue	(string)	The string that will be counted. 
//	Return		the number of byte
/*--------------------------------------------------------------------------*/
function stringCountByte(sValue){
  var str = sValue;
  var str_length = str.length;
  var code;
  var cntByte=0;
  for (var i = 0; i < str_length; i++) {
    code = str.charCodeAt(i);
	if ((0 <= code && code <= 255) || (65382 <= code && code <= 65439)) { cntByte++;}
	else cntByte+=2;
  }
  return cntByte;
}

function checkchar1Byte(sValue){
	var str = sValue;
  	var str_length = str.length;
  	var code;
	for (var i = 0; i < str_length; i++) {
		code = str.charCodeAt(i);
		if ((0 <= code && code <= 255) || (65382 <= code && code <= 65439)) { }else{ return false; }
	}
	return true;
}

function checkchar2Byte(sValue){
  var str = sValue;
  var str_length = str.length;
  var code;
  var cntByte=0;
  for (var i = 0; i < str_length; i++) {
    code = str.charCodeAt(i);
	if ((0 <= code && code <= 255) || (65382 <= code && code <= 65439)) { return false;}
  }
  return true;
}

//Date string

/*--------------------------------------------------------------------------*/
//	Function	Check input year, month, day is a valid date or not
//	Input		sYear	string, int		Year value 
//				sMonth	string, int		Month value 
//				sDay	string, int		Day value 
//	Return		true	is valid date
//				false
/*--------------------------------------------------------------------------*/
function isValidDate(sYear, sMonth, sDay){
  year = parseInt(sYear,10);
  month = parseInt(sMonth,10)-1;
  day = parseInt(sDay,10);

  var source_date = new Date(year,month,day);
  
  if((year != source_date.getFullYear()) || (month != source_date.getMonth()) || (day != source_date.getDate()) )
  { return false; }
  return true;
}

/*--------------------------------------------------------------------------*/
//	Function	Check date string is a valid date or not (YYYY/M/D)
//	Input		sDate	string		Date value 
//	Return		true	is valid date
//				false	wrong format or invalid date
/*--------------------------------------------------------------------------*/
function checkDate(sDate){
 
  //var re = new RegExp(/(19|20)\d\d([\- \/\.])(0[1-9]|1[012])\2(0[1-9]|[12][0-9]|3[01])/);
  var re = new RegExp(/(19|20)\d\d([\- \/\.])(0?[1-9]|1[012])\2(0?[1-9]|[12][0-9]|3[01])/);//YYYY/M/D
  if (sDate.match(re)) {
	separator = sDate.charAt(4);
	var aDate = sDate.split(separator);
	return isValidDate(aDate[0], aDate[1], aDate[2]);
  } else {
    return false;
  }
}
/*--------------------------------------------------------------------------*/
//	Function	Check date string is a valid date or not (YYYY年MM月DD日)
//	Input		sDate	string		Date value 
//	Return		true	is valid date
//				false	wrong format or invalid date
/*--------------------------------------------------------------------------*/
function checkDate_Ja(sDate){
  var re = new RegExp(/^(19|20)\d\d年(0[1-9]|1[012])月(0[1-9]|[12][0-9]|3[01])日$/);
  if (sDate.match(re)) {
    var aDate = sDate.split(/[年月日]/);
	return isValidDate(aDate[0], aDate[1], aDate[2]);
  } else {
    return false;
  }
}

/*--------------------------------------------------------------------------*/
//	Function	Check Zip Code string is right format or not (000-0000)
//	Input		sZip	string		The string that will be checked
//	Return		true	is right format
//				false	
/*--------------------------------------------------------------------------*/
function isZip(sZip){
  var re = new RegExp(/\d{3}\-\d{4}/ );//000-0000
  return (sZip.match(re))?true:false;
}

/*--------------------------------------------------------------------------*/
//	Function	Check Phone string is right format or not (number-number-number)
//	Input		text	string		The string that will be checked
//	Return		true	is right format
//				false	
/*--------------------------------------------------------------------------*/
function checkPhone( text ){
/*	var re = /^[0-9]{3}\-[0-9]{3}\-[0-9]{4}$/;
	//var re = /^[0-9]+\-[0-9]+\-[0-9]+$/;
	if(text.match(re) == null)
		return false;
	else
		return true;*/
		
  if (text.length == 12) {
    var re = /^[0-9]{2,}\-[0-9]{1,}\-[0-9]{4}$/;
	if(text.match(re) == null)
		return false;
	else
		return true;  
  }
  else{
	var re = /^[0-9]{3}\-[0-9]{4}\-[0-9]{4}$/;
	if(text.match(re) == null)
		return false;
	else
		return true;  
  }
}

function checkCode(phone) {
	var re= /\b[a-zA-z]{2}[0-9]{5}\b/;
	//var re=/^[0-9\-]*$/
	if(!phone.match(re))
	{
		return false;
	}
	return true;
}

/*--------------------------------------------------------------------------*/
//	Function	Check Username string is right format or not (alphabet,number,_-)
//	Input		text	string		The string that will be checked
//	Return		true	is right format
//				false	blank or wrong format
/*--------------------------------------------------------------------------*/
function checkUsername ( user ) {
	user = trim(user);

	if( user.indexOf(" ") >=0 ) 	return false;

	var re=/^[a-zA-Z0-9\_\-]*$/ ;

	if(user.match(re) == null)	return false;

	return true;
}

/*--------------------------------------------------------------------------*/
//	Function	Check Username string is right format or not (alphabet,number,_)
//				and minLength <= StringLength <= maxLength
//	Input		sUserNm		string		The string that will be checked
//				iMinLen		int			min length
//				iMaxLen		int			max Length
//	Return		true	is right format
//				false	
/*--------------------------------------------------------------------------*/
function checkUserNm ( sUserNm, iMinLen, iMaxLen ) {
  var strRe = '^[a-zA-Z0-9_]{'+iMinLen+','+iMaxLen+'}$';
  var re = new RegExp( strRe);
  return (sUserNm.match(re))?true:false;
}

//Email
/*--------------------------------------------------------------------------*/
//	Function	Check Mail Address is right format or not
//	Input		mail	string		The string that will be checked
//	Return		true	is right format
//				false	
/*--------------------------------------------------------------------------*/
function checkEmail(mail){
  mail = trim(mail);
  var re=/^[\w-\.]+(\.[\w-\.]+)*@([\w-]+\.)+[a-zA-Z]{2,4}$/
  if(mail.match(re) == null) return false;
  return true;
}

/*========== TextArea ==========*/
/*--------------------------------------------------------------------------*/
//	Function	Replace all breakline of the string with the replacement string
//	Input		sValue		(string)	
//				sRepStr		(string)	replacement string
//	Return		A string with the replaced values. 
/*--------------------------------------------------------------------------*/
function repBreakLine(sValue,sRepStr){
  var re = new RegExp(/\r?\n/g);
  return sValue.replace(re, sRepStr);
}

/*========== Radio & Checkbox ==========*/
/*--------------------------------------------------------------------------*/
//	Function	Check the checkbox is checked at least one
//				Check the radio is checked or not
//	Input		sFormNm		(string)	Name of Form Tag
//				sElementNm	(string)	Name of Radio or Checkbox[]
//	Return		true	is checked
//				false	
/*--------------------------------------------------------------------------*/
function isChecked(sFormNm, sElementNm){
  myChecked = false;
  myForm = document.forms[sFormNm];
  myArray = myForm.elements[sElementNm];
  if (myArray){
	  if (myArray.length){
	  for (i = 0; i < myArray.length; i++){
		if (myArray[i].checked){
		  myChecked = true;
		}
	  }
	  }
	  else
		if (myArray.checked) return true;
		else return false;
  }
  return myChecked;
}

/*--------------------------------------------------------------------------*/
//	Function	Remove All Check of the checkbox
//	Input		sElementNm	(string)	Name of Checkbox
/*--------------------------------------------------------------------------*/
function clearChk(sElementNm){
	var oChk = document.getElementsByName(sElementNm);
	if(oChk){
		for(var i=0; i<oChk.length; i++) 
			oChk[i].checked = false;
	}
}

/*--------------------------------------------------------------------------*/
//	Function	Check All Check of the checkbox
//	Input		sElementNm	(string)	Name of Checkbox[]
/*--------------------------------------------------------------------------*/
function checkAllChkBox(sChkBoxNm){
	var oCheckBox = document.getElementsByName(sChkBoxNm);
	if(oCheckBox){
		for(var i=0; i<oCheckBox.length; i++) 
			oCheckBox[i].checked = true;
	}
}

/*--------------------------------------------------------------------------*/
//	Function	Check the checkbox is CheckAll
//	Input		sElementNm	(string)	Name of Checkbox[]
//	Output		true-->checkAll		false
/*--------------------------------------------------------------------------*/
function isCheckAll(sChkBoxNm){
	var oCheckBox = document.getElementsByName(sChkBoxNm);
	if(oCheckBox){
		for(var i=0; i<oCheckBox.length; i++) 
			if (oCheckBox[i].checked == false) return false;
	}
	return true;
}

/*========== Input File ==========*/
/*--------------------------------------------------------------------------*/
//	Function	Check All Check of the checkbox
//	Input		sFile	(string)	File path
//				aExt	Array		File extension array	Array(".jpg", ".png", ".txt"); 
/*--------------------------------------------------------------------------*/
function checkFileEx(sFile, aExt) {
  extArray = aExt;

  if (!sFile) return null;

  sFile = sFile.slice(sFile.indexOf("\\") + 1);
  ext = sFile.slice(sFile.lastIndexOf(".")).toLowerCase();

  for (var i = 0; i < extArray.length; i++) {
    if (extArray[i] == ext) return true;
  }
  
  return false;
}


function checkInput(patern, e)	{
	var Reg = new RegExp(patern);
	e = e || event;
	var key = e.which?e.which:e.keyCode;
	if(String.fromCharCode(key).match(Reg) || (e.keyCode && e.which != undefined)) return true;
	return false;
}


function checkUrl(theurl){
     //var tomatch= /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/
	var tomatch = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	
     if (tomatch.test(theurl)){
         return true;
     }else {
         return false;
     }
}

function check_enter(textarea,limit){
	var val = textarea.replace(/\r/g,'').split('\n');
	if(val.length>limit)
		return false;
		//textarea.value=val.slice(0,-1).join('\n')
	else
		return true;
}

/*--------------------------------------------------------------------------*/
//	Function	Check CustomerID is number string or not
//	Input		sValue	string		The string that will be checked
//	Return		true	is right format
//				false	
/*--------------------------------------------------------------------------*/
function chkCusID(sValue){
  var re = new RegExp(/\d/ );
  return (sValue.match(re))?true:false;
}

/*--------------------------------------------------------------------------*/
//	Function	Check Password is right format or not
//	Input		sValue	string		The string that will be checked
//	Return		true	is right format
//				false	
/*--------------------------------------------------------------------------*/
function checkPass(sValue){
  var re = new RegExp(/[0-9a-zA-Z]{8,16}/ );
  return (sValue.match(re))?true:false;
}


function dec2hex ( textString ) 
{
 return (textString+0).toString(16).toUpperCase();
}


function convertChar2CP ( textString ) { 
	var haut = 0;
	var n = 0;
	CPstring = '';
	for (var i = 0; i < textString.length; i++) {
		var b = textString.charCodeAt(i);		
		if (b < 0 || b > 0xFFFF) {
			CPstring += 'Error ' + dec2hex(b) + '!';
		}
		if (haut != 0) {
			if (0xDC00 <= b && b <= 0xDFFF) {
				CPstring += dec2hex(0x10000 + ((haut - 0xD800) << 10) + (b - 0xDC00)) + ' ';
				haut = 0;
				continue;
				}
			else {
				CPstring += '!erreur ' + dec2hex(haut) + '!';
				haut = 0;
				}
			}
		if (0xD800 <= b && b <= 0xDBFF) {
			haut = b;
		}
		else {
			CPstring += dec2hex(b) + ' ';
		}
		}
	CPstring = CPstring.substring(0, CPstring.length-1);
	
	return convertCP2DecNCR( CPstring );
	//document.getElementById('decNCRs').value = convertCP2DecNCR( CPstring );	
}

function convertCP2DecNCR ( textString ) 
{
  var outputString = "";
  textString = textString.replace(/^\s+/, '');
  if (textString.length == 0) { return ""; }
  textString = textString.replace(/\s+/g, ' ');
  var listArray = textString.split(' ');
  for ( var i = 0; i < listArray.length; i++ ) {
    var n = parseInt(listArray[i], 16);
    outputString += ('&#' + n + ';');
  }
  return( outputString );
}

/*--------------------------------------------------------------------------*/
//	Function	get formated string date (YYYY/MM/DD)
//	Input		sDate				string	The string that will be checked 
//											(valid date string YYYY/MM/DD || YYYY/M/D)
//	Return		formated string		string	
/*--------------------------------------------------------------------------*/
function getDateFrStr(sDate){
 	var sReturn = '';
	separator = sDate.charAt(4);
	var aDate = sDate.split(separator);
	
	year = parseInt(aDate[0],10);
	month = parseInt(aDate[1],10);
	day = parseInt(aDate[2],10);

	sReturn = year;
	if (month<10) sReturn += '/0' + month;
	else sReturn += '/' + month;
	
	if (day<10) sReturn += '/0' + day;
	else sReturn += '/' + day;
	
	return sReturn;
}
