/***************************************************************************************************
 * This collection of Javascript is used by /store/calculate-shipping.php.
 **************************************************************************************************/
/***************************************************************************************************
 * This function toggles between the city and country controls and, if applicable, the city/state 
 * menu.
 **************************************************************************************************/
function toggle_international()
{
// Get the controls
	var shipping_city_state = document.getElementById('shipping_city_state');
	shipping_city_state.style.display = 'none';
	var shipping_city = document.getElementById('shipping_city');
	var shipping_state = document.getElementById('shipping_state');
	var shipping_zipcode = document.getElementById('shipping_zipcode');
	var shipping_country = document.getElementById('shipping_country');

// Change to Canada
	for (var i=0;i<shipping_country.options.length;i++)
	{
		if (shipping_country.options[i].value == 41)
		{
			shipping_country.selectedIndex = i;
			changeStateValue(shipping_state,'41');
			break;
		}
	}

// Clear the other fields
	shipping_city_state.options.length = 0;
	shipping_city.value = '';
	shipping_zipcode.value = '';

// Update the state menu
//	update_shipping_state();

// Update appearance
	update_shipping_estimate_controls();
}

/***************************************************************************************************
 * This function updates the labels and the city/state control visbility.
 **************************************************************************************************/
function update_shipping_estimate_controls()
{
// Get the controls
	var shipping_city_state = document.getElementById('shipping_city_state');
	var shipping_city = document.getElementById('shipping_city');
	var shipping_state = document.getElementById('shipping_state');
	var shipping_country = document.getElementById('shipping_country');
	var international_shipments = document.getElementById('international_shipments');
	var ship_to = document.getElementById('ship_to');

// If the country is US, hide the city and state fields and show the international shipments message
	if (shipping_country.value == '249')
	{
		shipping_country.style.display = shipping_city.style.display = shipping_state.style.display = 'none';
		international_shipments.style.display = 'inline';
		ship_to.innerText = 'Ship To Zip Code';
		ship_to.innerHTML = 'Ship To Zip Code';
	}
	else
	{
		shipping_country.style.display = shipping_city.style.display = shipping_state.style.display = 'inline';
		international_shipments.style.display = 'none';
		ship_to.innerText = 'Ship To City, Postal Code';
		ship_to.innerHTML = 'Ship To City, Postal Code';
	}
	shipping_city_state.style.display = 'none';
}

/***************************************************************************************************
 * This function validates that the shipping zipcode is filled in and find out if it passes the
 * address validator.
 **************************************************************************************************/
function validateAndRequestShippingEstimate()
{
	var shipping_city_state_ctl = document.getElementById('shipping_city_state');
	var shipping_city_ctl = document.getElementById('shipping_city');
	var shipping_state_ctl = document.getElementById('shipping_state');
	var shipping_zipcode_ctl = document.getElementById('shipping_zipcode');
	var shipping_country_ctl = document.getElementById('shipping_country');
	var address = new Array();
	if (shipping_city_state_ctl.options.length > 0 && shipping_city_state_ctl.value && shipping_city_state_ctl.style.display != 'none')
	{
	// Separate the city and state
		var tmp = shipping_city_state_ctl.value.split(',');
		address['city'] = tmp[0];
		address['state'] = tmp[1];
	}
	else
	{
		if (shipping_city_ctl.value && shipping_city_ctl.style.display != 'none')
			address['city'] = shipping_city_ctl.value;
		if (shipping_state_ctl.value && shipping_state_ctl.style.display != 'none')
			address['state'] = shipping_state_ctl.value;
	}
	if (shipping_zipcode_ctl.value)
		address['zipcode'] = shipping_zipcode_ctl.value;
	if (shipping_country_ctl.value != 249)
		address['country'] = shipping_country_ctl.value;
	var response = validateAddress(address,'handleAddressValidation');
	handleAddressValidation(response);
}

/***************************************************************************************************
 * This function receives the address validator response. If there is an error, alert the user. If
 * it passes, submit the form.
 **************************************************************************************************/
function handleAddressValidation(response)
{
// If there's no valid, find out why
	var valid = response.getElementsByTagName('Valid');
	if (valid != null && valid.length > 0)
	{
		request_shipping_estimate();
		return;
	}

// Look for candidates
	var candidate = response.getElementsByTagName('Candidate');
	if (candidate != null && candidate.length > 0)
	{
		var shipping_city_state = document.getElementById('shipping_city_state');
		var shipping_city = document.getElementById('shipping_city');
		var shipping_state = document.getElementById('shipping_state');
		var shipping_zipcode = document.getElementById('shipping_zipcode');
		var shipping_country = document.getElementById('shipping_country');
		if (candidate.length > 1)
		{
		// Populate the city/state menu
			var x = 0;
			for (var i=0;i<candidate.length;i++)
			{
				var zip = parseInt(shipping_zipcode.value);
				var ziphigh = candidate[i].getElementsByTagName('Zipcodehighend');
				ziphigh = ziphigh[0].firstChild.data;
				var ziplow = candidate[i].getElementsByTagName('Zipcodelowend');
				ziplow = ziplow[0].firstChild.data;
				if (zip >= parseInt(ziplow) && zip <= parseInt(ziphigh))
				{
					var city = candidate[i].getElementsByTagName('City');
					city = city[0].firstChild.data;
					var state = candidate[i].getElementsByTagName('State');
					state = state[0].firstChild.data;
					shipping_city_state.options[x++] = new Option(city+', '+state,city+','+state);
				}
			}
			shipping_city_state.length = x;

		// Display the city/state menu based on whether there options
			if (x > 0)
			{
				shipping_city_state.style.display = 'inline';
				shipping_city.style.display = 'none';
				shipping_state.style.display = 'none';

			// Alert the user
				alert('Please select the correct the city and town.');
				return;
			}

			shipping_city_state.style.display = 'none';
			shipping_city.style.display = (shipping_country.value == 249) ? 'none' : 'inline';
			shipping_state.style.display = (shipping_country.value == 249) ? 'none' : 'inline';
			alert('Invalid zip code. Please change your shipping information and try again.');
			return;
		}

	// If this is just a zipcode, plug in the city and state and let it go
		var city = candidate[0].getElementsByTagName('City');
		var state = candidate[0].getElementsByTagName('State');
		send_shipping_estimate(city[0].firstChild.data,state[0].firstChild.data,shipping_zipcode.value,shipping_country.value);
		return;
	}

// If there's an error, print it
	var error = response.getElementsByTagName('Error');
	if (error != null && error.length > 0)
	{
		alert(error[0].firstChild.data);
		return;
	}

	if (document.getElementById('shipping_zipcode').value == '')
	{
		alert('Please enter your zip code in the textbox labeled \"Ship to Zip Code\".');
		return;
	}

// Alert the user that we can't get estimates right now
	alert('Our shipping service is out of service at the moment. Please try again in a few minutes.');
}

/***************************************************************************************************
 * This function submits the shipping estimate request with the necessary 
 * city/state/zipcode/country info.
 **************************************************************************************************/
function request_shipping_estimate()
{
// Get the shipping info
	var shipping_city_state_ctl = document.getElementById('shipping_city_state');
	var shipping_city_ctl = document.getElementById('shipping_city');
	var shipping_state_ctl = document.getElementById('shipping_state');
	var shipping_zipcode_ctl = document.getElementById('shipping_zipcode');
	var shipping_country_ctl = document.getElementById('shipping_country');

// Build the shipping estimate url
	var shipping_city = '';
	var shipping_state = '';
	var shipping_zipcode = '';
	var shipping_country = '';
	var sep = '?';
	var url = '/store/calculate-shipping.php';
	var arg = '';
	if (shipping_city_state_ctl.options.length > 0 && shipping_city_state_ctl.value && shipping_city_state_ctl.style.display != 'none')
	{
	// Separate the city and state
		var tmp = shipping_city_state_ctl.value.split(',');
		shipping_city = tmp[0];
		shipping_state = tmp[1];
	}
	else
	{
		if (shipping_city_ctl.value && shipping_city_ctl.style.display != 'none')
			shipping_city = shipping_city_ctl.value;
		if (shipping_state.value && shipping_state_ctl.style.display != 'none')
			shipping_state = shipping_state_ctl.value;
	}
	if (shipping_zipcode_ctl.value)
		shipping_zipcode = shipping_zipcode_ctl.value;
	if (shipping_country_ctl.value != 249)
		shipping_country = shipping_country_ctl.value;
	send_shipping_estimate(shipping_city,shipping_state,shipping_zipcode,shipping_country);
}

/***************************************************************************************************
 * This function formats the url to submit.
 **************************************************************************************************/
function send_shipping_estimate(shipping_city,shipping_state,shipping_zipcode,shipping_country)
{
// Build the shipping estimate url
	var sep = '?';
	var url = '/store/calculate-shipping.php';
	var arg = '';
	if (shipping_city)
	{
		arg += sep+'shipping_city='+escape(shipping_city);
		sep = '&';
	}
	if (shipping_state)
	{
		arg += sep+'shipping_state='+escape(shipping_state);
		sep = '&';
	}
	if (shipping_zipcode)
	{
		arg += sep+'shipping_zipcode='+escape(shipping_zipcode);
		sep = '&';
	}
	if (shipping_country)
	{
		arg += sep+'shipping_country='+escape(shipping_country);
		sep = '&';
	}

// If no argument was found, warn
	if (sep == '?')
	{
		alert('Please fill in the shipping information.');
		return;
	}
	var name = arg.replace(/\?/g,'');
	name = name.replace(/\&/g,'');
	name = name.replace(/=/g,'');
	name = name.replace(/\s/g,'');
	name = name.replace(/%[A-F0-9][A-F0-9]/g,'');
	name = name.replace(/\+/g,'');

// Pop up the shipping estimate
	if (document.location.href.indexOf('calculate-shipping.php') > -1)
		document.location.href = url+arg;
	else
	{
		open_window(url+arg,name,700,475)
	}
}
