var tmpls = $H({
	haku:'<img src="/images/palvelut/verkkotunnukset/domainhaku_ylaosa.gif" alt="Tarkista onko verkkotunnus vapaana" width="460" height="50" />'+
		'<form action="" method="post" id="hakuformi"><div id="domainform">'+
		'<span id="domainwww">www.</span><input type="text" name="new_domain" id="new_domain" /><select id="new_tld" name="new_tld">'+
		'<option value="fi" class="tld_list" selected="selected" >.fi</option>'+
		'<option value="com" class="tld_list" >.com</option>'+
		'<option value="net" class="tld_list" >.net</option>'+
		'<option value="eu" class="tld_list" >.eu</option>'+
		'<option value="org" class="tld_list" >.org</option>'+
		'<option value="info" class="tld_list" >.info</option>'+
		'<option value="biz" class="tld_list" >.biz</option>'+
		'</select><input name="check_domain" type="image" id="check_domain" value="Tarkista" src="/images/palvelut/verkkotunnukset/nappi_tarkista.gif" alt="Tarkista" />'+
		'</div></form>',
	vapaa:'<img src="/images/palvelut/verkkotunnukset/domainhaku_ylaosa.gif" alt="Tarkista onko verkkotunnus vapaana" width="460" height="50" />'+
	'<form name="domain_order_form" id="domain_order_form" action="https://' + window.location.hostname +  '/verkkotunnus/tilauslomake.html" target="_self" method="post">'+
	'<input type="hidden" name="new_domain" value="#{domain}" />'+
	'<input type="hidden" name="new_tld" value="#{tld}" />'+
	'<input type="hidden" name="submit_from" value="domain" />'+
	'<input type="hidden" name="action_type" value="register_new" />'+
	'<img src="/images/palvelut/verkkotunnukset/icon_check.gif" alt="Verkkotunnus on vapaa" id="img_domain_available" width="64" height="66" /> <span id="domain_text"> Verkkotunnus<br />'+
	'<strong id="domain_name">#{domain}.#{tld}</strong><br />on vapaa! <a id="uusi_haku">Tee uusi haku</a></span>'+
	'<span id="submitcontainer"><input name="order_domain" type="image" id="order_domain" src="/images/palvelut/verkkotunnukset/nappi_tilaa.gif" alt="Tilaa t&auml;st&auml;!" /></span>'+
	'</form>',
	varattu:'<img src="/images/palvelut/verkkotunnukset/domainhaku_ylaosa.gif" alt="Tarkista onko verkkotunnus vapaana" width="460" height="50" />'+
		'<img src="/images/palvelut/verkkotunnukset/icon_sorry.gif" alt="Verkkotunnus on varattu" id="img_domain_reserved" width="64" height="66" /> <span id="domain_text"> Verkkotunnus<br />'+
		'<strong id="domain_name">#{domain}.#{tld}</strong><br /> on valitettavasti jo varattu.</span>'+
		'<span id="submitcontainer"><input name="new_check_reserved" type="image" id="new_check_reserved" src="/images/palvelut/verkkotunnukset/nappi_uusihaku.gif" alt="Uusi haku" /></span>',
		virhe:'<img src="/images/palvelut/verkkotunnukset/domainhaku_ylaosa.gif" alt="Tarkista onko verkkotunnus vapaana" width="460" height="50" />'+
		'<img src="/images/palvelut/verkkotunnukset/icon_sorry.gif" alt="Verkkotunnus on virheellinen" id="img_domain_invalid" width="64" height="66" /> <span id="domain_text"> Verkkotunnus<br />'+
		'<strong id="domain_name">#{domain}.#{tld}</strong><br /> on virheellinen, yrit&auml; uudelleen.</span>'+
		'<span id="submitcontainer"><input name="new_check_invalid" type="image" id="new_check_invalid" src="/images/palvelut/verkkotunnukset/nappi_uusihaku.gif" alt="Uusi haku" /></span>'
});

  function showLoadingIndicator() {
    $('verkkotunnushakuprogress').show();
  }
  
  function hideLoadingIndicator() {
    $('verkkotunnushakuprogress').hide();
  }
  
	function resetpromobox() {
	var t = new Template(tmpls.get('haku'));
	$('verkkotunnushakubox').update(t.evaluate({}));
	$('hakuformi').observe('submit', function(event){
		showLoadingIndicator();
	  $('hakuformi').disable();
	  Event.stopObserving('hakuformi');
	  new Ajax.Request(
		'/verkkotunnus/whoislomake.html', 
		{
		  method:'post',
		  parameters:{domain:$F('new_domain')+'.'+$F('new_tld')},
		  onSuccess: function(transport) { regNew_callback(transport);},
		  onFailure: function(transport) { regNew_callback(false);},
		  onException: function(t, e) { regNew_callback(false);}
		}
	  );
	  Event.stop(event);
	  return false;
	});
  }
	function regNew_callback(r) {    
	var domain = $F('new_domain');
	var tld = $F('new_tld');
	var t; 
	hideLoadingIndicator();
	if (typeof(r) == 'undefined' || r == false) { 
	  t = new Template(tmpls.get('virhe'));
	  $('verkkotunnushakubox').update(t.evaluate({'domain':domain, 'tld':tld}));
	  return false;
	}
	if (r.responseText == -1) {
	  t = new Template(tmpls.get('virhe'));
	  $('verkkotunnushakubox').update(t.evaluate({'domain':domain, 'tld':tld}));
	  $('new_check_invalid').observe('click', function() {
		Event.stopObserving('new_check_invalid');
		resetpromobox();
	  });
	  return false;
	} else if (r.responseText == 0) {
	  t = new Template(tmpls.get('varattu'));
	  $('verkkotunnushakubox').update(t.evaluate({'domain':domain, 'tld':tld}));
	  $('new_check_reserved').observe('click', function() {
		Event.stopObserving('new_check_reserved');
		resetpromobox();
	  });
	  return false;
	} else if (r.responseText == 1) {
	  t = new Template(tmpls.get('vapaa'));
	  $('verkkotunnushakubox').update(t.evaluate({'domain':domain, 'tld':tld}));
	  $('uusi_haku').observe('click', function() {
		Event.stopObserving('uusi_haku');
		resetpromobox();
		return false;
	  });
	  return false;
	} else if (r.responseText == 2) {
	  t = new Template(tmpls.get('varattu'));
	  $('verkkotunnushakubox').update(t.evaluate({'domain':domain, 'tld':tld}));
	  $('uusi_haku').observe('click', function() {
		Event.stopObserving('uusi_haku');
		resetpromobox();
		return false;
	  });
	  return false;
	} else {
	  t = new Template(tmpls.get('virhe'));
	  $('verkkotunnushakubox').update(t.evaluate({'domain':domain, 'tld':tld}));
	  $('new_check_invalid').observe('click', function() {
		Event.stopObserving('new_check_invalid');
		resetpromobox();
		return false;
	  });
	  return false;
	  /*alert('undefined error');
	  return false;*/
	  // something happened in the ajax call. submit the form as if nothing happened?
	}  
  }
  
  document.observe("window, 'load'", function() {
	resetpromobox();
  });

