﻿var indexCode = document.getElementById("jsIndexCode").value;
var jsCur = document.getElementById("jsCur").value;
var jsTypeID = document.getElementById("jsTypeID").value;
var jsCount = document.getElementById("jsCount").value;
var jsMainHobbyType = document.getElementById("jsMainHobbyType").value;
var jsCurName = document.getElementById("jsCurName").value;
var jsSymbol = document.getElementById("jsSymbol").value;

var featureCodeStr = document.getElementById("jsFeatureCode").value;
var featureCodeArray = featureCodeStr.split("@");
var suffixCodeStr = document.getElementById("jsSuffixCode").value;
var suffixCodeArray = suffixCodeStr.split("@");
var mainTypePointStr = document.getElementById("jsMainTypePoint").value;
var mainTypePointArray = mainTypePointStr.split("@");

var hobbyFloatPriceStr = document.getElementById("jsHobbyFloatPrice").value;
var hobbyFloatPriceArray = hobbyFloatPriceStr.split("@");
var HFP_sffixCodeStr = document.getElementById("js_HFP_sffixCode").value;
var HFP_sffixCodeArray = HFP_sffixCodeStr.split("@");

var photoPathStr = document.getElementById("jsphotopath").value;
var photoPathArray = photoPathStr.split("@");
var HP_sffixCodeStr = document.getElementById("js_HP_sffixCode").value;
var HP_sffixCodeArray = HP_sffixCodeStr.split("@");

var HobbyCode = new Array();
var HobbyCode2 = new Array();
var HobbyFloatPrice = new Array();
var HobbyPhotos = new Array();

for(var i =0;i<featureCodeArray.length;i++){
	HobbyCode[featureCodeArray[i]] = suffixCodeArray[i];//???
}

for(var i =0;i<mainTypePointArray.length;i++){
	HobbyCode2.push({code:mainTypePointArray[i], codelist:suffixCodeArray[i]});
}

for(var i =0;i<HFP_sffixCodeArray.length;i++){
	HobbyFloatPrice[HFP_sffixCodeArray[i]] = hobbyFloatPriceArray[i];
}

for(var i =0;i<HP_sffixCodeArray.length;i++){
	HobbyPhotos.push({code: HP_sffixCodeArray[i], path: photoPathArray[i]});
}

function toPage(pageSize){
	$.ajax({url:jsMainSiteUrl+'ProductShowAjax.cfm?cur='+jsCur,
		   type:'get',
		   dataType:'html',
		 data:'&PageID='+pageSize+'&IndexCode='+indexCode
		 +'&TypeId='+jsTypeID+'&count='+jsCount,
		 error:function(XMLHttpRequest){this;},success:function(data){$('#listProducts').html(data);$('#curPage').text(pageSize);}
		   });
}

function setLastProductPrice(IsDiscountLoss5,IsDiscount,Discount){
	var strhobbycode = "";
	var isSelect = 0;
	var AddToSelect = 0;
	var strhobby = "";
	var isCustom = 0;
	var chgStock = true;
	for (i=1;i<=document.getElementById("HobbyCount").value;i++){
		ID = "HobbyName"+i;
		ID = String(document.getElementById(ID).value);
		UpdateTogeterFirstHoby(document.getElementById("Hobby"+i).value,i);
		if(String(document.getElementById("Hobby"+i).value)!=0){
			ID = ID + String(document.getElementById("Hobby"+i).value);
			ID = document.getElementById(ID).innerHTML;
			strhobbycode = ID;
			strhobby = strhobby + ID;
			if((strhobbycode).indexOf("T"+jsMainHobbyType) >=0 ){
				for(var i1=0;i1<HobbyCode2.length;i1++){
					if (strhobbycode == HobbyCode2[i1].code) {
						if(HobbyFloatPrice[HobbyCode2[i1].codelist]){
						AddToSelect = Math.max(HobbyFloatPrice[HobbyCode2[i1].codelist],AddToSelect);
						}
					}
				}
				newprice = (parseFloat(document.getElementById("productcurprice2").innerHTML) + parseFloat(AddToSelect));
				if(parseFloat(AddToSelect)==0){
					oldprice = parseFloat(document.getElementById("productoldprice2").innerHTML);
				}else{
					if(IsDiscountLoss5==1){
						oldprice = newprice*2;
					}else{
						if(IsDiscount==1){
							oldprice = newprice*100/Discount;
						}else{
							oldprice = newprice;
						}
					}
				}
				isSelect = 1;
			}
		}else{
			chgStock = false;
			ID = ID + String(document.getElementById("Hobby"+i).value);
			ID = document.getElementById(ID).innerHTML;
			strhobby = strhobby + ID;
		}
		if(document.getElementById("Hobby"+i).disabled==true){
			isCustom = i;
		}
	}
	var isUnStockEnd = document.getElementById("isUnStockEnd");
	if(chgStock && isUnStockEnd && isUnStockEnd.value ==1)
	{
		for(var i1=0;i1<featureCodeArray.length;i1++){
			if (strhobby == featureCodeArray[i1]) {
				if(document.getElementById('stock_'+suffixCodeArray[i1])){
					stockNum = document.getElementById('stock_'+suffixCodeArray[i1]).value;
				}else{
					stockNum = 0;
				}
			}
		}
		var f1 = 0;
		var s2 = 0;
		var t3 = 0;
		if(stockNum >= 100)
		{
			f1 = parseInt(stockNum/100);
			s2 = parseInt((stockNum - f1*100)/10);
			t3 = stockNum - f1*100 - s2*10;
		}
		else if(stockNum >= 10)
		{
			s2 = parseInt(stockNum/10);
			t3 = stockNum - s2*10;
		}
		else{
			t3 = stockNum;
		}
		document.getElementById('stock_f1').innerHTML = f1;
		document.getElementById('stock_s2').innerHTML = s2;
		document.getElementById('stock_t3').innerHTML = t3;
	}else{
		stockNum = 0;	
	}
	//document.getElementById('stockMaxNum').value = stockNum;
	if(isSelect==0){
		oldprice = parseFloat(document.getElementById("productoldprice2").innerHTML);
		newprice = parseFloat(document.getElementById("productcurprice2").innerHTML);
	}
	if(isCustom!=0){
		checkCustom(isCustom,IsDiscountLoss5,IsDiscount,Discount);
		return;
	}
//	if(isfull==1){
//		newprice = (parseFloat(document.getElementById("productcurprice2").innerHTML) + parseFloat(HobbyFloatPrice[HobbyCode[strhobbycode]]));
//	}else{
//		for(var i=0;i<HobbyCode2.length;i++){
//		}
//	}
	if(jsCurName != "jpy" && jsCurName != 'idr' && jsCurName != 'rub')
	{
		oldprice = oldprice.toFixed(2);
		newprice = newprice.toFixed(2);
	}
	else
	{
		oldprice = oldprice.toFixed(0);
		newprice = newprice.toFixed(0);
	}
	if(isNaN(newprice)){}
	else{
		document.getElementById("productoldprice").innerHTML = getCurShow(jsCurName,jsSymbol,oldprice,1);
		document.getElementById("productcurprice").innerHTML = getCurShow(jsCurName,jsSymbol,newprice,1);
	}

	var arrPic4 = new Array();
	var iLen = HobbyPhotos.length;
	for(i=0;i<iLen;i++){
		if (HobbyCode[strhobby] == HobbyPhotos[i].code) {
			var strPic = HobbyPhotos[i].path;
			if(strPic && strPic != "") {
				arrPic4.push(strPic);
			}
		}
	}
	setSelectLastPrice();
	if(arrPic4!=""){
	MagicShowTypeE.smalloffset = 0;
	MagicShowTypeE.smallimgloadobjs = [];
	productImageShow(arrPic4);
	}
		
}
function setSelectLastPrice(){
	var strhobbycode = "";
	var HobbyAddPrice = new Array();
	var HobbyCountTotal = document.getElementById("HobbyCount").value;
	for (i = 1;i<=HobbyCountTotal;i++){
		HobbyAddPrice[i] = document.getElementById("HobbyName" + i).value;
		HobbyAddPrice[i] = document.getElementById(HobbyAddPrice[i] + document.getElementById("Hobby" + i).value).innerHTML;
	}
	for (i=1;i<=HobbyCountTotal;i++){
		ID = "HobbyName"+i;
		ID = String(document.getElementById(ID).value);
		var AddToSelect = 0;
		var obj2 = document.getElementById("Hobby"+i);
		for(ii=0;ii<obj2.length;ii++){
			var sAddPriceCode = "";
			AddToSelect = 0;
			for(iii=0;iii<HobbyCode2.length;iii++){
				if (document.getElementById(ID + obj2[ii].value).innerHTML == HobbyCode2[iii].code) {
					if(HobbyFloatPrice[HobbyCode2[iii].codelist]){
					AddToSelect = Math.max(HobbyFloatPrice[HobbyCode2[iii].codelist],AddToSelect);
					}
				}
			}
			if(AddToSelect>0){
				if((document.getElementById(ID + obj2[ii].value).innerHTML).indexOf("T"+jsMainHobbyType) >=0 ){
				obj2[ii].text = document.getElementById(ID + obj2[ii].value + "Value").innerHTML + "(+ "+getCurShow(jsCurName,jsSymbol,$.trim(String(AddToSelect)))+")";
				}
			}
		}
	}
	
}

function checkCustom(sid,IsDiscountLoss5,IsDiscount,Discount){
	var sizecheck = document.getElementById("isCustom");	
	var sizeselect = document.getElementById("Hobby"+sid);
	var customdiv = document.getElementById("customdiv"+sid);
	if(sizecheck.checked==true){
		sizeselect.value = "0";
		sizeselect.disabled=true;
		customdiv.style.display = ""; 
		var price=parseFloat(document.getElementById("productcurprice2").innerHTML)+parseFloat(document.getElementById("customprice"+sid).value);
		var oldprice = price;
		if(IsDiscountLoss5==1){
			oldprice = price*2;
		}else{
			if(IsDiscount==1){
				oldprice = price*100/Discount;
			}else{
				oldprice = price;
			}
		}
		if(jsCurName != "jpy" && jsCurName != 'idr' && jsCurName != 'rub')
		{
			oldprice = getCurShow(jsCurName,jsSymbol,oldprice.toFixed(2),1);
			price = getCurShow(jsCurName,jsSymbol,price.toFixed(2),1);
		}
		else
		{
			oldprice = getCurShow(jsCurName,jsSymbol,oldprice.toFixed(0),1);
			price = getCurShow(jsCurName,jsSymbol,price.toFixed(0),1);
		}
		document.getElementById("productoldprice").innerHTML = oldprice;
		document.getElementById("productcurprice").innerHTML = price;
		return;
	}else{
		sizeselect.disabled=false;
		if(customdiv.style.display != "none"){
			customdiv.style.display = "none";
			document.getElementById("productoldprice").innerHTML=getCurShow(jsCurName,jsSymbol,parseFloat(document.getElementById("productoldprice2").innerHTML),1);
			document.getElementById("productcurprice").innerHTML=getCurShow(jsCurName,jsSymbol,parseFloat(document.getElementById("productcurprice2").innerHTML),1);
		}
		setLastProductPrice(IsDiscountLoss5,IsDiscount,Discount);
	}
}

