﻿
//Existing registrations. Get the school ID to make sure it exists.
function getSchoolSignInID() {
    if (document.getElementById(txtSchoolSignInIDTextBox).value != "")
        var schoolAndSuburb = document.getElementById(txtSchoolIDTextBox).value;
    var school = schoolAndSuburb.split(',');
    if (school[0] != null) {
        PageMethods.GetSchoolIDBySchoolName(school[0], onSchoolIDSignInSuccess, onSchoolIDSignInFail);

    }
}
function onSchoolIDSignInSuccess(sender, e) {
    if (sender == -1)
        document.getElementById(txtSchoolSignInIDTextBox).value = "";
    document.getElementById(txtSchoolSignInIDHiddenField).value = sender;
}
function onSchoolIDSignInFail(sender, e) {
    document.getElementById(txtSchoolSignInIDTextBox).value = "";
    document.getElementById(txtSchoolSignInIDHiddenField).value = "-1";
}

function onSchoolSignInSelected() {
    document.getElementById(txtSchoolSignInIDTextBox).blur();
}


//New Registrations. Get the school ID to make sure it exists.
function getSchoolID() {
    if (document.getElementById(txtSchoolIDTextBox).value != "")
        var schoolAndSuburb = document.getElementById(txtSchoolIDTextBox).value;
    var school = schoolAndSuburb.split(',');
    if (school[0] != null) {
        PageMethods.GetSchoolIDBySchoolName(school[0], onSchoolIDSuccess, onSchoolIDFail);
    }
}
function onSchoolIDSuccess(sender, e) {
    if (sender == -1)
        document.getElementById(txtSchoolIDTextBox).value = "";
    document.getElementById(txtSchoolIDHiddenField).value = sender;
}
function onSchoolIDFail(sender, e) {
    document.getElementById(txtSchoolIDTextBox).value = "";
    document.getElementById(txtSchoolIDHiddenField).value = "-1";
}
function onSchoolSelected() {
    document.getElementById(txtSchoolIDTextBox).blur();
}




//New Registrattions. Hide the asp.net page validation summary when we first enter the screen.
function fncHideSummary() {
    //clear text fields.
    var pnl = document.getElementById(pnlRegister);
    var ctrlArray = pnl.getElementsByTagName("input");
    for (var i = 0; i < ctrlArray.length; ++i) {
        if (ctrlArray[i].id != btnEnterNow) {
            ctrlArray[i].value = "";
        }

    }

    //show form fields div. Hide confirmation message
    $('.formFields_body').show();
    $('.formFields_conf').hide();

    if (typeof (Page_ValidationSummaries) != "undefined") { //hide the validation summaries
        for (sums = 0; sums < Page_ValidationSummaries.length; sums++) {
            summary = Page_ValidationSummaries[sums];
            summary.style.display = "none";
        }
    }

}

//Existing Registrattions. Hide the asp.net page validation summary when we first enter the screen. We also need to show the correct page images.
function fncHideSignInSummary() {
    //show fields div. Hide confirmation message
    $('.formFieldsSignIn_body').show();
    $('.formFieldsSignIn_conf').hide();
    if (typeof (Page_ValidationSummaries) != "undefined") { //hide the validation summaries
        for (sums = 0; sums < Page_ValidationSummaries.length; sums++) {
            summary = Page_ValidationSummaries[sums];
            summary.style.display = "none";
        }
    }
    //Disable controls until we login.
    fncDisableControls();
}

function fncDisableControls() {
    var pnl = document.getElementById(pnlSignIn)

    //Select all input ctrls.
    var array = pnl.getElementsByTagName("input");
    for (var n = 0; n < array.length; ++n) {
        //don`t disable login controls.
        if (array[n].id == userNameEntry || array[n].id == passwordEntry || array[n].id == login) {
            array[n].disabled = false;
        }
        else {
            array[n].disabled = true;
            array[n].value = "";
        }
    }
    //Select all drop down ctrls.
    var array = pnl.getElementsByTagName("select");
    for (var n = 0; n < array.length; ++n) {
        array[n].disabled = true;
    }

    //Show disabled images
    var img1 = document.getElementById(enabledButtonImg);
    var img2 = document.getElementById(imgChkDetails);

    img2.src = 'Images/Registration_checkDetailsDisabled.png';
    img1.src = 'Images/Registration_EnterNowDisabled.png';

}


// Add click handlers for buttons to show and hide modal popup and others;
function pageLoad() {

    $addHandler($get("hideNewRegistrationForm"), 'click', hideModalPopupNewViaClient);
    $addHandler($get("closeExistngRegistrationForm"), 'click', hideModalPopupNewViaClient);
    $addHandler($get("hideExistngRegistrationForm"), 'click', hideModalPopupExistingViaClient);
    $addHandler($get("closeRegistrationFormExisting"), 'click', hideModalPopupExistingViaClient);
    $addHandler($get("fgtPasswordLnk"), 'click', sendPassword);
    $addHandler($get(txtSchoolIDTextBox), 'onblur', getSchoolID);
    $addHandler($get(txtSchoolSignIn), 'onblur', getSchoolSignInID);
}

function sendPassword() {
    var emailSignIn = document.getElementById(_emailSignIn).value;
    if (emailSignIn != "" && emailSignIn != null) {
        PageMethods.DoSendEmail(emailSignIn, sendPwdSuccess, sendPwdFail);
    }
    else {
        $("#ForgotPasswordValidation").append("Must enter email address");
        $("#ForgotPasswordValidation").css("color", "Red");
        $("#ForgotPasswordValidation").fadeIn("fast");
    }

}

function sendPwdSuccess(response) {
    //First, we need to create a new DIV element
    var divResponse = document.createElement("div");
    //Give it ID so we can identify it later.
    divResponse.setAttribute("id", "divConfEmail");
    //write response into new element
    $("#ForgotPasswordValidation").remove();
    divResponse.innerHTML = "Email has been sent";
    $("#ForgotPasswordConf").append(divResponse);
    $("#ForgotPasswordConf").fadeIn("slow");
    $("#ForgotPassword").fadeOut("slow");


}

function sendPwdFail(response) {
    $("#ForgotPasswordConf").append("Unable to send email. Please try again later.");
    $("#ForgotPasswordConf").css("color", "Red");
    $("#ForgotPasswordConf").fadeIn("fast");
}


function hideModalPopupNewViaClient(ev) {
    ev.preventDefault();
    var modalPopupBehavior = $find(_modalPopupExtender1);
    modalPopupBehavior.hide();
}

function hideModalPopupExistingViaClient(ev) {
    ev.preventDefault();
    var modalPopupBehavior = $find(_modalPopupExtender2);
    modalPopupBehavior.hide();
}



function addExistingRegistration() {

    if (Page_ClientValidate("SignInInfoGroup") == true) {
        var tncSignIn = document.getElementById(_tncSignIn).checked;
        if (tncSignIn == true) {
            var IDSignIn = document.getElementById(_schoolIDSignIn).value;
            var ageSignIn = document.getElementById(_ageSignIn).value;
            var emailSignIn = document.getElementById(_emailSignIn).value;
            var entryCodeSignIn = document.getElementById(_entryCodeSignIn).value;
            var firstSignIn = document.getElementById(_firstSignIn).value;
            var lastSignIn = document.getElementById(_lastSignIn).value;
            var genderSignIn = document.getElementById(_genderSignIn).value;
            var passwordSignIn = document.getElementById(_passwordSignIn).value;
            var phoneSignIn = document.getElementById(_phoneSignIn).value;
            //            var relationshipSignIn = document.getElementById(_relationshipSignIn).value;
            var optinSignIn = document.getElementById(_optinSignIn).checked;
            var studentSignIn = document.getElementById(_studentSignInList);
            var radioList = studentSignIn.getElementsByTagName('input');
            var isStudent;
            for (var i = 0; i < radioList.length; i++) {
                if (radioList[i].checked == true) {
                    isStudent = radioList[i].value;
                }
            }

            PageMethods.SaveRegistrationInfo("True", IDSignIn, ageSignIn, emailSignIn, entryCodeSignIn, firstSignIn, lastSignIn, genderSignIn, passwordSignIn, phoneSignIn, tncSignIn, optinSignIn, isStudent, addRegistrationSignInSuccess, addRegistrationSignInFail)


        }
    }
}


//Show confirmation element on new registrations
function addRegistrationSuccess(response) {
    var repsonseArray = response.split(';');
    var msg = repsonseArray[1];
    //First, we need to create a new DIV element
    var divResponse = document.createElement("div");
    //Give it ID so we can identify it later.
    divResponse.setAttribute("id", "divResponse");
    //write response into new element
    divResponse.innerHTML = msg;
    if (repsonseArray[0] == 'true') {

        //clear child elements if exists
        $("#divResponse").remove();

        //add new element to containing element
        // $(".formFields_conf").appendChild(divResponse);
        $(".formFields_conf").append(divResponse);
        // $(".formFields_conf").html(msg);

        $('.formFields_body').fadeOut(500, function() {
            $('.formFields_conf').fadeIn(500);

        });
        
        
      


    }
    else {
        $("#divResponse").remove();
        $("#customValidation").innerHTML = "";
        $("#customValidation").append(divResponse);
    }


}

//Show confirmation on existing registrations.
function addRegistrationSignInSuccess(response) {
    var modalHeight = $(window).height();

    var repsonseArray = response.split(';');
    var msg = repsonseArray[1];
    //First, we need to create a new DIV element
    var divResponse = document.createElement("div");
    //Give it ID so we can identify it later.
    divResponse.setAttribute("id", "divSignInResponse");
    //write response into new element
    divResponse.innerHTML = msg;
    if (repsonseArray[0] == 'true') {

        //clear child elements if exists
        $("#divSignInResponse").remove();
        //add new element to containing element
        // $(".formFields_conf").appendChild(divResponse);
        $(".formFieldsSignIn_conf").append(divResponse);
        // $(".formFieldsSignIn_conf").height(200);

        //  $('.formFieldsSignIn_body').fadeOut("3000");


        $(".formFieldsSignIn_body").fadeOut(1000, function() {
            $('.formFieldsSignIn_conf').fadeIn(1000);
        });


        // $(".formFields_conf").html(msg);
    }
    else {
        $("#divSignInResponse").remove();
        $("#customValidationSignIn").innerHTML = "";
        $("#customValidationSignIn").append(divResponse);
    }


}

function addRegistrationSignInFail(response) {
    alert(response.value);
}

function addRegistrationFail(response) {
    alert(response.value);
}

function addNewRegistration() {
    if (Page_ClientValidate("RegisterInfoGroup") == true) {
        var tnc = document.getElementById(_tnc).checked;
        if (tnc == true) {

            var ID = document.getElementById(_schoolID).value;
            var age = document.getElementById(_age).value;
            var email = document.getElementById(_email).value;
            var entryCode = document.getElementById(_entryCode).value;
            var first = document.getElementById(_first).value;
            var last = document.getElementById(_last).value;
            var gender = document.getElementById(_gender).value;
            var password = document.getElementById(_password).value;
            var phone = document.getElementById(_phone).value;
            var optin = document.getElementById(_optin).checked;
            var student = document.getElementById(_studentList);
            var radioList = student.getElementsByTagName('input');
            var isStudent;
            for (var i = 0; i < radioList.length; i++) {
                if (radioList[i].checked == true) {
                    isStudent = radioList[i].value;
                }
            }
            PageMethods.SaveRegistrationInfo("False", ID, age, email, entryCode, first, last, gender, password, phone, tnc, optin, isStudent, addRegistrationSuccess, addRegistrationFail)


        }
    }

}
