// JavaScript Document
var is_ie = ( /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent) );
/**
 * Short-hand function
 */
function AjaxCall( url, opt )
{
	if ( $('AjaxLoading') )
	{
		$('AjaxLoading').style.display = 'block';	
	}
	
	url += (url.indexOf('?') == -1) ? "?_ajax_request=true" : "&_ajax_request=true";

	new Ajax.Request(url, 
						{ 
							onComplete:
								function( one ){
									if ( $('AjaxLoading') )
									{
										new Effect.Fade('AjaxLoading');	
									}
									if(one.responseXML)
									{
										xajax.processResponse(one.responseXML);
										if ( opt.onComplete )
										{
											opt.onComplete(one);		
										}
									} else alert(one.responseText);
								}
						}
					);	
}

function AjaxForm( form_id )
{
	url = $(form_id).getAttribute("action");
	url_params = "";
	$$("#"+form_id+" input").each(
								  	function(el)
									{
										if ( (el.type == 'radio' || el.type == 'checkbox') && el.checked )
											url_params += "&"+el.name+"="+el.value;
										else
											url_params += "&"+el.name+"="+el.value;
									}
								)

	url += (url.indexOf('?') == -1) ? "?"+url_params.substr(1) : url_params;

	return AjaxCall(url);
}

function initShippingCalendar()
{
	Calendar.setup(
		{
			inputField  : "shipped_on",      // ID of the input field
			ifFormat    : "%m/%d/%Y",    // the date format
			button      : "cal"    // ID of the button
		}
	);
}

function getOffset(what, offsettype){
	var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
	var parentEl=what.offsetParent;
	while (parentEl!=null){
		totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
		parentEl=parentEl.offsetParent;
	}
	return totaloffset;
}

function adjustPrice( trigger, original, priceEl )
{
	var toAdjust = parseFloat(trigger.options[trigger.selectedIndex].getAttribute('price'));
	var newprice = '$'+(original+toAdjust).toFixed(2);

	if ( !isNaN(toAdjust) )
		$(priceEl).innerHTML = newprice;
}

function removeItem( url )
{
	if ( confirm('Are you sure you want to remove this item from your cart?') )
	{
		AjaxCall(url)
		return true;
	}
	
	return false;
}

function formatFloat(pFloat, pDp){
    var m = Math.pow(10, pDp);
    return parseInt(pFloat * m, 10) / m;
}


function ShippingSame(form) {
  if (form.ShipSame.checked) {
	form.ShipContactName.value = $('name').value;
    form.ShipCompany.value = $('company').value;
    form.ShipStreet1.value = $('street1').value;
    form.ShipStreet2.value = $('street2').value;
	form.ShipCity.value = $('city').value;
    form.ShipState.value = $('state').value;
	form.ShipZip.value = $('zip').value;
	form.ShipStateOther.value = $('state_other').value;
    form.ShipCountry.value = $('country').value;
  }
  else {
    form.ShipContactName.value = "";
    form.ShipCompany.value = "";
    form.ShipStreet1.value = "";
    form.ShipStreet2.value = "";
    form.ShipCity.value = "";
	form.ShipState.value = "";
    form.ShipZip.value = "";
	form.ShipStateOther.value = "";
    form.ShipCountry.value = "";
  }
}

function populateAddress( drop, to )
{
	var value = drop.options[drop.selectedIndex].value;
	if ( value )
	{
		var arr = value.split("|")
		if ( typeof(to) == 'undefined' || to == 'bill' )
			var fields = new Array("name", "street1", "street2", "city", "state", "state_other", "zip", "country", "day_phone")
		else
			var fields = new Array("ShipContactName", "ShipStreet1", "ShipStreet2", "ShipCity", "ShipState", "ShipStateOther", "ShipZip", "ShipCountry")
		
		for( i = 2; i < (arr.length-2); i++ )
		{
			if ( $(fields[i-2]) )
			{
				if ( arr[i] )
					$(fields[i-2]).value = arr[i]
				else
					$(fields[i-2]).value = ""
			}
		}
	}
}

function showOtherState( obj, field )
{
	if ( obj.value == 'UNK' )
	{
		$(field).style.display = ''	
	}
	else
	{
		$(field).style.display = 'none'		
	}
}

function submitCheckout()
{
	$("checkout").disabled = true
}