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
	}
	return convert(std_time_offset);
}

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;
	display_hours = (hours < 10 && hours > 0) ? "+0"+hours : "+"+hours; // positive
	display_hours = (hours == 0) ? "0"+hours : display_hours; // handle GMT case (00:00)
	display_hours = (hours < 0 && hours > -10) ? "-0"+Math.abs(hours) : display_hours; // neg
	mins = (mins < 10) ? "0"+mins : mins;
	return display_hours+":"+mins;
}

function loadPage(page, element) {
	$("#"+element).html("<div class='waitImage'><img src='images/wait.gif'></div>");
	$("#"+element).load(page, '', function(html) {
		$('img[@src$=.png], input[type="image"]').ifixpng();
	});
}

function changeTab(tabIndex, controller, action) {
	$('.ui-tabs-nav li').removeClass('ui-tabs-selected');
	$('#tab'+tabIndex).addClass('ui-tabs-selected');
	loadPage('index.php?controller='+controller+'&action='+action, 'ajaxContent');
}

/*********************************/
/* Location Targeting */
/*********************************/
function addLocation(location, parentId, id) {
	var element;
	if (parentId == 0)
		element = "locationList"
	else
		element = "current"+parentId

	recursiveRemove(parentId, element);
	//if (locations[parentId] && $('#'+element).length > 0)
	//	removeLocation(locations[parentId]['location'], locations[parentId]['parentId'], parentId);

	for (i in locations) {
		if (locations[i]['parentId'] == id) {
			removeLocation(locations[i]['location'], id, i);
		}
	}
	displayLocation = location;
	if (displayLocation.search(/, /)) {
		var temp = displayLocation.split(",");
		displayLocation = temp[0];
	}	
	
	locations[id] = new Object();
	locations[id]['location'] = location;
	locations[id]['parentId'] = parentId;
	$('#list'+id).html(displayLocation);
	$('#locationList').append("<li><span id='current"+id+"' class='closed'>"+location+" <a href='javascript: removeLocation(\""+location+"\", "+parentId+", "+id+");'><img src='/images/tree/close.gif' alt='remove location'></a><input type='hidden' name='location["+id+"]' value='"+id+"'></span>");
}

function recursiveRemove(parentId, element) {
	if (locations[parentId] && $('#'+element).length > 0) {
		removeLocation(locations[parentId]['location'], locations[parentId]['parentId'], parentId);
		
		/*if (locations[locations[id]] > 0) {
			parentId = locations[locations[id]];
			element = "current"+parentId
			recursiveRemove(parentId, element);
		}*/
	}
}

function removeLocation(location, parentId, id) {
	displayLocation = location;
	if (displayLocation.search(/, /)) {
		var temp = displayLocation.split(",");
		displayLocation = temp[0];
	}
	locations[id]['location'] = '';
	locations[id]['parentId'] = '';
	$('#list'+id).html("<a href='javascript:addLocation(\""+location+"\", "+parentId+", "+id+");'>"+displayLocation+"</a>");
	$('#current'+id).parent().remove();
}
/*********************************/


/*********************************/
/* Language Targeting */
/*********************************/
function addLanguage(language, id) {
	$('#lang'+id).html(language);
	$('#languageList').append("<li><span id='current"+id+"'>"+language+" <a href='javascript: removeLanguage(\""+language+"\", "+id+");'><img src='/images/tree/close.gif' alt='remove language'></a><input type='hidden' name='language["+id+"]' value='"+id+"'></span>");
}

function removeLanguage(language, id) {
	$('#lang'+id).html("<a href='javascript:addLanguage(\""+language+"\", "+id+");'>"+language+"</a>");
	$('#current'+id).parent().remove();
}
/*********************************/


function pop_window(url, width, height) {
	var popit = window.open(url,"",'scrollbars=1,width=' + width + ',height=' + height + ',top=0,left=0');
}

////////////////////////////////////////////////////////////////////////////
function thickboxStart(url, width, height){
	
	// Set default values
	if(width==0){
		width=600;
	}
	if(height==0){
		height=330;
	}

	tb_show("",url + "&placeValuesBeforeTB_=savedValues&TB_iframe=true&height=" + height + "&width=" + width +  "&modal=true","");							
}
////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////
function thickboxEnd(reloadParent){
	if(reloadParent!=false){
		self.parent.location.reload();
	}
	self.parent.tb_remove();
}
////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////
function changePlan(){
	var currentPrice = parseFloat(prices[$('#plan').val()]);
	var currentDuration = parseInt(durations[$('#duration').val()])/100;
	var dur = parseInt($('#duration').val());
	var planPrice = roundNumber((currentPrice*dur)-((currentPrice*dur)*currentDuration), 2);
	var managedPrice = 0;
	if ($('#managedYes')[0].checked == true) {
		managedPrice = managementFee;
		$('#planManagement').html("<br>$"+managedPrice+" management fee");
	} else {
		$('#planManagement').html("");
	}
		
	$('#planPrice').html(planPrice);
	$('#planDuration').html(dur);
	
	var total = setup;
	total += planPrice;
	total += managedPrice;
	$('#total').html(total);
}
////////////////////////////////////////////////////////////////////////////

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function submitSignupForm(gForm, element, page) {
	// generate query string	
	var queryString = $('#'+gForm).serialize( ); 
	
	// make request
	$.ajax({
		type: "POST",
		url: page,
		data: queryString,
		beforeSend: function(html){
			$("#"+element).html("<div class='waitImage'><center><img src='/user/images/wait.gif'><br>Proccessing Order, Please Wait...</center></div>");
		},
		success: function(html){
			$("#"+element).html(html);
			$('img[@src$=.png], input[type="image"]').ifixpng();
		}
	});
}
