/*
 * PowerBeans common javascript functions
 */
function trim(argValue){
    return String(argValue).replace(/^[ ?]*/gim, "").replace(/[ ?]*$/gim, "")
}

function isEmptyObjs(obj) {

		for(var i = 0; i < obj.length; i++) {
			if (isEmptyObj(obj[i][0],obj[i][1])) {
				return true;
			}
		}
	return false;
}

function isEmptyObj(obj, msg) {

		if (trim(obj.value).length == 0) {
			if (msg.length > 0) {
				alert(msg);
			}
			obj.focus();
			return true;
		}
		obj.value = trim(obj.value);
	return false;
}


function RollOver (obj,val) {
  obj.src = val;}
  
function getLabel(e, defaultLabel, addMsg) {
 if (e.getAttribute("label")) {
  return e.getAttribute("label") + addMsg;
 } else {
  return defaultLabel + addMsg;
 }
}

// input check js (need jQuery library)
function pbInputCheck(e) {
				 var msg = false;
				 var val = String($(e).val());
	console.log("need_size=" + $(e).siblings(".need").size());			 
				 if ($(e).siblings(".need").size() > 0) {
		             if (trim(val).length == 0) {
		             msg = getLabel(e, "必須項目", "入力してください");
		             }
         }
         if (!msg && val.length > 0 && $(e).siblings(".eng").size() > 0) {
	console.log("eng_size1=" + $(e).siblings(".eng").size() + " val=" + val);
            if (!val.match(/^([a-z]|[A-Z]){1}([a-z]|[A-Z]|[0-9])*$/)) {
             msg = getLabel(e, "この項目", "は半角英数字（先頭は半角英字)で入力してください");
            }
         }
         if (!msg && val.length > 0 && $(e).siblings(".number").size() > 0) {
	console.log("number_size1=" + $(e).siblings(".number").size() + " val=" + val);
           if (!val.match(/^[0-9]*$/)) {
            msg = getLabel(e, "この項目", "は半角数値で入力してください");
           }
         }
         if (msg) {
             alert(msg);
             $(e).focus();
         }
         return msg;
}

// regist click event for input check
function pbRegistInputCheck(btn_obj, items_filter, parent_filter, ok_exec_func) {
		btn_obj.click(function() {
console.log("start check");

		 var msg = false;
     // 1.1 need check
// console.log("target count=" + $(items_filter, $(parent_filter)).size());
     var items_size = $(items_filter, $(parent_filter)).size();
     var c = 0;
	   $(items_filter, $(parent_filter)).
		    each(function() {
		       if (msg) {
		        return;
		       }
					 var val = String($(this).val());
console.log("name=" + $(this).attr("name") + "/ val=" + val);			 
console.log("need_size=" + $(this).siblings(".need").size());			 
					 if (!msg && $(this).siblings(".need").size() > 0) {
			             if (trim(val).length == 0) {
			             msg = getLabel(this, "必須項目", "を入力してください");
			             }
	         }
	         if (!msg && val.length > 0 && $(this).siblings(".eng").size() > 0) {
console.log("eng_size1=" + $(this).siblings(".eng").size() + " val=" + val);
	            if (!val.match(/^([a-z]|[A-Z]){1}([a-z]|[A-Z]|[0-9]|_)*$/)) {
	             msg = getLabel(this, "この項目", "は半角英数字（先頭は半角英字)で入力してください");
	            }
	         }
	         if (!msg && val.length > 0 && $(this).siblings(".number").size() > 0) {
console.log("number_size1=" + $(this).siblings(".number").size() + " val=" + val);
	           if (!val.match(/^[0-9]*$/)) {
	            msg = getLabel(this, "この項目", "は半角数値で入力してください");
	           }
	         }
	         if (!msg && val.length > 0 && $(this).siblings(".email").size() > 0) {
console.log("email_size=" + $(this).siblings(".email").size() + " val=" + val);
	           if (!val.match(/^([a-z]|[A-Z]|[0-9]|_|-|.)*@([a-z]|[A-Z]|[0-9]|_|-|.)*$/)) {
	            msg = getLabel(this, "この項目", "は有効なメールアドレスを入力してください");
	           }
	         }
	         if (!msg && val.length > 0 && $(this).siblings(".min").size() > 0) {
	          var min_len = $($(this).siblings(".min")[0]).text();
console.log("min_size=" + $(this).siblings(".min").size() + " val=" + val + " min_len=" + min_len);
            if (trim(val).length < min_len) {
             msg = getLabel(this, "必須項目", "は" + min_len + "文字以上で入力してください");
            }
	         }
	         if (!msg && val.length > 0 && $(this).siblings(".pswd").size() > 0) {
console.log("pswd_size1=" + $(this).siblings(".pswd").size() + " val=" + val);
	            if (!val.match(/^([a-z]|[A-Z]|[0-9]|_)*$/)) {
	             msg = getLabel(this, "この項目", "は半角英数字,_で入力してください");
	            }
	         }
	         
	         if (msg) {
             alert(msg);
             $(this).focus();
	         }
	         if (++c == items_size) {
							if (!msg) {
								ok_exec_func();
							}
	         }
				});	
			return msg;
		});
		
}

// get form param
function getFormItems(formid) {
	var param = {};
	var frm = $("#" + formid);
	$("input", frm).each(function() {
	   if (!param[this.name]) { param[this.name] = new Array();}
	   param[this.name].push($(this).val());
  });
	$("select", frm).each(function() {
	   if (!param[this.name]) { param[this.name] = new Array();}
	   param[this.name].push($(this).val());
  });
	$("textarea", frm).each(function() {
	   if (!param[this.name]) { param[this.name] = new Array();}
	   param[this.name].push($(this).val());
  });
	return param;
}
