﻿function saveCustomer() {
    var customerId = document.getElementById("CustomerId").value;
    var username = document.getElementById("NewUsername").value;
    var password = document.getElementById("NewPassword").value;
    var password2 = document.getElementById("NewPassword2").value;
    var Representative = document.getElementById("Representative").value;
    var Organization = document.getElementById("Organization").value;
    var Type = document.getElementById("OrganizationType").value;
    var Phone = document.getElementById("Phone").value;
    var CellPhone = document.getElementById("CellPhone").value;
    var Address1 = document.getElementById("Address1").value;
    var Address2 = document.getElementById("Address2").value;
    var City = document.getElementById("City").value;
    var StateSelect = document.getElementById("state");
    var State = StateSelect.options.item(StateSelect.selectedIndex).text;
    var Zip = document.getElementById("Zip").value;
    var Email = document.getElementById("Email").value;
    var EmailUpdates = document.getElementById("emailUpdates").checked;
    var TermsAndConditions = document.getElementById("tc").checked;

    if (password.length == 0) {
	document.getElementById("NewPassword").className='errorInput';
	document.getElementById("NewPassword").focus();
    document.getElementById("NewPassword").select();
	document.getElementById("NewPasswordError").className='showError';
        return;
    }
	
    if (password != password2) {
	document.getElementById("NewPassword").className='errorInput';
	document.getElementById("NewPassword2").className='errorInput';
	document.getElementById("NewPassword").focus();
    document.getElementById("NewPassword").select();
	document.getElementById("NewPasswordError2").className='showError';
        return;
    }
	
    if (!TermsAndConditions) {
        //alert("You must read and accept the terms and conditions to proceed.");
		document.getElementById("tcError").className='showError';
        return;
    }
	
    
    var customer = { CustomerId: customerId, Username: username, Password: password, Representative: Representative, Organization: Organization, Type: Type, Phone: Phone, CellPhone: CellPhone, Address1: Address1, Address2: Address2, City: City, State: State, Zip: Zip, Email: Email, EmailUpdates: EmailUpdates };
    CustomerService.SaveCustomer(customer, saveCustomerSuccess, saveCustomerFail);
}
function loadCustomer() {
    var customer = CustomerService.LoadCustomer(loadCustomerSuccess);
}
function loadCustomerSuccess(customer) {
    document.getElementById("CustomerId").value = customer.CustomerId;
    document.getElementById("NewUsername").value = customer.Username;
    document.getElementById("NewPassword").value = customer.Password;
    document.getElementById("NewPassword2").value = customer.Password;
    document.getElementById("Representative").value = customer.Representative;
    document.getElementById("Organization").value = customer.Organization;
    document.getElementById("OrganizationType").value = customer.Type;
    document.getElementById("Phone").value = customer.Phone;
    document.getElementById("CellPhone").value = customer.CellPhone;
    document.getElementById("Address1").value = customer.Address1;
    document.getElementById("Address2").value = customer.Address2;
    document.getElementById("City").value = customer.City;
    var StateSelect = document.getElementById("state");
    for (var i = 0; i < StateSelect.options.length;  i++) {
        var option = StateSelect.options[i].text;
        if (option == customer.State) {
            StateSelect.selectedIndex = i;
        }
    }
    //var State = StateSelect.options.item(StateSelect.selectedIndex).text;
    document.getElementById("Zip").value = customer.Zip;
    document.getElementById("Email").value = customer.Email;
    document.getElementById("emailUpdates").checked = customer.EmailUpdates;
    //var TermsAndConditions = document.getElementById("tc").checked;
}
function saveCustomerSuccess() {
    var username = document.getElementById("NewUsername").value;
    var password = document.getElementById("NewPassword").value;

    CustomerService.Login(username, password, newLoginSuccess);
}
function newLoginSuccess() {
    document.location = "Default.aspx";
}
function saveCustomerFail() {
	document.getElementById("NewUsernameError").className='showError';
	document.getElementById("NewUsername").className='errorInput';
    document.getElementById("NewUsername").focus();
    document.getElementById("NewUsername").select();
}

