/*extern KIC, Ajax, AjaxUpdater, $, debug, debugObj, debug_request_objects */
///////////////////////////////////////////////////////////////////////////
//    define Services object
///////////////////////////////////////////////////////////////////////////
if(typeof KIC.Services         === "undefined") { KIC.Services  = {}; /* new Object */ } 
if(typeof KIC.Services.Main    === "undefined") { KIC.Services.Main = {}; /* new Object */ }
if(typeof KIC.Services.General === "undefined") { KIC.Services.General = {}; /* new Object */ }

///////////////////////////////////////////////////////////////////////////
//    common functions
///////////////////////////////////////////////////////////////////////////
KIC.Services.isReady = function() {
	var status, statusText;
	var exceptionWindow;
	var responseHeaders;
	var responseText;
	if ( Ajax.isReady() )
	{
		status = Ajax.getRequestStatus();
		if (status === 200) {
			return true;
		} else {
			// otherwise, server returned an HTTP error
			// this would generally be 500 Internal Server Error, 
			// when an aspx/ashx page throws an uncaught exception
			statusText = Ajax.getRequestStatusText();
			responseHeaders = Ajax.getResponseHeaders();
			responseText = Ajax.getResponse();
			alert("An error occurred.");
			debug(status);
			debug(statusText);
			debug(responseHeaders);
			debug(responseText);

			// There are three ways this exception can be handled 
			// (besides the unhelpful "An error occurred" mesage)
			//   1) Write content to hidden div used for this purpose, and show the div
			//   2) Overwrite the entire page with the IIS error
			//   3) Open a new window and write the IIS error there
			// Option #1 won't work, since this function can be called by any page (login, main, etc).
			/*
			exceptionWindow = window.open('', 'exception', 'height=400,width=500,resizable=yes,scrollbars=yes,status=yes');
			exceptionWindow.document.write(responseText);
			exceptionWindow.document.close();
			exceptionWindow.focus();
			*/
			// Issues with option #3 above is that a popup blocker won't show the error by default, and
			// you still have to reload/refresh to re-try action on page (for instance, logging in).
			document.open();
			document.write(responseText);
			document.close();
		}
	}
	return false;
};

// for development only
KIC.Services.echoObject = function( object, handler ) {
	AjaxUpdater.updateWithPost( '/services/Echo.ashx', 'request='+encodeURIComponent(object.toJSONString()), handler );
};

///////////////////////////////////////////////////////////////////////////
//    specific service calls
///////////////////////////////////////////////////////////////////////////
KIC.Services.Main.showEvents = function( handler ) {
	AjaxUpdater.update( 'GET', '/content/Home.aspx', handler );
};

KIC.Services.Main.showEventsByCsrType = function( csrtype, handler ) {
	if (csrtype !== "") {
		AjaxUpdater.update( 'GET', '/content/Home.aspx?id='+encodeURIComponent(csrtype), handler );
	}
};

// deprecated, InfoHeader is loaded along with Selector in KIC.Services.Main.showNav
KIC.Services.Main.loadUserInfoHeader = function( handler ) {
	AjaxUpdater.update( 'GET', '/content/UserInfoHeader.aspx', handler );
};

KIC.Services.Main.showNav = function( handler ) {
	AjaxUpdater.update( 'GET', '/content/Selector.aspx', handler );
};

KIC.Services.General.GetAccessibleCompanies = function( handler ) {
	AjaxUpdater.update( 'GET', '/services/General/ListAccessibleCompanies.ashx', handler );
};

KIC.Services.General.GetBrands = function( companyId, handler ) {
	AjaxUpdater.update( 'GET', '/services/General/ListBrands.ashx?companyId='+encodeURIComponent(companyId), handler );
};

KIC.Services.General.GetAccessibleBrands = function( companyId, handler ) {
	AjaxUpdater.update( 'GET', '/services/General/ListAccessibleBrands.ashx?companyId='+encodeURIComponent(companyId), handler );
};

KIC.Services.General.GetFamilies = function( companyId, brandId, handler ) {
	AjaxUpdater.update( 'GET', '/services/General/ListFamilies.ashx?companyId='+encodeURIComponent(companyId)+'&brandId='+encodeURIComponent(brandId), handler );
};

KIC.Services.General.GetAccessibleFamilies = function( companyId, brandId, handler ) {
	AjaxUpdater.update( 'GET', '/services/General/ListAccessibleFamilies.ashx?companyId='+encodeURIComponent(companyId)+'&brandId='+encodeURIComponent(brandId), handler );
};

KIC.Services.General.GetProductNames = function( companyId, brandId, familyId, handler ) {
	AjaxUpdater.update( 'GET', '/services/General/ListProductNames.ashx?companyId='+encodeURIComponent(companyId)+'&brandId='+encodeURIComponent(brandId)+'&familyId='+encodeURIComponent(familyId), handler );
};

//
//	Company
//
KIC.Services.General.createCompany = function( request, handler ) {
	AjaxUpdater.updateWithPost( '/services/General/CreateCompany.ashx', 'request='+encodeURIComponent(request.toJSONString()), handler );
};

KIC.Services.General.updateCompany = function( request, handler ) {
	AjaxUpdater.updateWithPost( '/services/General/UpdateCompany.ashx', 'request='+encodeURIComponent(request.toJSONString()), handler );
};

KIC.Services.General.deleteCompany = function( id, handler ) {
	AjaxUpdater.update( 'GET', '/services/General/DeleteCompany.ashx?id='+id, handler );
};

//
//	Brand
//
KIC.Services.General.createBrand = function( request, handler ) {
	AjaxUpdater.updateWithPost( '/services/General/CreateBrand.ashx', 'request='+encodeURIComponent(request.toJSONString()), handler );
};

KIC.Services.General.updateBrand = function( request, handler ) {
	AjaxUpdater.updateWithPost( '/services/General/UpdateBrand.ashx', 'request='+encodeURIComponent(request.toJSONString()), handler );
};

KIC.Services.General.deleteBrand = function( id, handler ) {
	AjaxUpdater.update( 'GET', '/services/General/DeleteBrand.ashx?id='+id, handler );
};

//
//	Family
//
function CreateFamilyRequest( companyId, brandId, value ) {
	this.CompanyId = companyId;
	this.BrandId = brandId;
	this.Value = value;
}

KIC.Services.General.createFamily = function( request, handler ) {
	AjaxUpdater.updateWithPost( '/services/General/CreateFamily.ashx', 'request='+encodeURIComponent(request.toJSONString()), handler );
};

function UpdateFamilyRequest( id, brandId, value ) {
	this.Id = id;
	this.Value = value;
}

KIC.Services.General.updateFamily = function( request, handler ) {
	AjaxUpdater.updateWithPost( '/services/General/UpdateFamily.ashx', 'request='+encodeURIComponent(request.toJSONString()), handler );
};

KIC.Services.General.deleteFamily = function( id, handler ) {
	AjaxUpdater.update( 'GET', '/services/General/DeleteFamily.ashx?id='+id, handler );
};

//
//	Web Brand
//
KIC.Services.General.createWebBrand = function( request, handler ) {
	AjaxUpdater.updateWithPost( '/services/General/CreateWebBrand.ashx', 'request='+encodeURIComponent(request.toJSONString()), handler );
};

KIC.Services.General.updateWebBrand = function( request, handler ) {
	AjaxUpdater.updateWithPost( '/services/General/UpdateWebBrand.ashx', 'request='+encodeURIComponent(request.toJSONString()), handler );
};

KIC.Services.General.deleteWebBrand = function( id, handler ) {
	AjaxUpdater.update( 'GET', '/services/General/DeleteWebBrand.ashx?id='+id, handler );
};

//
// Web Promotion
//
KIC.Services.General.createWebPromotion = function( request, handler ) {
	AjaxUpdater.updateWithPost( '/services/General/CreateWebPromotion.ashx', 'request='+encodeURIComponent(request.toJSONString()), handler );
};

KIC.Services.General.updateWebPromotion = function( request, handler ) {
	AjaxUpdater.updateWithPost( '/services/General/UpdateWebPromotion.ashx', 'request='+encodeURIComponent(request.toJSONString()), handler );
};

KIC.Services.General.deleteWebPromotion = function( id, handler ) {
	AjaxUpdater.update( 'GET', '/services/General/DeleteWebPromotion.ashx?id='+id, handler );
};

//
//	CSR Type
//
KIC.Services.General.createCsrType = function( request, handler ) {
	AjaxUpdater.updateWithPost( '/services/General/CreateCsrType.ashx', 'request='+encodeURIComponent(request.toJSONString()), handler );
};

KIC.Services.General.updateCsrType = function( request, handler ) {
	AjaxUpdater.updateWithPost( '/services/General/UpdateCsrType.ashx', 'request='+encodeURIComponent(request.toJSONString()), handler );
};

KIC.Services.General.deleteCsrType = function( id, handler ) {
	AjaxUpdater.update( 'GET', '/services/General/DeleteCsrType.ashx?id='+id, handler );
};

//
//	View History
//
KIC.Services.General.clearViewHistory = function( table, handler ) {
	AjaxUpdater.update( 'GET', '/services/General/ClearViewHistory.ashx?table='+table, handler );
};

//
//	General purpose IdSetValue requests
//
function CreateIdSetValueTupleRequest( setId, value ) {
	this.SetId = setId;
	this.Value = value;
}

function UpdateIdSetValueTupleRequest( id, value ) {
	this.Id = id;
	this.Value = value;
}

//
//	Email
//
KIC.Services.General.loadEmail = function( manager, handler ) {
	AjaxUpdater.update( 'GET', '/content/Email.aspx?manager='+manager, handler );
};

function SendEmailRequest( recipients, text, area, id ) {
	this.Recipients = recipients;
	this.Text = text;
	this.Area = area;
	this.Id = id;
}

KIC.Services.General.sendEmail = function( request, handler ) {
	AjaxUpdater.updateWithPost( '/services/General/SendEmail.ashx', 'request='+encodeURIComponent(request.toJSONString()), handler );
};

// create alias
var Services = KIC.Services;

///////////////////////////////////////////////////////////////////////////
//    BaseServicesManager object
//		not really necessary
///////////////////////////////////////////////////////////////////////////
function BaseServicesManager() {}

BaseServicesManager.prototype.isReady = function() {
	// this is redundant
	debug("calling wrong isReady() function");
	return Services.isReady();
};

BaseServicesManager.prototype.log = function(request) {
	if (debug_request_objects) {
		debugObj(request);
	}
};

