	function invoice_address() {
		var f = document.getElementById("invoice_address_form");
		if (document.getElementById("addressesAreEquals").checked) {
			f.style.display='none';
 		}
		else {
			f.style.display='block';
		} 

	} 


var countries_zones = new Array();
var recentZone = 0;

 function carriers_display(id_country) {
  if (id_country > 0)
  {
   if (recentZone > 0) {
     document.getElementById('zone_'+recentZone).style.display = 'none';
   }
   recentZone = countries_zones[id_country];
   document.getElementById('zone_'+recentZone).style.display = 'block';
  }
 }
 function add_country_zone(id_country, id_zone) {
   countries_zones[id_country] = id_zone;
 }
 function getRadioNum(radioName) {
   var rads = document.getElementsByName(radioName);

   return rads.length;
 }


//fix for IE's getElementsByName
if(typeof(window.external) != 'undefined'){
document.getElementsByName = function(name){
    var elems = document.getElementsByTagName('input');
    var res = []
    for(var i=0;i<elems.length;i++){
        att = elems[i].getAttribute('name');
        if(att == name) {
            res.push(elems[i]);
        }
    }
    return res;
}
} 

 function getRadioVal(radioName) {
   var rads = document.getElementsByName(radioName);

   //fix for IE's 2 radio buttons selected
   var selected_count = 0;
   var selected_radio_value = null;

   for(rad=0;rad<rads.length;rad++) {
     if(rads[rad].checked)
       //return rads[rad].value;
     {
	if (selected_count > 0) {
	  //uncheck already selected value
	  rads[rad].checked = false;
	} else {
	  selected_radio_value = rads[rad].value;
          selected_count++;
	}
     }
   }
   return selected_radio_value;
 }
 function set_carrier(carrier_price) {

   if (carrier_price != null && carrier_price != "")
     carrier_price = parseFloat(carrier_price.replace(/[^\d,.]/g,'').replace(',','.'));

   //document.getElementById('id_carrier_hidden').value = getRadioVal('id_carrier_zone_'+recentZone);
   if (document.getElementById('id_carrier_hidden') != null) {
    document.getElementById('id_carrier_hidden').value = getRadioVal('id_carrier');
    
    var id_state = $('#id_state').val();
    if (id_state == "") {
      document.getElementById('id_zone_hidden').value = countries_zones[$('#id_country').val()];
    } else {
      document.getElementById('id_zone_hidden').value = csz[$('#id_country').val()][id_state];
    }
    document.getElementById('price_carrier_hidden').value = carrier_price;
    ajaxShipp.updateShipping($('#id_carrier_hidden').val());
   }

   return true;
 }

function onepage_cartupdate(doc, form, link)
{
  //set_carrier();
  doc.validate_conditions = 'no';
  form.step.value = '2';
  form.cartupdateflag.value = '1';
  form.link.value = link;
  form.submit();
}

function updateTotals(total_delivery_id, total_price_id, carrier_price_id) {
	delivery_price = $(total_delivery_id).text();
	new_carrier_price = $(carrier_price_id).val();
	total_price = $(total_price_id).text();
	if (delivery_price == '' || total_price == '') { return; }

	has_comma = (total_price.indexOf(',') > 0);
	
	delivery_price_uniform = delivery_price.replace(',','.');
	new_carrier_price_uniform = new_carrier_price.replace(',','.');
	total_price_uniform = total_price.replace(',','.');

	actualTotalShipping = parseFloat(delivery_price_uniform.replace(/[^\d,.]/g,''));
	newTotalShipping = parseFloat(new_carrier_price_uniform.replace(/[^\d,.]/g,''));
	actualTotalPrice =  parseFloat(total_price_uniform.replace(/[^\d,.]/g,''));

	newTotalShippingStr = newTotalShipping.toFixed(2) + '';
	newTotalPrice = (actualTotalPrice - actualTotalShipping + newTotalShipping);
	newTotalPriceStr = newTotalPrice.toFixed(2) + '';

//				alert('[deliver_price_unifomr]actual/newTotalShippingStr = '+delivery_price_uniform+']' +actualTotalShipping +'/'+ newTotalShippingStr);
	newTotalShippingStr = delivery_price_uniform.replace(''+actualTotalShipping.toFixed(2), newTotalShippingStr);
	newTotalPriceStr = total_price_uniform.replace(''+actualTotalPrice.toFixed(2), newTotalPriceStr);

	if (has_comma) {
	  newTotalShippingStr = newTotalShippingStr.replace('.', ',');
	  newTotalPriceStr = newTotalPriceStr.replace('.',',');
	}

	$(total_delivery_id).html(newTotalShippingStr);// + ' &euro;');
	if (!isNaN(actualTotalShipping)) 
	$(total_price_id).html(newTotalPriceStr);// + ' &euro;');

}



var ajaxShipp = {
	
	//override every button in the page in relation to the cart
        overrideButtonsInThePage : function(){

                $('#id_country').unbind('change').change(function(){
                        ajaxShipp.country_change_handler(false);
                        updateState(); // USA states
                }),

                $('#id_state').unbind('change').change(function(){
                        ajaxShipp.state_change_handler(false);
                       // updateState(); // USA states
                }),

                $('#quick_login').unbind('click').click(function(){
                        ajaxShipp.quick_login_click_handler()
                }),

                $('#alr_click_here').unbind('click').click(function(){
                        return ajaxShipp.alr_here_click_handler()
                });

        },

        country_change_handler : function(force_display) {
                id_country_x = $('#id_country').val();
                recentZone_x = countries_zones[id_country_x];
                if (recentZone_x != $('#id_zone_hidden').val() || force_display) {
                  ajaxShipp.updateZonesCarriers(recentZone_x);
                }
                return false;
        },

	state_change_handler : function(force_display) {
		id_country_x = $('#id_country').val();
		id_state_x = $('#id_state').val();
		if (id_state_x != "") {
                  recentZone_x = csz[id_country_x][id_state_x];
                  if (recentZone_x != $('#id_zone_hidden').val() || force_display) {
                    ajaxShipp.updateZonesCarriers(recentZone_x);
                  }
		}
                return false;

	},

        quick_login_click_handler : function() {
		email = $('#alr_email').val();
		passwd = $('#alr_passwd').val();
		
		//alert('clicked log in: ' + email + ', ' + passwd);
		//$('#alr_email').css('background', 'red');
		ajaxShipp.quickLogin();
        },

        alr_here_click_handler : function() {
		
		$('#alr_body').toggle(300);
		return false;
        },


	updateShipping : function(shippingId){

		//send the ajax request to the server
		$.ajax({
			type: 'GET',
			url: 'order.php',
			async: true,
			cache: false,
			dataType : "json",
			data: 'ajax_carrier=' + shippingId + '&ajax_zone_id='+$('#id_zone_hidden').val(),
			success: function(jsonData)
			{
				ajaxCart.refresh();
			//	$('#order-detail-content td.price').val();

				updateTotals('#order-detail-content tr.cart_total_delivery td.price', '#order-detail-content tr.cart_total_price td.price', '#price_carrier_hidden');			
				updateTotals('#order-detail-content tr.cart_total_deliveryEx td.price', '#order-detail-content tr.cart_total_priceEx td.price', '#price_carrier_hidden');			
	
//				alert('success');
								//reactive the button when adding has finished
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				//alert("TECHNICAL ERROR: unable to updateShipping.\n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);
				//alert('ajax_carrier=' + shippingId + '&ajax_zone_id='+countries_zones[$('#id_country').val()]);
			}
		});
	},

	updateZonesCarriers : function(zoneId){
	
		//send the ajax request to the server
		$.ajax({
			type: 'GET',
			url: 'order.php',
			async: true,
			cache: false,
			dataType : "json",
			data: 'zone_carriers=' + zoneId,
			success: function(jsonData)
			{
		//		ajaxCart.refresh();
			//	$('#order-detail-content td.price').val();
				$('#carriers2').html(jsonData.html_data);
				set_carrier(jsonData.selected_price);
			//	alert('success');
								//reactive the button when adding has finished
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				//alert("TECHNICAL ERROR: unable to updateZonesCarriers.\n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);
			}
		});
	},


	quickLogin : function(){
	
		//send the ajax request to the server
		$('#alr_wait_img').show();
		$('#alr_body').fadeTo(500, 0.2);
		$.ajax({
			type: 'POST',
			url: 'authentication.php',
			async: true,
			cache: false,
			dataType : "json",
			data: 'ajax_auth=1&alr_email='+$('#alr_email').val()+'&alr_passwd='+$('#alr_passwd').val(),
			success: function(jsonData)
			{
				success_login = jsonData.success;
				//alert('success login: '+success_login);

				if (success_login == 1) {
				  $('#alr_fieldset').hide(500); 
				  window.location = 'order.php?step=1';
				} else {
				  $('#alr_body').fadeTo(1, 1);
				  $('#alr_wait_img').fadeOut(500);
				  $('#alr_error').html(jsonData.error);
				  $('#alr_error').show(400);
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				//alert("TECHNICAL ERROR: unable to updateZonesCarriers.\n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);
			}
		});
	}



}//var ajaxShipp


//when document is loaded...
$(document).ready(function(){
	ajaxShipp.overrideButtonsInThePage();
	ajaxShipp.country_change_handler(true);
	ajaxShipp.state_change_handler(true);
//	ajaxCart.refresh();
});


