// function to calculate local time in a different city given the city's UTC offset

function showTime(offset, divid) {

  // create Date object for current location
  d = new Date();
  
  // convert to msec
  // add local time zone offset 
  // get UTC time in msec
  utc = d.getTime() + (d.getTimezoneOffset() * 60000);
  
  // create new Date object for different city
  // using supplied offset
  nd = new Date(utc + (3600000*offset));

  var monthVal = nd.getMonth();
  var thisMonth = "";

  //format month output
  if(monthVal == 0) {
    thisMonth = "January";
  } else if(monthVal == 1) {
    thisMonth = "February";
  } else if(monthVal == 2) {
    thisMonth = "March";
  } else if(monthVal == 3) {
    thisMonth = "April";
  } else if(monthVal == 4) {
    thisMonth = "May";
  } else if(monthVal == 5) {
    thisMonth = "June";
  } else if(monthVal == 6) {
    thisMonth = "July";
  } else if(monthVal == 7) {
    thisMonth = "August";
  } else if(monthVal == 8) {
    thisMonth = "September";
  } else if(monthVal == 9) {
    thisMonth = "October";
  } else if(monthVal == 10) {
    thisMonth = "November";
  } else if(monthVal == 11) {
    thisMonth = "December";  
  }
  
  //format hour output
  var hourVal = nd.getHours();
  var thisHour = "";

  if(hourVal == 0) {
    thisHour = 12;
  } else if(hourVal > 12) {
    thisHour = hourVal - 12;
  } else {
    thisHour = hourVal;
  }


  //format badge AM:PM
  var thisBadge = "";

  if(hourVal < 12) {
    thisBadge = "AM";
  } else {
    thisBadge = "PM";
  }


  //format minutes
  var thisMinute = nd.getMinutes();
  
  if(thisMinute < 10)
    thisMinute = "0" + thisMinute;


  //format seconds
  var thisSecond = nd.getSeconds();
  
  if(thisSecond < 10)
    thisSecond = "0" + thisSecond;


  var dateOut = nd.getDate() + " " + thisMonth + " " + nd.getFullYear();
  var timeOut = thisHour + ":" + thisMinute + ":" + thisSecond + " " + thisBadge;
  
  
  // return time as a string
  document.getElementById(divid + "time").innerHTML = timeOut;
  document.getElementById(divid + "date").innerHTML = dateOut;

}

setInterval("showTime('+11', 'worldclock1')", 1000); //Sydney
setInterval("showTime('-5', 'worldclock2')", 1000); //Peru

