//confirmBox.js

//商品購入時、確認ボックスを表示する
function dispConfirmBox(articleId,episodeId,memberId,isSample,viwsingtype,authflag){
	/*var end = location.href.indexOf(":",0);
	var p   = location.href.substring(0,end);
	var domain = location.hostname;
	domain = p+"://"+domain;
	var url = domain+"/buy_program_confirm.php?cache=" + (new Date()).getTime();


	jQuery('<form id="post" method-"post" action="' + url +'"/>').appendTo( document.body );
	jQuery('<input type="hidden" name="ARTICLE_ID" value="' + articleId + '" />').appendTo( '#post' );
	jQuery('<input type="hidden" name="EPISODE_ID" value="' + episodeId + '" />').appendTo( '#post' );
	$('#post').submit();
	return false;*/

	//iPhone&iPad&Androidの場合
	if(authflag == 'iphone' || authflag == 'ipad' || authflag == 'android'){
		AuthCheckBox();
		return false;
	}

	//MACの場合
	if(viwsingtype == '2' && checkMac()){
		MacConfirmBox();
		return false;
	}
	var mode = document.documentMode || 0;
	var ie6 = $.browser.msie && /MSIE 6.0/.test(navigator.userAgent) && !mode;
	var end = location.href.indexOf(":",0);
	var p   = location.href.substring(0,end);
	var domain = location.hostname;
	domain = p+"://"+domain;

	if(document.getElementById("LOGIN_ID")){
		var loginid = document.getElementById("LOGIN_ID").value;
	}else{
		var loginid = null;
	}
	if(document.getElementById("PASSWORD")){
		var password = document.getElementById("PASSWORD").value;
	}else{
		var password = null;
	}

	$.ajax({
		type : "POST",
		url : domain+"/getConfirmHtml_ajax.php?cache=" + (new Date()).getTime(),
		data: {
				"ARTICLE_ID": articleId,
				"EPISODE_ID":episodeId,
				"MEMBER_ID":memberId,
				"LOGIN_ID":loginid,
				"PASSWORD":password,
				"IS_SAMPLE":isSample
			},
		dataType : "json",
		success : function(value){
			if(value == "REFRESH"){
				window.location.reload();
				return false;
			}
			if(value == "SAMPLEMOVIE"){
				popupMovie("http://"+location.hostname+"/getAnimation.php?EPISODE_ID="+episodeId+"&VIDEO_ID="+articleId+"","","540","324",authflag);
				return false;
			}
			//PHPで生成した確認画面（HTML）を画面に追加
			var div = $("<DIV>");
			div.html(value);
			$('body').append(div);

			//確認画面オブジェクトのID
			var id = "#dialog";

			//ブラックアウトする領域の指定
			$('#mask').css({'width':'100%','height':'100%'});
			if(!ie6){
				$('#mask').css('position',  'fixed');
			}

			//ブラックアウト効果の設定
			$('#mask').fadeIn(500);//表示スピード
			$('#mask').fadeTo(1,0.5);//透過率、透過スピード

			//確認画面のCSS定義
			$(id).css('position',  'fixed');
			//確認画面のCSS定義
			$(id).css('position',  'fixed');

			if (typeof document.body.style.maxHeight != "undefined") {
				$(id).css('top',  '50%');
				$(id).css('left', '50%');

			} else {
				var h = $(id).height()*2.0
				var w = $(id).width()*1.3
				$(id).css('top',  h+'px');
				$(id).css('left', w+'px');

			}
			var h = $(id).height()/2*(-1)
			var w = $(id).width()/2*(-1)
			$(id).css('margin-left',  w+'px');
			$(id).css('margin-top', h+'px');
			$(id).css('_display', 'block');
			if(ie6){
				$('select').css('display','none');
			}

			//確認画面の表示&表示スピードの設定
			$(id).fadeIn(1000);
		},
		error:function(data){
			alert('通信に失敗しました');
			return false;
		}
	});
}

//確認ボックスを閉じる
function boxClose() {
	var mode = document.documentMode || 0;
	var ie6 = $.browser.msie && /MSIE 6.0/.test(navigator.userAgent) && !mode;
	$('#mask').remove();
	$('#confirm').remove();
	$('#boxes').remove();

	if(ie6){
		$('select').css('display','block');
	}
}
//DL回数をカウント
function dlCountUp(licenseId,url,authflag){

	//iPhone&iPad&Androidの場合
	if(authflag == 'iphone' || authflag == 'ipad' || authflag == 'android'){
		AuthCheckBox();
		return false;
	}

	//MACの場合
	if(checkMac()){
		MacConfirmBox();
		return false;
	}
	var end = location.href.indexOf(":",0);
	var p   = location.href.substring(0,end);
	var domain = location.hostname;
	domain = p+"://"+domain;
	//domain = "https://"+domain;
	$.ajax({
		type : "POST",
		url : domain+"/dlCountUp_ajax.php?cache=" + (new Date()).getTime(),
		data: {
				"LICENSE_ID": licenseId
			},
		dataType : "json",
		success : function(value){

			//ダウンロード有効回数制限
			if(value == "DL_CNT_NG"){
				alert("ダウンロード有効回数を超えています。");
				return;
			}

			//GET
			if(value == "OK"){
				location.href = url;
				//window.open(url);
			}else if(value == "NG"){
				location.href = domain+"/sysError.php";
			}
		},
		error:function(data){
			alert('通信に失敗しました');
			return false;
		}
	});
}

function openWebSales(url){
	window.open(url);
	boxClose();
}
//解約する
function withdrawConfirmBox(authflag){

	//iPhone&iPad&Androidの場合
	if(authflag == 'iphone' || authflag == 'ipad' || authflag == 'android'){
		AuthCheckBox();
		return false;
	}

	//MACの場合
	/*if(checkMac()){
		alert('対応範囲外');
		return false;
	}*/
	var mode = document.documentMode || 0;
	var ie6 = $.browser.msie && /MSIE 6.0/.test(navigator.userAgent) && !mode;
	var end = location.href.indexOf(":",0);
	var p   = location.href.substring(0,end);
	var domain = location.hostname;
	domain = p+"://"+domain;

	//PHPで生成した確認画面（HTML）を画面に追加
	var div = $("<DIV>");
	var value ="";

	value  = "<div id=\"boxes\"> ";
	value += "<form id=\"confirm\" method=\"POST\"> ";
	value += " <table id=\"dialog\" class=\"window\" style=\"text-align:center;\">  ";
	value += " <tr>";
	value += " <td style=\"border:solid 1px #FF9900;padding: 0px;margin:3px;height:30px;background-color:#FF9900\"> ";
	value += " <font style=\"font-size:30;color:#FFFFFF;\"><b>解約する</b></font>";
	value += " </td>";
	value += " </tr>";
	value += " <tr>";
	value += " <td style='height:80px;'><br /><br /><br />解約と同時に、月額動画を視聴する事ができなくなってしまいます。<br />解約してもよろしいですか?</td>";
	value += " </tr>";
	value += " <tr>";
	value += " <td>";
	value += " <input style=\"width:80px;\" onclick=\"$('#due_withdraw').submit();\" type=\"button\" value=\"OK\" />";
	value += " <input style=\"width:80px;\" onclick=\"boxClose()\" class=\"close\" type=\"button\" value=\"キャンセル\"/>";
	value += " </td>";
	value += " </tr>";
	value += " </table> ";
	value += "</form> ";
	value += "</div>";
	value += "<div id=\"mask\" onCtrlKey=\"resize()\">";

	div.html(value);
	$('body').append(div);

	//確認画面オブジェクトのID
	var id = "#dialog";

	//ブラックアウトする領域の指定
	$('#mask').css({'width':'100%','height':'100%'});
	if(!ie6){
		$('#mask').css('position',  'fixed');
	}

	//ブラックアウト効果の設定
	$('#mask').fadeIn(500);//表示スピード
	$('#mask').fadeTo(1,0.5);//透過率、透過スピード

	//確認画面のCSS定義
	$(id).css('position',  'fixed');

	if (typeof document.body.style.maxHeight != "undefined") {
		$(id).css('top',  '50%');
		$(id).css('left', '50%');

	} else {
		var h = $(id).height()*2.0
		var w = $(id).width()*1.3

		$(id).css('top',  '50%');
		$(id).css('left', '50%');

	}
	var h = $(id).height()/2*(-1)
	var w = $(id).width()/2*(-1)
	$(id).css('margin-left',  w+'px');
	$(id).css('margin-top', h+'px');
	$(id).css('_display', 'block');
	if(ie6){
		$('select').css('display','none');
	}

	//確認画面の表示&表示スピードの設定
	$(id).fadeIn(1000);

}

//MACの場合:対応範囲外
function MacConfirmBox(){

	var mode = document.documentMode || 0;
	var ie6 = $.browser.msie && /MSIE 6.0/.test(navigator.userAgent) && !mode;
	var end = location.href.indexOf(":",0);
	var p   = location.href.substring(0,end);
	var domain = location.hostname;
	domain = p+"://"+domain;

	//PHPで生成した確認画面（HTML）を画面に追加
	var div = $("<DIV>");
	var value ="";

	value  = "<div id=\"boxes\"> ";
	value += "<form id=\"confirm\" method=\"POST\"> ";
	value += " <table id=\"dialog\" class=\"window\" style=\"text-align:center;\">  ";
	value += " <tr>";
	value += " <td style=\"border:solid 1px #FF9900;padding: 0px;margin:3px;height:30px;background-color:#FF9900\"> ";
	value += " <font style=\"font-size:30;color:#FFFFFF;\"></font>";
	value += " </td>";
	value += " </tr>";
	value += " <tr>";
	value += " <td><br><br>この動画はWMVファイル形式です。<br>Machintoshではご利用いただけません。</td>";
	value += " </tr>";
	value += " <tr>";
	value += " <td>";
	value += " <input style=\"width:80px;\" onclick=\"boxClose()\" type=\"button\" value=\"OK\" />";
	value += " </td>";
	value += " </tr>";
	value += " </table> ";
	value += "</form> ";
	value += "</div>";
	value += "<div id=\"mask\" onCtrlKey=\"resize()\">";

	div.html(value);
	$('body').append(div);

	//確認画面オブジェクトのID
	var id = "#dialog";

	//ブラックアウトする領域の指定
	$('#mask').css({'width':'100%','height':'100%'});
	if(!ie6){
		$('#mask').css('position',  'fixed');
	}

	//ブラックアウト効果の設定
	$('#mask').fadeIn(500);//表示スピード
	$('#mask').fadeTo(1,0.5);//透過率、透過スピード

	//確認画面のCSS定義
	$(id).css('position',  'fixed');

	if (typeof document.body.style.maxHeight != "undefined") {
		$(id).css('top',  '50%');
		$(id).css('left', '50%');

	} else {
		var h = $(id).height()*2.0
		var w = $(id).width()*1.3

		$(id).css('top',  '50%');
		$(id).css('left', '50%');

	}
	var h = $(id).height()/2*(-1)
	var w = $(id).width()/2*(-1)
	$(id).css('margin-left',  w+'px');
	$(id).css('margin-top', h+'px');
	$(id).css('_display', 'block');
	if(ie6){
		$('select').css('display','none');
	}

	//確認画面の表示&表示スピードの設定
	$(id).fadeIn(1000);
}

//iPhone&iPad&Androidの場合
function AuthCheckBox(){

	var mode = document.documentMode || 0;
	var ie6 = $.browser.msie && /MSIE 6.0/.test(navigator.userAgent) && !mode;
	var end = location.href.indexOf(":",0);
	var p   = location.href.substring(0,end);
	var domain = location.hostname;
	domain = p+"://"+domain;

	//PHPで生成した確認画面（HTML）を画面に追加
	var div = $("<DIV>");
	var value ="";

	value  = "<div id=\"boxes\"> ";
	value += "<form id=\"confirm\" method=\"POST\"> ";
	value += " <table id=\"dialog\" class=\"window\" style=\"text-align:center;\">  ";
	value += " <tr>";
	value += " <td style=\"border:solid 1px #FF9900;padding: 0px;margin:3px;height:30px;background-color:#FF9900\"> ";
	value += " <font style=\"font-size:30;color:#FFFFFF;\"></font>";
	value += " </td>";
	value += " </tr>";
	value += " <tr>";
	value += " <td><br><br>大変申し訳ございません。お使いの機種からは御利用頂くことができません。</td>";
	value += " </tr>";
	value += " <tr>";
	value += " <td>";
	value += " <input style=\"width:80px;\" onclick=\"boxClose()\" type=\"button\" value=\"OK\" />";
	value += " </td>";
	value += " </tr>";
	value += " </table> ";
	value += "</form> ";
	value += "</div>";
	value += "<div id=\"mask\" onCtrlKey=\"resize()\">";

	div.html(value);
	$('body').append(div);

	//確認画面オブジェクトのID
	var id = "#dialog";

	//ブラックアウトする領域の指定
	$('#mask').css({'width':'100%','height':'100%'});
	if(!ie6){
		$('#mask').css('position',  'fixed');
	}

	//ブラックアウト効果の設定
	$('#mask').fadeIn(500);//表示スピード
	$('#mask').fadeTo(1,0.5);//透過率、透過スピード

	//確認画面のCSS定義
	$(id).css('position',  'fixed');

	if (typeof document.body.style.maxHeight != "undefined") {
		$(id).css('top',  '50%');
		$(id).css('left', '50%');

	} else {
		var h = $(id).height()*2.0
		var w = $(id).width()*1.3

		$(id).css('top',  '50%');
		$(id).css('left', '50%');

	}
	var h = $(id).height()/2*(-1)
	var w = $(id).width()/2*(-1)
	$(id).css('margin-left',  w+'px');
	$(id).css('margin-top', h+'px');
	$(id).css('_display', 'block');
	if(ie6){
		$('select').css('display','none');
	}

	//確認画面の表示&表示スピードの設定
	$(id).fadeIn(1000);
}

//SBPS連携時仮売上を立てて、ポストパラメータを生成する
function postSbps(point_course_id,mode,url){
	var end = location.href.indexOf(":",0);
	var p   = location.href.substring(0,end);
	var domain = location.hostname;
	domain = p+"://"+domain;
	$.ajax({
		type : "POST",
		url : domain+"/SBPSconnect.php?cache=" + (new Date()).getTime(),
		data: {
				"POINT_COURSE_ID": point_course_id,
				"MODE":mode
			},
		dataType : "json",
		success : function(value){

			//フォームのIDを生成
			var id = "#"+value[0];

			//PHPで生成したポストパラメータをフォームに追加
			$(id).append(value[1]);

			//文字コード変換してサブミット
			buff=document.charset;
			document.charset='shift_jis';
			submitBtn(value[0],url);
			document.charset=buff;
		},
		error:function(data){
			alert('通信に失敗しました');
			return false;
		}
	});
}

//
function payRedirect(articleId,episodeId,url,mode){
	var payType;
	var paytypes = document.getElementsByName( "PAY_TYPE");
	var quickcharge;
	var quickcharges = document.getElementsByName("QUICK_CHARGE");
	var modeflg;
	for(var k=0;k<paytypes.length;k++){
		if(paytypes[k].checked){
			payType = paytypes[k].value;
		}
	}
	for(var k=0;k<quickcharges.length;k++){
		if(quickcharges[k].checked == true){
			//alert("checked！");
			quickcharge = "1";
		}
	}


	if(quickcharge ==1){
		modeflg = 1;
		//alert("modeflg:1");
	}else{
		modeflg = mode;
		//alert("modeflg:"+mode);
	}

	if(payType == 1){
		postZero(articleId,episodeId,url,modeflg);
//		postZero(articleId,episodeId,url,mode);
//		postZero(articleId,episodeId,url,"1");
	}else if(payType == 2){
		boxClose();
	}else if(payType == 3){
		boxClose();
		postBitCash(articleId,episodeId);
	}else{
		boxClose();
	}
}

var dCheck = true;
//ZERO連携時仮売上を立てて、ポストパラメータを生成する
function postZero(articleId,episodeId,url,mode){

	if(dCheck){
		//二重クリック防止用変数
		dCheck = false;

//エラーになるのでとりあえずコメントアウト　moriyoshi
		//QuickChargeの時だけ
		if(mode == 1){
			jQuery.blockUI({
		        message: jQuery('#settlementMes'),
		        css: { top: '20%' , padding: '15px'}
		    });
		}

		var end = location.href.indexOf(":",0);
		var p   = location.href.substring(0,end);
		var domain = location.hostname;
		domain = p+"://"+domain;
		$.ajax({
			type : "POST",
			url : domain+"/ZEROconnect.php?cache=" + (new Date()).getTime(),
			data: {
					"ARTICLE_ID": articleId,
					"EPISODE_ID": episodeId,
					"MODE":mode
				},
			dataType : "json",
			success : function(value){

				if(mode == 0){
					if(value[0]=="OK"){
						//PHPで生成したポストパラメータをフォームに追加
						$("#buy").append(value[1]);

						//サブミット
						$("#" + "buy").attr('action', url);
						$("#" + "buy").submit();

					}else{
						//エラーページへ遷移
						//PHPで生成したポストパラメータをフォームに追加
						$("#error").append(value[1]);
						submitBtn('error','/buy_program_error.php');
					}
				}else if(mode == 1){
					if(value[0]=="OK"){
						jQuery.unblockUI({});
						//$("#quickChargeOK").append(value[1]);
						//$("#quickChargeOK").append(value[2]);
						//$("#quickChargeOK").append(value[3]);
						//$("#quickChargeOK").append(value[4]);
						//submitBtn('quickChargeOK','/buy_program_complete.php');
						$("#error").append(value[1]);
						submitBtn('error','/buy_program_error.php');
					}else{
						//エラーページへ遷移
						//PHPで生成したポストパラメータをフォームに追加
						$("#error").append(value[1]);
						submitBtn('error','/buy_program_error.php');
					}
				}else{

				}

			},
			error:function(data){
				jQuery.unblockUI({});
				alert('通信に失敗しました');
				return false;
			}
		});
	}
}

//BitCash
function postBitCash(articleId,episodeId){
	var mode = document.documentMode || 0;
	var ie6 = $.browser.msie && /MSIE 6.0/.test(navigator.userAgent) && !mode;
	var end = location.href.indexOf(":",0);
	var p   = location.href.substring(0,end);
	var domain = location.hostname;
	domain = p+"://"+domain;

	if(document.getElementById("LOGIN_ID")){
		var loginid = document.getElementById("LOGIN_ID").value;
	}else{
		var loginid = null;
	}
	if(document.getElementById("PASSWORD")){
		var password = document.getElementById("PASSWORD").value;
	}else{
		var password = null;
	}
	if(document.getElementById("CARD_NUMBER1")){
		var cardnum1 = document.getElementById("CARD_NUMBER1").value;
	}else{
		var cardnum1 = null;
	}
	if(document.getElementById("CARD_NUMBER2")){
		var cardnum2 = document.getElementById("CARD_NUMBER2").value;
	}else{
		var cardnum2 = null;
	}
	$.ajax({
		type : "POST",
		url : domain+"/BitCashconnect_ajax.php?cache=" + (new Date()).getTime(),
		data: {
				"ARTICLE_ID": articleId,
				"EPISODE_ID":episodeId,
				"LOGIN_ID":loginid,
				"PASSWORD":password,
				"CARD_NUMBER1":cardnum1,
				"CARD_NUMBER2":cardnum2
			},
		dataType : "json",
		success : function(value){
			//PHPで生成した確認画面（HTML）を画面に追加
			var div = $("<DIV>");
			div.html(value);
			$('body').append(div);

			//確認画面オブジェクトのID
			var id = "#dialog";

			//ブラックアウトする領域の指定
			$('#mask').css({'width':'100%','height':'100%'});
			if(!ie6){
				$('#mask').css('position',  'fixed');
			}

			//ブラックアウト効果の設定
			$('#mask').fadeIn(500);//表示スピード
			$('#mask').fadeTo(1,0.5);//透過率、透過スピード

			//確認画面のCSS定義
			$(id).css('position',  'fixed');
			//確認画面のCSS定義
			$(id).css('position',  'fixed');

			if (typeof document.body.style.maxHeight != "undefined") {
				$(id).css('top',  '50%');
				$(id).css('left', '50%');

			} else {
				var h = $(id).height()*2.0
				var w = $(id).width()*1.3
				$(id).css('top',  h+'px');
				$(id).css('left', w+'px');

			}
			var h = $(id).height()/2*(-1)
			var w = $(id).width()/2*(-1)
			$(id).css('margin-left',  w+'px');
			$(id).css('margin-top', h+'px');
			$(id).css('_display', 'block');
			if(ie6){
				$('select').css('display','none');
			}

			//確認画面の表示&表示スピードの設定
			$(id).fadeIn(1000);
		},
		error:function(data){
			alert('通信に失敗しました');
			return false;
		}
	});
}

//楽天連携時仮売上を立てて、ポストパラメータを生成する
function postRakuten(articleId,episodeId,url){

	if(dCheck){
		//二重クリック防止用変数
		dCheck = false;

		var end = location.href.indexOf(":",0);
		var p   = location.href.substring(0,end);
		var domain = location.hostname;
		domain = p+"://"+domain;
		$.ajax({
			type : "POST",
			url : domain+"/Rakutenconnect.php?cache=" + (new Date()).getTime(),
			data: {
					"ARTICLE_ID": articleId,
					"EPISODE_ID":episodeId
				},
			dataType : "json",
			success : function(value){

				if(value[0]=="OK"){
					//PHPで生成したポストパラメータをフォームに追加
					$("#buy").append(value[1]);

					//サブミット
					$("#" + "buy").attr('action', url);
					$("#" + "buy").submit();

				}else{
					//エラーページへ遷移
					//PHPで生成したポストパラメータをフォームに追加
					$("#error").append(value[1]);
					submitBtn('error','/buy_program_error.php');
				}

			},
			error:function(data){
				jQuery.unblockUI({});
				alert('通信に失敗しました');
				return false;
			}
		});
	}
}

//SBPS連携ポストパラメータを生成する
function createSbpsParam(url){
	var end = location.href.indexOf(":",0);
	var p   = location.href.substring(0,end);
	var domain = location.hostname;
	domain = p+"://"+domain;
	$.ajax({
		type : "GET",
		url : domain+"/SBPSconnect_infoReg.php?cache=" + (new Date()).getTime(),
		dataType : "json",
		success : function(value){
			if(value.success != true) {
				alert('通信に失敗しました');
			}
			else {
				buff=document.charset;
				document.charset='shift_jis';
				var param = value.param;
				for (key in param) {
					str = '<input type="hidden" name="' + key +'" value="' + param[key] + '" />';
					$(str).appendTo('#SBPSREG');
				}
				$('#SBPSREG').attr('method', 'post').attr('action', url).attr('accept-charset', 'shift_jis').submit();
				document.charset=buff;
			}
			return false;
		},
		error:function(data){
			alert('通信に失敗しました');
			return false;
		}
	});
}

//sample download
function sampleDL(){
	//MACの場合
	if(checkMac()){
		MacConfirmBox();
		return false;
	}

	location.href ="http://bw.uliza.jp/ulwmvdl2/503/107_sample_wmv_samplewmv_sample_wmv_230383_1372000_640_360_24_20110512171735.wmv";
	//window.open("http://bw.uliza.jp/ulwmvdl2/503/107_12_612_PV-RD-0358-wmv-_110825_1404000_320_240_24_20101201131759.wmv");
}

//MAC OSチェック
function checkMac(){
	var xLBua=navigator.userAgent;
	if(xLBua.indexOf("Mac OS X")!=-1){
		return true;
	}else{
		if(xLBua.indexOf("Mac")!=-1){
			return true;
		}
	}

	return false;
}
//通販のカートを見る
function showCart(authflag,url){

	//iPhone&iPad&Androidの場合
	if(authflag == 'iphone' || authflag == 'ipad' || authflag == 'android'){
		AuthCheckBox();
		return false;
	}else{
		window.open(url,target="_blank");
	}
}
