/*
 * フィールドの値を上書きする
 */
function change_field_value(field_name, field_value){
	$(field_name).update('<input type="hidden" name="' + field_name + '" value="' + field_value + '" />');
}

/*
 * カテゴリ1から2を検索
 */
function FCategory1_Search_FCategory2(){

	var url = '?action=Js_FCategory1_Search_FCategory2';
	var queryStriong = 'f_category_1_id=' + $('f_category_1_id').value;
	new Ajax.Request(
		url, {
			method : 'get',
			parameters : queryStriong,
			onLoading : function(response)
			{
				$('f_category_3_id_area').update('');
				$('f_category_2_id_area').update('<img style="margin-left:22px;" src="System/images/CS_ajax-loader.gif" alt="Search..." width="16" hight="16" />');
			},
			onSuccess : function(response)
			{
				var resultText = response.responseText;
				$('f_category_3_id_area').update('');
				$('f_category_2_id_area').update(resultText);
			},
			onFailure : function(response)
			{
				$('f_category_3_id_area').update('');
				$('f_category_2_id_area').update('[System Error]');
			}
		}
	);

}

/*
 * カテゴリ2から3を検索
 */
function FCategory2_Search_FCategory3(){

	var url = '?action=Js_FCategory2_Search_FCategory3';
	var queryStriong = 'f_category_2_id=' + $('f_category_2_id').value;
	new Ajax.Request(
		url, {
			method : 'get',
			parameters : queryStriong,
			onLoading : function(response)
			{
				$('f_category_3_id_area').update('<img style="margin-left:22px;" src="System/images/CS_ajax-loader.gif" alt="Search..." width="16" hight="16" />');
			},
			onSuccess : function(response)
			{
				var resultText = response.responseText;
				$('f_category_3_id_area').update(resultText);
			},
			onFailure : function(response)
			{
				$('f_category_3_id_area').update('[System Error]');
			}
		}
	);

}


/*
 * カンプのダウンロード注意文
 */
function Media_Comprehensive(f_media_id){

	var url = '?action=Js_Media_Comprehensive';
	var queryStriong = 'f_media_id=' + f_media_id;
	new Ajax.Request(
		url, {
			method : 'get',
			parameters : queryStriong,
			onLoading : function(response)
			{
				$('comprehensive').update('<p class="center"><img src="System/images/MC_ajax-loader.gif" alt="Loading..." width="32" hight="32" /></p>');
			},
			onSuccess : function(response)
			{
				var resultText = response.responseText;
				$('comprehensive').update(resultText);
			},
			onFailure : function(response)
			{
				$('comprehensive').update('[System Error]');
			}
		}
	);

}


/*
 * ツリー構造を作る
 */
function FCategory_TreeVisible(this_id){
	var visible_id = this_id + '_p';
	var hide_id = this_id + '_m';
	$(visible_id).style.display = "none";
	$(hide_id).style.display = "block";
}
function FCategory_TreeHide(this_id){
	var visible_id = this_id + '_p';
	var hide_id = this_id + '_m';
	$(visible_id).style.display = "block";
	$(hide_id).style.display = "none";
}


/*
 * マウスオーバー時にTrail画像を出力
 */
var offsetfrommouse=[15,25];
var displayduration=0;
var defaultimageheight = 40;
var defaultimagewidth = 40;
var timer;
function gettrailobj(){
	if(document.getElementById)
	return document.getElementById("trailWrapper").style
}

function gettrailobjnostyle(){
	if(document.getElementById)
	return document.getElementById("trailWrapper")
}
function truebody(){
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}
function hidetrail(){	
	gettrailobj().display = "none";
	document.onmousemove = ""
	gettrailobj().left = "-500px"
	clearTimeout(timer);
}
function showtrail(imagename){
	i = imagename
	w = 400
	h = 400
	timer = setTimeout("show('"+i+"',w,h);",200);
}
function show(imagename,width,height){
	var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth - offsetfrommouse[0]
	var docheight=document.all? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight)
	if((navigator.userAgent.indexOf("Konqueror")==-1 || navigator.userAgent.indexOf("Firefox")!=-1 || (navigator.userAgent.indexOf("Opera")==-1 && navigator.appVersion.indexOf("MSIE")!=-1)) && (docwidth>650 && docheight>500)){
		( width == 0 ) ? width = defaultimagewidth: '';
		( height == 0 ) ? height = defaultimageheight: '';
		width+=20
		height+=20
		defaultimageheight = height
		defaultimagewidth = width
		document.onmousemove=followmouse; 
		newHTML = '<table class="photoTrail" style="width:' + width + 'px; height:'+ height +'px;"><tr><td><img src="' + imagename + '" border="0" alt="Loading....." /></td></tr></table>';
		if(navigator.userAgent.indexOf("MSIE")!=-1 && navigator.userAgent.indexOf("Opera")==-1 ){
			newHTML = newHTML+'<iframe src="about:blank" scrolling="no" frameborder="0" width="'+width+'" height="'+height+'"></iframe>';
		}		
		gettrailobjnostyle().innerHTML = newHTML;
		gettrailobj().display="block";
	}
}
function followmouse(e){
	var xcoord=offsetfrommouse[0]
	var ycoord=offsetfrommouse[1]
	var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15
	var docheight=document.all? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight)
	if (typeof e != "undefined"){
		if (docwidth - e.pageX < defaultimagewidth + 2*offsetfrommouse[0]){
			xcoord = e.pageX - xcoord - defaultimagewidth; // Move to the left side of the cursor
		} else {
			xcoord += e.pageX;
		}
		if (docheight - e.pageY < defaultimageheight + 2*offsetfrommouse[1]){
			ycoord += e.pageY - Math.max(0,(2*offsetfrommouse[1] + defaultimageheight + e.pageY - docheight - truebody().scrollTop));
		} else {
			ycoord += e.pageY;
		}
	} else if (typeof window.event != "undefined"){
		if (docwidth - event.clientX < defaultimagewidth + 2*offsetfrommouse[0]){
			xcoord = event.clientX + truebody().scrollLeft - xcoord - defaultimagewidth; // Move to the left side of the cursor
		} else {
			xcoord += truebody().scrollLeft+event.clientX
		}
		if (docheight - event.clientY < (defaultimageheight + 2*offsetfrommouse[1])){
			ycoord += event.clientY + truebody().scrollTop - Math.max(0,(2*offsetfrommouse[1] + defaultimageheight + event.clientY - docheight));
		} else {
			ycoord += truebody().scrollTop + event.clientY;
		}
	}
	gettrailobj().left=xcoord+"px"
	gettrailobj().top=ycoord+"px"
}


/*
 * name属性から配列を生成する関数$N()
 */

function $N(element) {
	var elms = document.getElementsByName(element);
	elmArray = Array();
	for(i = 0; i < elms.length; i++){
		elmArray.push(Element.extend(elms.item(i)));
	}
	return elmArray;
}


/*
 * チャージのリミットチェック
 */
function Charge_Limit(){

	var url = '?action=Js_Charge_Limit';
	var elms = $N('kind');
	var kind = '';
	for(i = 0; i < elms.length; i++){
		if(elms[i].checked){
			kind = elms[i].value;
		}
	}
	var queryStriong = 'kind=' + kind + '&amount=' + $('amount').value;
	new Ajax.Request(
		url, {
			method : 'get',
			parameters : queryStriong,
			onLoading : function(response)
			{
				$('js_charge_limit_area').update('');
			},
			onSuccess : function(response)
			{
				var resultText = response.responseText;
				$('js_charge_limit_area').update(resultText);
			},
			onFailure : function(response)
			{
				$('js_charge_limit_area').update('[System Error]');
			}
		}
	);

}


/*
 * クーポンのチェック
 */
function Charge_Coupon(){

	var url = '?action=Js_Charge_Coupon';
	var queryStriong = 'coupon=' + $('coupon').value;
	new Ajax.Request(
		url, {
			method : 'get',
			parameters : queryStriong,
			onLoading : function(response)
			{
				$('js_charge_coupon_area').update('');
			},
			onSuccess : function(response)
			{
				var resultText = response.responseText;
				$('js_charge_coupon_area').update(resultText);
			},
			onFailure : function(response)
			{
				$('js_charge_coupon_area').update('[System Error]');
			}
		}
	);

}


/*
 * クーポンによるボーナス
 */
function Charge_Coupon_Add(){

	var url = '?action=Js_Charge_Coupon_Add';
	var queryStriong = 'amount=' + $('amount').value + '&coupon=' + $('coupon').value;
	new Ajax.Request(
		url, {
			method : 'get',
			parameters : queryStriong,
			onLoading : function(response)
			{
				$('js_charge_coupon_add_area').update('');
			},
			onSuccess : function(response)
			{
				var resultText = response.responseText;
				$('js_charge_coupon_add_area').update(resultText);
			},
			onFailure : function(response)
			{
				$('js_charge_coupon_add_area').update('[System Error]');
			}
		}
	);

}

/*
 * チャージミックス
 */
function Charge(){
	Charge_Limit();
	Charge_Coupon();
	Charge_Coupon_Add();
}


/*
 * カートに追加
 */
function Cart_Add(f_media_id){

	var url = '?action=Js_Cart_Add';
	var queryStriong = 'f_media_id=' + f_media_id;
	var cart_add_area_name = 'cart_add_area_' + f_media_id;
	new Ajax.Request(
		url, {
			method : 'get',
			parameters : queryStriong,
			onLoading : function(response)
			{
				$(cart_add_area_name).update('<img src="System/images/BR_ajax-loader.gif" alt="カートに追加中" width="130" height="18" />');
			},
			onSuccess : function(response)
			{
				var resultText = response.responseText;
				$(cart_add_area_name).update(resultText);
			},
			onFailure : function(response)
			{
				$(cart_add_area_name).update('[System Error]');
			}
		}
	);

}


/*
 * カート一覧を表示
 */
function Pc_Public_Cart_List(){

	var url = '?action=Pc_Public_Cart_List';
	var queryStriong = 'js=true';
	new Ajax.Request(
		url, {
			method : 'get',
			parameters : queryStriong,
			onLoading : function(response)
			{
				$('Pc_Public_Cart_List_area').update('<p style="text-align:center; margin-top:120px;"><img src="System/images/CA_ajax-loader.gif" alt="Loading..." width="128" hight="15" /><br />' + $('js_Pc_Public_Cart_List_message1').value + '</p><p style="text-align:center; margin-top:2em;">' + $('js_Pc_Public_Cart_List_message2').value + '</p>');
			},
			onSuccess : function(response)
			{
				var resultText = response.responseText;
				$('Pc_Public_Cart_List_area').update(resultText);
			},
			onFailure : function(response)
			{
				$('Pc_Public_Cart_List_area').update('[System Error]');
			}
		}
	);

}


/*
 * カートから削除
 */
function Cart_Delete(f_media_id){

	var url = '?action=Js_Cart_Delete';
	var queryStriong = 'f_media_id=' + f_media_id;
	var cart_delete_area_name = 'cart_item_area_' + f_media_id;
	new Ajax.Request(
		url, {
			method : 'get',
			parameters : queryStriong,
			onLoading : function(response)
			{
				$(cart_delete_area_name).update('<p class="center" style="margin:30px 0;"><img src="System/images/MC_ajax-loader.gif" alt="Loading..." width="32" hight="32" /></p>');
			},
			onSuccess : function(response)
			{
				var resultText = response.responseText;
				$(cart_delete_area_name).update(resultText);
				Cart_Delete_ReAddMessage(f_media_id);
				Cart_ChangeLicense_Receipt();
			},
			onFailure : function(response)
			{
				$(cart_delete_area_name).update('[System Error]');
			}
		}
	);

}

/*
 * カートから削除したあと元に戻すメッセージ
 */
function Cart_Delete_ReAddMessage(f_media_id){

	var url = '?action=Js_Cart_Delete';
	var queryStriong = 'f_media_id=' + f_media_id + '&mode=message';
	new Ajax.Request(
		url, {
			method : 'get',
			parameters : queryStriong,
			onLoading : function(response)
			{
				$('cart_readd_message').update('<p class="center" style="margin:30px 0;"><img src="System/images/MC_ajax-loader.gif" alt="Loading..." width="32" hight="32" /></p>');
			},
			onSuccess : function(response)
			{
				var resultText = response.responseText;
				$('cart_readd_message').update(resultText);
			},
			onFailure : function(response)
			{
				$('cart_readd_message').update('[System Error]');
			}
		}
	);

}


/*
 * カートに入っているライセンスを変更する
 */
function Cart_ChangeLicense(f_media_id){

	var url = '?action=Js_Cart_ChangeLicense';
	var Cart_ChangeLicense_select = 'Cart_ChangeLicense_select_' + f_media_id;
	var queryStriong = 'f_media_id=' + f_media_id + '&ms_license_id=' + $(Cart_ChangeLicense_select).value;
	var cart_ChangeLicense_main_css_name = 'cart_item_css_area_' + f_media_id;
	var cart_ChangeLicense_main_alert_name = 'cart_item_alert_area_' + f_media_id;
	new Ajax.Request(
		url, {
			method : 'get',
			parameters : queryStriong,
			onSuccess : function(response)
			{
				var resultText = response.responseText;
				if(resultText == "success"){
					Element.removeClassName($(cart_ChangeLicense_main_css_name), "Unselected"); 
					$(cart_ChangeLicense_main_alert_name).style.display = "none";
				}else{
					Element.addClassName($(cart_ChangeLicense_main_css_name), "Unselected"); 
					$(cart_ChangeLicense_main_alert_name).style.display = "block";
				}
				Cart_ChangeLicense_Receipt();
			},
			onFailure : function(response)
			{
				Pc_Public_Cart_List();
			}
		}
	);

}


/*
 * カートに入っているライセンスから明細を変更する
 */
function Cart_ChangeLicense_Receipt(){

	var url = '?action=Js_Cart_ChangeLicense_Receipt';
	var queryStriong = '';
	new Ajax.Request(
		url, {
			method : 'get',
			parameters : queryStriong,
			onLoading : function(response)
			{
				$('Cart_ChangeLicense_Receipt').update('<p class="center" style="margin:20px 20px;"><img src="System/images/MC_ajax-loader.gif" alt="Loading..." width="32" hight="32" /></p>');
			},
			onSuccess : function(response)
			{
				var resultText = response.responseText;
				$('Cart_ChangeLicense_Receipt').update(resultText);
			},
			onFailure : function(response)
			{
				$('Cart_ChangeLicense_Receipt').update('[System Error]');
			}
		}
	);

}

