function showOrderReason(self) {

  var self_id = /^.*_([0-9]*)$/.exec(self.id)[1];

  if ($('order_cancelled_'+self_id).checked || $('order_fulfilled_'+self_id).checked) {

    $('edit_reason_'+self_id).show();

  } else {

    $('edit_reason_'+self_id).hide();

  };

};

// copies over shipping info within the page (not in the model)
function sameShip() {
	if(document.getElementById("copy_shipping").checked)
	{
        document.getElementById("order_billing_first_name").value = document.getElementById("order_shipping_first_name").value;
        document.getElementById("order_billing_last_name").value = document.getElementById("order_shipping_last_name").value;
        document.getElementById("order_billing_company").value = document.getElementById("order_shipping_company").value;
        document.getElementById("order_billing_address_1").value = document.getElementById("order_shipping_address_1").value;
        document.getElementById("order_billing_address_2").value = document.getElementById("order_shipping_address_2").value;
        document.getElementById("order_billing_city").value = document.getElementById("order_shipping_city").value;
        document.getElementById("order_billing_state").value = document.getElementById("order_shipping_state").value;
        document.getElementById("order_billing_zip").value = document.getElementById("order_shipping_zip").value;
        document.getElementById("order_billing_phone").value = document.getElementById("order_shipping_phone").value;
	}
	else   {
        document.getElementById("order_billing_first_name").value = "";
        document.getElementById("order_billing_last_name").value = "";
        document.getElementById("order_billing_company").value = "";
        document.getElementById("order_billing_address_1").value = "";
        document.getElementById("order_billing_address_2").value = "";
        document.getElementById("order_billing_city").value = "";
        document.getElementById("order_billing_state").value = "";
        document.getElementById("order_billing_zip").value = "";
        document.getElementById("order_billing_phone").value = "";
	}
}

Event.observe(window, 'load', function() {
	MM_preloadImages('/images/head_nav_bx.jpg','/images/head_nav_dx.jpg','/images/head_nav_fx.jpg');
});

// for the announcements in the footer
Event.onReady(function() {
	element = $$('#footer p').first();
	if (element) {
		element.visualEffect('Highlight', {startcolor : '#9F0000', endcolor : '#5E2307', duration : 2})
	}

	if ($('retail_store_image')) {
		$('retail_store_image').visualEffect('Fade', { delay : 4, duration : 2, fps : 30 });
	}
});

Event.onReady(function() {
	if ($$('#checkout').any()) {
		// giftNote.wantGiftNote();
		// new Form.Element.EventObserver('order_use_gift_message', function() {
		// 	giftNote.wantGiftNote();
		// });

		giftNote.wordCount();
		new Field.Observer('order_gift_message', 0.5, function() {
			giftNote.wordCount();
		})

		deliverDate.execute();
		new Form.EventObserver('checkout', function() {
			deliverDate.execute();
		})

		deliverMethod.execute();
		new Form.EventObserver('checkout', function() {
			deliverMethod.execute();
		})
	}
});

// focus the reset password field
Event.onReady(function() {
	if($('resetPassword')) {
		$('resetPassword').focusFirstElement();
	}
});

var giftNote = {
	// wantGiftNote : function() {
	// 	value = $F('order_use_gift_message');
	// 	if (value == 'yes' || value == '') {
	// 		$('message').show();
	// 		$('order_gift_message').focus();
	// 	} else {
	// 		$('message').hide();
	// 		$('order_gift_message').clear();
	// 	}
	// },
	wordCount : function() {
		count = $F('order_gift_message').split("").reject(function(n) { return n == ""; }).size();
		$('wordCount').innerHTML = (150 - count) + " characters left";
	}
}

var deliverDate = {
	execute : function() {
		radioValue = getRadioValue('checkout', 'order[ship_later]')
		if (radioValue == 'true') {
			$('deliver_on_field').show();
			$('ship_now_message').hide();
		} else {
			$('deliver_on_field').hide();
			$('order_deliver_on').clear();
			$('ship_now_message').show();
		}
	}
}

var deliverMethod = {
	execute : function() {
		radioValue = getRadioValue('checkout', 'order[shipping_type]')
		if (radioValue == 'standard') {
			$('deliver_on').show();
		} else {
			$('deliver_on').hide();
			$('order_deliver_on').clear();
			$('order_ship_later_false').checked = true;
			$('order_ship_later_true').checked = false;
		}
	}
}

function getRadioValue( form, radioGroupName ) {
	checked = Form.getInputs(form,'radio',radioGroupName).find(function(radio) { return radio.checked; });
	if (checked) {
		return checked.value;
	}
}

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];}
}