﻿//
// Data
//
var UserID = null;
var Email;
var Password;

//
// Methods
//
function SetCookie(c_name, value, expiredays) 
{
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + exdate.toGMTString());
}

function DeleteCookie(cookie_name) 
{
    var cookie_date = new Date();  // current date & time
    cookie_date.setTime(cookie_date.getTime() - 1);
    document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

function GetCookie(cookie_name) 
{
    var results = document.cookie.match('(^|;) ?' + cookie_name + '=([^;]*)(;|$)');

    if (results)
        return (unescape(results[2]));
    else
        return null;
}

function ShowProjectFolder()
{
    ActivateMenu("UserFolder.aspx?UserID=" + UserID + "&rnd=" + Math.random());
}

function setUserID(id)
{
    UserID = id;
}

function doLogin()
{
    Email = document.getElementById("txtEmail").value;
    Password = document.getElementById("txtPassword").value;
    AJAXReplace("divProductSelect", "UserFolder.aspx?email=" + Email + "&password=" + Password + "&rnd=" + Math.random());
}

function doLogout()
{
    UserID = null
    DeleteCookie("UserID");
    Password = "";
    AJAXReplace("divProductSelect", "Login.aspx");
}

function createNewAccount() {
    var result = "";

    // Josh put this in on 10/28/08 because the way we were referencing the elements wasn't working in Firefox
    var txtNewEmail = document.getElementById("txtNewEmail");
    var txtNewEmailConfirm = document.getElementById("txtNewEmailConfirm");
    var txtNewPassword = document.getElementById("txtNewPassword");
    var txtNewPasswordConfirm = document.getElementById("txtNewPasswordConfirm");
    var txtFirstName = document.getElementById("txtFirstName");
    var txtLastName = document.getElementById("txtLastName");
    var txtAddress = document.getElementById("txtAddress");
    var txtCity = document.getElementById("txtCity");
    var txtState = document.getElementById("txtState");
    var txtCountry = document.getElementById("txtCountry");
    var txtZipCode = document.getElementById("txtZipCode");
    var txtPhone1 = document.getElementById("txtPhone1");

    // Verify a few things first
    if (txtNewEmail.value == "" || txtNewEmailConfirm.value == "") {
        result += "Email and confirm Email cannot be empty.\n";
    }
    if (txtNewEmail.value != txtNewEmailConfirm.value) {
        result += "Email and confirm Email must match.\n";
    }
    if (txtNewPassword.value == "" || txtNewPasswordConfirm.value == "") {
        result += "Password and confirm Password cannot be empty.\n";
    }
    if (txtNewPassword.value != txtNewPasswordConfirm.value) {
        result += "Password and confirm Password must match.\n";
    }
    if (result != "") {
        alert("There were errors: \n" + result);
    }
    else {
        var qs = "newAccount.aspx?email=" + txtNewEmail.value;
        qs += "&password=" + txtNewPassword.value;
        qs += "&FirstName=" + txtFirstName.value;
        qs += "&LastName=" + txtLastName.value;
        qs += "&Address=" + txtAddress.value;
        qs += "&City=" + txtCity.value;
        qs += "&State=" + txtState.value;
        qs += "&Country=" + txtCountry.value;
        qs += "&Zip=" + txtZipCode.value;
        qs += "&Phone1=" + txtPhone1.value;
        //alert(qs);
        AJAXReplace("divProductSelect", qs);
    }
}

function SaveProject()
{
    ActivateMenu("UserFolder.aspx?UserID=" + UserID + "&email=" + Email + "&password=" + Password + 
    "&action=Save&Sel=" + AppliedProducts.toString() + "&specifier=" + Specifier + "&rnd=" + Math.random());
}

function ShowFeedback()
{
    ActivateMenu("Feedback.aspx?rnd=" + Math.random());
}

function ShowImageMapper()
{
    ActivateMenu("ImageMapper.aspx?rnd=" + Math.random());
}

function submitFeedback(email, name, message) {
    AJAXReplace("divProductSelect", "Feedback.aspx?email=" + email + "&name=" + name + "&message=" + message);
    DeactivateMenu();
    alert("Feedback Sent.  Thank you.");
}

function UpdateImage(specifier, sel)
{
    Specifier = specifier;
    var aplist = sel.split(',');
    AppliedProducts.length = 0;
    var ind=0;
    for (ind = 0; ind < aplist.length; ind++) {
        AppliedProducts[ind] = aplist[ind];
    }
    
    CurrentSurface = "";
    CurrentSortOrder = 1;
    deactivate = true;

    window.location = "designer.aspx?Specifier=" + Specifier + "&Sel=" + sel + "&Applied=" + AppliedProducts.toString();
}

function RemoveImage(srid)
{
    ActivateMenu("UserFolder.aspx?UserID=" + UserID + "&action=remove" + "&srid=" + srid);
}
