/* Autofusion Manufacturer Incentives Library API v0.1.02 by Josh Lizarraga */
/* Copyright 2009 Autofusion.com */

/* AF Library */

if(typeof(YAHOO.AUTOFUSION) == "undefined"){
	YAHOO.namespace("AUTOFUSION");
}
if(typeof(YAHOO.AUTOFUSION.items) == "undefined"){
	YAHOO.AUTOFUSION.items = new Object();
}

/* MILAPI */

function MILAPI(confid, extra){
	this.request = false;
	this.XML = false;
	this.asyncSuccess = function (pObj){
		if(pObj.responseText != ""){
			this.XML = pObj.responseXML;
		}
		if(typeof(pObj.argument["callback"]) != "undefined"){
			if(typeof(pObj.argument["args"]) != "undefined"){
				pObj.argument["callback"](pObj.argument["args"]);
			} else {
				pObj.argument["callback"]();
			}
		}
	};
	this.asyncFailure = function (pObj){
		//alert("Asynchronous connection failed.");
	};
	this.asyncJWS = function (pData, pCallback, pArgs){
		var oConfID = (confid) ? confid : "jws_api";
		var oParamString = "confid=" + oConfID;
		for(var i in pData){
			oParamString += "&" + i + "=" + pData[i];
		}
		for(var i in extra){
			oParamString += "&" + i + "=" + extra[i];
		}
		var oCallback = {
			success:  this.asyncSuccess,
			failure: this.asyncFailure,
			scope: this,
			argument: new Object()
		};
		if(pCallback){
			oCallback.argument["callback"] = pCallback;
		}
		if(pArgs){
			oCallback.argument["args"] = pArgs;
		}
		this.request = YAHOO.util.Connect.asyncRequest("POST", "/AF2/milapi/0.2/mil.php", oCallback, oParamString);
	};
	this.getValue = function (pTarget){
		if(pTarget.childNodes.length > 0){
			return pTarget.childNodes[0].nodeValue;
		} else {
			return '';	
		}
	};
	this.getAll = function (pCallback, pArgs){
		this.asyncJWS({}, pCallback, pArgs);
	};
	this.getMake = function (pMake, pCallback, pArgs){
		this.asyncJWS({ make: pMake }, pCallback, pArgs);
	};
	this.getModel = function (pMake, pModel, pCallback, pArgs){
		this.asyncJWS({ make: pMake, model: pModel }, pCallback, pArgs);
	};
	this.getYear = function (pMake, pModel, pYear, pCallback, pArgs){
		this.asyncJWS({ make: pMake, model: pModel, year: pYear }, pCallback, pArgs);
	};
};