/**
 * @author michitaka
 */
/**
 *
 * @param str1 郵便番号左側
 * @param str2 郵便番号右側
 * @param called 呼ばれた画面
 * @return
 */
function searchDestAddress(str1,str2,called) {
	var url = '/zips/search';
	var zipcode = document.getElementById(str1).value +  document.getElementById(str2).value;
	url = url + "/" + zipcode;
	url = url + "/" + called;
	var zipwin = window.open(url, 'mywindow', 'width=480, height=100, menubar=no, toolbar=no, scrollbars=yes');
}
/**
 *
 * @param address
 * @param called
 * @return
 */
function setAddress(address,called) {
		elmid = "";
		if(called == "contact") {
			elmid = "ContactAddress";
		} else if(called =="member") {
			elmid = "MemberAddress";
		}
		window.opener.document.getElementById(elmid).value = address;
		window.self.close();
}
/*
function confirmOrder() {
	  if (confirm("この内容で注文を確定します。よろしいですか？")) {
		  document.getElementById("OrdersOrderForm").submit();
		  return false;
	  } else {
		return false;
	  }

}

function confirmSendMail(msg,formid) {
	  if (confirm(msg)) {
		  document.getElementById(formid).submit();
		  return false;
	  } else {
		return false;
	  }
}
*/

/**
 *
 * @return false
 */
function forward(str,forward) {
  document.getElementById(str).action = forward;
  document.getElementById(str).submit();
  return false;
}
/*
function isChecked(obj,str) {
	if (obj.checked) {
		document.getElementById(str).style.display = 'block';
	} else {
		document.getElementById(str).style.display = 'none';
	}

}

function forwardOrderDetail(id) {
	var url = "/kitchenfarm/admins/detail/" + id;
	location.href = url;
}

function checkParam(f) {
	asciiRegex = new RegExp('^[a-zA-Z0-9~`!@#$%^&*()_=+|\'";:\\\\\/?.,<>{}\\[\\]-]*$');
	spaceRegex = new RegExp('^[\\s　]*$');

	with (f.form) {
		//TODO:入力チェック実装判断を考える
		if(id == "MembersRegistForm") {
			if(MemberMemberid.value.match(spaceRegex)){
				alert("お客様IDが入力されていません。※印は必ず入力してください。");
				return false;
			}
			if(!MemberMemberid.value.match(asciiRegex)) {
				alert("お客様IDは半角英数字でご入力下さい。");
				return false;
			}

			if (MemberPassword.value.match(spaceRegex)){
				alert("パスワードが入力されていません。※印は必ず入力してください。");
				return false;
			}
			if(!MemberPassword.value.match(asciiRegex)) {
				alert("パスワードは半角英数字でご入力下さい。");
				return false;
			}
		}
		if (MemberName.value.match(spaceRegex)){
			alert("お名前が入力されていません。※印は必ず入力してください。");
			return false;
		}
		if (MemberKana.value.match(spaceRegex)){
			alert("ふりがなが入力されていません。※印は必ず入力してください。");
			return false;
		}
		if (MemberMailaddress.value.match(spaceRegex)){
			alert("Emailアドレスが入力されていません。※印は必ず入力してください。");
			return false;
		}
		if (!MemberMailaddress.value.match("^[a-zA-Z0-9~?/.;:|_=+-]+@[a-zA-Z0-9~?/.;:|_=+-]+$") || MemberMailaddress.value.indexOf(",") != -1) {
			alert("Eメールアドレスが正しくないか、未入力です。お間違えがないか再度確認してください。");
			return false;
		}
		if(MemberMobileaddress.value != "") {
			if (!MemberMobileaddress.value.match("^([a-zA-Z0-9~?/.;:|_=+-]+@[a-zA-Z0-9~?/.;:|_=+-]+)?$") || MemberMobileaddress.value.indexOf(",") != -1) {
				alert("携帯Eメールアドレスが正しくないか、未入力です。お間違えがないか再度確認してください。");
				return false;
			}
		}
		if (MemberZipcode1.value.match(spaceRegex) || MemberZipcode2.value.match(spaceRegex)){
			alert("郵便番号が入力されていません。※印は必ず入力してください。");
			return false;
		}

		if ( (MemberZipcode1.value.length != 0 || MemberZipcode2.value.length != 0) && (!MemberZipcode1.value.match("^[0-9]+$") || !MemberZipcode2.value.match("^[0-9]+$")) ){
			alert("郵便番号に半角の0～9の数字ではないものが入力されています。お間違えがないか再度確認してください。");
			return false;
		}
		if (MemberAddress.value.match(spaceRegex)){
			alert("市区町村・町域が入力されていません。※印は必ず入力してください。");
			return false;
		}
		if (MemberTel1.value.match(spaceRegex) || MemberTel2.value.match(spaceRegex) || MemberTel3.value.match(spaceRegex)) {
			alert("電話番号が入力されていません。※印は必ず入力してください。");
			return false;
		}
		if (!MemberTel1.value.match("^[0-9]+$") || !MemberTel2.value.match("^[0-9]+$") || !MemberTel3.value.match("^[0-9]+$")){
			alert("電話番号が正しくないようです。お間違えがないか再度確認してください。");
			return false;
		}
	}
	return true;
}

function checkDestination(f) {
	asciiRegex = new RegExp('^[a-zA-Z0-9~`!@#$%^&*()_=+|\'";:\\\\\/?.,<>{}\\[\\]-]*$');
	spaceRegex = new RegExp('^[\\s　]*$');
	with(f.form) {
			if(c1.checked) {
				if (DestinationName.value.match(spaceRegex)){
					alert("お名前が入力されていません。※印は必ず入力してください。");
					return false;
				}
				if (DestinationKana.value.match(spaceRegex)){
					alert("ふりがなが入力されていません。※印は必ず入力してください。");
					return false;
				}
				if (DestinationZipcode1.value.match(spaceRegex) || DestinationZipcode2.value.match(spaceRegex)){
					alert("郵便番号が入力されていません。※印は必ず入力してください。");
					return false;
				}

				if ((DestinationZipcode1.value.length != 0 || DestinationZipcode2.value.length != 0) && (!DestinationZipcode1.value.match("^[0-9]+$") || !DestinationZipcode2.value.match("^[0-9]+$")) ){
					alert("郵便番号に半角の0～9の数字ではないものが入力されています。お間違えがないか再度確認してください。");
					return false;
				}
				if (DestinationAddress.value.match(spaceRegex)){
					alert("市区町村・町域が入力されていません。※印は必ず入力してください。");
					return false;
				}
				if (DestinationTel1.value.match(spaceRegex) || DestinationTel2.value.match(spaceRegex) || DestinationTel3.value.match(spaceRegex)) {
					alert("電話番号が入力されていません。※印は必ず入力してください。");
					return false;
				}
				if (!DestinationTel1.value.match("^[0-9]+$") || !DestinationTel2.value.match("^[0-9]+$") || !DestinationTel3.value.match("^[0-9]+$")){
					alert("電話番号が正しくないようです。お間違えがないか再度確認してください。");
					return false;
				}
			}
	}
	return true;
}

function checkOrderPram(f) {
	if(checkDestination(f) && validShipdate(f)) {
		return true;
	} else {
		return false;
	}

}

function checkAllParam(f) {
	if(checkParam(f) && checkDestination(f) && validShipdate(f)) {
		return true;
	} else {
		return false;
	}
}

function changeOrderNum(idx,ordernumform) {
	var formname = ordernumform +""+idx;
	var selectedNum;
	for(var i=0; i<document.getElementById(formname).options.length; i++){
		if(document.getElementById(formname).options[i].selected){
				selectedNum = document.getElementById(formname).options[i].value;
		}
	}
	var url = "/kitchenfarm/carts/change/"+idx+"/"+selectedNum+"/";
	location.href = url;
}

function checkInquiryParam(f) {
	asciiRegex = new RegExp('^[a-zA-Z0-9~`!@#$%^&*()_=+|\'";:\\\\\/?.,<>{}\\[\\]-]*$');
	spaceRegex = new RegExp('^[\\s　]*$');

	with(f.form) {
		if (CustomerName.value.match(spaceRegex)){
			alert("お名前が入力されていません。※印は必ず入力してください。");
			return false;
		}
		if (CustomerTel1.value.match(spaceRegex) || CustomerTel2.value.match(spaceRegex) || CustomerTel3.value.match(spaceRegex)) {
			alert("電話番号が入力されていません。※印は必ず入力してください。");
			return false;
		}
		if (!CustomerTel1.value.match("^[0-9]+$") || !CustomerTel2.value.match("^[0-9]+$") || !CustomerTel3.value.match("^[0-9]+$")){
			alert("電話番号が正しくないようです。お間違えがないか再度確認してください。");
			return false;
		}
		if (CustomerMailaddress.value.match(spaceRegex)){
			alert("Emailアドレスが入力されていません。※印は必ず入力してください。");
			return false;
		}
		if (!CustomerMailaddress.value.match("^[a-zA-Z0-9~?/.;:|_=+-]+@[a-zA-Z0-9~?/.;:|_=+-]+$") || CustomerMailaddress.value.indexOf(",") != -1) {
			alert("Eメールアドレスが正しくないか、未入力です。お間違えがないか再度確認してください。");
			return false;
		}

	}

	return true;
}
function checkContactParam(f) {
	asciiRegex = new RegExp('^[a-zA-Z0-9~`!@#$%^&*()_=+|\'";:\\\\\/?.,<>{}\\[\\]-]*$');
	spaceRegex = new RegExp('^[\\s　]*$');

	with(f.form) {
		if (ContactName.value.match(spaceRegex)){
			alert("お名前が入力されていません。※印は必ず入力してください。");
			return false;
		}
		if (ContactMailaddress.value.match(spaceRegex)){
			alert("Emailアドレスが入力されていません。※印は必ず入力してください。");
			return false;
		}
		if (!ContactMailaddress.value.match("^[a-zA-Z0-9~?/.;:|_=+-]+@[a-zA-Z0-9~?/.;:|_=+-]+$") || ContactMailaddress.value.indexOf(",") != -1) {
			alert("Eメールアドレスが正しくないか、未入力です。お間違えがないか再度確認してください。");
			return false;
		}
		if (ContactSubject.value.match(spaceRegex)){
			alert("件名が入力されていません。※印は必ず入力してください。");
			return false;
		}
		if (ContactQuery.value.match(spaceRegex)){
			alert("お問い合わせ内容が入力されていません。※印は必ず入力してください。");
			return false;
		}
	}

	return true;
}

function switchVisible(obj,id1,id2) {
	radio_id = obj.id;
	if (obj.checked && radio_id == "OrderShippingtype1") {
		document.getElementById(id1).style.display = 'none';
		document.getElementById(id2).style.display = 'none';
	} else {
		document.getElementById(id1).style.display = 'table-row';
		document.getElementById(id2).style.display = 'table-row';
	}

}

function diffDateFromToday(year,month,day) {
	shipdate = new Date(year,month-1,day);
	today = new Date();
	diff = shipdate - today;
    diffDay = diff / 86400000;//1日は86400000ミリ秒

    return diffDay;
}

function validShipdate(f) {
	with(f.form) {
		if(OrderShippingtype2.checked) {
			year = OrderYear.value;
			month = OrderMonth.value;
			day = OrderDay.value;

			diffdate = diffDateFromToday(year,month,day);

			if(diffdate <= 2) {
				alert("発送年月日は、本日より３日以上先の日付をご指定下さい。");
				return false;
			}
		}
	}
	return true;

}
function checkShipdate(f) {
	with(f.form) {
		year = calYear.value;
		month = calMonth.value;
		day = calDay.value;

		diffdate = diffDateFromToday(year,month,day);

		if(diffdate <= 2) {
			alert("発送年月日は、本日より３日以上先の日付をご指定下さい。");
			return false;
		}
	}
	return true;

}
*/
