/* Javascript utilities for chaos2 links page.
   Copyright(c) 2003 Justin David Smith.
   All Rights Reserved. */

function failWith(message) {
   alert(message);
   return(false);
}

function validateNonempty(theform) {
   for(var i = 0; i < theform.elements.length; ++i) {
      if(theform.elements[i].type == 'text' && theform.elements[i].value.length == 0) {
         return failWith('Form \"' + theform.name + '\", element \"' +
                         theform.elements[i].name + '\" is empty.');
      }
   }
   return(true);
}

function swapCurrencies(fromsel, tosel) {
   var old_from   = fromsel.selectedIndex;
   var old_to     = tosel.selectedIndex;
   tosel.selectedIndex     = old_from;
   fromsel.selectedIndex   = old_to;
   return true;
}

function propagateInput(form, input, altinput) {
   if(!validateNonempty(form)) return(false);
   altinput.value = input.value;
   return true;
}

function propagateInputA(form, input, altinputs) {
   if(!validateNonempty(form)) return(false);
   for(var i = 0; i < altinputs.length; ++i) {
      altinputs[i].value = input.value;
   }
   return true;
}

function resetAll() {
   for(var i = 0; i < document.forms.length; ++i) {
      document.forms[i].reset();
   }
   return true;
}

function livejournalBuildUrl(user, urlty) {
   if(user.value.length == 0 || user.value.indexOf('/') == 0) {
      return failWith('Livejournal URL builder cannot build with an empty username');
   }
   var url = '';
   if(urlty == 'comm') {
      url = 'http://community.livejournal.com/' + user.value;
   } else {
      var idx = user.value.indexOf('/');
      if(idx >= 0) {
         var username = user.value.substr(0, idx);
         var extra = user.value.substr(idx + 1);
         url = 'http://' + username + '.livejournal.com/' + extra;
      } else {
         url = 'http://' + user.value + '.livejournal.com/';
      }
   }
   window.location = url;
   return false;
}

function flightawareBuildUrl(flightno) {
   if(flightno.value.length == 0) {
      return(failWith('Flightaware URL builder cannot build with an empty flight number'));
   }
   var url = '';
   url = 'http://flightaware.com/live/flight/' + flightno.value;
   window.location = url;
   return false;
}
