﻿/*
 * Create an empty CleaningInfo object
 * document:     The Document object where the cookie is stored.  Required.
 * name:         The name for this cookie.  Required.
 * franchise_id: The SS franchise ID.  Required.
 * target_url:   Target URL to post this order for pricing info.  Required.
 * expires:      The Date this object expires.  Optional.  Defaults to 1 hour from now.
 *
 * Prerequisites:
 *   jquery.cookie.pack
 *   JSON
 */
function CleaningInfo(document, name) {
	this.Carpets = new Carpets();
	this.Tile = new Tile();
	this.Ducts = new Ducts();
	this.ZipCode = null;
	this.City = null;
	this.State = null;
	this.SpecialID = null;
	this.SpecialHTML = null;
	this.isPortable = null;
	this.HardwoodEstimate = false;
	this.scheduleMinimum = 210.00;

	// set this estimate to expire in 1 hour
	var ms = new Date().getTime();
	ms = ms + 3600000;
	// necessary to translate from UTC milliseconds to local date
	this.Expiration = new Date(ms);
	this.ExpirationMS = Date.UTC(this.Expiration.getFullYear(), this.Expiration.getMonth(), this.Expiration.getDate(), this.Expiration.getHours(), this.Expiration.getMinutes(), this.Expiration.getSeconds(), this.Expiration.getMilliseconds());
	
	this.$name = name;
}

CleaningInfo.prototype.ClearOrder = function(carpet, tile, duct) {
	if (carpet) {
		this.Carpets = new Carpets();
	}
	
	if (tile) {
		this.Tile = new Tile();
	}
	
	if (duct) {
		this.Ducts = new Ducts();
	}
};

CleaningInfo.prototype.toJsonString = function() {
	var s = JSON.stringify(this);
	return s;
};

CleaningInfo.prototype.switchToGuarantee = function() {
    for (var i in this.Carpets.Rooms) {
        var carpet = this.Carpets.Rooms[i];
        var guarantees = this.Carpets.Guarantees;

        carpet.isGuaranteed = true;
        guarantees[i] = carpet;
    }

    this.Carpets.Rooms = {};
};

CleaningInfo.prototype.switchToCarpet = function() {
    for (var i in this.Carpets.Guarantees) {
        var guarantee = this.Carpets.Guarantees[i];
        var carpets = this.Carpets.Rooms;

        if (carpets[i] === null || carpets[i] === undefined) {
            carpets[i] = guarantee;
        } else {
            var clean = parseInt(carpets[i].CleanQuantity, 10) + parseInt(guarantee.CleanQuantity, 10);
            var protect = parseInt(carpets[i].ProtectQuantity, 10) + parseInt(guarantee.ProtectQuantity, 10);
            var deodorize = parseInt(carpets[i].DeodorizeQuantity, 10) + parseInt(guarantee.DeodorizeQuantity, 10);

            carpets[i].CleanQuantity = clean;
            carpets[i].ProtectQuantity = protect;
            carpets[i].DeodorizeQuantity = deodorize;
        }

        carpets[i].isGuaranteed = false;
    }

    this.Carpets.Guarantees = {};
};

CleaningInfo.prototype.Save = function() {
    this.ErrorMessage = undefined;
    $.cookie(SESSION_GUID, this.$name, this.toJsonString(), { async: false });
};

CleaningInfo.prototype.Load = function() {
    var json_string = $.cookie(SESSION_GUID, this.$name);
    if (json_string !== null) {
        var cleaning_info = JSON.parse(json_string);
                
        this.Carpets = cleaning_info['Carpets'];
        this.Tile = cleaning_info['Tile'];
        this.Ducts = cleaning_info['Ducts'];
        // necessary to translate from UTC milliseconds to local date
        this.Expiration = new Date(cleaning_info['ExpirationMS']);
        this.ExpirationMS = Date.UTC(this.Expiration.getFullYear(), this.Expiration.getMonth(), this.Expiration.getDate(), this.Expiration.getHours(), this.Expiration.getMinutes(), this.Expiration.getSeconds(), this.Expiration.getMilliseconds());
        this.SpecialID = cleaning_info['SpecialID'];
        this.SpecialHTML = cleaning_info['SpecialHTML'];
        this.ZipCode = cleaning_info['ZipCode'];
        this.ErrorMessage = undefined;
        this.isPortable = cleaning_info['isPortable'];
        this.City = cleaning_info['City'];
        this.State = cleaning_info['State'];
        this.ScheduledDate = cleaning_info['ScheduledDate'];
        this.TimeSlot = cleaning_info['TimeSlot'];
        this.scheduleMinimum = cleaning_info['scheduleMinimum'];
        this.HardwoodEstimate = cleaning_info['HardwoodEstimate'];

        if (this.ZipCode === null) {
            this.ZipCode = $.cookie(SESSION_GUID, 'zip_code');
        }
    } else {
        this.ErrorMessage = 'Your quote has expired.';
    }
};

function Carpets() {
	this.Rooms = {};
	this.ClothFurniture = {};
	this.LeatherFurniture = {};
	this.Autos = {};
	this.Guarantees = {};
	this.Renewals = {};
	this.GSC = {};
}

function Tile () {
	this.Rooms = {};
}

function Ducts () {
	this.Rooms = {};
	this.HVACs = 1;
	this.AC = false;
	this.Dryer = false;
}

function CarpetItem(name, clean_quantity, protect_quantity, deodorize_quantity, guaranteed, orderNum) {
	this.Name = name;
	this.CleanQuantity = clean_quantity;
	this.ProtectQuantity = protect_quantity;
	this.DeodorizeQuantity = deodorize_quantity;
	this.isGuaranteed = guaranteed;
	this.orginalOrderNubmer = orderNum;
}

function TileItem(name, clean_quantity, seal_quantity) {
	this.Name = name;
	this.CleanQuantity = clean_quantity;
	this.SealQuantity = seal_quantity;
}

function DuctItem(name, quantity) {
	this.Name = name;
	this.Quantity = quantity;
}