/*
 * Copyright (c) 2006 AWS Convergence Technologies, Inc. 
 * All rights reserved.
 * Visit http://www.aws.com
 *
 *  WBDataRequest.js
 *
 * This scripts contains the WBDataRequest resuable class. A DataRequest object
 * takes care of loading data from an XMLHttpRequest object.
 */

// Object Definition
function WBDataRequest(wBUrlObject,loadCallback,wBPreferencesObject)
{
	this.url = wBUrlObject; // Holds the URL for the data request
	this.request; // Holds the XMLHttpRequest Object
	this.loadCallback = loadCallback;
	this.preferences = wBPreferencesObject;
}

// Do not call this directly, only from an initialized DataRequest object.
// Used to reload the information from the local URL.
WBDataRequest.prototype.load = function (str)
{
	// terminate any open active requests from this object.
	if (this.request != null)
	{
		this.request.abort();
		this.request = null;
	}

	// Create a new request and fetch the data
	this.request=new ActiveXObject("Microsoft.XMLHTTP")
    this.request.onreadystatechange = this.loadCallback;

	var baseUrl = this.replaceParams(this.url.GetUrl());
	
	if(str)
		baseUrl += str;

	this.request.open("GET",baseUrl,true);
	this.request.send(null);
}

// Object Definition
function WBDataRequestXML(wBUrlObject,loadCallback,wBPreferencesObject)
{
	this.url = wBUrlObject; // Holds the URL for the data request
	this.request; // Holds the XMLHttpRequest Object
	this.loadCallback = loadCallback;
	this.preferences = wBPreferencesObject;
}

// Do not call this directly, only from an initialized DataRequest object.
// Used to reload the information from the local URL.
WBDataRequestXML.prototype.load = function (str)
{
	// terminate any open active requests from this object.
	if (this.request != null)
	{
		this.request.abort();
	}

	// Create a new request and fetch the data
	this.request=new ActiveXObject("Microsoft.XMLDOM")
    this.request.ondataavailable = this.loadCallback;

	var baseUrl = this.replaceParams(this.url.GetUrl());
	
	if(str)
		baseUrl += str;
		
	this.request.load(baseUrl);
}

WBDataRequest.prototype.replaceParams = function (url)
{
    // Build the URL, replacing everything possible parameter with values set in the preferences object
	url = url.replace("^^^UserId^^^",this.preferences.GetGuid());
	url = url.replace("^^^LinkName^^^",this.preferences.GetStringValue(this.preferences.GetCurrentLinkName()));
	url = url.replace("^^^AppVersion^^^",this.preferences.GetCurrentAppVersion());
	url = url.replace("^^^AppName^^^",this.preferences.GetCurrentAppName());
	url = url.replace("^^^ZCode^^^",this.preferences.GetCurrentZCode());
	url = url.replace("^^^HitType^^^",this.preferences.GetCurrentHitType());
	url = url.replace("^^^PartnerId^^^",g_preferencesObject.GetCurrentPartnerId());
	url = url.replace("^^^PageType^^^",g_preferencesObject.GetCurrentPageType());
	url = url.replace("^^^ChangeLocation^^^",g_preferencesObject.GetCurrentChangeLocation());
	url = url.replace("^^^UpdateAction^^^",g_preferencesObject.GetCurrentUpdateAction());
	url = url.replace("^^^SearchString^^^",g_preferencesObject.GetCurrentSearchString());
	url = url.replace("^^^UnitType^^^",g_preferencesObject.GetCurrentUnitType());
	url = url.replace("^^^LanguageType^^^",g_preferencesObject.GetCurrentLanguageType());
	url = url.replace("^^^CityCode^^^",g_preferencesObject.GetCurrentCityCode());
	url = url.replace("^^^ZipCode^^^",g_preferencesObject.GetCurrentZipCode());
	url = url.replace("^^^StationId^^^",g_preferencesObject.GetCurrentStationId());
	url = url.replace("^^^ShowPoints^^^",g_preferencesObject.GetCurrentShowPoints());
	url = url.replace("^^^IconSet^^^",g_preferencesObject.GetCurrentIconSet());
	url = url.replace("^^^LinkName^^^",g_preferencesObject.GetCurrentLinkName());
	url = url.replace("^^^NextCameraId^^^",g_preferencesObject.GetCurrentNextCameraId());
	url = url.replace("^^^rnd^^^",Math.floor(Math.random()*10000000000));
	url = url.replace("^^^SkippedVersion^^^",System.Gadget.Settings.read("skippedVersionNumber"));
	url = url.replace("^^^rDate^^^",g_preferencesObject.getRnd());

	// after constructing the URL, zero out the HitType and PageType for the next uses
	this.preferences.SetCurrentHitType("");
	this.preferences.SetCurrentPageType("");
	this.preferences.SetCurrentChangeLocation("");
	this.preferences.SetCurrentUpdateAction("");
	return url;
}

WBDataRequestXML.prototype.replaceParams = function (url)
{
    // Build the URL, replacing everything possible parameter with values set in the preferences object
	url = url.replace("^^^UserId^^^",this.preferences.GetGuid());
	url = url.replace("^^^LinkName^^^",this.preferences.GetStringValue(this.preferences.GetCurrentLinkName()));
	url = url.replace("^^^AppVersion^^^",this.preferences.GetCurrentAppVersion());
	url = url.replace("^^^AppName^^^",this.preferences.GetCurrentAppName());
	url = url.replace("^^^ZCode^^^",this.preferences.GetCurrentZCode());
	url = url.replace("^^^HitType^^^",this.preferences.GetCurrentHitType());
	url = url.replace("^^^PartnerId^^^",g_preferencesObject.GetCurrentPartnerId());
	url = url.replace("^^^PageType^^^",g_preferencesObject.GetCurrentPageType());
	url = url.replace("^^^ChangeLocation^^^",g_preferencesObject.GetCurrentChangeLocation());
	url = url.replace("^^^UpdateAction^^^",g_preferencesObject.GetCurrentUpdateAction());
	url = url.replace("^^^SearchString^^^",g_preferencesObject.GetCurrentSearchString());
	url = url.replace("^^^UnitType^^^",g_preferencesObject.GetCurrentUnitType());
	url = url.replace("^^^LanguageType^^^",g_preferencesObject.GetCurrentLanguageType());
	url = url.replace("^^^CityCode^^^",g_preferencesObject.GetCurrentCityCode());
	url = url.replace("^^^ZipCode^^^",g_preferencesObject.GetCurrentZipCode());
	url = url.replace("^^^StationId^^^",g_preferencesObject.GetCurrentStationId());
	url = url.replace("^^^ShowPoints^^^",g_preferencesObject.GetCurrentShowPoints());
	url = url.replace("^^^IconSet^^^",g_preferencesObject.GetCurrentIconSet());
	url = url.replace("^^^LinkName^^^",g_preferencesObject.GetCurrentLinkName());
	url = url.replace("^^^NextCameraId^^^",g_preferencesObject.GetCurrentNextCameraId());
	url = url.replace("^^^rnd^^^",Math.floor(Math.random()*10000000000));
	url = url.replace("^^^SkippedVersion^^^",System.Gadget.Settings.read("skippedVersionNumber"));
	url = url.replace("^^^rDate^^^",g_preferencesObject.getRnd());

	// after constructing the URL, zero out the HitType and PageType for the next uses
	this.preferences.SetCurrentHitType("");
	this.preferences.SetCurrentPageType("");
	this.preferences.SetCurrentChangeLocation("");
	this.preferences.SetCurrentUpdateAction("");
	return url;
}