/*
 * Copyright (c) 2006 AWS Convergence Technologies, Inc. 
 * All rights reserved.
 * Visit http://www.aws.com
 *
 * WBPreferences.js
 *
 * This scripts contains the WBPreferences resuable class. A WBPreferences object
 * containg accessor methods for getting to various preferences. It is basically
 * a wrapper object around the dashboard Preferences methods, that allows us to
 * make similar calls from our universal library of classes in both YWE 3.0 and Dashboard.
 */

// Object Definition
function WBPreferences()
{
    this.userId = "";
	this.partnerId = "";
	this.categoryId = "";
	this.photoId = "";
	this.linkName = "";
	this.zCode = "";
	this.appVersion = "";
	this.appName = "";
	this.hitType = "";
	this.pageType = "";
	this.changeLocation = "";
	this.updateAction = "";
	this.searchString = "";
	this.unitType = "";
	this.languageType = "";
	this.cityCode = "";
	this.zipCode = "";
	this.stationId = "";
	this.showPoints = "";
	this.iconSet = "";
	this.linkName = "";
	this.nextCameraId = "";
}

// Do not call this directly, only from an initialized WBPreferences object.
// Used to get the current zip code.
WBPreferences.prototype.getZipCode = function () {
	var zpcd = System.Gadget.Settings.read("zipCode");
	if(zpcd != "")
	{
		zpcd = zpcd + "";
		while(zpcd.length < 5)
			zpcd = "0" + zpcd;
		return zpcd;
	}
	else
	{
		return System.Gadget.Settings.read("zipCode");	
	}
}

WBPreferences.prototype.IsGuidValid = function()
{
    var prefGUID = this.getGUID();
	if(prefGUID.length == 36 && prefGUID.indexOf("-") == 8 && prefGUID.lastIndexOf("-") == 23)
	{
		return true;
	}
	else
	{
	    return false;
	}
}

// Do not call this directly, only from an initialized WBPreferences object.
// Returns the stationID.
WBPreferences.prototype.getStationID = function () {
	return System.Gadget.Settings.read("stationId");
}

WBPreferences.prototype.getUnits = function ()
{
	if(System.Gadget.Settings.read("units") == "")
	{
		System.Gadget.Settings.write("units", "0");
	}
	return System.Gadget.Settings.read("units");
}

WBPreferences.prototype.setGUID = function (str)
{
    var fso, f, ts, s, fileName, filePath;
    var ForReading = 1, ForWriting = 2, ForAppending = 8;
    var TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0;
    
    fileName = "install.txt";
    filePath = System.Gadget.path + "\\" + fileName;
    
    fso = new ActiveXObject("Scripting.FileSystemObject");
    
    try
    {
        f = fso.GetFile(filePath); // attempt to open up the file
        ts = f.OpenAsTextStream(ForWriting, TristateUseDefault);
        ts.Write(str);
        ts.Close( );
    }
    catch(e) // No file exists
    {
        try
        {
            fso.CreateTextFile(filePath);           // Create a file
            f = fso.GetFile(filePath);              // Open up the new file
            ts = f.OpenAsTextStream(ForWriting, TristateUseDefault);
            ts.Write(str);
            ts.Close( );
        }
        catch(e) // scripting disabled, use instance GUID
        {
            System.Gadget.Settings.write("guid", str); // return instance GUID
        }
    }
}

WBPreferences.prototype.getGUID = function ()
{
    var fso, f, ts, s, fileName, filePath;
    var ForReading = 1, ForWriting = 2, ForAppending = 8;
    var TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0;
    
    fileName = "install.txt";
    filePath = System.Gadget.path + "\\" + fileName;
    
    fso = new ActiveXObject("Scripting.FileSystemObject");
    
    try
    {
        f = fso.GetFile(filePath); // attempt to open up the file
        ts = f.OpenAsTextStream(ForReading, TristateUseDefault);
        s = ts.ReadLine( );
        ts.Close( );
        return s;
    }
    catch (e) // the file was not found. if file exists, guid exists
    {
        return System.Gadget.Settings.read("guid"); // return instance GUID
    }
}

WBPreferences.prototype.GetGuid = function ()
{
	return this.getGUID();
}

WBPreferences.prototype.setStationOrLocation = function (type)
{
	System.Gadget.Settings.write("stationOrLocation", type);
}

WBPreferences.prototype.getStationOrLocation = function ()
{
	if(!System.Gadget.Settings.read("stationOrLocation"))
	{
		System.Gadget.Settings.write("stationOrLocation",0);
	}
	return System.Gadget.Settings.read("stationOrLocation");
}

WBPreferences.prototype.setType = function (str)
{
	return System.Gadget.Settings.read("type");
}

WBPreferences.prototype.getType = function ()
{
	if(System.Gadget.Settings.read("type") == "")
	{
		System.Gadget.Settings.write("type",false);
	}
	return System.Gadget.Settings.read("type");
}

WBPreferences.prototype.getTypeIntValue = function ()
{
	if(this.getType() == true)
		return "1";
	else
		return "0";
}

WBPreferences.prototype.setCountryName = function (str)
{
	System.Gadget.Settings.write("countryName",str);
}

WBPreferences.prototype.getCountryName = function ()
{
	if(!System.Gadget.Setting.read("countryName"))
	{
		System.Gadget.Settings.write("countryName",false);
	}
	return System.Gadget.Setting.read("countryName");
}

WBPreferences.prototype.setNotifyForUpdates = function (str)
{
	System.Gadget.Settings.write("updateNotification",str);
}

WBPreferences.prototype.getNotifyForUpdates = function ()
{
	if(System.Gadget.Setting.read("updateNotification") == "")
	{
		System.Gadget.Setting.write("updateNotification",1);
	}
	return System.Gadget.Setting.read("updateNotification");
}

WBPreferences.prototype.GetStringValue = function (object)
{
	if(!object)
		return "";
	else
		return "" + object; // casts the str object into a string
}

WBPreferences.prototype.getRnd = function ()
{
	if(System.Gadget.Settings.read("rnd") == "")
	{
	    var rnd = new WBTime();
		System.Gadget.Settings.write("rnd",rnd.currentTime());
	}
	return System.Gadget.Settings.read("rnd");
}

WBPreferences.prototype.SetCurrentLinkName = function (str) { this.linkName = str; }
WBPreferences.prototype.GetCurrentLinkName = function() { return this.linkName; }

WBPreferences.prototype.SetCurrentUserId = function (str) { this.userId = str; }
WBPreferences.prototype.GetCurrentUserId = function() { return this.userId; }

WBPreferences.prototype.SetCurrentZCode = function (str) { this.zCode = str; }
WBPreferences.prototype.GetCurrentZCode = function () { return this.zCode; }

WBPreferences.prototype.SetCurrentAppVersion = function (str) { this.appVersion = str; }
WBPreferences.prototype.GetCurrentAppVersion = function () { return this.appVersion; }

WBPreferences.prototype.SetCurrentAppName = function (str) { this.appName = str; }
WBPreferences.prototype.GetCurrentAppName = function () { return this.appName; }

WBPreferences.prototype.SetCurrentHitType = function (str) { this.hitType = str; }
WBPreferences.prototype.GetCurrentHitType = function () { return this.hitType; }

WBPreferences.prototype.SetCurrentPartnerId = function(str) { this.partnerId = str; }
WBPreferences.prototype.GetCurrentPartnerId = function () { return this.partnerId; }

WBPreferences.prototype.SetCurrentPageType = function (str) { this.pageType = str; }
WBPreferences.prototype.GetCurrentPageType = function () { return this.pageType; }

WBPreferences.prototype.SetCurrentPageType = function (str) { this.pageType = str; }
WBPreferences.prototype.GetCurrentPageType = function () { return this.pageType; }

WBPreferences.prototype.SetCurrentChangeLocation = function (str) { this.changeLocation = str; }
WBPreferences.prototype.GetCurrentChangeLocation = function () { return this.changeLocation; }

WBPreferences.prototype.SetCurrentUpdateAction = function (str) { this.updateAction = str; }
WBPreferences.prototype.GetCurrentUpdateAction = function () { return this.updateAction; }

WBPreferences.prototype.SetCurrentSearchString = function (str) { this.searchString = str; }
WBPreferences.prototype.GetCurrentSearchString = function () { return this.searchString; }

WBPreferences.prototype.SetCurrentUnitType = function (str) { this.unitType = str; }
WBPreferences.prototype.GetCurrentUnitType = function () { return this.unitType; }

WBPreferences.prototype.SetCurrentLanguageType = function (str) { this.languageType = str; }
WBPreferences.prototype.GetCurrentLanguageType = function () { return this.languageType; }

WBPreferences.prototype.SetCurrentCityCode = function (str) { this.cityCode = str; }
WBPreferences.prototype.GetCurrentCityCode = function () { return this.cityCode; }

WBPreferences.prototype.SetCurrentZipCode = function (str) { this.zipCode = str; }
WBPreferences.prototype.GetCurrentZipCode = function () { return this.zipCode; }

WBPreferences.prototype.SetCurrentStationId = function (str) { this.stationId = str; }
WBPreferences.prototype.GetCurrentStationId = function () { return this.stationId; }

WBPreferences.prototype.SetCurrentShowPoints = function (str) { this.showPoints = str; }
WBPreferences.prototype.GetCurrentShowPoints = function () { return this.showPoints; }

WBPreferences.prototype.SetCurrentIconSet = function (str) { this.iconSet = str; }
WBPreferences.prototype.GetCurrentIconSet = function () { return this.iconSet; }

WBPreferences.prototype.SetCurrentLinkName = function (str) { this.linkName = str; }
WBPreferences.prototype.GetCurrentLinkName = function () { return this.linkName; }

WBPreferences.prototype.SetCurrentNextCameraId = function (str) { this.nextCameraId = str; }
WBPreferences.prototype.GetCurrentNextCameraId = function () { return this.nextCameraId; }
