
var cps = new Array(6,12,18,24,36,48);
var selected_ids = new Array();
var maps_ids = new Array();
var selected_num = 0;

function mapClick(MapName)
{

	var check_added = can_add(MapName);
	if(check_added)
	{
		$(MapName).style.backgroundColor = 'yellow';
		$("chk_"+MapName).style.backgroundColor = 'yellow';
		displayNames();
	}
} //end f

function can_add(MapName)
{
	var allowed_num = whichPackage();
	//alert('allOWED '+ allowed_num);
	if(selected_num >= allowed_num)
	{
		if(allowed_num == 48)
		{
		alert('Your Custom Select Package is full. Please delete a map form your selected list.');
		}
		else
		{
		alert('Your Custom Select Package is full. Please choose the next package or delete a map form your selected list.');
		}
		
		return false;
	}
	else
	{
		selected_ids.push(MapName);
		selected_num = selected_ids.length;
		return true;
	}
}

function addtolist(MapName)
{

	var check_added = can_add(MapName);
	if(check_added)
	{
		$(MapName).style.backgroundColor = 'yellow';
		$("chk_"+MapName).style.backgroundColor = 'yellow';
		displayNames();
	}
} //end f

function deletefromlist(whichel)
{
//alert(whichel);
//return;
	var tempArray = new Array();
	var mapid = selected_ids[whichel];
alert('Removing Map No. '+ mapid);
//return;
	for(i=0;i<selected_ids.length;i++)
	{
		if(i == whichel)
		{
			//skip
			//alert( 'skip ' + i );
		}
		else
		{
			//alert( 'add ' + i + selected_ids[i] );
			tempArray.push(selected_ids[i]);
		}
	}

	selected_ids = tempArray;
	selected_num = tempArray.length;
	initSelectedList();
	displayNames();
	var turnoff = inmanytimes(mapid);
	if(! turnoff)
	{
		$(mapid).style.backgroundColor = 'transparent';
		$('chk_'+mapid).style.backgroundColor = 'white';

	}

}

function initSelectedList()
{
	for(i=0;i<48;i++)
	{
		$("sel_mapno_"+i).innerHTML ='';
		$("sel_mapname_"+i).innerHTML ='';
		$("sel_mapprice_"+i).innerHTML ='';
		$("sel_mapdel_"+i).innerHTML ='';
	}
}

function inmanytimes(mapid)
{
	var isin = false;
	for(i=0;i<selected_ids.length;i++)
	{
		if(mapid == selected_ids[i])
		{
			isin = true;
		}
	}

	return isin;
}

function whichPackage()
{

var whichpack = 0;

for(i=0;i<cps.length;i++)
{ 
var lookfor = 'cp'+cps[i];
if (document.getElementById(lookfor).checked)
	{whichpack = cps[i];}
}
return whichpack;

}


function addToMapList(doWhat,MapName)
{
chkSelected();
alert("No selected Addtomap" + no_maps_selected );

		no_maps_selected++;
}

function chkSelected()
{
	var inputs = document.getElementsByTagName("input");
	var onList = new Array();
	var onNum = 0;
	for(index = 0; index < inputs.length; index++)
	{
		el = inputs[index];
		elname = inputs[index].name;
		if(elname.match("^chk_") )
		{
			if(el.checked == true)
			{
				onNum++;
				onList.push(elname.substring(4));
			}
		}
	}

	selected_ids = onList;
	selected_num = selected_ids.length;
}

function displayNames()
{

	var thenames = '';
	var theids = '';
	var totalprice = 0;

	var idsArray = selected_ids.sort();

	for(index = 0; index < idsArray.length; index++)
	{
		$("sel_mapno_"+[index]).innerHTML = '<div class="cm_add_mapno">' +selected_ids[index]+ '</div>';
		$("sel_mapname_"+[index]).innerHTML = '<div class="cm_add_mapname">' +GTMDATA[selected_ids[index]][0]+ '</div>';
		$("sel_mapprice_"+[index]).innerHTML = '<div class="cm_add_mapprice">' + GTMDATA[selected_ids[index]][1]+ '</div>';
		$("sel_mapdel_"+[index]).innerHTML ='<div class="buttonCMdelete" onClick="deletefromlist(\''+index+'\');">X</div>';
		theids = theids + ',' + selected_ids[index];
		totalprice = eval(totalprice) + eval(GTMDATA[selected_ids[index]][1]);
	}

	thenames = thenames.substring(1);
	theids = theids.substring(1);

	$("mapsselectednames").innerHTML=thenames;
	$("mapsselectedids").innerHTML=theids;
	$("cnt_nomaps").innerHTML = selected_num;
var pround= roundNumber(totalprice,2);

	$("totalprice").innerHTML = pround;

}
					

function addSelectedToCart()
{

var whichpack = 0;
var numinpack = 0;
numinpack = whichPackage();

if (numinpack != selected_num || numinpack == 0)
{
alert ("Please select a Total of " + numinpack + " maps");
return false;
}

$("frm_custompack").value = "cp"+ numinpack;
$("frm_mapids").value = $("mapsselectedids").innerHTML;
document.customtocart.submit();
}

function checkPackage(noPackage)
{
if(selected_num > noPackage)
{
		alert('Please remove maps from your selected list in order to choose this package');
	return false;
}

$("allowed_nomaps").innerHTML = noPackage;
return true;

}


function updateMapHintBox(mapno)
{

$("mhint_mapno").innerHTML = mapno;
$("mhint_mapname").innerHTML = GTMDATA[mapno][0];
$("mhint_mapprice").innerHTML = GTMDATA[mapno][1];
$("mhint_org_issuse").innerHTML = GTMDATA[mapno][2];
$("mhint_current_issuse").innerHTML = GTMDATA[mapno][3];
$("mhint_scale").innerHTML = GTMDATA[mapno][4];
$("mhint_inset_scale").innerHTML = GTMDATA[mapno][5];
$("mhint_projection").innerHTML = GTMDATA[mapno][6];
$("mhint_utm").innerHTML = GTMDATA[mapno][7];
$("mhint_datum").innerHTML = GTMDATA[mapno][8];
$("mhint_sheetsize").innerHTML = GTMDATA[mapno][9];
$("mhint_fold").innerHTML = GTMDATA[mapno][10];
$("mhint_topimg").src ="/images/thumbs/"+GTMDATA[mapno][11];

}


function showCMmapdata(mapno)
{

var top = $(mapno).getTop();
var left = $(mapno).getLeft();
left = left + 50;
if(top > 200) { top = top -160; }

updateMapHintBox(mapno);
$(mapno).setStyle('backgroundColor','green');
//var themsg = hinttxt(mapno);
//themsg = top + ' - ' + left;

//$("maphintbox").innerHTML =themsg;
$("maphintbox").setStyle('visibility','visible');
$("maphintbox").setStyle('top',top +'px');
$("maphintbox").setStyle('left',left + 'px');

}

function showCMmapdataOFF(mapno)
{

var top = $(mapno).getTop();
var left = $(mapno).getLeft();


$("maphintbox").setStyle('visibility','hidden');
//$("maphintbox").setStyle('top',top +'px');
//$("maphintbox").setStyle('left',left + 'px');
	var turnoff = inmanytimes(mapno);
	if(! turnoff)
	{
		$(mapno).setStyle('backgroundColor','transparent');
	}
	else
	{
		$(mapno).setStyle('backgroundColor','yellow');
	}

}

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