function focusElementById(id) {
	document.getElementById(id).focus();
}
function toggleLoginView () {
 var loginNode = document.getElementById('loginForm');
 if(loginNode.style.visibility == 'hidden'){
	loginNode.style.visibility = 'visible';
 }
 else loginNode.style.visibility = 'hidden';	
}

function removeLayer(layerName, id) {
	if( confirm('Are you sure you want to remove "' +layerName+'"?')) {
		window.location.href='/script/delete_layer.php?id=' + id;	
	}
}



function toggleTilesSection() {
	document.myForm.from.disabled= !document.myForm.from.disabled;
	document.myForm.to.disabled = !document.myForm.to.disabled;	
}
function toggleDevSection() {
	document.myForm.num_hours.disabled= !document.myForm.num_hours.disabled;
	document.myForm.task_description.disabled = !document.myForm.task_description.disabled;	
}

function calculatePrice() {
	setTimeout(calcPrice, 500);
}

function sumInvoice() {
	var setup = parseFloat(document.invForm.setup.value);
	var mysql = document.invForm.mysql_price?parseFloat(document.invForm.mysql_price.value):0;
	var num_hours = document.invForm.num_hours?parseFloat(document.invForm.num_hours.value):0;
	var tiles = document.invForm.tiles_price?parseFloat(document.invForm.tiles_price.value):0;
	var ship = parseFloat(document.invForm.delivery_cost.value);
//	alert(setup + ', ' + mysql + ', ' + num_hours+ ', ' + tiles + ', ' + ship);

	var total = setup + mysql + (num_hours * 90) + tiles + ship;
	//alert(total);
	document.getElementById('total').innerHTML = total;
}
function removeShapefile(shapefileTitle, id) {
	if( confirm('Are you sure you want to remove "' +shapefileTitle+ '"?')) {
		window.location.href='/script/delete_shapefile.php?id=' + id;	
	}
}
function removeItem(itemTitle, id) {
	if( confirm('Are you sure you want to remove "' + itemTitle +'"?')) {
		window.location.href='/script/remove_cart_item.php?id=' + id;	
	}
}

function calculatePrice() {
	setTimeout(calcPrice, 500);
}

//puts the number of tiles where it belongs, and calculates the price
function calcPrice() {
	var total_price = 0;
	total_price += 500; // for the processing fee
//	alert(document.getElementById('mail').checked + ', ' + document.getElementById('download').checked);
	if(document.getElementById('mail').checked==true) total_price += 50; // mail delivery options
	else if(document.getElementById('download').checked==true) total_price += 0;

	if(document.getElementById('mysql').checked == true) total_price += 30;
	// calculate the number of tiles and associated price
	var zl_from = document.getElementById('from').value;	
	var zl_to = document.getElementById('to').value;
	if(zl_from > zl_to){
		document.getElementById('num_tiles').innerHTML = 0;
		document.getElementById('price_disp').innerHTML = '$ ' + total_price.toFixed(2);
		return;
	}

	var num_tiles = getNumTiles(zl_from, zl_to);	
	var rate;
	if(num_tiles < 101) {
		rate= 10;
	}
	else if (num_tiles < 1001) {
		rate = .5;
	}
	else if (num_tiles < 100001) {
		rate = .01;
	}
	else if (num_tiles < 10000001) {
		rate = .001;
	}
	else if (num_tiles < 17000000001) {
		rate = .0001;
	}
	else {
		document.getElementById('num_tiles').innerHTML = '[Too Many Tiles]';
		document.getElementById('price_disp').innerHTML = '[N/A]';
		return;
	}
	// calculate the cost of the tiles
	var cost_tiles = rate * num_tiles;
	total_price += cost_tiles;

	document.getElementById('num_tiles').innerHTML = num_tiles;
	document.getElementById('price_disp').innerHTML = '$ ' + total_price.toFixed(2);

}
function getNumTiles(startZoom, endZoom) {
	return 500;
}

function readColor(str){
	color = new Object;
	str = str.substring(4,str.indexOf(")"));
	color.r=str.split(",")[0];
	color.g=str.split(",")[1];
	color.b=str.split(",")[2];
	return color;
}

function copyColor(obj){
	color = readColor(obj.style.borderColor);
	pastes = document.getElementsByName('paste');
	for(i=0;i<pastes.length;i++){
		pastes[i].style.visibility='visible';
		pastes[i].setAttribute('r',color.r);
		pastes[i].setAttribute('g',color.g);
		pastes[i].setAttribute('b',color.b);
	}
}

function pasteColor(obj, num){
	document.getElementById('color_r_'+num).value=obj.getAttribute('r');
	document.getElementById('color_g_'+num).value=obj.getAttribute('g');
	document.getElementById('color_b_'+num).value=obj.getAttribute('b');
	pastes = document.getElementsByName('paste');
	for(i=0;i<pastes.length;i++){
		pastes[i].style.visibility='hidden';
		setColorBox(color, num);
	}
}

function clearColor(num){
	color = new Object;
	color.r='';
	color.g='';
	color.b='';
	document.getElementById('color_r_'+num).value=color.r;
	document.getElementById('color_g_'+num).value=color.g;
	document.getElementById('color_b_'+num).value=color.b;
	setColorBox(color, num);
}

function setColorBox(color, num){
	if(color.r=='' && color.g=='' && color.b==''){
		color.r=255; color.g=255; color.b=255;
	}
	document.getElementById('color_'+num).style.borderColor="rgb("+color.r+","+color.g+","+color.b+")";
	document.getElementById('color_'+num).childNodes[0].style.backgroundColor="rgb("+color.r+","+color.g+","+color.b+")";
}

function setColorBoxTyped(num){
	color = new Object;
	color.r = document.getElementById('color_r_'+num).value;
	color.g = document.getElementById('color_g_'+num).value;
	color.b = document.getElementById('color_b_'+num).value;
	setColorBox(color, num);
}

function copyLegend(ctnrID){
	colors = document.getElementById(ctnrID).childNodes;
	j=0;
	for(i=0;i<colors.length;i++){
		if(colors[i].nodeType!=1) continue;
		color = readColor(colors[i].style.borderColor);
		document.getElementById('color_r_'+j).value = color.r;
		document.getElementById('color_g_'+j).value = color.g;
		document.getElementById('color_b_'+j).value = color.b;
		setColorBox(color, j);
		j++;
	}
}

function formatNumber(e) {
	var nf = new NumberFormat(e.value);
	nf.setPlaces(0);
	e.value = nf.toFormatted();
}

function prorate(amount)
{
	amount = amount.replace(",", "");
	var now = new Date();
	var today = now.getDate();
	var month = now.getMonth();
	var year = now.getFullYear();
	var num_days = (32 - new Date(year, month, 32).getDate());
	var prorate_ratio = (num_days - (today - 1)) / num_days; // Subtract 1 to include the current day
	return amount * prorate_ratio;
}

function onProrateChange() {
	var proratedElement = document.getElementById('initial_prorated_amount');
	proratedElement.value = new NumberFormat(proratedElement.value.replace(",","")).toFormatted();
}

function onMonthlyChange() {
	var monthly = document.getElementById('monthly_amount');
	monthly.value = new NumberFormat(monthly.value.replace(",","")).toFormatted();
	
	var proratedAmount = prorate(monthly.value);
	var proratedElement = document.getElementById('initial_prorated_amount');
	proratedElement.value = new NumberFormat(proratedAmount).toFormatted();
	
	var annual = document.getElementById('annual_amount');
	annual.value = new NumberFormat(monthly.value.replace(",","")*12).toFormatted();
}

function onAnnualChange() {
	var annual = document.getElementById('annual_amount');
	annual.value = new NumberFormat(annual.value.replace(",","")).toFormatted();

	var monthly = document.getElementById('monthly_amount');
	monthly.value = new NumberFormat(annual.value.replace(",","")/12).toFormatted();
	
	var proratedAmount = prorate(monthly.value);
	var proratedElement = document.getElementById('initial_prorated_amount');
	proratedElement.value = new NumberFormat(proratedAmount).toFormatted();
}

function displayBoxQuestion(obj){
	sibs = obj.parentNode.childNodes;
	for(i=0;i<sibs.length;i++){
		if(sibs[i].tagName == 'DT')sibs[i].firstChild.style.color='#444444';
		else if(sibs[i].tagName == 'DD')sibs[i].style.display='none';
	}
	obj.firstChild.style.color='#70a100';
	if(obj.nextSibling.style) obj.nextSibling.style.display='inline'
	else obj.nextSibling.nextSibling.style.display='inline'
}

function toggleInputs(on, idToScanForInputs) {
	var subInputs = [];
	if (arguments.length >= 4) {
		subInputs = toggleInputs.apply({}, Array.prototype.slice.apply(arguments, [2]));
	}
	var scanMe = document.getElementById(idToScanForInputs);
	if (!scanMe)
		return;
	
	var inputs = scanMe.getElementsByTagName('INPUT');
	for (var i = 0; i < inputs.length; i++) {
		var fnd = false;
		for (var j = 0; j < subInputs.length; j++) {
			if (subInputs[j] == inputs[i] && subInputs[j].disabled) {
				fnd = true; break;
			}
		}
		if (fnd) continue;
		inputs[i].disabled = !on;
	}

	var selects = scanMe.getElementsByTagName('SELECT');
	for (var i = 0; i < selects.length; i++) {
		var fnd = false;
		for (var j = 0; j < subInputs.length; j++) {
			if (subInputs[j] == selects[i] && subInputs[j].disabled)
				fnd = true;
		}
		if (fnd) continue;
		selects[i].disabled = !on;
	}
	return Array.prototype.concat.apply(Array.prototype.slice.apply(inputs, [0]), Array.prototype.slice.apply(selects, [0]));
}
// enables or disables the color map section
function setEnabledColorMap(isEnabled) {
  try {
  var form = document.forms.layerForm;
  for(i=0; i<form.elements.length; i++) {
    var element = form.elements[i];
    if (element.name == "heatmap" || element.name =="heatmapFile" || element.name == "uploadHeatmap") {
      element.disabled = !isEnabled;
      //alert('name: '+element.name+', id: ' + element.id);
    }
  }
  // enable/disable anchors
  var cmt = document.getElementById('colormapTable');
  var anArray = cmt.getElementsByTagName('A');
  for( i=0; i<anArray.length; i++) {

    if( isEnabled == true ) {
			if(anArray[i].onclick == null) {
				anArray[i].onclick = anArray[i].backupOnClick;
			}
    }
    else {
			anArray[i].backupOnClick =  anArray[i].onclick;
			anArray[i].onclick = null;
    }
	/*	alert(anArray[i].onclick);
	alert(anArray[i].backupOnClick);*/

  }
  } catch(e) {alert(e);}
}

// enables or disables the fill color selecter section
function setEnabledFillColor(isEnabled) {
  document.getElementById('colorPickerShowMe-fill').disabled=!isEnabled;
}
