// All scripts are original and have been have been written by Torvald Hessel 
// for the Friends of the Austin Planetarium website.
// All scripts a copyrighted by Torvald Hessel for the Friends of the Planetarium. 
// These scripts may not be copied or used without written approval from Torvald Hessel
// and the friends of the Austin Planetarium.




// **************************** START scrolling text ********************************

  var scrollingText = '';
  scrollArray = scrollingText.split('');
  var leftPosition = 599;
  var topPosition  = 0;
  //document.write('<form name="dummyform" action="supportUs.php">')
  //document.write('<input type=image src="images/saturn.gif" style="position:absolute; top:'+topPosition+'; left:'+leftPosition+';" alt="Image of Saturn: click on this to visit the support us page">');
  //document.write('</form>')  
  // document.write('<img src="images/saturn.gif" style="position:absolute; top:'+topPosition+'; left:'+leftPosition+';">');

  var X = leftPosition + 80; // starting X position
  var Y = topPosition  + 40; // starting Y position
  var A = 75;                // length of long axis
  var B = 35;                // length of short axis
  var currentPhi = 10.2;     // starting angle. w/ 10.2 text will be right in front of planet
  var dPhi = 0.04;           // step size for moving letters
  var spacingPhi = 0.15;     // amount of spacing between letters

  for (i=0;i<scrollArray.length;i++)
  {
    var idOfElement = 'ID' + i;
    var newX = X + (A * Math.cos(currentPhi - 1.5));
    var newY = Y + (B * Math.sin(currentPhi - 0.5));
 
    document.write('<span style="font: bold 11pt Arial, Times, serif; color: red; position: absolute; top:'
                   + newY
                   + '; left:'
                   + newX
                   +';" id="'
                   + idOfElement
                   + '">'
                   + scrollArray[i]
                   + '</span>');
    currentPhi -= spacingPhi;
  }

  currentPhi = 10.2;
  function moveText()
  {
    currentPhi += dPhi;
    if (currentPhi > (2 * Math.Pi))
    {
      currentPhi -= (2 * Math.Pi);
    }
    var phiForLetter = currentPhi;
    for (i=0;i<scrollArray.length;i++)
    {
      var letterToMove = document.getElementById('ID'+i);
      letterToMove.style.left = X + (A * Math.cos(phiForLetter - 1.5));
      letterToMove.style.top  = Y + (B * Math.sin(phiForLetter - 0.5));
      // resizing section
      // max size at approc 1 1/4 pi (size=11 pt) minimum at 1/4 pi (size=1 pt)
      size = 1 + Math.abs(parseInt((Math.cos(0.5*(phiForLetter - (1.00*3.14))) * 16)));
      letterToMove.style.fontSize = size;
      //end of resizing section
      phiForLetter -= spacingPhi;
    }
  }

  function startMover()
  {
    moveText();
    setTimeout('startMover()',100);
  }

// **************************** END scrolling text   **********************************

// **************************** START navbar effects **********************************

  function navBar(tableRow, inOrOut, donate)
  {
    var leftId   = document.getElementById('l' + tableRow);  // Object Ref of left <td>
    var middleId = document.getElementById('m' + tableRow);  // Object Ref of middle <td>
    var rightId  = document.getElementById('r' + tableRow);  // Object Ref of right <td>
    
		if(donate=="donate")
		{
			var colorNormal = '#FFE4BC'; // rgb = 255,228,188 (Salmon)
      var colorOver   = '#008'; // rgb(27,38,102)   blue
      var white       = '#FFF'; // rgb = 255,255,255
      var cursorPoint = 'pointer';
		}
		else
		{
      var colorNormal = '#5E85BE'; // rgb = 94,133,190 (Pale Navy)
      var colorOver   = '#C10'; // rgb = 204,17 ,0   (warm Red)
      var white       = '#FFF'; // rgb = 255,255,255
      var cursorPoint = 'pointer';
    }
    if(tableRow==0)
    {
      return;
    }
    else
    {
      if (inOrOut)
      {
        leftId.style.backgroundColor  = colorOver;
        leftId.style.borderTopColor   = colorOver;
        middleId.style.borderTopColor = colorOver;
        middleId.style.textDecoration = 'underline';
        rightId.style.borderTopColor  = colorOver;
      }
      else
      {
        leftId.style.backgroundColor  = colorNormal;
        leftId.style.borderTopColor   = colorNormal;
        middleId.style.borderTopColor = colorNormal;
        middleId.style.textDecoration = 'none';
        rightId.style.borderTopColor  = colorNormal;
      }
    }
  }

// **************************** END navbar effects **********************************

// **************************** START moving menu  **********************************

  var isNetscape = (navigator.appName.indexOf("Netscape") != -1);  // true or false
  var defaultPosition = 200;             // Highest position of menu, we don't want things to overlap (constant)
  var currentPosition = defaultPosition; // latest position of menu (pixels from top)
  var dTop = 0;               		 // distance in pixels to top of window (variable)
  var dbottom = 0;            		 // distance in pixels to bottom of window (variable)
  var menuSize = 375;         		 // size of menu.
  var gap = 50;               		 // gap between menu and window before we scroll

  function keepMenuVisible()
  {
     moveMenu();
     setTimeout("keepMenuVisible()", 30);
  }

  function moveMenu()
  {
    // note: because of Netscape limitations we cannot nest divs and position them
    // relative to each other. We are forced to make 2 divs and move them
    // 'independently'
    
    var divToMove1 = document.getElementById("menuDiv1"); // image of Jupiter
    var divToMove2 = document.getElementById("menuDiv2"); // buttons
    
    pixelsScrolled = isNetscape ? pageYOffset : document.body.scrollTop;
    windowSize     = isNetscape ? window.innerHeight : document.body.offsetHeight;

    dTop = currentPosition - pixelsScrolled;
    dBottom = windowSize - currentPosition - menuSize + pixelsScrolled;

//  Now when dTop or dBottom become negative it means it has scrolled
//  out of view and we need to move it back. We like a gap so we trigger
//  it at 20 pixels. This is simple to do if the window is large enough
//  to display the whole menu (i.e. larger than menuSize = 375). To generate
//  proper behavior when this is NOT the case we will have to do some extra
//  tinkering so when we scroll down we keep bottom of menu visible and 
//  when we scroll up keep top of menu visible. Also in this
//  case when BOTH are negative we should NOT move the menu, untill one
//  becomes positive: this will be our direction trigger! To sumarize
//  we have 2 distinct cases: 1 menu fits in window, 2 it does not.

    if(windowSize >= (menuSize+defaultPosition+(2*gap)))  // menu fits in window.
    {
      if(dTop<gap)
      { 
        var holder = currentPosition + (pixelsScrolled + defaultPosition - currentPosition)/30;
        if((holder) <= defaultPosition)
	    {
	      divToMove1.style.top = defaultPosition;
	      divToMove2.style.top = defaultPosition - 5;
	      return;
        }
        currentPosition = holder;
        divToMove1.style.top = currentPosition;
	    divToMove2.style.top = currentPosition - 5;
        return;
      }
      if(dBottom<gap)
      {
        var holder = currentPosition + (pixelsScrolled + defaultPosition - menuSize - currentPosition)/30;
        if((holder) <= defaultPosition)
	    {
	      divToMove1.style.top = defaultPosition;
	      divToMove2.style.top = defaultPosition - 5;
	      return;
        }
        currentPosition = holder;
        divToMove1.style.top = currentPosition;
	    divToMove2.style.top = currentPosition - 5;
        return;
      }
    }
    else // menu does not fit in window
    {
    
//    in this case there are three distinct posibilities and
//    cocurrently 3 actions to take:
//    1:   both dTop & dBottom are negative.   Action: do nothing
//    2:   dTop is negative, dBottom positive. Action: keep it this way
//    3:   dBottom is negative, dTop positive. Action: keep it this way.
//
//    In cases 2 & 3 the user is scrolling. If we scroll down I want to
//    see and keep seeing the bottom of the menu. Similarly for the reverse.
//    These cases flip when the user changes scrolling direction, by not moving
//    the menu when both sides are off the window we establish that without
//    keeping track of scrolling direction or other difficult code.


      if((dTop<gap)&&(dBottom<gap)) // both top and bottom are outside the window. Do not move menu
      {
        return; //do nothing
      }
      
      var holder = currentPosition + ((pixelsScrolled - currentPosition)/20);
      if((holder) <= defaultPosition)
      {
        divToMove1.style.top = defaultPosition;
	    divToMove2.style.top = defaultPosition - 5;
        return;
      }
      else
      {
        currentPosition = holder;
        divToMove1.style.top = currentPosition;
	    divToMove2.style.top = currentPosition - 5;
        return;
      }
    }
  }

// **************************** END moving menu  **********************************

// **************************** Form Submission Section ***************************

  function submitContactForm()
  {
    // first we will audit if the user has filled in all forms.
    var formIsOK = true;
    var msg1 = "___________________________________________________________\n\n"+
               "We're sorry, we found one or more problems with your submission.\n" +
               "Please correct these and try again.\n"+
	       "___________________________________________________________\n\n";
    var msg2 = "Error(s):\n";
    
    if(document.contactUsForm.FirstName.value == "")
    {
      formIsOK = false;
      msg2 = msg2 + "-You did not fill in a first Name.\n";
    }
    if(document.contactUsForm.LastName.value == "")
    {
      formIsOK = false;
      msg2 = msg2 + "-You did not fill in a last Name.\n";
    }
    if(document.contactUsForm.email.value == "")
    {
      formIsOK = false;
      msg2 = msg2 + "-You did not fill in an email address.\n";
    }
    if(document.contactUsForm.subjectSelector.value == "")
    {
      formIsOK = false;
      msg2 = msg2 + "-Please select a subject.\n";
    }
    if(document.contactUsForm.subjectSelector.value == 'Mailing')
    {
      if(document.contactUsForm.Address.value == "")
      {
	formIsOK = false;
	msg2 = msg2 + "-Please enter your mailing address.\n";
      }
      if(document.contactUsForm.City.value == "")
      {
	formIsOK = false;
	msg2 = msg2 + "-Please enter the city you live in.\n";
      }
      if(document.contactUsForm.State.value == "")
      {
	formIsOK = false;
	msg2 = msg2 + "-Please enter the state you live in.\n";
      }
      if(document.contactUsForm.Zip.value == "")
      {
	formIsOK = false;
	msg2 = msg2 + "-Please enter your zip code.\n";
      }
    }
    if((document.contactUsForm.comments.value == "")
       && (document.contactUsForm.subjectSelector.value != 'Mailing'))
    {
      formIsOK = false;
      msg2 = msg2 + "-You did not fill in any comments to submit.\n";
    }
    
    if(formIsOK)
    {
      document.contactUsForm.subject.value = document.contactUsForm.subjectSelector.value;
      return true;
    }
    else
    {
       alert(msg1 +msg2);
       return false;
    }
  }
  
  function submitAstroQuestionForm()
  {
    return; // kill form
    // first we will audit if the user has filled in all forms.
    var formIsOK = true;
    var msg1 = "___________________________________________________________\n\n"+
               "We're sorry, we found one or more problems with your submission.\n" +
               "Please correct these and try again.\n"+
	       "___________________________________________________________\n\n";
    var msg2 = "Error(s):\n";
    if(document.astroQuestionForm.Name.value == "")
    {
      formIsOK = false;
      msg2 = msg2 + "-You did not fill in a Name\n";
    }
    if(document.astroQuestionForm.email.value == "")
    {
      formIsOK = false;
      msg2 = msg2 + "-You did not fill in an email address\n";
    }
    if(document.astroQuestionForm.question.value == "")
    {
      formIsOK = false;
      msg2 = msg2 + "-You did not fill in a question\n";
    }
    if(formIsOK)
    {
      return true;
    }
    else
    {
       alert(msg1 +msg2);
       return false;
    }
  }
  
  function donate(type)
  {
     if(type==1) // network for good
     {
	     window.open('http://www.guidestar.org/partners/networkforgood/donate.jsp?ein=77-0615845','donateWindow','width=800,height=600,resizable=yes,scrollbars=yes,status=yes');
     }
     if(type==2) // PayPal
     {
			 // first we need to find out what the user selected
			 
			 var amounts   = document.paypalForm.amount;
			 var frequency = document.paypalForm.term;
			 
			 for(i=0;i<amounts.length;i++)
			 {
			   if(amounts[i].checked)
				 {
			     var donation = amounts[i].value;
				 }
       }
			 if(donation=="other")
			 {
				  var otherDonation = document.getElementById('amountText').value
			    if(otherDonation=="")
			    {
				    alert("Please enter or select an amount");
				    return;
			    }
					if(parseFloat(otherDonation)<=0)
			    {
				   alert("Please enter an amount larger then zero");
				   return;
			    }
					// at this point we know the user enetered something valid
					var donation = Math.round(parseFloat(otherDonation)*100)/100;
			 }
			 
			
			 for(i=0;i<frequency.length;i++)
			 {
			   if(frequency[i].checked)
				 {
			     var frequencyChoosen = frequency[i].value;
				 }
			 }
			 
			 
			 if(frequencyChoosen=="O")
			 {
				 url = "https://www.paypal.com/cgi-bin/webscr?cmd=_donations&bn=PP-DonationsBF&amount=" + donation;
			 }
			 else
			 {
				 if(frequencyChoosen=='Q')
				 {
					 url = "https://www.paypal.com/cgi-bin/webscr?cmd=_xclick-subscriptions&bn=PP-SubscriptionsBF" +
				         "&a3=" + donation                              +
                 "&p3=3"                                        +
                 "&t3=M";
				 }
				 else
				 {
				   url = "https://www.paypal.com/cgi-bin/webscr?cmd=_xclick-subscriptions&bn=PP-SubscriptionsBF" +
				         "&a3=" + donation                              +
                 "&p3=1"                                        +
                 "&t3=" + frequencyChoosen;
				 }
			 }
			 url = url +
						 "&business=treasurer@austinplanetarium.org"    +
             "&item_name=Friends of the Austin Planetarium" +
             "&page_style=PayPal"                           +
             "&no_shipping=1"                               +
             "&return=http://www.AustinPlanetarium.org/thankyouDonation.php" +
             "&no_note=1"                                   +
             "&currency_code=USD"                           +
             "&lc=US"                                       +
             "&src=1"                                       +
             "&sra=1"                                       +
						 "&tax=0";

       window.open(url,'donateWindow','width=900,height=600,resizable=yes,scrollbars=yes,status=yes');
			 return;
     }
		 if(type==3) // PayPal individual ticket purchase
     {
			 // first we need to find out what the user selected
			 
			 var nbrTickets = parseInt(document.paypalForm.NbrTickets.value);
			 var donation   = nbrTickets * 10;
			 
			 var name = nbrTickets + ' zero-G raffle ticket(s), drawing May 8, 2009';
			 
			 
			 url = "https://www.paypal.com/cgi-bin/webscr?cmd=_donations&bn=PP-DonationsBF&amount=" + donation;
			 
			 url = url +
						 "&business=treasurer@austinplanetarium.org"    +
             "&item_name=" + name                           +
             "&page_style=PayPal"                           +
             "&no_shipping=1"                               +
             "&return=http://www.AustinPlanetarium.org/thankyouRaffle.php" +
             "&no_note=1"                                   +
             "&currency_code=USD"                           +
             "&lc=US"                                       +
             "&src=1"                                       +
             "&sra=1"                                       +
						 "&tax=0";

       window.open(url,'donateWindow','width=900,height=600,resizable=yes,scrollbars=yes,status=yes');
			 return;
     }
  }	  
	
	function openPrivacy()
	{
	  window.open('privacy.html','privacy','width=350,height=400,scrollbars=yes,resizable=yes'); 
  }
   
    function openVcard(name)
	{
      var fullName = 'images/' + name + 'Vcard.jpg';
	  window.open(fullName,'Vcard','width=525,height=325,scrollbars=yes,resizable=yes'); 
    }
	
	function openWindow(width,height,script)
	{
	  window.open(script,'popUp','width=' + width + ',height=' + height + ',scrollbars=yes,resizable=yes,toolbar=yes,location=yes'); 
  }
	
	 function findPreselect(selectElement, val) 
   {
		 for (var i=0; i< selectElement.length; i++)
     {
			 if (selectElement[i].value == val) 
       { 
	       selectElement.selectedIndex = i; 
       }
     }
   }
   
   
function openLoginWindow()
{
  window.open('login.php','loginWindow','width=300,height=300');
}

function underlineLine(objectID,which)
{
  var objectToChange = document.getElementById(objectID);
  if(which)
  {
    objectToChange.style.textDecoration = 'underline';
  }
  else
  {
    objectToChange.style.textDecoration = 'none';
  }
  
}


function limitEntry(e,field) 
   {
     var el = 0, f = field.form;
     
     if (e.keyCode) // for IE
     {
       // for some strange reason special characters in Netscape (see below) are
       // triggering the keyCode instead of which. To make sure that the following
       // still work we have catch them: backspace (8), delete (46), <- (37)
       //  -> (39) and tab (9)
       if((e.keyCode==46)||(e.keyCode==8)||(e.keyCode==37)||(e.keyCode==39)||(e.keyCode==9))
       {
         return true;
       }
       if((e.keyCode >= 96)&&(e.keyCode<=105)) // numbers from Numlock
       {
         keyChar = String.fromCharCode(e.keyCode-48);
       }
       else
       {
         keyChar = String.fromCharCode(e.keyCode);
       }
     }
     if (e.which) // for netscape
     {
       if((e.which >= 96)&&(e.which<=105)) // numbers from Numlock
       {
         keyChar = String.fromCharCode(e.which-48);
       }
       else
       {
         keyChar = String.fromCharCode(e.which);
       }
     }
     
     // '/D' is the JavaScript identifier for a number (0,..,9)
     if (/\D/.test(keyChar)) 
     {
       return false; //digits only
     }
     while (f.elements[el])
     {
       if (f.elements[el++] == field) break; //find current field
     }
     if ((field.value.length + 1 == field.maxLength) && f.elements[el])
     {
       f.elements[el-1].value += keyChar;
       // f.elements[el].focus(); we do not have to change focus
       return false;
     }
     return true;
   }
