//KBB URL for Nissan: http://kbb2000.kbb.com/kb/ki.dll/ke.kb.sp?nna&&&yrmkmdtr;cm
//KBB callback: http://www.nissanusa.com/js/payment_estimator/kbb.html?YEAR=2005&MAKE=Audi&MODEL=A8&TRIM=Sedan+4D&CONDITION=Good&MILEAGE=50000&TOTALPRICE=34420&passback=Calculate+with+Payment+Estimates

var wKbb;

// getRemoteData()
// opens KBB window, calls getTradein()
// Example: getRemoteData( 'myCallbackFunctionName' );
function getRemoteData( callBackFunc ) { //added callback function hander

//Changes by chandan lakkoju

		//	var remoteUrl = 'http://www.kbb.com/kb/ki.dll/ke.kb.sp?nna&&&yrmkmdtr;cm';
		//	var remoteUrl = 'http://web1.intellipriceauto.com/staging/etradeinmatrix/identify.aspx?lcdealer=972001&modelyear=XXXX&modelname=XXXXX&makename=XXXX&reqtype=P';
		//	var remoteUrl = 'http://web1.intellipriceauto.com/staging/etradeinmatrix/identify.aspx?lcdealer';


		//We need to send the purchase/lease flag and zip code to the intelliprice popup.

			var remoteUrl;
			var zipCodeValue;
			var reqtypeStr = String(callBackFunc);
			var reqtype;

			var modelname,makename,modelyear;

		// Getting the ZIP CODE
				zipCodeValue = document.getElementById("txtZipcode").value;

		// Checking reqType and setting the remoteURL

			if(reqtypeStr == "updateLoanTradeIn"){
				reqtype="P";
			}
			else if(reqtypeStr == "updateLeaseTradeIn"){
				reqtype="L";
			}

		// Model Name of the CAR
			//modelname = document.getElementById("vehicle").name;
			var id = document.getElementById("vehicle");
			modelname = id.options[id.selectedIndex].text + "+" + document.getElementById("drpTrim").value;


		// Make Name of the CAR
			makename = "Nissan";


		// Model Year of the CAR
			var id = document.getElementById("vehicle");
			modelyear = id.options[id.selectedIndex].value;


			remoteUrl = 'http://www.intelliprice.com/intellipricedealer/start.htm?dealerid=970101&next=estimator.trade-in&modelyear='+modelyear+'&modelname='+modelname+'&makename='+makename+'&zipcode='+zipCodeValue+'&reqtype='+reqtype+'';


// End here

	var remoteParam = 'TOTALPRICE';
	var uniqueLocalStr = 'nissanusa';
	wKbb = window.open(remoteUrl,'intelliprice','width=600,height=800,scrollbars,resizable');
	wKbb.focus();
	getTradein( wKbb, remoteParam, uniqueLocalStr, callBackFunc );
}

// getTradein()
// Recursive function, waits while KBB operations occur.
// When KBB site calls back to a nissanusa.com page, gets the value of "remoteParam" and closes the window
function getTradein(win,remoteParam,uniqueLocalStr,callBackFunc) {

	try {
		var isNissan = (win.location.href.indexOf(uniqueLocalStr)!=-1);
	} catch (e) {}

	var sTradein;
	var sYear;
	var sMake;
	var sModel;

	if (isNissan) {

		sTradein = getQueryParam(win.location.search.substring(1),'TOTALPRICE');
		sYear = getQueryParam(win.location.search.substring(1),'YEAR');
		sMake = getQueryParam(win.location.search.substring(1),'MAKE');
		sModel = getQueryParam(win.location.search.substring(1),'MODEL');
	}

	if (sTradein) {

		eval(callBackFunc+'(' + sTradein + ','+sYear+',\'' + sMake + '\',\'' + sModel + '\');');
		win.close();
	} else {
		setTimeout(function () { getTradein(wKbb,remoteParam,uniqueLocalStr,callBackFunc); }, 1000);
	}
}

// getQueryParam()
// extracts param value from query string
// (different from global.js getQueryParam(), which assumes that the query string comes from local.href)
function getQueryParam(qString,paramName) {
    if (qString.indexOf(paramName) == -1) return '';
    var pValueStart = qString.indexOf(paramName) + paramName.length + 1;
    var pValueEnd = qString.indexOf('&', pValueStart);
    if ( pValueEnd==-1 ) pValueEnd = qString.length;
    return unescape( qString.substring( pValueStart,pValueEnd ) );
}
