/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 5/25/2009
 * @author Ariel Flesler
 * @version 1.4.2
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);;
//OM Subtheme script

jQuery(document).ready(function($) {
  
  // Apply now zip code calculator
  $('#edit-submitted-personal-information-address').focusout(function() {
    var fulllocNames = new Array();
    fulllocNames['eleads-mcmc-lancaster-mcmc-1972@app.autoraptor.com'] = 'Lancaster';
    //locNames['eleads-mike-hemphill-mcmc-5347@app.autoraptor.com'] = 'Hemphill';
    fulllocNames['eleads-mike-east-belnap-mcmc-6881@app.autoraptor.com'] = 'East Belknap';
    fulllocNames['eleads-mike-camp-bowie-mcmc-3710@app.autoraptor.com'] = 'West Freeway';
    fulllocNames['eleads-mike-exchange-mcmc-6146@app.autoraptor.com'] = 'Exchange';
    fulllocNames['eleads-mike-southwest-wilshire-mcmc-9454@app.autoraptor.com'] = 'Southwest Wilshire';
    fulllocNames['eleads-mike-jacksboro-mcmc-9195@app.autoraptor.com'] = 'Jacksboro';
    var prefText = $('#webform-component-time--place--preferred-mcmc-location span.ui-btn-text');
    var model = $('select#edit-submitted-time-place-preferred-mcmc-location');
	
    calculateDistance($('#edit-submitted-personal-information-zipcode').val(), 0);
	prefText.html(fulllocNames[model.val()]);
  });
  
  $('#edit-submitted-personal-information-address-2').focusout(function() {
    var fulllocNames = new Array();
    fulllocNames['eleads-mcmc-lancaster-mcmc-1972@app.autoraptor.com'] = 'Lancaster';
    //locNames['eleads-mike-hemphill-mcmc-5347@app.autoraptor.com'] = 'Hemphill';
    fulllocNames['eleads-mike-east-belnap-mcmc-6881@app.autoraptor.com'] = 'East Belknap';
    fulllocNames['eleads-mike-camp-bowie-mcmc-3710@app.autoraptor.com'] = 'West Freeway';
    fulllocNames['eleads-mike-exchange-mcmc-6146@app.autoraptor.com'] = 'Exchange';
    fulllocNames['eleads-mike-southwest-wilshire-mcmc-9454@app.autoraptor.com'] = 'Southwest Wilshire';
    fulllocNames['eleads-mike-jacksboro-mcmc-9195@app.autoraptor.com'] = 'Jacksboro';
    var prefText = $('#webform-component-time--place--preferred-mcmc-location span.ui-btn-text');
    var model = $('select#edit-submitted-time-place-preferred-mcmc-location');
	
    calculateDistance($('#edit-submitted-personal-information-zipcode').val(), 0);
	prefText.html(fulllocNames[model.val()]);
  });
  
  $('#edit-submitted-personal-information-city').focusout(function() {
    var fulllocNames = new Array();
    fulllocNames['eleads-mcmc-lancaster-mcmc-1972@app.autoraptor.com'] = 'Lancaster';
    //locNames['eleads-mike-hemphill-mcmc-5347@app.autoraptor.com'] = 'Hemphill';
    fulllocNames['eleads-mike-east-belnap-mcmc-6881@app.autoraptor.com'] = 'East Belknap';
    fulllocNames['eleads-mike-camp-bowie-mcmc-3710@app.autoraptor.com'] = 'West Freeway';
    fulllocNames['eleads-mike-exchange-mcmc-6146@app.autoraptor.com'] = 'Exchange';
    fulllocNames['eleads-mike-southwest-wilshire-mcmc-9454@app.autoraptor.com'] = 'Southwest Wilshire';
    fulllocNames['eleads-mike-jacksboro-mcmc-9195@app.autoraptor.com'] = 'Jacksboro';
    var prefText = $('#webform-component-time--place--preferred-mcmc-location span.ui-btn-text');
    var model = $('select#edit-submitted-time-place-preferred-mcmc-location');
	
    calculateDistance($('#edit-submitted-personal-information-zipcode').val(), 0);
	prefText.html(fulllocNames[model.val()]);
  });
  
  $('#edit-submitted-personal-information-state').focusout(function() {
    var fulllocNames = new Array();
    fulllocNames['eleads-mcmc-lancaster-mcmc-1972@app.autoraptor.com'] = 'Lancaster';
    //locNames['eleads-mike-hemphill-mcmc-5347@app.autoraptor.com'] = 'Hemphill';
    fulllocNames['eleads-mike-east-belnap-mcmc-6881@app.autoraptor.com'] = 'East Belknap';
    fulllocNames['eleads-mike-camp-bowie-mcmc-3710@app.autoraptor.com'] = 'West Freeway';
    fulllocNames['eleads-mike-exchange-mcmc-6146@app.autoraptor.com'] = 'Exchange';
    fulllocNames['eleads-mike-southwest-wilshire-mcmc-9454@app.autoraptor.com'] = 'Southwest Wilshire';
    fulllocNames['eleads-mike-jacksboro-mcmc-9195@app.autoraptor.com'] = 'Jacksboro';
    var prefText = $('#webform-component-time--place--preferred-mcmc-location span.ui-btn-text');
    var model = $('select#edit-submitted-time-place-preferred-mcmc-location');
	
    calculateDistance($('#edit-submitted-personal-information-zipcode').val(), 0)
	prefText.html(fulllocNames[model.val()]);
  });
  
  $('#edit-submitted-personal-information-zipcode').focusout(function() {
    var fulllocNames = new Array();
    fulllocNames['eleads-mcmc-lancaster-mcmc-1972@app.autoraptor.com'] = 'Lancaster';
    //locNames['eleads-mike-hemphill-mcmc-5347@app.autoraptor.com'] = 'Hemphill';
    fulllocNames['eleads-mike-east-belnap-mcmc-6881@app.autoraptor.com'] = 'East Belknap';
    fulllocNames['eleads-mike-camp-bowie-mcmc-3710@app.autoraptor.com'] = 'West Freeway';
    fulllocNames['eleads-mike-exchange-mcmc-6146@app.autoraptor.com'] = 'Exchange';
    fulllocNames['eleads-mike-southwest-wilshire-mcmc-9454@app.autoraptor.com'] = 'Southwest Wilshire';
    fulllocNames['eleads-mike-jacksboro-mcmc-9195@app.autoraptor.com'] = 'Jacksboro';
    var prefText = $('#webform-component-time--place--preferred-mcmc-location span.ui-btn-text');
    var model = $('select#edit-submitted-time-place-preferred-mcmc-location');
	
    calculateDistance($(this).val(), 1);
	prefText.html(fulllocNames[model.val()]);
  });
  
  // Apply now zip code calculator
  $('#edit-submitted-address').focusout(function() {
    calculateDistanceTwo($('#edit-submitted-zipcode').val(), 0);
  });
  
  $('#edit-submitted-address-2').focusout(function() {
    calculateDistanceTwo($('#edit-submitted-zipcode').val(), 0);
  });
  
  $('#edit-submitted-city').focusout(function() {
    calculateDistanceTwo($('#edit-submitted-zipcode').val(), 0);
  });
  
  $('#edit-submitted-state').focusout(function() {
    calculateDistanceTwo($('#edit-submitted-zipcode').val(), 0);
  });
  
  $('#edit-submitted-zipcode').focusout(function() {
    calculateDistanceTwo($(this).val(), 1);
  });
  
  initialize();
  
});


// This is a test

var geocoder, clientLoc;
 
function initialize() {
  geocoder = new GClientGeocoder();
}

function calculateDistance(zipCode, alertUser)
{
  if (zipCode.length > 0) {
    var brokenzip = zipCode.split("-"); 
    if ((zipCode > 0 && zipCode == Math.round(zipCode) && zipCode.length == 5) || (zipCode.length == 10 && (brokenzip[0] > 0 && brokenzip[0] == Math.round(brokenzip[0]) && brokenzip[0].length == 5) && (brokenzip[1] > 0 && brokenzip[1] == Math.round(brokenzip[1]) && brokenzip[1].length == 4)))
    {
      // Do nothing
    } 
    else {
	  alert("Please enter a valid zipcode.");
      document.getElementById('edit-submit').disabled = true;
	  return;
    }
  } else {
    document.getElementById('edit-submit').disabled = false;
  }
  
  document.getElementById('edit-submit').disabled = false;

  var clientAddress = document.getElementById('edit-submitted-personal-information-address').value + ' ' + document.getElementById('edit-submitted-personal-information-address-2').value + ' ' + document.getElementById('edit-submitted-personal-information-city').value + ' ' + document.getElementById('edit-submitted-personal-information-state').value + ' ' + document.getElementById('edit-submitted-personal-information-zipcode').value;
  var clientAdd = clientAddress.replace(/^\s+|\s+$/g,"");

  geocoder.getLocations(clientAdd, function (response) {
	if (!response || response.Status.code != 200)
	{ 
	  if (alertUser == 1 && zipCode.length > 0)
	  {
	    alert("Please check if you have entered a valid address.");
		document.getElementById('edit-submit').disabled = true;
	  }
	  document.getElementById('edit-submitted-time-place-preferred-mcmc-location').value = '';
    }
	else {
	  clientLoc = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
	  var clientLoc1 = new GLatLng(clientLoc.lat, clientLoc.lon);
	  var dealLoc1 = new GLatLng(32.746977, -97.2997);
	  // var dealLoc2 = new GLatLng(32.714635, -97.331222);
	  var dealLoc3 = new GLatLng(32.8044, -97.2561769);
	  var dealLoc4 = new GLatLng(32.722594, -97.442614);
	  var dealLoc5 = new GLatLng(32.5396871, -97.334804);
	  var dealLoc6 = new GLatLng(32.5326359, -97.337833);
	  var dealLoc7 = new GLatLng(32.782868, -97.379323);
		  
      var locDistances = new Array();
	  locDistances[0] = clientLoc1.distanceFrom(dealLoc1, 3959).toFixed(1);
	  // locDistances[1] = clientLoc1.distanceFrom(dealLoc2, 3959).toFixed(1);
	  locDistances[1] = clientLoc1.distanceFrom(dealLoc3, 3959).toFixed(1);
	  locDistances[2] = clientLoc1.distanceFrom(dealLoc4, 3959).toFixed(1);
	  locDistances[3] = clientLoc1.distanceFrom(dealLoc5, 3959).toFixed(1);
	  locDistances[5] = clientLoc1.distanceFrom(dealLoc6, 3959).toFixed(1);
      locDistances[4] = clientLoc1.distanceFrom(dealLoc7, 3959).toFixed(1);
		  
	  var locNames = new Array();
	  locNames[0] = 'eleads-mcmc-lancaster-mcmc-1972@app.autoraptor.com';
      //locNames[1] = 'eleads-mike-hemphill-mcmc-5347@app.autoraptor.com';
	  locNames[1] = 'eleads-mike-east-belnap-mcmc-6881@app.autoraptor.com';
	  locNames[2] = 'eleads-mike-camp-bowie-mcmc-3710@app.autoraptor.com';
	  locNames[3] = 'eleads-mike-exchange-mcmc-6146@app.autoraptor.com';
	  locNames[5] = 'eleads-mike-southwest-wilshire-mcmc-9454@app.autoraptor.com';
	  locNames[4] = 'eleads-mike-jacksboro-mcmc-9195@app.autoraptor.com';
		  
	  var intialValue = parseFloat(locDistances[0]);
	  var initialKey = 0;
		  
	  for (i=0;i<=4;i++)
      {
        if (parseFloat(intialValue) >= parseFloat(locDistances[i]))
		{
	      var intialValue = parseFloat(locDistances[i]);
		  var initialKey = i;
	    }
      }
		  
	  // Set default value for preferred location
	  document.getElementById('edit-submitted-time-place-preferred-mcmc-location').value = locNames[initialKey];
	  document.getElementById('edit-submit').disabled = false;
	  return TRUE;
    }
  });
}



function calculateDistanceTwo(zipCode, alertUser)
{
  if (zipCode.length > 0) {
    var brokenzip = zipCode.split("-"); 
    if ((zipCode > 0 && zipCode == Math.round(zipCode) && zipCode.length == 5) || (zipCode.length == 10 && (brokenzip[0] > 0 && brokenzip[0] == Math.round(brokenzip[0]) && brokenzip[0].length == 5) && (brokenzip[1] > 0 && brokenzip[1] == Math.round(brokenzip[1]) && brokenzip[1].length == 4)))
    {
      // Do nothing
    } 
    else {
	  alert("Please enter a valid zipcode.");
      document.getElementById('edit-submit').disabled = true;
	  return;
    }
  } else {
    document.getElementById('edit-submit').disabled = false;
  }
  
  document.getElementById('edit-submit').disabled = false;

  var clientAddress = document.getElementById('edit-submitted-address').value + ' ' + document.getElementById('edit-submitted-address-2').value + ' ' + document.getElementById('edit-submitted-city').value + ' ' + document.getElementById('edit-submitted-state').value + ' ' + document.getElementById('edit-submitted-zipcode').value;
  var clientAdd = clientAddress.replace(/^\s+|\s+$/g,"");

  geocoder.getLocations(clientAdd, function (response) {
	if (!response || response.Status.code != 200)
	{ 
	  if (alertUser == 1 && zipCode.length > 0)
	  {
	    alert("Please check if you have entered a valid address.");
		document.getElementById('edit-submit').disabled = true;
	  }
	  document.getElementById('edit-submitted-preferred-mcmc-location').value = '';
    }
	else {
	  clientLoc = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
	  var clientLoc1 = new GLatLng(clientLoc.lat, clientLoc.lon);
	  var dealLoc1 = new GLatLng(32.746977, -97.2997);
	  // var dealLoc2 = new GLatLng(32.714635, -97.331222);
	  var dealLoc3 = new GLatLng(32.8044, -97.2561769);
	  // var dealLoc4 = new GLatLng(32.722594, -97.442614); // Original Camp Bowie
	  var dealLoc4 = new GLatLng(32.7384865, -97.4550233);
	  var dealLoc5 = new GLatLng(32.5396871, -97.334804);
	  var dealLoc6 = new GLatLng(32.5326359, -97.337833);
	  var dealLoc7 = new GLatLng(32.782868, -97.379323);
		  
      var locDistances = new Array();
	  locDistances[0] = clientLoc1.distanceFrom(dealLoc1, 3959).toFixed(1);
	  // locDistances[1] = clientLoc1.distanceFrom(dealLoc2, 3959).toFixed(1);
	  locDistances[1] = clientLoc1.distanceFrom(dealLoc3, 3959).toFixed(1);
	  locDistances[2] = clientLoc1.distanceFrom(dealLoc4, 3959).toFixed(1);
	  locDistances[3] = clientLoc1.distanceFrom(dealLoc5, 3959).toFixed(1);
	  locDistances[5] = clientLoc1.distanceFrom(dealLoc6, 3959).toFixed(1);
      locDistances[4] = clientLoc1.distanceFrom(dealLoc7, 3959).toFixed(1);
		  
	  var locNames = new Array();
	  locNames[0] = 'eleads-mcmc-lancaster-mcmc-1972@app.autoraptor.com';
      //locNames[1] = 'eleads-mike-hemphill-mcmc-5347@app.autoraptor.com';
	  locNames[1] = 'eleads-mike-east-belnap-mcmc-6881@app.autoraptor.com';
	  locNames[2] = 'eleads-mike-camp-bowie-mcmc-3710@app.autoraptor.com';
	  locNames[3] = 'eleads-mike-exchange-mcmc-6146@app.autoraptor.com';
	  locNames[5] = 'eleads-mike-southwest-wilshire-mcmc-9454@app.autoraptor.com';
	  locNames[4] = 'eleads-mike-jacksboro-mcmc-9195@app.autoraptor.com';
		  
	  var intialValue = parseFloat(locDistances[0]);
	  var initialKey = 0;
		  
	  for (i=0;i<=4;i++)
      {
        if (parseFloat(intialValue) >= parseFloat(locDistances[i]))
		{
	      var intialValue = parseFloat(locDistances[i]);
		  var initialKey = i;
	    }
      }
		  
	  // Set default value for preferred location
	  document.getElementById('edit-submitted-preferred-mcmc-location').value = locNames[initialKey];
	  document.getElementById('edit-submit').disabled = false;
    }
  }); 
}


;

