/* ************************************************
 * Script: sightings.js
 * --------------------
 * 
 * AJAX based navigation for:
 * loggedin, post and thanks pages 
 * for Posting a Sighting
 **************************************************/
 // Variables for Sightings
 var xmlHttp;
 var divTagID;
 
 // Do Sighting Action
 function doSighting(url,queryString,divTagIDIn) {
   /*
   alert("URL = " + url + "\n" +
         "queryString = " + queryString + "\n" +
         "divTagIDIn = " + divTagIDIn);
   */
   divTagID = divTagIDIn;
   if (window.ActiveXObject) {
     xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
   } else {
     xmlHttp = new XMLHttpRequest();
   }
   xmlHttp.open("POST",url,true);
   xmlHttp.onreadystatechange = handleStateChange;
   xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
   xmlHttp.send(queryString);
 }
 
 // Handle State Change
 function handleStateChange() {
   //alert("IN HANDLE STATE CHANGE");
   if (xmlHttp.readyState == 4) { // COMPLETE
     if (xmlHttp.status == 200) { // SUCCESS
       var result = xmlHttp.responseText;
       //alert("result="+result);
       var response = document.getElementById(divTagID);
       if (response.hasChildNodes()) {
         response.removeChild(response.childNodes[0]);
       }
       response.innerHTML = result;
     } else {
       alert("FAILURE");
     }
   }
 }
 
 // Update Sightings Order
 function updateSightingsOrder() {
   var ind = document.formSightingsSort.selectSightingsOption.selectedIndex;
   var val = document.formSightingsSort.selectSightingsOption.options[ind].value;
   /* NON AJAX
   document.location = "/celebrities/sightings/index.jsp?&sort_by=" + val; 
   */
   // AJAX
   //alert("Sort By = " + val);
   doSighting("/celebrities/sightings/includes/jsp/ajax_sightings_content.jsp","sort_by=" + val,"sightings_list");
 }
 
 // Show Sightings Page
 function showSightingsPage(sortBy,pageNo) {
   doSighting("/celebrities/sightings/includes/jsp/ajax_sightings_content.jsp","sort_by=" + sortBy + "&pageNo="+pageNo,"sightings_list");
 }
 
 // Post Sighting
 function postSighting() {
   var error="";
   var nameSight=document.postSighting.nameSight.value;
   if (nameSight == "") {
     error+="Celebrity Name not defined\n"
   }
   var locSight=document.postSighting.locSight.value;
   if (locSight == "") {
     error+="Location not defined\n"
   }
   var descSight=document.postSighting.descSight.value;
   if (descSight == "") {
     error+="No Description of Sighting\n"
   }
   var queryString = "action_section=thanks&nameSight="+nameSight+"&locSight="+locSight+"&descSight="+descSight;
   var year=parseInt(document.postSighting.year.options[document.postSighting.year.selectedIndex].value);
   var month=parseInt(document.postSighting.month.options[document.postSighting.month.selectedIndex].value);
   var day=parseInt(document.postSighting.day.options[document.postSighting.day.selectedIndex].value);
   queryString += "&year="+year+"&month="+month+"&day="+day;
   if (error != "") {
     alert(error);
   } else {
     doSighting("/celebrities/sightings/includes/jsp/ajax_sightings_action.jsp",queryString,"action_box");
   }
 }