
function _(strId) {
	return document.getElementById(strId);
}

window.JS_TYPE_BOOLEAN = "boolean";
window.JS_TYPE_FUNCTION = "function";
window.JS_TYPE_NUMBER = "number";
window.JS_TYPE_OBJECT = "object";
window.JS_TYPE_STRING = "string";
window.JS_TYPE_UNDEFINED = "undefined";

var ProStores = {

	ApiRequest : function(strRequestType) {
		this.m_reqType = strRequestType;
		this.m_objReq = {};
	},
	
	CustomerLoginRequest : function() {
		this.m_reqType = this.CUSTOMER_LOGIN;
		this.m_objReq = {misc : {}};
	},
	
	CustomerEchoRequest : function() {
		this.m_reqType = this.CUSTOMER_ECHO;
		this.m_objReq = {objects : [{name : "customer", properties : []}]};
	},
	
	CustomerAddRequest : function() {
		this.m_reqType = this.CUSTOMER_ADD;
		this.m_objReq = {objects : [{name : "customer", properties : []}], misc : {}};
	},
	
	CustomerUpdateRequest : function() {
		this.m_reqType = this.CUSTOMER_UPDATE;
		this.m_objReq = {objects : [{name : "customer", properties : []}], misc : {}};
	},
	
	InvoiceEchoRequest : function() {
		this.m_reqType = this.INVOICE_ECHO;
		this.m_objReq = {objects : [{name : "invoice", properties : []}]};
	},
	
	InvoiceUpdateRequest : function() {
		this.m_reqType = this.INVOICE_UPDATE;
		this.m_objReq = {objects : [{name : "invoice", properties : []}], misc : {}};
	},
	
	UpdateShipMethodRequest : function() {
		this.m_reqType = this.INVOICE_UPDATE;
		this.m_objReq = {objects : [{name : "invoice", properties : []}], misc : {}};
	},
	
	UpdatePaymentMethodRequest : function() {
		this.m_reqType = this.INVOICE_SET_PAYMEMT;
		this.m_objReq = {};
	},
	
	ApplyPromotionRequest : function(strPromoCode) {
		this.m_reqType = this.INVOICE_APPLY_PROMOTION;
		this.m_objReq = {"promocode" : strPromoCode};
	},
	
	UpdateInsuranceOptionRequest : function() {
		this.m_reqType = this.INVOICE_UPDATE;
		this.m_objReq = {objects : [{name : "invoice", properties : []}], misc : {}};
	},
	
	ConfirmInvoiceRequest : function() {
		this.m_reqType = this.INVOICE_CONFIRM;
		this.m_objReq = {};
	}
};


ProStores.ApiRequest.prototype = {
	
	CUSTOMER_ADD : "CUSTOMER_ADD",
	CUSTOMER_LOGIN : "CUSTOMER_LOGON",
	CUSTOMER_ECHO : "CUSTOMER_QUERY",
	CUSTOMER_UPDATE : "CUSTOMER_UPDATE",
	INVOICE_ECHO : "INVOICE_QUERY",
	INVOICE_UPDATE : "INVOICE_UPDATE",
	INVOICE_SET_PAYMEMT : "INVOICE_SET_PAYMENT",
	INVOICE_APPLY_PROMOTION : "INVOICE_APPLY_PROMOTION",
	INVOICE_CONFIRM : "INVOICE_CONFIRM",
	
	addDirective : function(strName, objValue) {
		if (strName)
			this.m_objReq.misc[strName] = objValue;
	},
	
	addEchoField : function(strName, objParam) {
		var objProperty = {};
		objProperty.name = strName;
		if (objParam) {
			if (typeof objParam == JS_TYPE_STRING)
				objProperty.format = objParam;
			else
				objProperty.properties = objParam;
		}
		this.m_objReq.objects[0].properties.push(objProperty);
	},

	addProperty : function(strName, value) {
		var objProperty = {};
		objProperty.name = strName;
		objProperty.value = value;
		this.m_objReq.objects[0].properties.push(objProperty);
	},
	
	send : function() {
		var req = new AjaxRequest();
		req.setAsyncRequest(false);
		req.setMethod("POST");
		req.setUrl(AjaxRequest.URL);
		
		var objReturn;
		req.addResponseHandler(
			function(strResponse) {
				Checkout.LogResponse(strResponse);
				if (strResponse)
					objReturn = eval("(" + strResponse + ")");
			}
		);
		
		if (this.m_reqType)
			req.addHeader("request-type", this.m_reqType);

		// Retry request if needed		
		for (i = 0; i < 3 && objReturn == null; i++) {
			Checkout.LogRequest(this.m_objReq.toJSONString());
			req.send(this.m_objReq.toJSONString().replace(/[<]/g, '&lt;'));
		}
		
		if (objReturn == null)
			objReturn = {"outcome":"ERROR","errors":["Error communicating with the server"]};

		return objReturn;
	}
};

ProStores.UpdatePaymentMethodRequest.prototype = ProStores.ApiRequest.prototype;
ProStores.CustomerAddRequest.prototype = ProStores.ApiRequest.prototype;
ProStores.CustomerLoginRequest.prototype = ProStores.ApiRequest.prototype;
ProStores.CustomerEchoRequest.prototype = ProStores.ApiRequest.prototype;
ProStores.CustomerUpdateRequest.prototype = ProStores.ApiRequest.prototype;
ProStores.InvoiceEchoRequest.prototype = ProStores.ApiRequest.prototype;
ProStores.InvoiceUpdateRequest.prototype = ProStores.ApiRequest.prototype;
ProStores.UpdateShipMethodRequest.prototype = ProStores.ApiRequest.prototype;
ProStores.ApplyPromotionRequest.prototype = ProStores.ApiRequest.prototype;
ProStores.UpdateInsuranceOptionRequest.prototype = ProStores.ApiRequest.prototype;
ProStores.ConfirmInvoiceRequest.prototype = ProStores.ApiRequest.prototype;


ProStores.CustomerLoginRequest.prototype.setEmail = function(strEmail) {
	this.m_objReq.email = strEmail;
}

ProStores.CustomerLoginRequest.prototype.setPassword = function(strPassword) {
	this.m_objReq.password = strPassword;
}




		

