﻿// ***** user name checking

var usernameCheckerTimer;

function usernameChecker(s, e) {
    var username = uname.GetValue();
    var spanAvailability = $get("spanAvailability");

    clearTimeout(usernameCheckerTimer);

    if (!username) return;

    if (username.length == 0)
        spanAvailability.innerHTML = "";
    else {
        spanAvailability.innerHTML = "<span style='color: #ccc;'>checking...</span>";
        usernameCheckerTimer = setTimeout("checkUsernameUsage('" + username + "');", 750);
    }
}

function checkUsernameUsage(username) {

    // initiate the ajax pagemethod call
    // upon completion, the OnSucceded callback will be executed
    PageMethods.IsUserAvailable(username, OnSucceeded);
}

// Callback function invoked on successful completion of the page method.
function OnSucceeded(result, userContext, methodName) {
    var spanAvailability = $get("spanAvailability");

    if (methodName == "IsUserAvailable") {
        if (result == true)
            spanAvailability.innerHTML = "<span style='color: DarkGreen;'>Available</span>";
        else
            spanAvailability.innerHTML = "<span style='color: Red;'>Unavailable</span>";
    }
}


// ===== user name checking


var __initialText = "[First]";

function OnTextBoxInit(textBox) {
    OnTextBoxLostFocus(textBox);
}
function OnTextBoxGotFocus(textBox) {
    if (textBox.GetText() == __initialText)
        textBox.SetText("");
}
function OnTextBoxLostFocus(textBox) {
    if (textBox.GetText() == "")
        textBox.SetText(__initialText);
}
var __initialText2 = "[Last]";

function OnTextBoxInit2(textBox) {
    OnTextBoxLostFocus2(textBox);
}
function OnTextBoxGotFocus2(textBox) {
    if (textBox.GetText() == __initialText2)
        textBox.SetText("");
}
function OnTextBoxLostFocus2(textBox) {
    if (textBox.GetText() == "")
        textBox.SetText(__initialText2);
}

function ClientValidation(source, args) { alert('here'); args.IsValid = $(source)[0].checked; }

// *************** my places code


function CheckPropertyID(s,e) {

    e.processOnServer = $get(hid).value;
    if ($get(hid).value) $('#propidreq').hide(); else $('#propidreq').show();

}

function SetProperty(s, e) {
    var id = s.name.substring(s.name.length - 1)
    $get(hid).value = id;
    $('#propidreq').hide();

}

$(function() {

    $("#elogin,#glogin").hide();
    $(".addubtn").click(function() { ShowAdd() });
});

function cbElectric_Changed(s, e) {
    var ec = cbElectric.GetText();
    var v = cbElectric.GetValue();

    if (v == 32 || v == 14 || v == 43 || v == 46) {
        $("#elogin").show();
        $("#electricco").html(ec);
    }
    else $("#elogin").hide();
}

function cbGas_Changed(s, e) {
    var eg = cbGas.GetText();
    var v = cbGas.GetValue();

    if (v == 32 || v == 14 || v == 43 || v == 46) {
        $("#glogin").show();
        $("#gasco").html(eg);
    }
    else $("#glogin").hide();
}


function ShowAdd() {
    $("#uaddbox").show();
    cbGasUnit.SetVisible(false);
}

function ckGas_Checked(s,e) {
    if (ckGas.GetChecked()) {
        cbGasUnit.SetVisible(true);
        $("#gulbl").show();
        cbGasUnit.SetSelectedIndex(-1);
    }
    else {
        cbGasUnit.SetVisible(false);
        $("#gulbl").hide();
    }
}

function btnAddCompany_Click(s, e) {

    btnCancel.SetVisible(false);
    btnAddCompany.SetEnabled(false);
    btnAddCompany.SetText('Please wait...');
    
    PageMethods.AddCompany(
        txtUtilityName.GetValue(),
        txtWebsite.GetValue(),
        ckElectric.GetChecked(),
        ckGas.GetChecked(),
        cbGasUnit.GetSelectedIndex(),
        AddCompanyDone
    );
}

function AddCompanyDone(result, userContext, methodName) {
    // insert and select new value into ddls
    if (ckElectric.GetChecked()) {
        i = cbElectric.AddItem(txtUtilityName.GetValue(), result);
        cbElectric.SetSelectedIndex(i);
    }
    if (ckGas.GetChecked()) {
        i = cbGas.AddItem(txtUtilityName.GetValue(), result);
        cbGas.SetSelectedIndex(i);
    }

    btnCancel.SetVisible(true);
    btnAddCompany.SetEnabled(true);
    btnAddCompany.SetText('Add Company');

    // hide the form
    HideForm();
}

function Cancel_Click(s, e) {
    HideForm();
}

function HideForm() {
    if (ckGas.GetChecked()) {
        cbGasUnit.SetSelectedIndex(-1);
    }
    cbGasUnit.SetVisible(true);
    $("#gulbl").hide();
    txtWebsite.SetValue('');
    txtUtilityName.SetValue('');
    ckElectric.SetChecked(false);
    ckGas.SetChecked(false);
    $("#uaddbox").hide();
}

function LoginVal() {
    if (gcuser.GetText() != "" || gcpw.GetText() != "") ASPxClientEdit.ValidateGroup("gGC");
    if (ecuser.GetText() != "" || ecpw.GetText() != "") ASPxClientEdit.ValidateGroup("gEC");

}

function ValidateForm(s, e) {
    LoginVal();
   
    CheckPropertyID(s,e);
   
}