function calculate_time_zone() {
	var rightNow = new Date();
	var jan1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);  // jan 1st
	var june1 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0); // june 1st
	var temp = jan1.toGMTString();
	var jan2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
	temp = june1.toGMTString();
	var june2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
	var std_time_offset = (jan1 - jan2) / (1000 * 60 * 60);
	var daylight_time_offset = (june1 - june2) / (1000 * 60 * 60);
	var dst;
	if (std_time_offset == daylight_time_offset) {
		dst = "0"; // daylight savings time is NOT observed
	} else {
		// positive is southern, negative is northern hemisphere
		var hemisphere = std_time_offset - daylight_time_offset;
		if (hemisphere >= 0)
			std_time_offset = daylight_time_offset;
		dst = "1"; // daylight savings time is observed
	}
	var i;
	// check just to avoid error messages
	if (document.getElementById('timezone') && document.getElementById('timezone').options) {
		for (i = 0; i < document.getElementById('timezone').options.length; i++) {
			if (document.getElementById('timezone').options[i].value == convert(std_time_offset)+","+dst) {
				document.getElementById('timezone').selectedIndex = i;
				break;
			}
		}
	}
	
	if ( document.getElementById('timezone_dropdown') ){
		$("div#timezone_dropdown").find("span").attr("id", convert(std_time_offset)+","+dst);
		$("div#timezone_dropdown").find("span").html(getName(convert(std_time_offset)+","+dst));
		$("input#timezone_hidden").val(convert(std_time_offset)+","+dst);
	}
}

function convert(value) {
	var hours = parseInt(value);
   	value -= parseInt(value);
	value *= 60;
	var mins = parseInt(value);
   	value -= parseInt(value);
	value *= 60;
	var secs = parseInt(value);
	var display_hours = hours;
	// handle GMT case (00:00)
	if (hours == 0) {
		display_hours = "00";
	} else if (hours > 0) {
		// add a plus sign and perhaps an extra 0
		display_hours = (hours < 10) ? "+0"+hours : "+"+hours;
	} else {
		// add an extra 0 if needed 
		display_hours = (hours > -10) ? "-0"+Math.abs(hours) : hours;
	}
	
	mins = (mins < 10) ? "0"+mins : mins;
	return display_hours+":"+mins;
}

function getName(offset){
	var timezones = [{ 
						'-12:00,0' : 'International Date Line West (GMT-12:00)',
						'-11:00,0' : 'Midway Island, Samoa (GMT-11:00)',
						 '-10:00,0' : 'Hawaii (GMT-10:00)' ,
						 '-09:00,1' : 'Alaska (GMT-09:00)' ,
						 '-08:00,1' : 'Pacific Time (US & Canada) (GMT-08:00)' ,
						 '-07:00,0' : 'Arizona (GMT-07:00)' ,
						 '-07:00,1' : 'Mountain Time (US & Canada) (GMT-07:00)' ,
						 '-06:00,0' : 'Central America, Saskatchewan (GMT-06:00)' ,
						 '-06:00,1' : 'Central Time (US & Canada), Guadalajara, Mexico city (GMT-06:00)' ,
						 '-05:00,0' : 'Indiana, Bogota, Lima, Quito, Rio Branco (GMT-05:00)' ,
						 '-05:00,1' : 'Eastern Time (US & Canada) (GMT-05:00)' ,
						 '-04:00,1' : 'Atlantic Time (Canada) (GMT-04:00)' ,
						 '-04:00,0' : 'Caracas, La Paz (GMT-04:00)' ,
						 '-03:30,1' : 'Newfoundland (GMT-03:30)' ,
						 '-03:00,1' : 'Greenland, Brasilia, Montevideo (GMT-03:00)' ,
						 '-03:00,0' : 'Buenos Aires, Georgetown (GMT-03:00)' ,
						 '-02:00,1' : 'Mid-Atlantic (GMT-02:00)' ,
						 '-01:00,0' : 'Cape Verde Is. (GMT-01:00)' ,
						 '-01:00,1' : 'Azores (GMT-01:00)' ,
						 '00:00,1' : 'Casablanca (GMT-00:00)' ,
						 '00:00,0' : 'GMT: Dublin, Edinburgh, Lisbon, London (GMT-00:00)' ,
						 '+1:00,1' : 'Amsterdam, Berlin, Rome, Vienna, Prague, Brussels (GMT+01:00)' ,
						 '+1:00,0' : 'West Central Africa (GMT+01:00)' ,
						 '+2:00,0' : 'Amman, Athens, Istanbul, Beirut, Cairo, Jerusalem (GMT+02:00)' ,
						 '+3:00,1' : 'Baghdad, Moscow, St. Petersburg, Volgograd (GMT+03:00)' ,
						 '+3:00,0' : 'Kuwait, Riyadh, Nairobi, Tbilisi (GMT+04:00)' ,
						 '+3:30,0' : 'Tehran (GMT+03:30)' ,
						 '+4:00,0' : 'Abu Dhabi, Muscat (GMT+04:00)' ,
						 '+4:00,1' : 'Baku, Yerevan (GMT+04:00)' ,
						 '+4:30,0' : 'Kabul (GMT+04:30)' ,
						 '+5:00,1' : 'Ekaterinburg (GMT+05:00)' ,
						 '+5:00,0' : 'Islamabad, Karachi, Tashkent (GMT+05:00)' ,
						 '+5:30,0' : 'Chennai, Kolkata, Mumbai, New Delhi, Sri Jayawardenepura (GMT+05:30)' ,
						 '+5:45,0' : 'Kathmandu (GMT+05:45)' ,
						 '+6:00,0' : 'Astana, Dhaka (GMT+06:00)' ,
						 '+6:00,1' : 'Almaty, Nonosibirsk (GMT+06:00)' ,
						 '+6:30,0' : 'Yangon (Rangoon) (GMT+06:30)' ,
						 '+7:00,1' : 'Krasnoyarsk (GMT+07:00)' ,
						 '+7:00,0' : 'Bangkok, Hanoi, Jakarta (GMT+07:00)' ,
						 '+8:00,0' : 'Beijing, Hong Kong, Singapore, Taipei (GMT+08:00)' ,
						 '+8:00,1' : 'Irkutsk, Ulaan Bataar, Perth (GMT+09:00)' ,
						 '+9:00,1' : 'Yakutsk (GMT+09:00)' ,
						 '+9:00,0' : 'Seoul, Osaka, Sapporo, Tokyo (GMT+09:00)' ,
						 '+9:30,0' : 'Darwin (GMT+09:30)' ,
						 '+9:30,1' : 'Adelaide (GMT+09:30)' ,
						 '+10:00,0' : 'Brisbane (GMT+10:00)' ,
						 '+10:00,1' : 'Melbourne, Sydney (GMT+10:00)' ,
						 '+11:00,0' : 'Solomon Is. (GMT+11:00)' ,
						 '+12:00,1' : 'Auckland, Wellington (GMT+12:00)' ,
						 '+12:00,0' : 'Fiji (GMT+12:00)' ,
						 '+13:00,0' : 'Nuku alofa (GMT+13:00)' }
	                 ];
	
	return timezones[0][offset];	
}

onload = calculate_time_zone;