﻿//オプションページ製品検索
function Change_Futo(size_id,shape_id,kind_id) {
    var frm = document.forms['search_form'];
    var param = new Array();
    param.push('size_id=' + encodeURIComponent(size_id));
    param.push('shape_id=' + encodeURIComponent(shape_id));
    param.push('kind_id=' + encodeURIComponent(kind_id));
    var url = frm.elements['url'];
    param.push('url=' + encodeURIComponent(url.value));
    var thickness_id = frm.elements['search_thickness_id'];
    if(thickness_id !=undefined){
      if(thickness_id.value !=""){
        var search_thickness_id = thickness_id.options[thickness_id.selectedIndex].value;
        param.push('search_thickness_id=' + encodeURIComponent(search_thickness_id));
      }
    }
    var post_id = frm.elements['search_post_id'];
    if(post_id.value !=undefined){
      if(post_id.value !=""){
        var search_post_id = post_id.options[post_id.selectedIndex].value;
        param.push('search_post_id=' + encodeURIComponent(search_post_id));
      }
    }
    var stick_position_id = frm.elements['search_stick_position_id'];
    if(stick_position_id !=undefined){
      if(stick_position_id.value !=""){
        var search_stick_position_id = stick_position_id.options[stick_position_id.selectedIndex].value;
        param.push('search_stick_position_id=' + encodeURIComponent(search_stick_position_id));
      }
    }
    var paste_id = frm.elements['search_paste_id'];
    if(paste_id !=undefined){
      if(paste_id.value !=""){
        var search_paste_id = paste_id.options[paste_id.selectedIndex].value;
        param.push('search_paste_id=' + encodeURIComponent(search_paste_id));
      }
    }
    query('search', param);

}
//Ajaxでデータベースにせつぞくする
function query(type, param_array){
    // XMLHttpRequestを生成する
    var xmlhttp = createXmlHttpRequest();
    xmlhttp.open("POST", "http://www.futo-online.net/data/class/pages/Product_Serch.php", false);
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            //結果をdivの内容として書き換える
            //document.getElementById(type).innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded;charset=UTF-8");
    param_str = 'mode=' + type;
    if(param_array != null){
        for(var i = 0; i < param_array.length; i++){
            param_str += '&' + param_array[i];
        }
    }
    xmlhttp.send(param_str);
    Respons_Make(type,xmlhttp.responseText);

}
//Ajaxの結果を加工する
function Respons_Make(type,responseText){
  if(type == "search"){
    var respons = responseText.split("|");
    document.getElementById(type).innerHTML = respons[0];
    document.getElementById("p_count").innerHTML = respons[1];
  }else if(type == "process"){
    var frm = document.forms['form1'];
    var s_price = frm.elements['s_price'];
    if(responseText ==""){responseText = 0;}
    s_price.value = responseText;
    document.getElementById("special_price").innerHTML = addFigure(responseText);
  }else if(type == "process_select"){
    var s_select;
    s_select = responseText;
    document.getElementById("special").innerHTML = s_select;
  }
 }
//Ajaxオブジェクト
function createXmlHttpRequest() {
    if(window.XMLHttpRequest){             // Firefox,Opera,Safari,IE7
        return new XMLHttpRequest();
    }else if(window.ActiveXObject){                 // IE6
        try{
            return new ActiveXObject("Msxml2.XMLHTTP");    // MSXML3
        }catch(e){
            return new ActiveXObject("Microsoft.XMLHTTP"); // MSXML2まで
        }
    }else{
        return null;
    }

}
//見積もり①　製品を選択する
function Estimate(product_id,order_unit,expected_date,special_id,sales_price,bero_id,product_name,sp_deliv_unit,deliv_rates,sp_deliv_fee,unit_deliv_fee,unit,category_id){
  //hiddenに入れる
    var frm = document.forms['form1'];
    //document.getElementById("estimate").innerHTML = "<a href=\"#\" onclick=\"fnChangeAction(\'/contact/estimate.php\');fnModeSubmit(\'\', \'product_id\', "+product_id+"); return false;\">→この製品で印刷加工の見積依頼</a>";
    var h_product_id = frm.elements['product_id'];
    h_product_id.value = product_id;
    var h_order_unit = frm.elements['order_unit'];
    h_order_unit.value = order_unit;
    var h_category_id = frm.elements['category_id'];
    h_category_id.value = category_id;
    var h_sales_price = frm.elements['sales_price'];
    h_sales_price.value = sales_price;
    var h_bero_id = frm.elements['bero_id'];
    h_bero_id.value = bero_id;
    var h_special_id = frm.elements['special_id'];
    h_special_id.value = special_id;
    var h_product_name = frm.elements['product_name'];
    h_product_name.value = product_name;
    var h_sp_deliv_unit = frm.elements['sp_deliv_unit'];
    h_sp_deliv_unit.value = sp_deliv_unit;
    var h_deliv_rates = frm.elements['deliv_rates'];
    h_deliv_rates.value = deliv_rates;
    var h_sp_deliv_fee = frm.elements['sp_deliv_fee'];
    h_sp_deliv_fee.value = sp_deliv_fee;
    var h_unit_deliv_fee = frm.elements['unit_deliv_fee'];
    h_unit_deliv_fee.value = unit_deliv_fee;
    var h_unit = frm.elements['unit'];
    h_unit.value = unit;
    var h_expected_date = frm.elements['expected_date'];
    h_expected_date.value = expected_date;
    var h_or_expected_date = frm.elements['or_expected_date'];
    h_or_expected_date.value = expected_date;
  //発注単位
    document.getElementById("order_unit_span").innerHTML = order_unit;
  //製品番号
    //document.getElementById("cart_pid").innerHTML = "M"+product_id;
  //特注加工のselect
    var s_select;
    if(special_id =="1"){
	    s_select= '<select name="s_option" disabled onChange="getSpecialEstimate();" id="s_option"><option value="0">なし</option><option value="1">エルコン</option></select>';
       document.getElementById("special").innerHTML = s_select;
    }else{
       var param = new Array();
       var size_id = frm.elements['size_id'];
       var bero_id = frm.elements['bero_id'];
       param.push('special_id=' + encodeURIComponent(special_id));
       param.push('size_id=' + encodeURIComponent(size_id.value));
       param.push('bero_id=' + encodeURIComponent(bero_id.value));
       query('process_select', param);
    }
  //配送日
    document.getElementById("expected_date_span").innerHTML = expected_date;
    EstimateCalculation(order_unit,sales_price,bero_id,sp_deliv_unit,deliv_rates,sp_deliv_fee,unit_deliv_fee,unit,deliv_fee_rates,disp_deliv_fee,under1900);
}


//見積もり②　金額計算
function  EstimateCalculation(order_unit,sales_price,bero_id,sp_deliv_unit,deliv_rates,sp_deliv_fee,unit_deliv_fee,unit,deliv_fee_rates,disp_deliv_fee,under1900){

 //部数の入力を調べる
    var frm = document.forms['form1'];
    var quantity = frm.elements['quantity'];
    var or_expected_date = frm.elements['or_expected_date'];
    var expected_date = frm.elements['expected_date'];
    if(quantity.value != ""){
      var result = quantity.value % order_unit;
      if(result !="0"){
        quantity.style.backgroundColor="#ffcccc";
       alert("部数は"+order_unit+"の倍数でご指定下さい。");
        exit;
      }else{
      //特注加工の選択を取得
        quantity.style.backgroundColor="";
       var s_option = frm.elements['s_option'];
       var sel_s_option = s_option.options[s_option.selectedIndex];
       if(sel_s_option !=undefined && sel_s_option.value!="" && sel_s_option.value!="0"){
         //特注加工料金取得
         var param = new Array();
         var fsm = document.forms['search_form'];
         if(fsm ==undefined){
           var frm = document.forms['form1'];
           var size_id = frm.elements['size_id'];
         }else{
          var size_id = fsm.elements['size_id'];
         }
         param.push('size_id=' + encodeURIComponent(size_id.value));
         param.push('process_id=' + encodeURIComponent(sel_s_option.value));
         param.push('bero_id=' + encodeURIComponent(bero_id));
         param.push('quantity=' + encodeURIComponent(quantity.value));
         query('process', param);
         document.getElementById("expected_date_span").innerHTML = parseFloat(or_expected_date.value) + 3;
         expected_date.value = parseFloat(or_expected_date.value) + 3;
       }else{
         var s_price = frm.elements['s_price'];
         s_price.value = 0;
         document.getElementById("special_price").innerHTML = 0;
         document.getElementById("expected_date_span").innerHTML =or_expected_date.value;
         expected_date.value =or_expected_date.value;
       }
         var ss_price = frm.elements['s_price'];
       //製品代金計算
         var total_sales;
         //入り数＜注文部数
         if(parseFloat(unit) < parseFloat(quantity.value)){
           var quan_unit =  Math.ceil(parseFloat(quantity.value) / parseFloat(unit) -1);
           var box_fee = parseFloat(quan_unit)* parseFloat(unit_deliv_fee)* parseFloat(deliv_rates);
           total_sales = ((parseFloat(quantity.value) * parseFloat(sales_price)/100) +  parseFloat(unit_deliv_fee) +  parseFloat(box_fee));
         }else{
           if(parseFloat(quantity.value) <= parseFloat(sp_deliv_unit)){
             total_sales = (parseFloat(quantity.value) * parseFloat(sales_price)/100)+ parseFloat(sp_deliv_fee);
           }else{
             total_sales = parseFloat(quantity.value) * parseFloat(sales_price) / 100 + parseFloat(unit_deliv_fee);
           }
         }
         //1900未満だったら、表示送料をカートで出すので表示送料引く
         if( parseFloat(total_sales) + parseFloat(ss_price.value) < 1900 ){
            total_sales =  parseFloat(total_sales) - parseFloat(deliv_fee_rates);
            //20110220matsuara (入力部数／1900円未満の場合の最大部数）×表示送料を足す
            var puras = (parseFloat(quantity.value)/parseFloat(under1900)) * parseFloat(deliv_fee_rates);
            puras = Math.ceil(parseFloat(puras));
            total_sales = parseFloat(total_sales) + parseFloat(puras);

         }
         document.getElementById("sales_price_span").innerHTML = addFigure(total_sales);
       //Totale金額計算
         var special_price = frm.elements['s_price'].value;
         if(special_price != '0'){
           document.getElementById("total_price").innerHTML = addFigure(parseFloat(special_price) + parseFloat(total_sales));
         }else{
           document.getElementById("total_price").innerHTML = addFigure(total_sales);
         }
      }
    }else{
            quantity.style.backgroundColor="";
    }

}
//特殊加工　計算
function getSpecialEstimate(){
    var frm = document.forms['form1'];
    var product_id = frm.elements['product_id'];
    //var cart_pid =  document.getElementById("cart_pid").innerHTML
  if(product_id.value !=""){
    if(product_id.value ==""){
      alert("商品を選択し直して下さい。");
      var quantity = frm.elements['quantity'];
      quantity.value = '';
    }else{
      var quantity = frm.elements['quantity'];
      var h_order_unit = frm.elements['order_unit'];
      var order_unit = h_order_unit.value;
      var h_sales_price = frm.elements['sales_price']
      var sales_price = h_sales_price.value;
      var h_bero_id = frm.elements['bero_id'];
      var bero_id = h_bero_id.value;
      var h_sp_deliv_unit = frm.elements['sp_deliv_unit'];
      var sp_deliv_unit = h_sp_deliv_unit.value;
      var h_deliv_rates = frm.elements['deliv_rates'];
      var deliv_rates = h_deliv_rates.value;
      var h_sp_deliv_fee = frm.elements['sp_deliv_fee'];
      var sp_deliv_fee = h_sp_deliv_fee.value;
      var h_unit_deliv_fee = frm.elements['unit_deliv_fee'];
      var unit_deliv_fee = h_unit_deliv_fee.value;
      var h_unit = frm.elements['unit'];
      var unit = h_unit.value;
      var h_deliv_fee_rates = frm.elements['deliv_fee_rates'];
      var deliv_fee_rates = h_deliv_fee_rates.value;
      var h_disp_deliv_fee = frm.elements['disp_deliv_fee'];
      var disp_deliv_fee = h_disp_deliv_fee.value;
      var h_under1900 = frm.elements['under1900'];
      var under1900 = h_under1900.value;
      EstimateCalculation(order_unit,sales_price,bero_id,sp_deliv_unit,deliv_rates,sp_deliv_fee,unit_deliv_fee,unit,deliv_fee_rates,disp_deliv_fee,under1900);
     }
  }else{
    alert("先に商品を選択してください");
    var quantity = frm.elements['quantity'];
    quantity.value = '';
  }
}
//金額　カンマ区切りフォーマッター
function addFigure(str) {

 var num = new String(str).replace(/,/g, "");

 while(num != (num = num.replace(/^(-?\d+)(\d{3})/, "$1,$2")));

 return num;

}
//オプション→詳細
function fnModeSubmit2(product_id) {
	document.form2['mode'].value = "detail";
   var f2m = document.forms['form2'];
   var frm = document.forms['form1'];
   var s_option = frm.elements['s_option'];
   var sel_s_option = s_option.options[s_option.selectedIndex];
   var quantity = frm.elements['quantity'];
   f2m.elements['quantity'].value = quantity.value;
   f2m.elements['s_option'].value = sel_s_option.value;
   f2m.elements['product_id'].value = product_id;
   document.form2.submit();
	//fnChangeAction(url);
   //fnSubmit();
}
//詳細→オプション
function fnModeSubmit3(url) {
   var frm = document.forms['form1'];
   var mode = frm.elements['mode'];
	mode.value = "detail_back";
	fnChangeAction(url);
   document.form1.submit();
   //fnSubmit();
}

function CheckFutoForm(url){
  //部数入力チェック
    var frm = document.forms['form1'];
    var quantity = frm.elements['quantity'];
   if(quantity.value =="" || quantity.value =="0"){
      quantity.style.backgroundColor="#ffcccc";
      alert("部数を入力してください。");
      exit;                      
    }else if(quantity.style.backgroundColor !=""){
      alert("部数を入力しなおして下さい");
      exit;                      
    }
    var product_id = frm.elements['product_id'].value;
    fnChangeAction(url);
    fnModeSubmit('cart', 'product_id',product_id );
    return false;
}
function SelectProduct(product_id){
  document.getElementById("r_"+product_id).checked = true;
}
function SetQuantity(quantity_id,s_option){
    var frm = document.forms['form1'];
    var quantity = frm.elements['quantity'];
    quantity.value = quantity_id;
   var sl_s_option = frm.elements['s_option'];
   var chec = 0;
   if(sl_s_option != undefined){
      for(var i = 1 ; i< sl_s_option.length ; i++){
        var sel_s_option = sl_s_option.options[i];
           if(sel_s_option.value == s_option){
             sel_s_option.selected = true;
             chec++;
           }
      } 
   }
   if(chec == 0){
     sl_s_option.options[0].selected = true;
   }
    getSpecialEstimate();
}
//詳細画面の部数を消去
function init_FutoDetail(){
    var frm = document.forms['form1'];
    var quantity = frm.elements['quantity'];
    quantity.value ="";
  	 var s_select= '<select name="s_option" onChange="getSpecialEstimate();" id="s_option"><option value="">なし</option><option value="1">エルコン</option><option value="2">アドヘア</option><option value="3">ワンタッチ</option><option value="4">玉ひも</option></select>';
    document.getElementById("special").innerHTML = s_select;
}

//カート内数字を変更するフォーム
function FutoCartChange(mode,id,value,id2,value2,category){
    var frm = document.forms['form1'];
    frm.elements['mode'].value = mode;
    if(mode == 'change'){
      frm.elements[id].value = value;
      var quantity = frm.elements['quantity_'+value];
      if(quantity.value == value2){
        exit;
      }else if (quantity.value == '0'){
           alert("削除します");
           frm.elements['mode'].value = 'delete';
           document.form1.submit();
      }else{
          var order_unit = frm.elements['order_unit_'+value];
          var result = quantity.value % order_unit.value;
          if(category =="4"){
            quantity.style.backgroundColor="#ffffff";
            frm.elements[id2].value = quantity.value;
            document.form1.submit();
          }else if(category !="4" && result !="0"){
            quantity.style.backgroundColor="#ffcccc";
            alert("部数は"+order_unit.value+"の倍数でご指定下さい。");
            exit;
          }else{
            quantity.style.backgroundColor="#ffffff";
            frm.elements[id2].value = quantity.value;
            document.form1.submit();
          }
      }
    }
}
//見積もりページへ飛ばす
function SubmitEstimate(){
    var frm = document.forms['form1'];
    var product_id = frm.elements['product_id'];
    fnModeSubmit('', 'product_id', product_id.value);
}

function MypageFutoCart(mode,special_id,product_name,product_id,order_unit,sales_price,bero_id,size_id,s_option,s_price,quantity,sp_deliv_unit,deliv_rates,sp_deliv_fee,unit_deliv_fee,unit,category_id,deliv_fee_rates,disp_deliv_fee,under1900){
    var frm = document.forms['form1'];
    frm.elements['mode'].value = mode;
    frm.elements['special_id'].value = special_id;
    frm.elements['product_name'].value = product_name;
    frm.elements['product_id'].value = product_id;
    frm.elements['order_unit'].value = order_unit;
    frm.elements['sales_price'].value = sales_price;
    frm.elements['bero_id'].value = bero_id;
    frm.elements['size_id'].value = size_id;
    frm.elements['s_option'].value = s_option;
    frm.elements['s_price'].value = s_price;
    frm.elements['sp_deliv_unit'].value = sp_deliv_unit;
    frm.elements['deliv_rates'].value = deliv_rates;
    frm.elements['sp_deliv_fee'].value = sp_deliv_fee;
    frm.elements['unit_deliv_fee'].value = unit_deliv_fee;
    frm.elements['quantity'].value = quantity;
    frm.elements['unit'].value = unit;
    frm.elements['category_id'].value = category_id;
    frm.elements['deliv_fee_rates'].value = deliv_fee_rates;
    frm.elements['disp_deliv_fee'].value = disp_deliv_fee;
    frm.elements['under1900'].value = under1900;
    document.form1.submit();
}

function FuncSimultaneously(mode,simult){
    var frm = document.forms['form1'];
    frm.elements['mode'].value = mode;
    var simultaneously = frm.elements['simultaneously'];
    if(simultaneously.checked == true){
        frm.elements['simult'].value = "1";
    }else{
        frm.elements['simult'].value = "0";
    }
    document.form1.submit();
}

function MyIDPasscheck(){
    var frm = document.forms['form1'];
    var login_id = frm.elements['login_id'];
    var password = frm.elements['password'];
    var password02 = frm.elements['password02'];
    var err = 0;
    var comment
    var passexit = 0;
    var loginexit = 0;
    if(password.value !=""){
       passexit = 1;
      if(password02.value == password.value){
        if(password.value.length >= 4){
        }else{
          alert("パスワードは４文字以上ご使用ください");
          err = 1;
          return false;
        }
      }else{
        alert("パスワードと確認パスワードが違っています。");
        err = 1;
        return false;
      }
    }
    if(login_id.value !=""){
        loginexit = 1;
        if(login_id.value.length >= 4){
        }else{
           alert("ログインIDは４文字以上ご使用ください");
           err = 1;
           return false;
        }
    }else{
        loginexit = 0;
    }
    if(err == 0){
       if(loginexit ==1 && passexit == 1) {
          comment = "ログインIDとパスワードを変更しますか？";
       }else if(loginexit == 1 && passexit == 0){
          comment = "ログインIDを変更しますか？";
       }else if(loginexit == 0 && passexit == 1){
          comment = "パスワードを変更しますか？";
       }
    }

 if(comment !=""){
  var res = confirm(comment);
  if(res){
    fnModeSubmit('confirm', '', ''); return false;
  }
 }
}

function AddSite(title,url,id){
if(id == 1){
   if (window.sidebar) {
     window.sidebar.addPanel(title, url,"");
   } else if( document.all ) {
     window.external.AddFavorite( url, title);
   } else if( window.opera && window.print ) {
     return true;
   }
 }else if(id == 2){
    window.open('http://bookmarks.yahoo.co.jp/bookmarklet/showpopup?t='+encodeURIComponent(title)+'&amp;u='+encodeURIComponent(url)+'&amp;ei=UTF-8','_blank','width=550,height=480,left=100,top=50,scrollbars=1,resizable=1',0);
 }else if(id == 3){
    window.open('http://www.google.com/bookmarks/mark?op=add&title='+encodeURIComponent(title)+'&bkmk='+encodeURIComponent(url)+'&amp;ei=UTF-8','_blank','width=550,height=480,left=100,top=50,scrollbars=1,resizable=1',0);
 }
}

function clearFormAll() {
    for (var i=0; i<document.forms.length; ++i) {
        clearForm(document.forms[i]);
    }
}
function clearForm(form) {
    for(var i=0; i<form.elements.length; ++i) {
        clearElement(form.elements[i]);
    }
}
function clearElement(element) {
    switch(element.type) {
        case "hidden":
        case "submit":
        case "reset":
        case "button":
        case "image":
            return;
        case "file":
            return;
        case "text":
        case "password":
        case "textarea":
            element.value = "";
            return;
        case "checkbox":
        case "radio":
            element.checked = false;
            return;
        case "select-one":
        case "select-multiple":
            element.selectedIndex = 0;
            return;
        default:
    }
}
function FutoShowDiko(){
 var chk_id = document.getElementById('chk_id_1').checked;
 var disp_daiko = document.getElementById('disp_daiko');
 if(chk_id == false){
    disp_daiko.style.display = "";
 }else{
    disp_daiko.style.display = "none";
    var frm = document.forms['form1'];
    frm.elements['comp'].value="";
    frm.elements['comp_kana'].value="";
    frm.elements['name01'].value="";
    frm.elements['name02'].value="";
    frm.elements['kana01'].value="";
    frm.elements['kana02'].value="";
    frm.elements['tel01'].value="";
    frm.elements['tel02'].value="";
    frm.elements['tel03'].value="";
    frm.elements['zip01'].value="";
    frm.elements['zip02'].value="";
    frm.elements['pref'].value="";
    frm.elements['addr01'].value="";
    frm.elements['addr02'].value="";
    frm.elements['daikou'].value="";
 }
}
function fnLoginFormCrear(){
    var frm = document.forms['form1'];
    frm.elements['login_id'].value="";
    frm.elements['password'].value="";
    frm.elements['password02'].value="";
}
function fnLoginPssCrear(){
    var frm = document.forms['form1'];
    frm.elements['password'].value="";
}
function getScrollPosition() {
    var obj = new Object();
    obj.x = document.documentElement.scrollLeft || document.body.scrollLeft;
    obj.y = document.documentElement.scrollTop || document.body.scrollTop;
    var frm = document.forms['form1'];
    frm.elements['position_x'].value=obj.x;
    frm.elements['position_y'].value=obj.y;
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

