﻿// JScript File

var XmlHttp;
var glb_var = 0;
var divToShowLoading = new String();

//Creating and setting the instance of appropriate XMLHTTP Request object to a “XmlHttp” variable  
function CreateXmlHttp() {
    //Creating object of XMLHTTP in IE
    try {
        XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
        try {
            XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

        }
        catch (oc) {
            XmlHttp = null;
        }
    }
    //Creating object of XMLHTTP in Mozilla and Safari 
    if (!XmlHttp && typeof XMLHttpRequest != "undefined") {
        XmlHttp = new XMLHttpRequest();
    }
}


function trimAll(sString) {
    while (sString.substring(0, 1) == ' ') {
        sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length - 1, sString.length) == ' ') {
        sString = sString.substring(0, sString.length - 1);
    }
    return sString;
}

function isValidEmail(str) {
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}


function checkAvailability() {

    if (trimAll(document.getElementById('moosId').value) != '') {
        if (document.getElementById('moosId').value.length >= 4) {
            var requestUrl = 'checkAvail.aspx';
            var params = 'moosId=' + trimAll(document.getElementById('moosId').value) + '&rand=' + Math.floor(Math.random() * 10000000);
            CreateXmlHttp();
            // If browser supports XMLHTTPRequest object
            if (XmlHttp) {
                //Setting the event handler for the response
                XmlHttp.onreadystatechange = HandleResponseCheckMoosId;
                //divToShowLoading = "RegisterDiv";
                ShowLoadingDiv("RegisterDiv");
                //Initializes the request object with GET (METHOD of posting), 
                //Request URL and sets the request as asynchronous.
                XmlHttp.open("POST", requestUrl, true);
                var contentType = "application/x-www-form-urlencoded; charset=UTF-8";
                XmlHttp.setRequestHeader("Content-Type", contentType);
                XmlHttp.setRequestHeader("Content-length", params.length);
                 XmlHttp.send(params);
            }
        }
        else {
            alert("Minimum length should be of 4 characters!!");
            document.getElementById('moosId').focus();
            //length less than 4
        }
    }
    else {
        //user id blank
        alert("Please Enter your Moos Id!!");
        document.getElementById('moosId').focus();
    }

}


function HandleResponseCheckMoosId() {
    // To make sure receiving response data from server is completed
    if (XmlHttp.readyState == 4) {
        //Remove the loading div as data has come.
        RemoveLoadingDiv("RegisterDiv");
        // To make sure valid response is received from the server, 200 means response received is OK
        if (XmlHttp.status == 200) {
            if (XmlHttp.responseText == '0') {
                alert('Sorry!! This MOOS ID is not AVAILABLE !!');
            }
            else {
                alert('Congrats!! This MOOS ID is AVAILABLE !!');
            }
        }
        else {
            alert("There was a problem retrieving data from the server.");
        }
    }
}



function CheckAuthenticate() {
    var retFlag = false;
    if (trimAll(document.getElementById('moosUserName').value) != '') {
        if (trimAll(document.getElementById('moosPasswd').value) != '') {
            document.getElementById('erDiv').style.display = 'none';
            //fire btnclick
            retFlag = true;
        }
        else {
            //empty password			
            document.getElementById('erDiv').innerHTML = '*Please enter Passsword.';
            document.getElementById('erDiv').style.display = '';
            document.getElementById('moosPasswd').focus();
        }
    }
    else {
        //empty user id
        document.getElementById('erDiv').innerHTML = '*Please enter Moos User Id.';
        document.getElementById('erDiv').style.display = '';
        document.getElementById('moosUserName').focus();
    }
    return retFlag;
    //    if(glb_var ==1)
    //    {
    //        document.getElementById('erDiv').style.display = 'none';
    //        
    //        
    //    }
    //    else 
    //    {
    //       document.getElementById('erDiv').innerHTML = '*Either Invalid Info OR Account not activaled till. Please check your mail and click on <b>Activation Link</b> to activate it.';
    //       document.getElementById('erDiv').style.display = ''; 
    //       document.getElementById('moosUserName').focus();      
    //    }
}

function ShowErrorMsg() {
    document.getElementById('erDiv').innerHTML = '*Either Invalid Info OR Account not activaled till. Please check your mail and click on <b>Activation Link</b> to activate it.';
    document.getElementById('erDiv').style.display = '';
    document.getElementById('moosUserName').focus();
}

function sendRequest(params) {
    ShowLoadingDiv(divToShowLoading);
    //Sends the request to server
    XmlHttp.send(params);
}

function signUpUser() {

    document.getElementById('save').disabled = true;
    if (trimAll(document.getElementById('emailTextId').value) != '') {
        if (isValidEmail(document.getElementById('emailTextId').value)) {
            if (trimAll(document.getElementById('moosId').value) != '') {
                if (trimAll(document.getElementById('passId').value) != '') {
                    document.getElementById('errorDiv').style.display = 'none';
                    var requestUrl = 'saveUser.aspx';
                    var params = 'emailid=' + trimAll(document.getElementById('emailTextId').value) + '&moosId=' + trimAll(document.getElementById('moosId').value) + '&password=' + trimAll(document.getElementById('passId').value) + '&rand=' + Math.floor(Math.random() * 10000000);
                    CreateXmlHttp();
                    if (XmlHttp) {
                        ShowLoadingDiv("RegisterDiv");
                        XmlHttp.onreadystatechange = callBackFunctSignUp;
                        XmlHttp.open("POST", requestUrl, true);
                        var contentType = "application/x-www-form-urlencoded; charset=UTF-8";
                        XmlHttp.setRequestHeader("Content-Type", contentType);
                        XmlHttp.setRequestHeader("Content-length", params.length);
                        XmlHttp.send(params);
                    }
                }
                else {
                    //password blank
                    document.getElementById('errorDiv').innerHTML = '*Please enter Passsword.';
                    document.getElementById('errorDiv').style.display = '';
                    document.getElementById('passId').focus();
                    document.getElementById('save').disabled = false;
                }
            }
            else {
                //username blank
                document.getElementById('errorDiv').innerHTML = '*Please enter UserName.';
                document.getElementById('errorDiv').style.display = '';
                document.getElementById('moosId').focus();
                document.getElementById('save').disabled = false;
            }

        }
        else {
            //not valid email
            document.getElementById('errorDiv').innerHTML = '*Please enter valid Email.';
            document.getElementById('errorDiv').style.display = '';
            document.getElementById('emailTextId').focus();
            document.getElementById('save').disabled = false;
        }
    }
    else {
        //email blank
        document.getElementById('errorDiv').innerHTML = '*Please enter Email.';
        document.getElementById('errorDiv').style.display = '';
        document.getElementById('emailTextId').focus();
        document.getElementById('save').disabled = false;
    }
}

function callBackFunctSignUp() {
    // To make sure receiving response data from server is completed
    if (XmlHttp.readyState == 4) {
        RemoveLoadingDiv("RegisterDiv");
        // To make sure valid response is received from the server, 200 means response received is OK
        if (XmlHttp.status == 200) {
            document.getElementById('save').disabled = false;
            if (XmlHttp.responseText == 'present') {
                alert('This Moos ID is already present in database. Please enter other.');
            }
            else if (XmlHttp.responseText == 'inserted') {
                alert('Congrats!! You have been successfully signed up. \nAn activation link has been sent to your E-mail.\nPlease click the link before 24 hours to Activate your Account!!');
            }
            else {
                alert("There was a problem in signing up. Please try again.");
            }

            document.getElementById('emailTextId').value = '';
            document.getElementById('moosId').value = '';
            document.getElementById('passId').value = '';

        }
        else {
            alert("There was a problem retrieving data from the server.");
        }
    }

}

var LoadForgotPasswordWin = function () {
    OpenModalWindow("ForgotPassword.aspx?", "MOOSTOOL :: Recover Your Password", 450, 170);
}

var OpenModalWindow = function (modalUrl, modalTitle, modalWidth, modalHeight) {
    showPopWin(modalUrl + 'rand=' + Math.floor(Math.random() * 100001), modalTitle, modalWidth, modalHeight, null);
}
