var lastOfferTitle = "";
var spoHeaderArray = new Array();

function specialOffer(id, shortDesc, longDesc, legalDesc, savings, endDate, image, type, headerPath, title) {
	this.id = id;
	this.shortDesc = shortDesc;
	this.longDesc = longDesc;
	this.savings = savings;
	this.legal = legalDesc;
	this.endDate = endDate;
	this.title = title;
	this.type = type;
	this.image = image;
	this.headerPath = headerPath;
}

function displayFirstOffer() {
	var query = location.search.substring(1);

	//query could be modelId=100018&loc=en-us or loc=en-us for example

	var qSplit = query.split("&");
	if (query != "" && typeof(qSplit[0]) != "undefined") {
		//this is sort of a hack, fix it better later
		if(qSplit[0].split("=")[0].substr(0, 3) == 'loc') {
			displayOffer('all');
		} else if (typeof(qSplit[1]) != "undefined" && qSplit[1].substr(0, 8) == "showForm") {
		    //I don't think this condition ever happens
			showForm(19);
			displayOffer(qSplit[0].split("=")[1], true);
		} else {
			displayOffer(qSplit[0].split("=")[1]);
		}

	} else {
		displayOffer('all');
	}
}

//move this somewhere better
function remove(s, t) {

  /*
  **  Remove all occurrences of a token in a string
  **    s  string to be processed
  **    t  token to be removed
  **  returns new string
  */
  i = s.indexOf(t);
  r = "";
  if (i == -1) return s;
  r += s.substring(0,i) + remove(s.substring(i + t.length), t);
  return r;

 }


function displayOffer(listId, dontHide) {

	listId = remove(listId, '&loc=en-us');
	listId = remove(listId, '&loc');

	offerDiv = getItem("offerDisplay");
	offerString = "";
	rfqForm = getItem('spoRfqForm');
	if (!dontHide) {
		hideForm();
		rfqForm.style.visibility = 'hidden';
		getItem('offerDisplay').style.visibility = 'visible';
	}
	containerDiv = rfqForm.parentNode.parentNode;
	containerDiv.style.height = "";
	var sortingArray = new Array();

	// there is always a listId, but it may be empty
	//load all the offers for the model id (could be model id 'all') into sortingArray
	for (offer in specialOffers["model" + listId + "key"]) {
		sortingArray.push(offer);
	}

	// determine the header, either a global, or model specific
	if (listId == "all" || listId== 20405) {
		getItem("spoHeaderImg").src = allOfferHeaderSrc;
	} else {
		try {
			getItem("spoHeaderImg").src = specialOffers["model" + listId + "key"][sortingArray[0]].headerPath;
		} catch (e) {}
	}

	for (var i = 0; i < sortingArray.length; i++) {

		curOffer = specialOffers["model" + listId + "key"][sortingArray[i]];

		// new offer
		if (curOffer.title != lastOfferTitle) {
			//Close previous spoItem
			if (offerString != "") { offerString += "</div>"; }
			offerString += "<div class='spoItem'>";
			// don't show title for hecka special offers
			if (curOffer.type == 19) offerString += "	<div class='spoHeader'>"+curOffer.title+"</div>";
			offerString += "	<div class='spoImage'>"+curOffer.image+"</div>";
			offerString += "	<div><div class='spoCopy'><b>"+curOffer.shortDesc+"</b><br/>"+curOffer.longDesc+"</div>";
			offerString += "	<div class='spoCopy'>"+curOffer.legal+"</div>";
			if(curOffer.type != 99){
				offerString += "	<div class='spoCopy'><a href='javascript:showForm(" + curOffer.type + ")'>"+spoLinkText+"</a></div>";
			}
		} else { //More offers for the same combination of vehicles
				offerString += "	<div style='padding-top: 8px;'><div class='spoCopy'><b>"+curOffer.shortDesc+"</b><br/>"+curOffer.longDesc+"</div>";
				offerString += "	<div class='spoCopy'>"+curOffer.legal+"</div>";
				if(curOffer.type != 99){
					offerString += "	<div class='spoCopy'><a href='javascript:showForm(" + curOffer.type + ")'>"+spoLinkText+"</a></div>";
				}
		}

		//Close last spoItem
		offerString += "</div>";
		offerDiv.innerHTML = offerString;
		lastOfferTitle = curOffer.title;
	}

	if (offerDiv.innerHTML == "") {
		offerDiv.innerHTML = noOffersCopy;
		showFormNoOffers(0);
	}

  if (listId == "all") lastOfferTitle = "";
}

var showHideElements = new Array();
showHideElements.push("offerSel");
showHideElements.push("phoneType");
showHideElements.push("state");
showHideElements.push("timeframe");
var locNavOpen = false;

function showHideFields() {

	var offerFormDiv = getItem('spoRfqForm');

	var elementCount = (offerFormDiv.style.visibility != "visible")?1:showHideElements.length;
	var startCount = 0;
	if(locNavOpen) startCount = 1;

	for(var i = startCount; i < elementCount; i++) {
		var elemRef = getItem(showHideElements[i]);
		if(elemRef) {
			if(elemRef.style.visibility != "hidden") {
				elemRef.style.visibility = "hidden";
			}
			else {
				elemRef.style.visibility = "visible";
			}
		}
	}
}

function recordZip() {
	sessionZipCode = getItem('spoZip').value;
	zipArgs = "zipCode=" + sessionZipCode;
	//Send zip to servelet for validation
	makeRequest('/MMNA/validateZip.do', zipArgs, 'eval(setZip(http_request[i].responseText));');
}

function setZip(isValid) {
	if(isValid) {
		getItem('spoZipText').innerHTML = zipValidMsg;
	} else {
		//Display error messaging
		getItem('spoZipText').innerHTML = zipInvalidMsg;
	}
}

function checkZip() {
	if (sessionZipCode != "") {
		setZip(true);
	}
}

function showForm(offerType) {
	if (failedFields.length > 0) {
		for (i = 0; i < failedFields.length; i++) {
			try {
				failedDiv = getItem(failedFields[i] + "div");
				failedDiv.className = "commonOfferField";
			} catch (e) {}
		}
	}
	rfqDiv = getItem('spoRfqForm');
	rfqDiv.style.top = getItem('offerDisplay').offsetTop;
	rfqDiv.style.visibility = 'visible';
	formDiv = getItem('form');
	formDiv.style.visibility = 'visible';
	formDiv.style.position = 'relative';
	getItem('offerDisplay').style.visibility = 'hidden';
	getItem(offerTypeField).value = offerType;
	containerDiv = rfqDiv.parentNode.parentNode;
	if (navigator.userAgent.indexOf("Safari/4") == -1) {
		containerDiv.style.height = (rfqDiv.offsetHeight + rfqDiv.offsetTop - containerDiv.offsetTop) + "px";
	} else {
		containerDiv.style.height = (formDiv.offsetHeight + rfqDiv.offsetTop - containerDiv.offsetTop + 20) + "px";
	}

	for(var i = 1; i < showHideElements.length; i++) {
		var elemRef = getItem(showHideElements[i]);
		if(elemRef) {
			elemRef.style.visibility = "visible";
		}
	}
	window.scrollTo(0, 0);

	//write the doubleclick tracker for special offer form start
	var axel = Math.random()+"";
	var a = axel * 10000000000000;
	getItem('specialOfferFormView').innerHTML = '<IFRAME SRC="http://fls.doubleclick.net/activityi;src=1033846;type=elead741;cat=speci629;ord='+ a + '?" WIDTH=1 HEIGHT=1 FRAMEBORDER=0></IFRAME>';

}

function showFormNoOffers(offerType) {
	if (failedFields.length > 0) {
		for (i = 0; i < failedFields.length; i++) {
			try {
				failedDiv = getItem(failedFields[i] + "div");
				failedDiv.className = "commonOfferField";
			} catch (e) {}
		}
	}
	rfqDiv = getItem('spoRfqForm');
	rfqDiv.style.top = (parseInt(getItem('offerDisplay').offsetTop) + 20) + "px";
	rfqDiv.style.visibility = 'visible';
	formDiv = getItem('form');
	formDiv.style.visibility = 'visible';
	formDiv.style.position = 'relative';
	//getItem('offerDisplay').style.visibility = 'hidden';
	getItem(offerTypeField).value = offerType;
	containerDiv = rfqDiv.parentNode.parentNode;
	if (navigator.userAgent.indexOf("Safari/4") == -1) {
		containerDiv.style.height = (rfqDiv.offsetHeight + rfqDiv.offsetTop - containerDiv.offsetTop) + "px";
	} else {
		containerDiv.style.height = (formDiv.offsetHeight + rfqDiv.offsetTop - containerDiv.offsetTop + 20) + "px";
	}
	for(var i = 1; i < showHideElements.length; i++) {
		var elemRef = getItem(showHideElements[i]);
		if(elemRef) {
			elemRef.style.visibility = "visible";
		}
	}
	window.scrollTo(0, 0);

}

function hideForm() {
	errorDiv = getItem('errorField');
	errorDiv.style.position = 'absolute';
	errorDiv.style.visibility = 'hidden';
	thanksDiv = getItem('commonOfferThankYou');
	thanksDiv.style.position = 'absolute';
	thanksDiv.style.visibility = 'hidden';
	formDiv = getItem('form');
	formDiv.style.visibility = 'hidden';
	formDiv.style.position = 'absolute';
	getItem('spoRfqForm').style.visibility = 'hidden';
	getItem('offerDisplay').style.visibility = 'visible';
	containerDiv = getItem('offerDisplay').parentNode.parentNode;
	containerDiv.style.height = (getItem('offerDisplay').offsetHeight + getItem('offerDisplay').offsetTop - containerDiv.offsetTop) + "px";
	for(var i = 1; i < showHideElements.length; i++) {
		var elemRef = getItem(showHideElements[i]);
		if(elemRef) {
			elemRef.style.visibility = "hidden";
		}
	}

}