//# GOOGLE MAP SCRIPTS #//

var geoXml=null;

var map = null;

var geocoder = null;



var directionsPanel;

var directions;



var rt = false;

var trip = new Array( 0,0 );

function googleMapInit() {

  try

  {

    if (GBrowserIsCompatible())

    {



      map = new GMap2(document.getElementById("gMap"));

      geocoder = new GClientGeocoder();



      directionsPanel = document.getElementById("route");

      directions = new GDirections(map, directionsPanel);

      

      GEvent.addListener(directions, "error", handleErrors);



      GEvent.addListener

      (

        directions

        ,"load"

        ,function() 

        {

          if(rt)

          {

            trip[1] = Math.round( directions.getDistance().meters*0.000621371192*10 )/10;

            rt = false;

            GetEstimate();

          }

          else

          {

            trip[0] = Math.round( directions.getDistance().meters*0.000621371192*10 )/10;

            rt = true;

            setTimeout( function(){ GetDirections( oForm.zip.value, "55033" ); }, 500 );

          }

        }

      ); 



    }

  }catch(e){alert(e.message)}

}



function GetDirections( sStartZip, sDestZip )

{



  var adr_from = sStartZip;

  var adr_to = sDestZip;

  

  if( (adr_from != "") && (adr_to != "-1" && adr_to != "") )

  {

    directions.clear(); 

    directions.load("from: " +adr_from + " to: " + adr_to);

  }



  return false;



}



function handleErrors(){

  if (directions.getStatus().code == G_GEO_UNKNOWN_ADDRESS)

    alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + directions.getStatus().code);

  else if (directions.getStatus().code == G_GEO_SERVER_ERROR)

    alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + directions.getStatus().code);



  else if (directions.getStatus().code == G_GEO_MISSING_QUERY)

    alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + directions.getStatus().code);



  //   else if (directions.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong

  //     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + directions.getStatus().code);



  else if (directions.getStatus().code == G_GEO_BAD_KEY)

    alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + directions.getStatus().code);



  else if (directions.getStatus().code == G_GEO_BAD_REQUEST)

    alert("A directions request could not be successfully parsed.\n Error code: " + directions.getStatus().code);



  else alert("An unknown error occurred.");

}






//# ESTIMATE FORM SCRIPTS #//

function SetSelectedDate(y,m,d)
{
  var sel = new Date(y,(m-1),d);
  var day = sel.getDay();
  
  oForm.date_text.value = m+"/"+d+"/"+y;
  
  oSelectedDate = { Month: m, Date: d, Year: y, Day: day };
}

function GetBasePrice()
{
  var mdisc = 0;
  var ddisc = 0;
  var screen = 0;
  var type = "";
  
  //# calendar day...
  switch(true)
  {
    case (oSelectedDate.Month < 4 || oSelectedDate.Month > 10): mdisc = 0.3; break;
    case (oSelectedDate.Month == 4 || oSelectedDate.Month == 10): mdisc = 0.2; break;
    case (oSelectedDate.Month == 5 || oSelectedDate.Month == 9): mdisc = 0.1; break;
    default: mdisc = 0; break;
  }
  
  //# day of week...
  ddisc = (oSelectedDate.Day < 4)
    ? 0
    : 0.1
  ;
  
  //# total...
  disc = mdisc + ddisc;
  if(disc>0){ disc = (Math.floor(disc*10)/10); }

  //# screen cost by type...
  for( var i = 0; i < oForm.moviescreen.length; i++ )
  {
    if( oForm.moviescreen[i].checked )
    {
      screen = parseInt(oForm.moviescreen[i].value);
      type = oForm.moviescreen[i].id;
      break;
    }
  }
  
  //# ???
  if(screen < 1){ alert("No... That's not true! That's IMPOSSIBLE!!!"); return; }

  //# PROFIT!!!
  return { BasePrice: screen, DiscountPrice: Math.floor(screen * (1-disc)*100)/100, Type: type, Monthly: mdisc, Daily: ddisc, Total: disc };
  
}


function ProcessForm()
{
  if(oForm.zip.value == ""){ alert("Please enter your zip code."); oForm.zip.focus(); return; }
  if(oForm.date_text.value == ""){ alert("Please select a delivery date."); oForm.date_text.focus(); return; }
  
  oForm.calc_button.value = "Please Wait...";
  oForm.calc_button.disabled = true;
  
  GetDirections( "55033", oForm.zip.value );
}

function GetEstimate()
{

  var Estimate = GetBasePrice();  
  var per_mile = 1.25;
  var day_limit = 120;
  
  Estimate.Miles = Math.ceil( ((trip[0] + trip[1]) * per_mile)*100 )/100;
  Estimate.Lodging = 0;
  if( trip[0] > 120 || trip[1] > 120 )
  {
    Estimate.Lodging = (Estimate.Type == "TZ-40")? 250 : 125 ;
  }
  Estimate.Date = oSelectedDate; //oForm.date_text.value;
  
  oForm.base_price.value = Estimate.BasePrice;
  oForm.discounted_price.value = Estimate.DiscountPrice;
  oForm.discount_month.value = Estimate.Monthly;
  oForm.discount_day.value = Estimate.Daily;
  oForm.discount_total.value = Estimate.Total;
  oForm.delivery_miles.value = Estimate.Miles;
  oForm.delivery_lodging.value = Estimate.Lodging;
  oForm.delivery_zip.value = oForm.zip.value;
  oForm.delivery_date.value = oForm.date_text.value;

  oForm.calc_button.value = "Calculate";
  oForm.calc_button.disabled = false;

}


var re = /^[0-9]*$/;
function NumbersOnly(evt)
{
  evt = (evt) ? evt : (window.event) ? event : null;
  if (evt)
  {
    var charCode = (evt.charCode) ? evt.charCode :
             ((evt.keyCode) ? evt.keyCode :
             ((evt.which) ? evt.which : 0));
    
    return ( charCode < 58 )? true : false ;
  }    
}

var oForm = null;
var now = new Date();
var then = new Date();
then = new Date( then.setMonth( then.getMonth()+6 ));

var oCal = null;
var oSelectedDate = null;

function FormIni()
{
  //# create the form object and assign default Date (tomorrow) and Zip filter function.
  oForm = document.forms[0];
  oForm.date_text.value = (now.getMonth()+1)+"/"+(now.getDate()+1)+"/"+now.getFullYear();
  oForm.zip.onkeypress = NumbersOnly;
  
  oSelectedDate = { Month: (now.getMonth()+1), Date: (now.getDate()+1), Year: now.getFullYear(), Day: (now.getDay()+1) };

  //# create our calendar object and assign the date select event and CSS
  oCal = new CalendarPopup('cal_div');
  oCal.setReturnFunction("SetSelectedDate");
  oCal.setCssPrefix("TEST");

  //# declare our date range, six months from today.
  //# The calendar (oCal) will be set to only allow selections from between now and then.
  //#oCal.addDisabledDates(null,formatDate(now,"yyyy-MM-dd"));
  //#oCal.addDisabledDates( formatDate( then,"yyyy-MM-dd"), null);
  
  oCal.addDisabledDates(null,formatDate(now,"MM/dd/yyyy"));
  oCal.addDisabledDates( formatDate( then,"MM/dd/yyyy"), null);
  
}
