﻿var checkAvailFormProxy;
var whichAptID;

// Initializes global and proxy default variables.
function pageLoad() {
    // Instantiate the service proxy.
    checkAvailFormProxy = new AptShopper.CheckAvailabilityForm();

    // Set the default call back functions.
    checkAvailFormProxy.set_defaultSucceededCallback(SucceededCallback);
    checkAvailFormProxy.set_defaultFailedCallback(FailedCallback);
}

// "Processes" the button click (onclick of button points here and form fields harvested here)
//  and calls the service method.
function OnClick_CA_Submit(aptID) {
    //get values from form fields
    //var aptID = document.getElementById("CA_AptID_" + aptID).value;
    var name = document.getElementById("CA_Name_" + aptID).value;
    var email = document.getElementById("CA_Email_" + aptID).value;
    var phone = document.getElementById("CA_Phone_" + aptID).value;
    var moveInDate = document.getElementById("CA_MoveInDate_" + aptID).value;
    var comments = document.getElementById("CA_Comments_" + aptID).value;
    var aptID2 = document.getElementById("CA_AptID_" + aptID);
    
    //validate
    if (name == "" || email == "" || phone == "" || moveInDate == "") {
        alert('Please complete all required* information.');
    }
    else {
        //hide the submit button to prevent multiple clicks & show loading gif
        document.getElementById("CA_Send_" + aptID).style.visibility = "hidden";
        document.getElementById("CA_Send_" + aptID).style.display = "none";
        document.getElementById("CA_Loading_" + aptID).style.display = "inline";
        document.getElementById("CA_Loading_" + aptID).style.visibility = "visible";
        
        //Save aptID which is also being used to identify correct popup inputs when page contains multiple popup forms.
        whichAptID = aptID;
        
        //call the webservice method
        checkAvailFormProxy.Submit(aptID, name, email, phone, moveInDate, comments)
    }
}

// Callback function that processes the service return value.
function SucceededCallback(result) {
    var RsltElem = document.getElementById("Results_" + whichAptID);
    RsltElem.innerHTML = result;
}

// Callback function invoked when a call to the service methods fails.
function FailedCallback(error, userContext, methodName) {
    if (error !== null) {
        var RsltElem = document.getElementById("Results_" + whichAptID);

        RsltElem.innerHTML = "An error occurred: " +
            error.get_message();
    }
}

if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
