// make ajax request object function createRequestObject() { var requestObject; var browser = navigator.appName; if (browser == "Microsoft Internet Explorer") { requestObject = new ActiveXObject("Microsoft.XMLHTTP"); } else { requestObject = new XMLHttpRequest(); } return requestObject; } // declare a variable for the request object var http = createRequestObject(); var distance,mileage,directions,state; var tooFar = 0; var invalidZip = 0; var cost = 0; var overstockShedID; // send the zipcode function getMileage(id) { overstockShedID = id; // save the id to a global var so value isn't lost in Ajax calls var zipcode = document.getElementById('zipcode_' + overstockShedID).value; if (zipcode > 0 && zipcode.match(/^\d{5}$/)) { document.getElementById('submitButton_' + overstockShedID).disabled = true; if (GBrowserIsCompatible()) { getState(zipcode); // setup distance query from google api directions = new GDirections(); directions.load('17603 to ' + document.getElementById('zipcode_' + overstockShedID).value); if (navigator.userAgent.indexOf("Opera") != -1) { GEvent.addListener(directions, "load", getMileagePrice()); } else { GEvent.addListener(directions, "load", getMileagePrice); } } else { alert ("Your web browser is incompatible with this website! \nPlease upgrade to one of: \nFirefox 1 or newer \nInternet Explorer 6 or newer \nSafari 3 or newer \nOpera 9 or newer"); } } } function getMileagePrice() { Shed = new shed('Overstock'); Shed.Shed_depth = document.getElementById('depth_' + overstockShedID).value; Shed.Shed_width = document.getElementById('width_' + overstockShedID).value; // disable submit button so the impatient don't cause problems // need a way to timeout the google query in case we have connection problems document.getElementById('submitButton_' + overstockShedID).disabled = true; distance = directions.getDistance(); if(distance === null){ tooFar = 0; invalidZip = 1; document.getElementById('message_' + overstockShedID).innerHTML = "Please place a valid zip code in the above field." GUnload(); document.getElementById('submitButton_' + overstockShedID).disabled = false; return; } var miles = distance.meters / 1609.344; miles = miles.toFixed(0); // if less than 400 miles, we're good to go if (miles <= 400 ) { // get a charged shipping distance if (miles - 20 < 0) { var mileage = 0; } else { var mileage = miles - 20; } // calculate the cost if (Number(Shed.Shed_width) >= Number(Shed.Shed_depth)) { var size = Shed.Shed_depth; } else { var size = Shed.Shed_width; } if (size < 10) { cost = 3.6 * mileage; } else { cost = (3.9 * mileage) + 0; if (state != "PA") { cost += 50; } if (state == "VA" || state == "WV" || state == "CT" || state == "VT" || state == "MA" || state == "KY" ) { cost = cost + 50; } if (state == "RI" || state == "NH" || state == "NC" || state == "TN") { cost = cost + 50 * 2; } if (state == "ME" || state == "SC") { cost += 50 * 3; } } document.getElementById('message_' + overstockShedID).innerHTML = "You are " + miles + " miles away, and it will cost $" + cost.toFixed(2) + " to ship your shed"; invalidZip = 0; tooFar = 0; getAPrice(overstockShedID); GUnload(); // need to enable the submit button once again document.getElementById('submitButton_' + overstockShedID).disabled = false; } // if it's too far away... else if (miles > 400) { tooFar = 1; invalidZip = 0; document.getElementById('message_' + overstockShedID).innerHTML = "Sorry, you live too far away for us to ship to you"; GUnload(); // we have an error otherwise } else { tooFar = 0; invalidZip = 1; document.getElementById('message_' + overstockShedID).innerHTML = "Please place a valid zip code in the above field." GUnload(); } } function getState(zip) { http.open("get", "./includes/getState.php?zip=" + zip, true); http.onreadystatechange = handleState; http.send(null); } function handleState() { if (http.readyState == "4") { var response = http.responseText; if (response == "error") { document.getElementById('message_' + overstockShedID).innerHTML = "Sorry, there was an error in the calculations. Make sure that you are putting a five digit zip code in the above box."; tooFar = 0; invalidZip = 1; } else { state = response; getAPrice(overstockShedID); } } } // end handleState