﻿function $(elementName) {
    return document.getElementById(elementName);
}
function CheckForNullOrEmpty(value) {
    if (value == null)
        return true;
    if (value.replace(" ", "").length == 0)
        return true;

    return false;
}

function ProductRegistration(instanceName, outputContainer, windowOptions) {
    ProductRegistration.baseConstructor.call(this, instanceName, new Ajax(), "/ajax/Ajax_ProductRegister.ashx", null);
    this.OutputContainer = outputContainer;
    this.Collections = null;
    this.ModelTypes = null;
    this.SeatingTypes = null;
    this.FabricTypes = null;
    this.TableBaseTypes = null;

    this.ProdInfos = new Array();
    this.ProductCounter = 0;
    this.Window.LoadOptions(windowOptions);
}
Extend(ProductRegistration, AjaxProcessorBase);

ProductRegistration.prototype.toString = function() {
    return "ProductRegistration object";
}

ProductRegistration.prototype.LaunchWizard = function() {
    var t = this, w = t.Window;
    var guid = Utils.GetGuid();
    // create new product info array
    this.ProdInfos[guid] = new ProductInfo();

    // setup window
    w.Size.Set(800, 700);
    w.Background.Set("#fff");
    w.Show();
    w.SetContent("<iframe src='/productregister/wizard.aspx?guid=" + guid + "' style='width: 98%; height: 640px; margin: 0 auto; display: block;'></iframe>");
}

ProductRegistration.prototype.CommitProduct = function(guid) {
    var t = this, w = t.Window;
    var prodInfo = t.ProdInfos[guid];
    if (prodInfo == null)
        return;

    t.ProductCounter++;

    this.Execute({
        mode: "render_product_info",
        name: t.Name,
        collectionid: prodInfo.CollectionID,
        productid: prodInfo.ProductID,
        modelnumber: prodInfo.ModelNumber,
        seatingoptionid: prodInfo.SeatingOptionID,
        fabricid: prodInfo.FabricID,
        tablebaseid: prodInfo.TableBaseID,
        description: prodInfo.Description,
        productcount: t.ProductCounter
    }, __callback);

    function __callback(success, returnValue) {
        var listItem = document.createElement("li");
        listItem.style.borderBottom = "1px solid #555";
        listItem.style.marginBottom = "10px";
        listItem.innerHTML = returnValue;

        $("productList").appendChild(listItem);
        $("hdnProductInfoCount").value = t.ProductCounter;
    }
}

ProductRegistration.prototype.Validate = function() {
    // do field validation
    if (CheckForNullOrEmpty($("txtFirstName").value)) {
        alert("Please enter your first name");
        return false;
    }
    if (CheckForNullOrEmpty($("txtLastName").value)) {
        alert("Please enter your last name");
        return false;
    }
    if (CheckForNullOrEmpty($("txtAddress1").value)) {
        alert("Please enter your mailing address");
        return false;
    }
    if (CheckForNullOrEmpty($("txtCity").value)) {
        alert("Please enter your city location");
        return false;
    }
    if (CheckForNullOrEmpty($("txtZipCode").value)) {
        alert("Please enter your zip/postal code");
        return false;
    }
    if (CheckForNullOrEmpty($("txtEmail").value)) {
        alert("Please enter your email address");
        return false;
    }
    if (CheckForNullOrEmpty($("txtDealerName").value)) {
        alert("Please enter the dealer's name");
        return false;
    }
    if (CheckForNullOrEmpty($("txtDealerCity").value)) {
        alert("Please enter the dealer's city");
        return false;
    }
    return true;
}


function ProductInfo() {
    this.CollectionID = null;
    this.ProductID = null;
    this.ModelNumber = null;
    this.SeatingOptionID = null;
    this.FabricID = null;
    this.TableBaseID = null;
    this.Description = null;
}
ProductInfo.prototype.toString = function() {
    return "ProductInfo object";
}

