//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 = crmGetCookie("ZipCode"); //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 = taggingArgs["model"];
			bodyname = taggingArgs["body"];
			msrpname = taggingArgs["msrp"];
			trimname = taggingArgs["trim"];
			
			//var id = document.getElementById("vehicle");
			//modelname = id.options[id.selectedIndex].text;

		// Make Name of the CAR
			makename = "Nissan"; //document.getElementById("drpTrim").value;

		// Model Year of the CAR
			//var id = document.getElementById("vehicle");
			modelyear = taggingArgs["MY"];

//var taggingArgs={'trim':'1.6 BASE','MDL':'VER','MY':'2009','body':'VRS','msrp':'$9,990','model':'Versa'};
//http://web1.intellipriceauto.com/staging/etradeinmatrix/identify.aspx?lcdealer=970101&modelyear=2009&modelname=Altima&makename=2.5%20S&zipCode=90802&reqtype=P
			crmEvent3( {'model':modelname,'body':bodyname,'trim':trimname,'msrp':msrpname});
			remoteUrl = 'http://www.intelliprice.com/intellipricedealer/start.htm?dealerid=970101&modelyear='+modelyear+'&modelname='+modelname+'&makename='+makename+'&zipcode='+zipCodeValue+'&next=config_estimator.trade-in';

// 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 ) );
}
