//====================setup

function setup() {
initRollovers();
}

//====================/setup

//------ Daniel Nolan's rollover
function initRollovers() {
    
    if (!document.getElementById) return;
				      
    var aPreLoad = new Array();
    var sTempSrc;
    var aImages = document.getElementsByTagName('img');
    
    for (var i = 0; i < aImages.length; i++) {
        if (aImages[i].className == 'img_o') {
	    var src = aImages[i].getAttribute('src');
	     var ftype = src.substring(src.lastIndexOf('.'), src.length);
	     var hsrc = src.replace(ftype, '_o'+ftype);
	    
	    aImages[i].setAttribute('hsrc', hsrc);
	    
	    aPreLoad[i] = new Image();
	    aPreLoad[i].src = hsrc;
	    
	    aImages[i].onmouseover = function() {
		sTempSrc = this.getAttribute('src');
		this.setAttribute('src', this.getAttribute('hsrc'));
	    }	
	    
	    aImages[i].onmouseout = function() {
		if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_o'+ftype, ftype);
		this.setAttribute('src', sTempSrc);
	    }
	}
    }
}

function format(num) {
    numStr = String(num);
    numStr = numStr.replace(/,/g, '');
    numStr = numStr.replace(/(\d{1,3})(?=(\d{3})+(?!\d))/g, "$1,");
    return numStr;
}
$(function(){
	function change_delievery_form(){
	    if ($("input[@name='deliv_to']:checked").val() == 'same')
		$("#delivery_to").hide();
	    else
		$("#delivery_to").show();
	}
	$("input[@name='deliv_to']").click(change_delievery_form);
	change_delievery_form();

	function change_order_detail(){
	    var deliv_fee = 840;
	    var COD_mail_fee = 420;
	    var total_deliv_fee = deliv_fee;
	    var par_price = Number($("input[@name='price']").val());
	    var quantity = Number($("select[@name='quantity'] option:selected").val());
	    var quantity_unit = $("input[@name='unit']").val();

	    var deliv_type = $("select[@name='pay'] option:selected").val();
		
	    var price = par_price * quantity;
	    var price_info = quantity + quantity_unit;

	    
	    $("#order_detail .product td").html(price_info + '　　　' + format(price) + '円 (税込)');
	    $("#order_detail .deriv_fee td").html(deliv_fee + '円 (税込)');
	    if (deliv_type == 'COD_mail'){
		$("#order_detail .COD_mail_fee td").html(COD_mail_fee + '円 (税込)');
		$("#order_detail .COD_mail_fee").show();
		total_deliv_fee += COD_mail_fee;
	    }else if (deliv_type == 'bank'){
		$("#order_detail .COD_mail_fee").hide();		
	    }
	    $("#order_detail .sum td").html(format(price + total_deliv_fee) + '円 (税込)');
	}
	
	$("select[@name=pay]").change(change_order_detail);
	$("select[@name=quantity]").change(change_order_detail);
	change_order_detail();

  var default_comment = 'カットの希望などございましたらその旨記載お願い致します。\n例:固まり（ブロック）希望など';
  if ($("input[@name=p_code]").val() * 1 > 0){
    if ($('#comment').val().match(/カットの希望など/i) &&
        $('#comment').val().match(/ございましたら/i) &&
        $('#comment').val().match(/その旨/i) &&
        $('#comment').val().match(/記載/i) &&
        $('#comment').val().match(/お願い/i) &&
        //$('#comment').val().match(/致し/i) &&    
        $('#comment').val().match(/ます/i) &&
        $('#comment').val().match(/例:固まり（ブロック）希望など/i))
      $('#comment').val('');
    if ($('#comment').val() == '')
      $('#comment').val(default_comment).css({'color': 'gray'});    
    $('#comment').focus(function(){
      if ($(this).val() == default_comment)
        $(this).val('').css({'color': 'black'});
    });
    $('#comment').blur(function(){
      if ($(this).val() == '')
        $(this).val(default_comment).css({'color': 'gray'});
    });
  }
  
	
});

