$(document).ready(function() {

	// add hidden field with js
	$("#createsmurlform").append('<input type="hidden" name="js" value="1" />');

	$('.fav').jFav();

	$('#url').bt({
		trigger: ['focus', 'blur'],
		positions: ['right'],
		padding: 20,
		width: 120,
		spikeLength: 30,
		spikeGirth: 40,
		overlap: 8,
		cornerRadius: 30,
		fill: 'rgba(0, 0, 0, .82)',
		strokeWidth: 3,
		strokeStyle: '#D18521',
		cssStyles: {color: '#ffffff', fontWeight: 'bold'}
	});

	$('#key').bt({
		trigger: ['focus', 'blur'],
		positions: ['left'],
		padding: 20,
		width: 120,
		spikeLength: 30,
		spikeGirth: 40,
		overlap: 2,
		cornerRadius: 30,
		fill: 'rgba(0, 0, 0, .82)',
		strokeWidth: 3,
		strokeStyle: '#D18521',
		cssStyles: {color: '#ffffff', fontWeight: 'bold'}
	});

	
	$('#notice a').bt({
		fill: '#fffcca', 
		positions: ['top'],
		strokeStyle: '#999999', 
		spikeLength: 10, 
		spikeGirth: 10, 
		padding: 8, 
		cornerRadius: 0, 
		hoverIntentOpts: {
			interval: 400,
			timeout: 0
		},
		cssStyles: {
			fontFamily: '"lucida grande",tahoma,verdana,arial,sans-serif', 
			fontSize: '11px'
			}
		});

	$("#key").alphanumeric();
	$("#smurlcode").alphanumeric();

	$('#createsubmit').click(function() {

var debug = 0;

		//
		var go = 0;

		// clean up prev errors
		
		if($("#create_message_box").hasClass('message_warning')) $("#create_message_box").removeClass('message_warning')
		if($("#create_message_box").hasClass('message_error')) $("#create_message_box").removeClass('message_error')
		if($("#create_message_box").hasClass('message_ok')) $("#create_message_box").removeClass('message_ok')
		$(".inputerror").removeClass('inputerror')		
		$(".inputerror2").removeClass('inputerror2')		

		$("#notice").html('<img id="loading" src="/gfx/ajax-loader.gif" alt="please wait" /> Please wait while we process your request..');

		// trim

		var smurl_url = $("input[name='url']").val();
		smurl_url = jQuery.trim( smurl_url );
		$("input[name='url']").val( smurl_url );
		
		var smurl_key = $("input[name='key']").val().replace(/ /g,'');
		$("input[name='key']").val( smurl_key );

if(debug == 1) { alert('start check'); }

		var is_protocol_ok = smurl_url.indexOf('http://');
		var is_protocol_ok2 = smurl_url.indexOf('https://');

		var has_dot = smurl_url.indexOf('.');
		if(is_protocol_ok !=0 && is_protocol_ok2 !=0)
		{
			$("#create_message_box").addClass('message_warning');
			$("input[name='url']").addClass('inputerror');
			$("#notice").html('<h3>ERROR: no valid url.</h3>Your url should begin with http:// or https:// .' );
			$("input[name='url']").focus();

			return false;
		}

		// basic url check
		
		if((smurl_url.length < 11) || (has_dot==-1))
		{
			$("#create_message_box").addClass('message_error');
			$("input[name='url']").addClass('inputerror2');
			$("#notice").html('<h3>ERROR: no url.</h3>Please enter a valid url.' );
			$("input[name='url']").focus();

			return false;
		}

		// check valid url

		$.ajax({
			type: "GET",
			url: "/check_url.php",
			dataType: "json",
			data: { u: smurl_url , k: smurl_key },
			async: false,
//			timeout: 500,
			success: function(data){
			
if(debug == 1) { alert( 'start succes function [' + data.ok + '][' + data.code + '][' + data.string + ']' ); }

				go = data.ok;

				switch( data.ok )
				{
					case -4:
						$("#create_message_box").addClass('message_error');
						$("input[name='url']").addClass('inputerror2');
						$("#notice").html('<h3>ERROR: url listed in SURBL.org.</h3>Sorry, this url cannot be accepted.<br />Please check out <a href="/faq.php">the FAQ</a> about this message.' );
						$("input[name='url']").focus();
						if(debug == 1) { alert('url listed in SURBL.org! [-4]'); }
						break;
					case -3:
						$("#create_message_box").addClass('message_warning');
						$("input[name='url']").addClass('inputerror');
						$("#notice").html('<h3>WARNING: url not allowed.</h3>Sorry, your url cannot be accepted.<br />Please check out <a href="/faq.php">the FAQ</a> about this message.' );
						$("input[name='url']").focus();
						if(debug == 1) { alert('no valid url! [-3]'); }
						break;
					case -2:
						$("#create_message_box").addClass('message_error');
						$("input[name='url']").addClass('inputerror2');
						$("#notice").html('<h3>ERROR: no url.</h3>Please enter a valid url.' );
						$("input[name='url']").focus();
						if(debug == 1) { alert('no url! [-2]'); }
						break;
					case -1:
						$("#create_message_box").addClass('message_warning');
						$("#notice").html('<h3>WARNING: we could not check this url</h3>Our check returned: <em>' + data.string + '</em>.<br />Please check the entered url and try again later.' );
						if(debug == 1) { alert('could not get url. [-1][' + data.string + ']'); }
						break;
					case 0:
						$("#create_message_box").addClass('message_error');
						$("input[name='url']").addClass('inputerror2');
						$("#notice").html('<h3>ERROR: no valid url.</h3>Our check returned: <em>' + data.code + ' - ' + data.string + '</em><br />Please check the entered url.');
						$("input[name='url']").focus();
						if(debug == 1) { alert('wrong return code [' + data.code + '][' + data.string + ']'); }
						break;
					case 1:
						if(debug == 1) { alert('OK! np !'); }
						break;
					case 2:
						$("#create_message_box").addClass('message_warning');
						$("#notice").html('<h3>Valid url, but...</h3>The choosen key already exists. Please choose another one and try again.');
						$("input[name='key']").addClass('inputerror');
						$("input[name='key']").focus();
						if(debug == 1) { alert('URL OK! [2] - but key exists.'); }
						break;
					case 3:
						$("#create_message_box").addClass('message_warning');
						$("#notice").html('<h3>Valid url, but...</h3>The choosen key is invalid. Use plain letters and numbers and try again.');
						$("input[name='key']").addClass('inputerror');
						$("input[name='key']").focus();
						if(debug == 1) { alert('URL OK! [3] - but key is invalid.'); }
						break;
					default:
						alert('unknown situation');
				}
			}
		});

		if(go == 1)
		{
			return true;
		}

		return false;
	});
}); 
