// Global Variables
var defaultBackground = "#dcdcdc";
NS4 = (document.layers) ? true : false;

function into(oObj)
{
	oObj.style.cursor="pointer"; 
	oObj.style.cursor = "hand";
}

function outof(oObj)
{
	
}	

function rollin(oObj)
{
	oObj.style.cursor="pointer"; 
	oObj.style.cursor = "hand";
	oObj.style.background="ivory";

}

function rollout(oObj)
{
oObj.style.cursor="default";
oObj.style.background=defaultBackground;

}

function copr()
{

	return("Copyright(C) Emily Clarke 2003 All Rights Reserved\n");

}

function checkEric(oObj)
{

	if(oObj.value == "e")
	{
		oObj.value="Eric Christensen";
		oMail=document.getElementById("email");
		oMail.value="\"Christensen, Eric\" <CHRISTENSENE@MAIL.ECU.EDU>";
	}

}	

function	checkClient()
{

	var winWidth;
	
	if(typeof(window.innerWidth) == 'number')
		winWidth=window.innerWidth;
	else
		winWidth=document.body.clientWidth;
	
	if(winWidth < 801)
		document.body.leftMargin=0;

}

function checkContact(form) { 
	var errors = false; 
	var missingFields="";

// Check to make sure there is a valid name, email address and phone number before allowing the form to be submitted

	if(form.name.value==""){
		errors=true;
		missingFields += "Name, ";
	}

	if(form.emailaddr.value==""){
		errors=true;
		missingFields += "Email Address, ";
	}

	if(form.reason.value=="none"){
		errors=true;
		missingFields += "Reason for Contact,";
	}
	if(form.Information.value==""){
		errors=true;
		missingFields += "Description";
	}


// if there aren't any errors, submit the form, otherwise issue an alert

	if (errors == false) { 

		return(true);

	} else {
		
		alert("You must provide the information in the required (*) fields.\n\nMissing fields are: \n" + missingFields + "\n\nPlease try again");
		return(false);
	} 
}



function checkEnter(event, oForm)
{
	var code = 0;
	
	if (NS4)
		code = event.which;
	else
		code = event.keyCode;
	
	if (code == 13)
  	 {
  	 document.oForm.submit();
  	 return false;
  	 }
	else
   	return true;
}


function loc2deg(oForm) 	// locator to degrees
{

var loc=oForm.loc.value.toUpperCase();
var x_l, x_m, x_r, y_l, y_m, y_r;
var letters = "ABCDEFGHIJKLMNOPQRSTUVWX";
var radius = 6378.16;

   var ff;

   if(loc.length == 4)   // goto the center of the gridsquare if less than 6 characters
   {
	loc +="MM";
	ff = false;
   }
   else ff = true;

   if(loc.length != 6)
   {
	alert("The locator must have\n4 chars i.e. JN68 or jn59\nor 6 chars i.e. JN68RN or jn59eg");
	return(false);
   }
   x_l = letters.indexOf(loc.charAt(0)); 
   x_m = parseInt(loc.charAt(2));
   x_r = letters.indexOf(loc.charAt(4));
   y_l = letters.indexOf(loc.charAt(1)); 
   y_m = parseInt(loc.charAt(3));
   y_r = letters.indexOf(loc.charAt(5));

   if(x_l<0 || x_l>17 || y_l<0 || y_l>17 || isNaN(x_m) || isNaN(y_m) || x_r<0 || x_r>23 || y_r<0 || y_r>23)
   {
	alert("The grid locator must be\nin the range of\nAA00AA .... RR99XX");
	return(false); 
   }
   x = x_l*10 + x_m + x_r/24;
   if(ff) x = x + 1/48;
   x *= 2;
   x -= 180;

   y = y_l*10 + y_m + y_r/24;
   if(ff) y = y + 1/48;
   y -= 90;

    oForm.lat.value=Math.abs(Math.ceil(y*10000)/10000);
    oForm.lng.value=Math.abs(Math.ceil(x*10000)/10000);
    
    if(x > 0)
    	oForm.longdir.selectedIndex="1";
    else
    	oForm.longdir.selectedIndex="0";
    	
    if(y > 0)
    	oForm.latdir.selectedIndex="0";
    else
    	oForm.latdir.selectedIndex="1";
    

   return(true);
}

function paintSat()
{
	if(posX.length != 0) 
	{
		window.setTimeout("paintSat()", 5000);  // set a callback come back in 5 seconds
	
		var map=document.getElementById("map");
		var imgLeft=map.offsetLeft;
		var imgTop=map.offsetTop;
		
		var satx=document.getElementById("sat");
		var yOff=parseInt(satx.clientHeight / 2);
		var xOff=parseInt(satx.clientWidth / 2);
		
		var xPos=posX.shift();
		var yPos=posY.shift();
	
		satx.style.top=(imgTop - yOff) + (yPos) + "px";
		satx.style.left=(imgLeft - xOff) + (xPos) + "px";
		satx.style.visibility="visible";
		
	} else {
		document.oF.submit(); 		// refresh from the server
	}


}

function paintQTH()
{

	var map=document.getElementById("map");
	var imgLeft=map.offsetLeft;
	var imgTop=map.offsetTop;
	
	var qth=document.getElementById("qth");
	var yOff=parseInt(qth.clientHeight / 2);
	var xOff=parseInt(qth.clientWidth / 2);

	qth.style.top=(imgTop - yOff) + (qthY) + "px";
	qth.style.left=(imgLeft) + (qthX) + "px";
	qth.style.visibility="visible";
	
}

function getCookie(Name) {
  var search = Name + "="
  var returnvalue = "";
  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    // if cookie exists
    if (offset != -1) { 
      offset += search.length
      // set index of beginning of value
      end = document.cookie.indexOf(";", offset);
      // set index of end of cookie value
      if (end == -1) end = document.cookie.length;
      returnvalue=unescape(document.cookie.substring(offset, end))
      }
   }
  return returnvalue;
}

function Delete_Cookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function paintTrack()
{
	var cnt=0;
	var max=posX.length;
	
	var map=document.getElementById("map");
	var imgLeft=map.offsetLeft;
	var imgTop=map.offsetTop;
	var oD="dot0";
	var dt=document.getElementById(oD);
	var yOff=parseInt(dt.clientHeight / 2);
	var xOff=parseInt(dt.clientWidth / 2);

	
	while(cnt < max)
	{
		var dotX=posX[cnt];
		var dotY=posY[cnt];
		
		var oD="dot" + cnt;
		var dot=document.getElementById(oD);

		dot.style.top=(imgTop - yOff) + (dotY) + "px";
		dot.style.left=(imgLeft - xOff) + (dotX) + "px";
		dot.style.visibility="visible";
		cnt+=10;
	}
}


function displayTime()
{
	window.setTimeout("displayTime()", 1000);
	var clock=new Date();
	clock.setTime(clock.getTime() - servOffset);
	var hours=checkTime(clock.getHours());
	var mins=checkTime(clock.getMinutes());
	var secs=checkTime(clock.getSeconds());
	
	sp=document.getElementById("curTime");
	sp.innerHTML=clock.toUTCString() + " (" + hours + ":" + mins + ":" + secs + " local time) ";
	
	var secs=Math.round(clock.getTime() / 1000);
	
	while(utc[0] < secs)	// adjust in case the browser went away
	{
		utc.shift();
		posX.shift();
		posY.shift();
	}
	
	if(secs == utc[0])
	{
		moveSat();
	}
}

var curDot=0;

function moveSat()
{
	if(posX.length != 0) 
	{
		if((curDot % 10) == 0)
		{
			var thisDot="theDot" + curDot;
			var dot=document.getElementById(thisDot);
			dot.style.color='red';
		}
		curDot += 1;
		
		var map=document.getElementById("map");
		var imgLeft=map.offsetLeft;
		var imgTop=map.offsetTop;
		
		var satx=document.getElementById("sat");
		var yOff=parseInt(satx.clientHeight / 2);
		var xOff=parseInt(satx.clientWidth / 2);
		
		var xPos=posX.shift();
		var yPos=posY.shift();
	
		satx.style.top=(imgTop - yOff) + (yPos) + "px";
		satx.style.left=(imgLeft - xOff) + (xPos) + "px";
		satx.style.visibility="visible";
		
		var sLat="N";
		var sLong="E";

		var sXp=180- (xPos / 2);
		var sYp=90 - (yPos/2);
		
		if(sXp > 0)
			sLong="W";
			
		if(sYp < 0)
			sLat="S";
		sXp=parseFloat(Math.round((Math.abs(sXp)) * 10) /10);
		sYp=parseFloat(Math.round((Math.abs(sYp)) * 10) /10);
				
		document.getElementById("pos").innerHTML= sXp + sLong + " "  + sYp + sLat;
		
		utc.shift();	
		
	} else {
		document.oF.submit(); 		// refresh from the server
	}
}

function checkTime(i)
{
	if (i<10) 
		i="0" + i;
	  
	return i;
}

