var destinationzipcolor = '#000';

function phoneKey( id ) {
  // only allows digits entry
  var x = document.getElementById(id).value;
  var last = x.charAt(x.length-1);
  if( isNaN( last ) ) 
    document.getElementById(id).value = x.substr(0,x.length-1);
  else
    document.getElementById(id).value = x;

  // transition to the next field when full
  x = document.getElementById(id).value;
  switch( id ) {
  case 'custarea':
    if( x.length == 3 ) 
      document.getElementById('custexch').focus();
    break;
  case 'custexch':
    if( x.length == 3 ) 
      document.getElementById('custphone').focus();
    break;
  case 'custphone':
    if( x.length == 4 ) 
      document.getElementById('email').focus();
    break;
  }
}

//---------- Form validation function
function validateForm( formtype ) {
  // validate form fields
  var results = true;
  var alertText = "";

  // form selection
  if( formtype == "none" || ( !document.getElementById("moving").checked && !document.getElementById("movstor").checked && !document.getElementById("storage").checked) ) {
    alertText += "<li>Select Moving, Moving & Storage, or Storage</li>";
    results = false;
  }

  // zipcodes
  var zip = document.getElementById("initzip").value;
  if( isNaN(zip) || ( !isNaN(zip) && (zip.length != 5) ) || zip == '00000' ) {
    //alertText += "- Initial Delivery ZIP Code is missing, not a number, all zeroes, too short, or too long.<br />";
    alertText += "<li>Enter a valid Initial Delivery ZIP code</li>";
    results = false;
  }
  if( formtype == 'moving' || formtype == 'movstor'  ) {
    var zip = document.getElementById("destzip").value;
    if( isNaN(zip) || ( !isNaN(zip) && (zip.length != 5) ) || zip == '00000' ) {
      //alertText += "- Destination ZIP Code is missing, not a number, all zeroes, too short, or too long.<br />";
      alertText += "<li>Enter a valid Final Delivery ZIP code</li>";
      results = false;
    }
  }

  // how many and what size
  var twelves = document.getElementById("twelves").value;
  var sixteens = document.getElementById("sixteens").value;
  if( (twelves == 0) && (sixteens == 0) ) {
    //alertText += "- Select 1 or more 12' or 16' UNITS.<br />";
    alertText += "<li>Select number of UNITS Needed</li>";
    results = false;
  }

  // name
  if( document.getElementById("custname").value.search("Required") != -1 || document.getElementById("custname").value.length < 1 ) {
    //alertText += "- Enter your name.<br />";
    alertText += "<li>Enter your name</li>";
    results = false;
  }

  // phone or email
  var phone = document.getElementById("custarea").value + document.getElementById("custexch").value + document.getElementById("custphone").value;
  var email = document.getElementById("email").value;
  var isphone = validatePhone( phone );
  var isemail = validateEmail( email );
  if( !isphone ) {
    alertText += "<li>Enter a valid phone number</li>";
    //alertText += "- Phone number must include areacode and number.<br />";
    results = false;
  }
 
  if( !isemail ) {
    alertText += "<li>Enter a valid email address</li>";
    //alertText += "- Email address is not accepted, please re-enter.<br />";
    results = false;
  }
  
  // put error results on page
  if( !results ) {
    document.getElementById("errortext").innerHTML = "<ul>"+alertText+"</ul>";
    //window.scrollTo(0,0);
    document.getElementById("formerrorbox").style.visibility = "visible";
    //document.getElementById("formerror").innerHTML = alertText;
    //} else {
    //document.getElementById("formerror").innerHTML = "";
  }
  return( results );
}

function closeErrorPopup() {
  document.getElementById("formerrorbox").style.visibility = "hidden";
  document.getElementById("quotebutton").src = "http://"+location.hostname+"/quote-units/pics/get-a-quote-button.png";
  document.getElementById("quotebutton").style.visibility = "visible";
}
function validatePhone(phonestr) {
  var results = true;
  if( phonestr.search("Area") == -1 && phonestr.search("Phone") == -1 && phonestr.search("Required") == -1 ) {
    if( phonestr.length == 0 ) {
      results = false;
    } else {
      if( (phonestr.length > 0) && (phonestr.length < 10) )
	results = false;
    }
  } else {
    results = false;
  }
  return( results );
}

function validateEmail(emailstr) {
  var results = true;
  if( emailstr.search("Required") == -1 ) {
    if( emailstr.length == 0 ) { // || (phone.match("areacode+phone") != null && email.length == 0) ) {
      results = false;
    } else {
      if( emailstr.length > 0 ) {
	if( emailstr.indexOf("@") == "-1" || emailstr.indexOf(".") == "-1" || emailstr.indexOf("@") == 0 )
	  results = false;
      }
    }
  } else {
    results = false;
  }
  return( results );
}

//---------- quote button functions
function buttonpressed() {
  document.getElementById("quotebutton").src = "http://"+location.hostname+"/quote-units/pics/get-a-quote-button-down.png";
}
function buttonreleased() {
  //document.getElementById("quotebutton").src = "http://"+location.hostname+"/quote-units/pics/get-a-quote-button-disabled.png";
  //document.getElementById("quotebutton").style.visibility = "hidden";
  submitQuote();
}
function submitQuote() {
  if( validateForm(document.getElementById("formtype").value) ) {
    document.getElementById("quickform").action = "http://unitsstorage.com/quote-units/index.html?q=startquote";
    //document.getElementById("quickform").action = "quote-units/index2-iframeonly.html?q=startquote";
    document.getElementById("quickform").submit();
  }
}

function selectForm( which ) {
  // disable unused fields, enable required fields, set formtype
  switch( which ) {
  case 'moving':
    //alert("moving");
    document.getElementById("destzip").disabled = false;
    if( destinationzipcolor != '#000' ) document.getElementById("destzip").style.background = destinationzipcolor;
    document.getElementById("storagelocation").disabled = true;
    document.getElementById("formtype").value = "moving";
    break;
  case 'movestore':
    //alert("movestore");
    document.getElementById("destzip").disabled = false;
    if( destinationzipcolor != '#000' ) document.getElementById("destzip").style.background = destinationzipcolor;
    document.getElementById("storagelocation").disabled = false;
    document.getElementById("formtype").value = "movestore";
    break;
  case 'storage':
    //alert("storage");
    var dest = document.getElementById("destzip");
    dest.disabled = true;
    destinationzipcolor = dest.style.background;
    dest.style.background = "#dbdacf";
    document.getElementById("storagelocation").disabled = false;
    document.getElementById("formtype").value = "storage";
    break;
  }
}


function clearfield( field, initialvalue ) {
  if( field.value.search(initialvalue) != -1 )
    field.value = "";
}

function infopopup( that, happened, which ) {
  var popupID = document.getElementById("infobox");
  var popAnswer = document.getElementById("infotext");
  var v = "visible";
  var top = 0; //happened.clientY; //that.offsetTop;
  var left = 0; //that.offsetLeft+15;

  var I;
  switch( which ) {
  case 'roof':
    document.getElementById('infotext').innerHTML = "Integrated translucent roof allows light in and keeps rain out.";
    top = 195;
    left = 350;
    break;
  case 'strap':
    document.getElementById('infotext').innerHTML = "Strapping system provides reliable way to secure items.";
    top = 330;
    left = 380;
    break;
  case 'container':
    document.getElementById('infotext').innerHTML = "Constructed of steel and fiberglass to keep items safe and dry.";
    top = 230;
    left = 350;
    break;
  case 'out':
    v = "hidden";
    popupID.style.visibility = v;
    return;
    break;
  }

  popupID.style.top = top;
  popupID.style.left = left;
  popupID.style.visibility = v;
}

function questionpop( that, happened, which ) {
  //var pointerID = document.getElementById("questionpointer");
  var popupID = document.getElementById("questionbox");
  var popAnswer = document.getElementById("answertext");
  var v = "visible";
  var top = 0; //happened.clientY; //that.offsetTop;
  var left = 0; //that.offsetLeft+15;
  var O;

  switch( which ) {
  case 'moving':
    popAnswer.innerHTML = 'Select "Moving" if you are moving your loaded UNIT directly to a new location.';
    O = document.getElementById("movequestion");
    break;
  case 'movstor': 
    popAnswer.innerHTML = 'Select "Moving & Storage" if you plan to store your UNIT at our storage facility and then have us either move it to a new location or redelivery to initial location.';
    O = document.getElementById("movstorquestion");
    break;
  case 'storage': 
    popAnswer.innerHTML = 'Select "Storage" if you plan to keep your UNIT at your location or store it at our secure storage facility.';
    O = document.getElementById("storagequestion");
    break;
  case 'out':
    v = "hidden";
    popupID.style.visibility = v;
    return;
    break;
  }

  /*-----
  if( (top = eventY(happened)) != 0 ) {
    left = "200px";
  } else {

    var browser = navigator.appName;
    if( browser.search('Microsoft') != -1 ) {
      // exploder
      if( O.offsetParent ) {
	do {
	  if( left == 0 ) left += O.offsetLeft;
	  if( top == 0 ) top += O.offsetTop;
	} while( O = O.offsetParent );
	left += 15;
	top += 3;
      } else {
	alert("no offsetParent");
      }
    } else {
      left = O.offsetLeft+18;
      top = O.offsetTop+2;
    }
  }

  popupID.style.top = top+25;
----*/

  popupID.style.visibility = v;
}

function eventY( evt ) {
  if (evt.pageY) return evt.pageY;
  else if (evt.clientY)
    return evt.clientY + (document.documentElement.scrollTop ?
			  document.documentElement.scrollTop :
			  document.body.scrollTop);
  else return 0;

}

function closeXover( id ) {
  //document.getElementById("closeX").src = "http://"+location.hostname+"/quote-units/pics/How-Many-Box-Xpush.png";
  id.src = "http://"+location.hostname+"/quote-units/pics/How-Many-Box-Xpush.png";
}
function closeXout( id ) {
  //document.getElementById("closeX").src = "http://"+location.hostname+"/quote-units/pics/How-Many-Box-X.png";
  id.src = "http://"+location.hostname+"/quote-units/pics/How-Many-Box-X.png";
}

function calculateButtonPress(which) {
  switch( which ) {
  case 'down':
    document.getElementById("calculatebutton").src = "http://"+location.hostname+"/quote-units/pics/How-Many-Box-Button-Down.png";
    break;
  case 'up':
    document.getElementById("calculatebutton").src = "http://"+location.hostname+"/quote-units/pics/How-Many-Box-Button.png";
    numberofunits();
  }
}
function unitcalculate( which ) {
  //clearQuote();

  switch( which ) {
  case "on":
    document.getElementById("howmanybox").style.visibility = "visible";
    break;
  case "off":
    document.getElementById("howmanybox").style.visibility = "hidden";
    break;
  }
}

function numberofunits() {
  var newHTML = new String();
  var largeunits =  new Number (0);
  var smallunits = new Number(0);
  var size = document.getElementById("squarefeet").value;
  var garage = document.getElementById("garage").checked;
  var basement = document.getElementById("basement").checked;
  var result = document.getElementById("calculatedresult");

  if( size < 1500 ) {
    if( basement ) {
      smallunits = 0;
      largeunits = 1;
    } else {
      smallunits = 1;
      largeunits = 0;
    }
  } else if( size < 1800 ) {
    if( basement ) {
      smallunits = 1;
      largeunits = 1;
    } else {
      smallunits = 0;
      largeunits = 1;
    }
  } else if( size < 2000 ) {
    if( garage && !basement ) {
      smallunits = 1;
      largeunits = 1;
    } else if( basement ) {
      smallunits = 1;
      largeunits = 1;
    } else {
      smallunits = 0;
      largeunits = 1;
    }
  } else if( size < 3000 ) {
    if( garage && !basement ) {
      smallunits = 0;
      largeunits = 2;
    } else if( basement ) {
      smallunits = 1;
      largeunits = 2;
    } else {
      smallunits = 0;
      largeunits = 2;
    }
  } else if( size < 5000 ) {
    if( garage && !basement ) {
      smallunits = 0;
      largeunits = 3;
    } else if( basement ) {
      smallunits = 1;
      largeunits = 3;
    } else {
      smallunits = 0;
      largeunits = 3;
    }
  } else if( size < 6000 ) {
    if( garage && !basement ) {
      smallunits = 0;
      largeunits = 4;
    } else if( basement ) {
      smallunits = 0;
      largeunits = 4;
    } else {
      smallunits = 0;
      largeunits = 4;
    }
  } else {
    newHTML = "<center>Please call for estimate</center>";
    smallunits = 0;
    largeunits = 0;
  }
  
  if( (smallunits + largeunits) > 0 ) {
    newHTML = '<p class="smallheading">You will need: </p><p>';
    if( largeunits > 0 ) newHTML += largeunits + " - 16FT UNITS<br />";
    if( smallunits > 0 ) newHTML += smallunits + " - 12FT UNITS";
    newHTML += "</p>";
  }
  result.innerHTML = newHTML;


  document.getElementById("twelves").selectedIndex = smallunits;
  document.getElementById("sixteens").selectedIndex = largeunits;
}

function phoneKey( id ) {
  // only allows digits entry
  var x = document.getElementById(id).value;
  var last = x.charAt(x.length-1);
  if( isNaN( last ) ) 
    document.getElementById(id).value = x.substr(0,x.length-1);
  else
    document.getElementById(id).value = x;

  // transition to the next field when full
  x = document.getElementById(id).value;
  switch( id ) {
  case 'custarea':
    if( x.length == 3 ) 
      document.getElementById('custexch').focus();
    break;
  case 'custexch':
    if( x.length == 3 ) 
      document.getElementById('custphone').focus();
    break;
  case 'custphone':
    if( x.length == 4 ) 
      document.getElementById('email').focus();
    break;
  }
}

var saveZip = new String();
var xmlHttp;

function findalocation() {
  if( form_validates("locationzip") ) {
    getLandingPage("zipcode="+document.getElementById("locationzip").value+"&form=0");
  }
}

function form_validates(zipfrom) {
  var results = true;
  var alertText = "";

  // need at least a zipcodefrom to submit
  if( document.getElementById(zipfrom) ) {
    var zip = document.getElementById(zipfrom).value;
    if( isNaN(zip) || ( !isNaN(zip) && (zip.length < 5) ) ) {
      alertText = "- Zipcode From is not entered, not a number, or too short.\n";
      results = false;
    }
  } else {
      alertText = "- Zipcode From is not entered, not a number, or too short.\n";
      results = false;
  }
  if( !results ) {
    alert("Request incomplete.\n"+alertText);
  }
  saveZip = zip;
  return(results);
}

function getLandingPage(str) { 
  xmlHttp = GetXmlHttpObject();
  if (xmlHttp==null) {
    alert ("Browser does not support HTTP Request");
    return;
  }
  var url = "http://"+location.hostname+"/quote-units/code/landing-page-lookup.php";
  url = url + "?q=getLandingPage&" + str;
  url = url + "&sid=" + Math.random();
  //alert("sendThis: "+url);
  xmlHttp.onreadystatechange = checkResult;
  xmlHttp.open("GET",url,true);
  //xmlHttp.open("GET",url,false); //waits for response before continuing
  xmlHttp.send(null);
  //alert("Your message is being processed...");
}

function checkResult() { 
  var landingpage = "quote-units/index.html";
  //var returnstatus = xmlHttp.status;
  if ( xmlHttp.readyState == 4 || xmlHttp.readyState == "complete" ) { 
    //alert(xmlHttp.responseText);
    if( xmlHttp.responseText.search("error") == -1 ) {
      landingpage = xmlHttp.responseText;
    }

    if( landingpage.length == 0 ) { landingpage = "quote-units/index.html?q="+saveZip; }
    document.getElementById("quickform").action = landingpage;
    document.getElementById("quickform").submit();
  }
}

function GetXmlHttpObject() {
  var xmlHttp = null;
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
  }
  catch (e) {
    //Internet Explorer
    try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  return xmlHttp;
}  


