//---------------------------------------------------
//買い物カート：初期ロード時、エラーメッセージ
//---------------------------------------------------
function cartViewLoad() {
	try{
		var cntErrMsg = document.getElementById("cntErrMsg").value;
		if(cntErrMsg > 0) {
			for(var i = 0; i < cntErrMsg; i++) {
				alert(document.getElementById("errMsg" + i).value);
			}
		}
	}catch(e){
//		alert(e.message);
		return false;
	}
}


//-----------------------------------------------------------
//セルフチョイス各画面：残り選択数、エラーメッセージ
//-----------------------------------------------------------
var SelfChoiceHandler				= new Object();

SelfChoiceHandler.E_NOERROR			= 0x0000;
SelfChoiceHandler.E_ONLY1BYTECHARA	= 0x0001;
SelfChoiceHandler.E_NOTENOUGHBAG	= 0x0002;
SelfChoiceHandler.E_NOTENOUGHBOX	= 0x0004;
SelfChoiceHandler.E_OVERCOUNTBAG	= 0x0008;
SelfChoiceHandler.E_OVERCOUNTBOX	= 0x0010;

SelfChoiceHandler.isSubmittable		= false;
SelfChoiceHandler.countMinder		= 0;
SelfChoiceHandler.inputCount		= 0;
SelfChoiceHandler.compositionMax	= 0;

// INITIALIZER
SelfChoiceHandler.initialize = function ()
{
	this.inputCount		= document.getElementById("count_field").value;
	this.compositionMax	= document.getElementById("item_composition_cnt").value;
}

// ERROR BOX BANISHER
SelfChoiceHandler.banishErrorBox = function ()
{
	document.getElementById("error_message1").style.display = "none";
	document.getElementById("error_message2").style.display = "none";
	document.getElementById("error_div").style.display = "none";
}

// ERROE BOX CONTROLER 
SelfChoiceHandler.controlErrorBox = function ( errorCode )
{
	var displayFlags	= new Array( false, false, false, false, false );
	var isDisplay		= false;

	if ( (errorCode & this.E_ONLY1BYTECHARA) != 0 )	displayFlags[0] = true;
	if ( (errorCode & this.E_NOTENOUGHBAG) != 0 )	displayFlags[1] = true;
	if ( (errorCode & this.E_NOTENOUGHBOX) != 0 )	displayFlags[2] = true;
	if ( (errorCode & this.E_OVERCOUNTBAG) != 0 )	displayFlags[3] = true;
	if ( (errorCode & this.E_OVERCOUNTBOX) != 0 )	displayFlags[4] = true;

	for ( var i = 0; i < displayFlags.length; i++ ) {
		if ( displayFlags[i] ) {
			if ( i == 0 ) {
				document.getElementById("error_message1").style.display = "block";
			}
			else {
				document.getElementById("error_message2").style.display = "block";
			}				
			if ( !isDisplay ) isDisplay = true;
		}
	}

	if ( isDisplay ) document.getElementById("error_div").style.display = "block";

	window.scroll( 0, 0 );
}

// CAPACITY CHECKER
SelfChoiceHandler.capacityChecker = function ( isBox, compositionCount )
{
	var freeCapacity	= compositionCount;

	var errorCode		= this.E_NOERROR;
	var strUnit			= ( isBox == 1 ) ? "箱" : "袋";

	if ( freeCapacity < 0 ) {
		errorCode		|= ( isBox == 1 ) ? this.E_OVERCOUNTBOX : this.E_OVERCOUNTBAG;
		document.getElementById('error_message2').innerHTML = "入力" + strUnit + "数が多すぎます";
	}

	return errorCode;
}

// FREE CHECKER
SelfChoiceHandler.freeChecker = function ( isBox, compositionCount )
{
	var freeCapacity	= compositionCount;

	var errorCode		= this.E_NOERROR;
	var strUnit			= ( isBox == 1 ) ? "箱" : "袋";

	if ( freeCapacity > 0 ) {
		errorCode		|= ( isBox == 1 ) ? this.E_NOTENOUGHBOX : this.E_NOTENOUGHBAG;
		document.getElementById('error_message2').innerHTML = "あと" + freeCapacity + strUnit + "選択できます";
	}

	return errorCode;
}

// VALIDATOR
SelfChoiceHandler.valid = function ( isBox )
{
	this.isSubmittable		= false;
	this.countMinder		= 0;

	this.banishErrorBox();

	var errorCode			= this.E_NOERROR;
	var itemCompositionCnt	= this.compositionMax;

	// Byte Character Check
	for(var i = 0; i < this.inputCount; i++) {
		var idName = "inputForm" + i;
		var inputVal = document.getElementById(idName).value;

		if(inputVal.match( /[^0-9]+/ )) {
			errorCode |= this.E_ONLY1BYTECHARA;
		}
		else {
			itemCompositionCnt = itemCompositionCnt - inputVal;
		}
	}

	// Capacity Check
	errorCode |= this.capacityChecker( isBox, itemCompositionCnt );

	if ( (errorCode & this.E_OVERCOUNTBOX) != 0 || (errorCode & this.E_OVERCOUNTBAG) != 0 ) {
		// Reset Composition Count When Capacity Over
		itemCompositionCnt = 0;
		document.getElementById("counter").innerHTML = itemCompositionCnt;
	}
	else if ( (errorCode | this.E_NOERROR) == 0 ) {
		this.isSubmittable		= true;
		this.countMinder		= itemCompositionCnt;
	}

	document.getElementById("counter").innerHTML = itemCompositionCnt;

	if ( (errorCode | this.E_NOERROR) != 0 ) {
		this.controlErrorBox( errorCode );
		return false;
	}
	else {
		return true;
	}
}

// EVENT DINYER
SelfChoiceHandler.eventDinyer = function ()
{
	if ( window.event ) {
		window.event.returnValue = false;
	}
}

// ONCLICK EVENT LISTENER NORMAL
SelfChoiceHandler.onClickCartBtn = function ( isBox )
{
	this.valid( isBox );

	var compositionCount	= document.getElementById("counter").innerHTML;
	var errorCode			= this.freeChecker( isBox, compositionCount );

	if ( (errorCode | this.E_NOERROR) != 0 ) {
		this.isSubmittable = false;
	}

	if ( this.isSubmittable ) {
		if ( this.countMinder >= 0 ) {
			document.forms[0].submit();
			return false;
		}
	}
	else {
		this.controlErrorBox( errorCode );
		return 1;
	}
}

// ONCLICK EVENT LISTENER WITH CONFIRM
SelfChoiceHandler.onClickCartBtnWithConfirm = function ( value )
{
	this.valid( 0 );

	var compositionCount	= document.getElementById("counter").innerHTML;
	var errorCode			= this.freeChecker( 0, compositionCount );

	if ( (errorCode | this.E_NOERROR) != 0 ) {
		this.isSubmittable = false;
	}

	var toSelectBagCereal = false;
	if ( value && document.getElementById('to_selectBagCereal').value ) {
		toSelectBagCereal = true;
	}

	if ( this.isSubmittable ) {
		if ( this.countMinder == 0 && !toSelectBagCereal ) {
			if ( confirm("この数量でカートにいれます。よろしいですか？") ) {
				document.forms[0].submit();
				return 0;
			}
		}
		else if ( this.countMinder >= 0 ) {
			document.forms[0].submit();
			return 0;
		}
	}
	else {
		this.controlErrorBox( errorCode );
		return false;
	}
}

// ONCLICK EVENT LISTENER FOR MEDIA
SelfChoiceHandler.onClickCartBtnForMedia = function ( value )
{
	this.valid( 0 );

	var compositionCount	= document.getElementById("counter").innerHTML;
	var errorCode			= this.freeChecker( 0, compositionCount );

	if ( (errorCode | this.E_NOERROR) != 0 ) {
		this.isSubmittable = false;
	}

	var toSelectBagCereal = false;
	if ( value && document.getElementById('to_selectBagCereal').value ) {
		toSelectBagCereal = true;
	}

	if ( this.isSubmittable ) {
		if ( this.countMinder == 0 && !toSelectBagCereal ) {
			if ( confirm("この数量でカートにいれます。よろしいですか？") ) {
				document.forms[0].submit();
				return 0;
			}
		}
		else if ( this.countMinder >= 0 ) {
			document.forms[0].submit();
			return 0;
		}
	}
	else {
		this.controlErrorBox( errorCode );
		return false;
	}
}

// WRAPPERS
function choiceAFormLoad() {
	SelfChoiceHandler.initialize();
	return SelfChoiceHandler.valid( 0 );
}

function valid( isBox ) {
	return SelfChoiceHandler.valid( isBox );
}

function cartButtonClickA(isBox) {
	return SelfChoiceHandler.onClickCartBtn( isBox );
}

function cartButtonClickB(isBox) {
	return SelfChoiceHandler.onClickCartBtnWithConfirm( isBox );
}

function cartButtonClickMedia(isBox) {
	return SelfChoiceHandler.onClickCartBtnForMedia( isBox );
}


//-----------------------------------------------------------
//配送指定・お支払い方法選択画面：入力可、不可設定
//-----------------------------------------------------------
function load_func(){
	var delivery_value = document.getElementById("delivery-01").checked;
	if(delivery_value){
		this.disable_calendar(true);
	}
	//disable_payment(-1);
	for(var i=0;i<=3;i++){
		var sel = 0;
		var payment = document.getElementById("payment_"+i).checked;
		if( payment ){
			disable_payment(i);
			sel = 1;
			break;
		}
	}
	if( !sel ){
		disable_payment( -1 );
	}
	this.changeCredit();
}

function changeCredit() {
	this.changePayment(document.getElementById("credit_company_select").value);
}

function changePayment(selected) {
	var totalCreditCom = document.getElementById("total_credit_company").value;
	//alert(totalCreditCom);
	for(var i = 1; i <= totalCreditCom; i++) {
		if(i < 10) var id = "0" + i;
		else 
		var id = i;
		if(document.getElementById(id)){
			document.getElementById(id).style.display = "none";
		}else{
			totalCreditCom++;
			//alert(totalCreditCom);
		}
	}
	if(document.getElementById(selected)){
		document.getElementById(selected).style.display = "";
	}
}

function exeSubmit(optionForm) {
	var id = document.getElementById("credit_company_select").value;
	var sel = document.getElementById( id ).value;
	//alert("ID:VAL"+id+":"+sel);
	document.getElementById("h_payment").value = sel;
	//alert(document.getElementById("h_payment").value);
}

function disable_calendar(boo){
	for(var i=1;i<=31;i++){
		var id = "r_cal_" + i;
		if(document.getElementById(id)){
			if(boo){
				document.getElementById( id ).checked = false;
			}
			document.getElementById( id ).disabled = boo;
		}
	}
}

function disable_payment(payment){
	//alert(payment);
	if(payment != 0){
		for(var i=0;i<3;i++){
			document.getElementById( "postal_pay_cnt_"+i ).checked = false;
			document.getElementById( "postal_pay_cnt_"+i ).disabled = true;
		}
	}else{
		for(var i=0;i<3;i++){
			if(document.getElementById("disable_conv_"+i) && !document.getElementById("disable_conv_"+i).value){
				document.getElementById( "postal_pay_cnt_"+i ).disabled = false;
			}else{
				document.getElementById( "postal_pay_cnt_"+i ).disabled = true;
			}
		}
	}
	if(payment != 3){
		document.getElementById("card_t_0").value = "";
		document.getElementById("card_t_0").disabled = true;
		document.getElementById("card_t_1").value = "";
		document.getElementById("card_t_1").disabled = true;
		document.getElementById("card_r_0").checked = false;
		document.getElementById("card_r_0").disabled = true;
		document.getElementById("card_r_1").checked = false;
		document.getElementById("card_r_1").disabled = true;
		document.getElementById("card_s_0").disabled = true;
		document.getElementById("card_s_0").selectedIndex = 0;
		document.getElementById("card_s_1").disabled = true;
		document.getElementById("card_s_1").selectedIndex = 0;
		document.getElementById("credit_company_select").disabled = true;
		document.getElementById("credit_company_select").selectedIndex = 0;
		if ( document.getElementsByName("payment_num") ) {
			var oPaymentNum = document.getElementsByName("payment_num");
			var len = oPaymentNum.length;
			for ( var i = 0; i < len; i++ ) {
				if ( oPaymentNum[i] ) {
					oPaymentNum[i].selectedIndex = 0;
					oPaymentNum[i].disabled = true;
				}
			}
		}

		var totalCreditCom = document.getElementById("total_credit_company").value;
			for(var i=1;i<=totalCreditCom;i++) {
			if(document.getElementById(i)){
				document.getElementById(i).disabled = true;
				document.getElementById(i).selectedIndex = 0;
				document.getElementById(i).style.display = "none";
			}
		}
	}else{
		document.getElementById("card_t_0").disabled = false;
		document.getElementById("card_t_1").disabled = false;
		document.getElementById("card_r_0").disabled = false;
		document.getElementById("card_r_1").disabled = false;
		document.getElementById("card_s_0").disabled = false;
		document.getElementById("card_s_1").disabled = false;
		document.getElementById("credit_company_select").disabled = false;
		var totalCreditCom = document.getElementById("total_credit_company").value;
			for(var i=1;i<=totalCreditCom;i++) {
			if(document.getElementById(i)){
				document.getElementById(i).disabled = false;
			}
		}
		if ( document.getElementsByName("payment_num") ) {
			var oPaymentNum = document.getElementsByName("payment_num");
			var len = oPaymentNum.length;
			for ( var i = 0; i < len; i++ ) {
				if ( oPaymentNum[i] ) {
					oPaymentNum[i].disabled = false;
				}
			}
		}
	}
}

//-----------------------------------------------------------
//商品番号入力欄クリア
//-----------------------------------------------------------
function offerMediaReset(){
	// 2008/05/20 変更
	// こんな設計あり得ないでしょ……
	for( i = 0; i <= 30; i++ ){
		if(document.getElementById("offer_no_"+i)){
			document.getElementById("offer_no_"+i).value = "";
		}
		if(document.getElementById("offer_cnt_"+i)){
			document.getElementById("offer_cnt_"+i).value = "";
		}
	}
}

function confirmOfferMedical(msg){
	if(!msg){
		//alert('no msg');
		return true;
	}
	for(i=1;i<=10;i++){
		var cnt = 0;
		if(document.getElementById("offer_no_"+i)){
			if(document.getElementById("offer_no_"+i).value){
				cnt = 1;
				break;
			}
		}
		if(document.getElementById("offer_cnt_"+i)){
			if(document.getElementById("offer_cnt_"+i).value){
				cnt = 1;
				break;
			}
		}
	}
	if(cnt){
		//alert('there is input');
		res = confirm(msg);
		if(res){
			document.getElementById("to_next_id").value=1;
			return true;
		}else{
			document.getElementById("to_next_id").value=0;
			return false;
		}
	}
	//alert('no input');
	document.getElementById("to_next_id").value=1;
	return true;
}
//-----------------------------------------------------------
//ショッピングトップ：エラーメッセージ
//-----------------------------------------------------------
function errApplicationNo(err_msg) {
	alert(err_msg);
}

//-----------------------------------------------------------
//共通：ポップアップ
//-----------------------------------------------------------
function wopen( popurl, popname ){
	window.open(popurl,popname,"scrollbars=yes,toolbar=no,location=no,directories=no,width=auto,height=auto"); 
	/*使い方サンプル
	javascript:wopen('http://({$HTTP_HOST})/html/health.html','health');
	*/
}
