function devRollover(img, obj) {
	$('#'+obj)[0].src = 'images/developer/'+img;
}
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 submitForm(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'><img src='images/wait.gif'></div>");
		},
		success: function(html){
			$("#"+element).html(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

	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];
	}	
	$('#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 removeLocation(location, parentId, id) {
	displayLocation = location;
	if (displayLocation.search(/, /)) {
		var temp = displayLocation.split(",");
		displayLocation = temp[0];
	}
	$('#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();
}
/*********************************/


/*********************************/
/* Ad */
/*********************************/
function updateSample(element, maxLength) {
	if (element.getAttribute && element.value.length>maxLength)
		element.value=element.value.substring(0,maxLength)

	var elementId = element.id;
	if ($('#sample_'+elementId).length > 0)
		$('#sample_'+elementId).html(element.value);
	
	var left = maxLength - element.value.length;
	$('#'+elementId+'CharactersLeft').html(left+"");
}
function fillUrl(url) {
	var displayUrl = $('#adDisplayUrl').val();
	if (displayUrl == '') {
		//var formattedUrl = url.replace(/https?:\/\//,'');
		var re = new RegExp("https?:\/\/(.*?)(\/|$)");
		var match = re.exec(url);
		if (match[1]) {
			$('#adDisplayUrl').val(match[1]);
			updateSample($('#adDisplayUrl')[0], 35);
		}
	}			
}
function generateKeywords(element) {
	// make request
	$.ajax({
		type: "POST",
		url: 'index.php',
		data: 'controller=signup&action=generateKeywords&searchTerm='+$('#searchTerm').val(),
		beforeSend: function(html){
			$("#"+element).html("<div class='waitImage'><img src='images/wait.gif'></div>");
		},
		success: function(html){
			$("#"+element).html(html);
		}
	});
}
function addKeyword(keyword, obj, maxKeywords){
	// get current keywords
	var foundSpot = -1;
	
	for (var i=0; i<maxKeywords; i++) {
		if ($('#keyword_'+i)[0]) {
			if ($('#keyword_'+i).val() == '') {
				foundSpot = i;
				break;
			}
		}
	}
	
	if (foundSpot == -1) {
		alert("You have reached your keyword limit of "+maxKeywords+".  Please remove a keyword first in order to add this one.");
	} else {
		// remove keyword from search list
		var row = obj.parentNode.parentNode.rowIndex;
		$('#keywordData')[0].deleteRow(row);

		$('#keyword_'+foundSpot).val(keyword);
		//if ($('#keywordId_'+foundSpot).length > 0)
		//	$('#keywordId_'+foundSpot).val(data[0]);
		
		/*
		if (saveKeyword == "no") {
			$('#keyword_'+foundSpot).val(keyword);
		} else {
			// add keyword to list
			var queryString = 'controller=campaign&action=saveKeyword&keyword=' + keyword;
			$.ajax({
				type: "POST",
				url: "index.php",
				data: queryString,
				success: function(html) {
					// parse response
					var data = html.split('|');

					// error check
					if (data[0] == "error") {
						alert(data[1]);
					} else {
						$('#keyword_'+foundSpot).val(keyword);
						$('#keywordId_'+foundSpot).val(data[0]);
					}
				}
			});
		}
		*/
	}
}
function fillData() {
	// get url
	var url = $('#adUrl')[0].value;

	// error check
	if (url == '' || url == 'http://') {
		alert("You need to input your site's URL to perform this action");
	} else {
		tb_show('','#TB_inline?height=150&width=175&inlineId=loadingMessage');
		$.ajax({
			url: "/index.php?controller=signup&action=getSiteData",
			timeout: 20000,
			data: {url: url},
			error: function(html) {
				tb_remove();
				alert("We could not connect to the supplied URL"+html);
			},
			success: fillForm	
		});
	}
}
function fillForm(result) {
	var data = result.split("|");
	
	if (data[0] == "error") {
		alert(data[1]);
	} else {
		// fill form
		$('#adTitle')[0].value = data[0];
		$('#adDescription1')[0].value = data[1];
		$('#adDescription2')[0].value = data[2];
		$('#adDisplayUrl')[0].value = data[4];
		
		// add keywords
		var keywords = data[3].split(",");
		for (var j=0; j<keywords.length; j++) {
			var foundSpot = -1;
			for (var i=0; i<20; i++) {
				if ($('#keyword_'+i)[0]) {
					if ($('#keyword_'+i).val() == '') {
						foundSpot = i;
						break;
					}
				}
			}
			if (foundSpot != -1) {
				$('#keyword_'+foundSpot).val(keywords[j]);
			} else {
				break;
			}
		}
		
		// update characters left
		updateSample($('#adTitle')[0], 25)
		updateSample($('#adDescription1')[0], 35)
		updateSample($('#adDescription2')[0], 35)
		updateSample($('#adDisplayUrl')[0], 35)
	}
	tb_remove();
}
function cancelFill() {
	tb_remove();
}
/*********************************/