if(typeof wzs == "undefined"){
    wzs = {};
}

wzs.ckeditors = new Array();

/***********************************************************
** 汎用関数群 Tools を設定
************************************************************/
wzs.Tools = function() {};

wzs.Tools.nl2br = function(ch) {
    ch = ch.replace(/\r\n/g,"\r") ;
    ch = ch.replace(/\r/g,"\n") ;
    ch = ch.replace(/\n/g,"<br />") ;
    return ch ;
};

wzs.Tools.escapehtml = function(ch) {
    ch = ch.replace(/&/g,"&amp;") ;
    ch = ch.replace(/"/g,"&quot;") ;
    ch = ch.replace(/'/g,"&#039;") ;
    ch = ch.replace(/</g,"&lt;") ;
    ch = ch.replace(/>/g,"&gt;") ;
    return ch ;
};

wzs.Tools.check_anitem = function(val, name, mandatory, length_min, length_max, asciionly) {

    var ret = "";
    
    if (mandatory) {
	if (wzs.Tools.isSpaceCharOnly(val)) {
	    ret += "* Please Input " + name + "!\n";
	}
    }
    
    if (length_min == 0) {
	if (val.length > length_max) {
	    ret += "* Too long of " + name + "! Max:" + length_max + " letters.\n";
	}
    } else {
	if ((val.length < length_min) || (val.length > length_max)) {
	    ret += "*" + name + " is equal or more than " + length_min + " letters and equal or less then " + length_max + " letters.\n";
	}
    }
    
    if (asciionly) {
	if (!wzs.Tools.isASCii(val)) {
	    ret += "* please input " + name + " by HANKAKU !\n";
	}
    }
    
    if (!wzs.Tools.isOKChar(val)) {
	ret += "* Hankaku '<' cannot be used in " + name + "!\n";
    }
    
    return ret;
    
}

/*************************************************************************************************************************
**
** isDate 正規表現による書式チェック
**
*************************************************************************************************************************/
wzs.Tools.isDate = function(datestr, format) {

    if (format == "%d-%m-%Y") {
	if(!datestr.match(/^\d{2}-\d{2}-\d{4}$/)){
	    return false;
	}
	return wzs.Tools.isValidDate(datestr.substr(6, 4), datestr.substr(3, 2) - 1, datestr.substr(0, 2));
    } else {
	if(!datestr.match(/^\d{4}-\d{2}-\d{2}$/)){
	    return false;
	}
	return wzs.Tools.isValidDate(datestr.substr(0, 4), datestr.substr(5, 2) - 1, datestr.substr(8, 2));
    }

};

/*************************************************************************************************************************
**
** isValidDate 月,日の妥当性チェック
**
*************************************************************************************************************************/
wzs.Tools.isValidDate = function(year, month, day) {

    if(month >= 0 && month <= 11 && day >= 1 && day <= 31){
	var vDt = new Date(year, month, day);
	if(isNaN(vDt)){
	    return false;
	}else if(vDt.getFullYear() == year && vDt.getMonth() == month && vDt.getDate() == day){
	    return true;
	}else{
	    return false;
	}
    }else{
	return false;
    }
};

/*************************************************************************************************************************
**
** isASCii 半角かどうかをチェック
**
*************************************************************************************************************************/
wzs.Tools.isASCii = function(val) {
    for(var i=0 ; i<val.length; i++){
	var code=val.charCodeAt(i);
	if (code<32 || code>127) {
	    return false;
	}
    }
    return true;
};

/*************************************************************************************************************************
**
** isOKChar  ajax リクエストで送れる文字列かどうかをチェック < が NG
**
*************************************************************************************************************************/
wzs.Tools.isOKChar = function(val) {
  for(var i=0 ; i<val.length; i++){
    var code=val.charCodeAt(i);
    // if (code == 92 || code== 39 || code== 34) { // \'" -> NG
    if (code == 60) { // < -> NG
      return false;
    }
  }
  return true;
};

/*************************************************************************************************************************
**
** isSpaceCharOnly  空白文字のみかをチェック 
**
*************************************************************************************************************************/
wzs.Tools.isSpaceCharOnly = function(val) {
    for(var i=0 ; i<val.length; i++){
	var code=val.charCodeAt(i);
	if (code != 32 && code != 12288 && code != 9 && code != 10 && code != 13) { // TAB, NL, CR
	    return false;
	}
    }
    return true;
};

/*************************************************************************************************************************
**
** isValidEmail  emailアドレスとして有効化かをチェック
**
*************************************************************************************************************************/
wzs.Tools.isValidEmail = function(val) {
  if(val.match(/[!#-9A-~]+@+[a-z0-9]+.+[^.]$/i)) {
    return true;
  }
  return false;
};

/*************************************************************************************************************************
**
** isNdigit N桁の数値かどうかをチェック
**
*************************************************************************************************************************/
wzs.Tools.isNdigit = function(val, n) {
  if (val.length != n) {
    return false;
  }
  return wzs.Tools.isNum(val);
}

/*************************************************************************************************************************
**
** isNum 半角数値かどうかをチェック
**
*************************************************************************************************************************/
wzs.Tools.isNum = function(val) {
    for(var i=0 ; i<val.length; i++){
	var code=val.charCodeAt(i);
	if (code<48 || code>57) {
            return false;
	}
    }
    return true;
}

/*************************************************************************************************************************
**
** 時間の差
**
*************************************************************************************************************************/
wzs.Tools.deltaYear = function(time1, time2) {
    return Math.floor((time1 - time2) / 10000);
}

/*************************************************************************************************************************
**
** requestAjax  ajax リクエストを送る jquery を利用
**
*************************************************************************************************************************/
wzs.requestAjax = function(params, url, func, type) {
    $.ajax({
	url: url,
	cache: true,
	type: "POST",
	dataType: type,
	data: params,
        success: function (response) {
	    func(response);
	},
        error: function () {
	    func();
        }
    });
    return false;
};

/*************************************************************************************************************************
**
** Loading Dialog を表示 google closure tool を利用
**
*************************************************************************************************************************/
wzs.loadingDialog = function() {
    var ret = new goog.ui.Dialog("loading-dialog", false);
    ret.setTitle(null);
    ret.setHasTitleCloseButton(false);
    ret.setButtonSet(null);
    ret.setContent($("#goog-loading-dialog").html());

    return ret;
}





