function str_pad( input, pad_length, pad_string, pad_type ) {
    // http://kevin.vanzonneveld.net
    var half = '', pad_to_go;

    function str_pad_repeater(s, len){
        var collect = '', i;

        while(collect.length < len) collect += s;
        collect = collect.substr(0,len);

        return collect;
    }

    if (pad_type != 'STR_PAD_LEFT' && pad_type != 'STR_PAD_RIGHT' && pad_type != 'STR_PAD_BOTH') { pad_type = 'STR_PAD_RIGHT'; }
    if ((pad_to_go = pad_length - input.length) > 0) {
        if (pad_type == 'STR_PAD_LEFT') { input = str_pad_repeater(pad_string, pad_to_go) + input; }
        else if (pad_type == 'STR_PAD_RIGHT') { input = input + str_pad_repeater(pad_string, pad_to_go); }
        else if (pad_type == 'STR_PAD_BOTH') {
            half = str_pad_repeater(pad_string, Math.ceil(pad_to_go/2));
            input = half + input + half;
            input = input.substr(0, pad_length);
        }
    }

    return input;
}

function populate_date(fieldid) {
  if (document.getElementById(fieldid+'_d').value == '' ||
      document.getElementById(fieldid+'_m').value == '' ||
      document.getElementById(fieldid+'_y').value == '') {
    document.getElementById(fieldid).value = '';
  } else {
    document.getElementById(fieldid).value =  document.getElementById(fieldid+'_y').value + '-' +
                                              str_pad(document.getElementById(fieldid+'_m').value,2,'0','STR_PAD_LEFT') + '-' +
                                              str_pad(document.getElementById(fieldid+'_d').value,2,'0','STR_PAD_LEFT');

  }
  return true;
}
function set_date(whichID,year,month,day) {
  document.getElementById(whichID+'_y').value = year;
  document.getElementById(whichID+'_m').value = month;
  document.getElementById(whichID+'_d').value = day;
  populate_date(whichID);
}

