var geocoder = null;

function validate(frm)
{
	// Check required fields
	if(frm.name.value == 'Name artist or gig' || frm.name.value == '') { alert('Please fill in the name of the gig or artist'); frm.name.focus(); return false; }
	if(frm.location_name.value == 'Name location' || frm.location_name.value == '') { alert('Please fill in the name of the location/venue'); frm.location_name.focus(); return false; }
	if(frm.location_address.value == 'Address, city, country' || frm.location_address.value == '') { alert('Please fill in the address'); frm.location_address.focus(); return false; }
	if(frm.door_price.value == '(E.g. $15)' || frm.door_price.value == '') { alert('Please fill in the price at the door'); frm.door_price.focus(); return false; }
	
	// Check if URL's are valid
	if(frm.gig_url.value != 'http://' && frm.gig_url.value != 'http:// artist website or gig' && frm.gig_url.value != '' && !validate_url(frm.gig_url.value)) { alert('Please fill in a valid URL'); frm.gig_url.focus(); return false; }
	if(frm.tickets_url.value != 'http://' && frm.tickets_url.value != 'http:// url online ticket sales' && frm.tickets_url.value != '' && !validate_url(frm.tickets_url.value)) { alert('Please fill in a valid URL'); frm.tickets_url.focus(); return false; }

	// Check for XSS
	if(!validate_value(frm.name.value)) { alert('Please don\'t use tags'); frm.name.focus(); return false; }
	if(!validate_value(frm.location_name.value)) { alert('Please don\'t use tags'); frm.location_name.focus(); return false; }
	if(!validate_value(frm.door_price.value)) { alert('Please don\'t use tags'); frm.door_price.focus(); return false; }
	if(!validate_value(frm.online_price.value)) { alert('Please don\'t use tags'); frm.online_price.focus(); return false; }
	if(!validate_value(frm.trono_nickname.value)) { alert('Please don\'t use tags'); frm.trono_nickname.focus(); return false; }

	// Check the date
	if(!validate_date(frm)) { alert('Please fill in a valid date'); return false; }

	// Check the time
	if(!validate_time(frm)) { alert('Please make sure the end time is not the same as the start time'); return false; }

	// Validate the address and submit the form
	validate_address_and_submit(frm);
}

function validate_url(url)
{
	var exp = new RegExp();
	exp.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%#&\+\$\?\/.=]+$");
	if(!exp.test(url)) { return false; } else { return true; }
}

function add_http(field)
{
	v = field.value.toLowerCase();
	if(v.indexOf('http://') == -1 && v.indexOf('https://') == -1) { field.value = 'http://' + v; }
}

function validate_value(val)
{
	var exp = new RegExp();
	exp.compile("<.+?");
	if(!exp.test(val)) { return true; } else { return false; }
}

function validate_time(frm)
{
	if(frm.start_time.value == frm.end_time.value) { return false; } else { return true; }
}

function validate_date(frm)
{
	month_year = frm.month_year.value.split('-');
	month = month_year[0];
	year = month_year[1];
	day = frm.day.value;
	is_leap = new Date(year, 1, 29).getDate() == 29;

	if(is_leap && month == 2 && day > 29) { return false; }
	else if(month == 2 && day > 28) { return false; }
	else if((month == 4 || month == 6 || month == 9 || month == 11) && day > 30) { return false; }
	return true;
}

function validate_address_detail(frm, result)
{
	if(get_city(result) == null)
	{
		document.getElementById('spinner').style.display = 'none';
		alert('Please fill in the nearest city or town. After that you can drag your marker to the desired location');
		frm.location_address.focus();
		return false;
	}
	else if(get_street(result) == null)
	{
		document.getElementById('spinner').style.display = 'none';
		alert('The address you provided wasn\'t as detailed as we expected. Check the map to see if the marker is positioned correctly. If not, please go back and fill in a more detailed address.');
		return true;
	}
	return true;
}

function validate_address_and_submit(frm)
{
	current_address = frm.location_address.value;

	geocoder.geocode({ 'address': current_address },
		function(results, status)
		{
			if(status != google.maps.GeocoderStatus.OK)
			{
				alert('Sorry, we couldn\'t find this address. Please try again.');
				frm.location_address.focus();
				return false;
			}
			else
			{
				var result = results[0];

				if(!validate_address_detail(frm, result))
				{
					return false;
				}
				else
				{
					frm.latitude.value = result.geometry.location.lat();
					frm.longitude.value = result.geometry.location.lng();
				}

				frm.address.value = get_street(result);
				frm.city.value = get_city(result);
				frm.country_code.value = get_address_component(result, 'country');
				frm.action = submit_url;
				frm.submit();
			}
		}
	);
}

function get_address_component(result, type)
{
	for(i = 0; i < result.address_components.length; i++)
	{
		ac = result.address_components[i];
		if(ac.types[0] == type) { return ac.short_name; }
	}

	return null;
}

function get_city(result)
{
	if(get_address_component(result, 'country') != null)
	{
		if(get_address_component(result, 'locality') != null)
		{
			return get_address_component(result, 'locality');
		}
	}

	return null;
}

function get_street(result)
{
	var street_name = get_address_component(result, 'route');

	if(street_name != null)
	{
		var street_number = get_address_component(result, 'street_number');

		if(street_number != null)
		{
			if(result.formatted_address.indexOf(street_number + ' ' + street_name) >= 0)
			{
				return street_number + ' ' + street_name;
			}
			else
			{
				return street_name + ' ' + street_number;
			}
		}
		else 
		{
			return street_name;
		}
	}

	// Exception for Japan
	if(get_address_component(result, 'country') == 'JP')
	{
		var address = '';
		var city = get_address_component(result, 'locality');

		for(i = result.address_components.length - 1; i >= 0; i--)
		{
			ac = result.address_components[i];
			if(ac.types[0] == 'sublocality' && ac.short_name != city) { address += ac.short_name + ' '; }
		}
		
		if(address != '') { return address.slice(0, -1); }
	}

	return null;
}
