
var request = null; // Varaible to hold Http request object

function createRequest() {
    try{
        request = new XMLHttpRequest(); // This create http request object for all the browsers except Microsoft IE
    }
    catch(trymicrosoft){
        try{
            request = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(othermicrosoft){
            try{
                request = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(failed){
                request = null;  // If something goes wrong
            }
        }
    }
    if(request == null){
        alert("Error creating request object!");
    }
}

function checkUserAge(birthDateVal){  // For checking User age that should be above and equal 18
    if(birthDateVal == "") {
        var span = document.getElementById("birthDateSpan");
        span.innerHTML = "This field cannot be left blank";
    }
    else{
        createRequest();
        var url = "AgeValidator?birthDate=" + birthDateVal;
        if(request != null){
            request.open("GET", url, true);
            request.onreadystatechange = checkUserAgeStatus;
            request.send(null);
        }
    }
}
function checkUserAgeStatus(){   // For checking User age that should be above and equal 18
    if (request.readyState == 4) {
        if (request.status == 200) {
            var response = request.responseText;
            if(response == ""){
                var span = document.getElementById("birthDateSpan");
                span.innerHTML = "";
            }
            else{                
                span = document.getElementById("birthDateSpan");
                span.innerHTML = "Warning - You need to be a minimum of 18 years of age to use this site.";
            }
        }
    }
}
/*
function updateCountrywiseState(countryOfResidenceVal){  // For showing countrywise states
    if(countryOfResidenceVal == "") {
        var span = document.getElementById("countryOfResidenceSpan");
        span.innerHTML = "This field cannot be left blank";
    }
    else{
        createRequest();
        var url = "CountryWiseStates?countryOfResidence=" + countryOfResidenceVal;
        if(request != null){
            request.open("GET", url, true);
            request.onreadystatechange = handleStateChange;
            request.send(null);
        }
    }
}
function handleStateChange() {
    if(request.readyState == 4) {
        if(request.status == 200) {
            parseResults();
        }
    }
}
function parseResults() {
    var responseText = document.createTextNode(request.responseText);
    var returnElements=request.responseText.split("||");
    //Process each of the elements
    for ( var i=0; i<returnElements.length; i++ ){

        if(returnElements[i]!="")
        {
            valueLabelPair = returnElements[i].split(";")

            if(valueLabelPair[1] != "noState"){
                document.getElementById("registrationForm:state").style.display="block";
                document.getElementById("otherStateDiv").style.display="none";
                document.getElementById('registrationForm:state').options.length= returnElements.length;
                document.getElementById('registrationForm:state').options[i] = new Option(valueLabelPair[1], valueLabelPair[0]);
            }
            else{
                document.getElementById("registrationForm:state").style.display="none";
                document.getElementById("registrationForm:otherStateDiv").style.display="block";
            //document.getElementById("registrationForm:otherStateDiv").style.display="block";
            }
        }
    }
}
 */

function checkProfileIDAlreadyExist(profileIDval){  // For Unique profile ID
    if(profileIDval == "") {
        var span = document.getElementById("profileIdSpan");
        span.innerHTML = "Please enter Profile ID";
    }
    else{
        createRequest();       
        var url = "UniqueProfileIdValidator?profileId=" + profileIDval;
        if(request != null){
            request.open("GET", url, true);
            request.onreadystatechange = checkProfileIdStatus;
            request.send(null);
        }
    }
}
function checkProfileIdStatus(){   // For Unique profile ID
    if (request.readyState == 4) {
        if (request.status == 200) {
            var response = request.responseText;
            if(response == ""){
                var span = document.getElementById("profileIdSpan");
                span.innerHTML = "";
            }
            else{
                var porifleIdValue = document.getElementById("registrationForm:profileId").value;
                span = document.getElementById("profileIdSpan");
                span.innerHTML = "Sorry, the Profile ID " + porifleIdValue + " already exists, please choose another.";
            }
        }
    }
}
/*
function checkProfileIDCharacter(profileIDval){  // For checking character in profile ID

    var url = "ProfileIDValidator?profileId=" + profileIDval;
    if(request != null){
        request.open("GET", url, true);
        request.onreadystatechange = checkProfileIdCharacterStatus;
        request.send(null);
    }

}

function checkProfileIdCharacterStatus(){   // For checking character in profile ID
    if (request.readyState == 4) {
        if (request.status == 200) {
            var response = request.responseText;
            if(response == ""){
                var span = document.getElementById("profileIdSpan");
                span.innerHTML = "";
            }
            else{
                var porifleIdValue = document.getElementById("registrationForm:profileId").value;
                span = document.getElementById("profileIdSpan");
                span.innerHTML = "The value entered for " + porifleIdValue + " is invalid, it can only contain letters and/or numbers (A-Z,a-z,0-9,'_').";
            }
        }
    }
}
 */

function checkEmailIDAlreadyExist(emailIDval){  // For Unique email ID
    if(emailIDval == "") {
        var span = document.getElementById("emailAddressSpan");
        span.innerHTML = "Please enter email address";
    }
    else{
        createRequest();
        var url = "UniqueEmailAddressValidator?emailId=" + emailIDval;
        if(request != null){
            request.open("GET", url, true);
            request.onreadystatechange = checkEmailIdStatus;
            request.send(null);
        }
    }
}
function checkEmailIdStatus(){   // For Unique email ID
    if (request.readyState == 4) {
        if (request.status == 200) {
            var response = request.responseText;
            if(response == ""){
                var span = document.getElementById("emailAddressSpan");
                span.innerHTML = "";
            }
            else{               
                if(document.getElementById("registrationForm:emailAddress") == null){
                    var emailIdValue = document.getElementById("saathiRegisterForm:emailAddress").value;
                }
                else{
                    var emailIdValue = document.getElementById("registrationForm:emailAddress").value;
                }
                span = document.getElementById("emailAddressSpan");
                span.innerHTML = "The e-mail address " + emailIdValue + " has already been registered, please check and try again.";
            }
        }
    }
}

function confimEmailIds(emailIDval){  // For Confirm email address
    if(emailIDval == "") {
        var span = document.getElementById("confirmEmailAddressSpan");
        span.innerHTML = "Please re-enter email address";
    }
    else{
        var emailIdValue = document.getElementById("registrationForm:emailAddress").value;
        var confirmEmailIdValue = document.getElementById("registrationForm:confirmEmailAddress").value;
       
        if (emailIdValue != confirmEmailIdValue){
            span = document.getElementById("confirmEmailAddressSpan");
            span.innerHTML = "The two e-mail addresses don't match";
        }
        else{
            span = document.getElementById("confirmEmailAddressSpan");
            span.innerHTML = "";
        }
    }
}
function checkPassword(passwordVal){  // For password
    if(passwordVal == "") {
        var span = document.getElementById("passwordSpan");
        span.innerHTML = "This field cannot be left blank";
    }
    else{
        span = document.getElementById("passwordSpan");
        span.innerHTML = "";
    }
}
function confimPasswords(passwordVal){  // For Confirm passwords
    if(passwordVal == "") {
        var span = document.getElementById("reEnterPasswordSpan");
        span.innerHTML = "This field cannot be left blank";
    }
    else{
        var passwordValue = document.getElementById("registrationForm:password").value;
        var confirmPasswordValue = document.getElementById("registrationForm:reEnterPassword").value;

        if (passwordValue != confirmPasswordValue){
            span = document.getElementById("reEnterPasswordSpan");
            span.innerHTML = "The values in the Password and confirm Password fields don't match ";
        }
        else{
            span = document.getElementById("reEnterPasswordSpan");
            span.innerHTML = "";
        }
    }
}
function checkFirstName(firstNameVal){  // For First Name
    if(firstNameVal == "") {
        var span = document.getElementById("firstNameSpan");
        span.innerHTML = "This field cannot be left blank";
    }
    else{
        span = document.getElementById("firstNameSpan");
        span.innerHTML = "";
    }
}
function checkLastName(lastNameVal){  // For First Name
    if(lastNameVal == "") {
        var span = document.getElementById("lastNameSpan");
        span.innerHTML = "This field cannot be left blank";
    }
    else{
        span = document.getElementById("lastNameSpan");
        span.innerHTML = "";
    }
}
function checkOtherState(otherStateVal){  // For First Name
    if(otherStateVal == "") {
        var span = document.getElementById("otherStateSpan");
        span.innerHTML = "This field cannot be left blank";
    }
    else{
        span = document.getElementById("otherStateSpan");
        span.innerHTML = "";
    }
}
function checkCity(cityVal){  // For First Name
    if(cityVal == "") {
        var span = document.getElementById("citySpan");
        span.innerHTML = "This field cannot be left blank";
    }
    else{
        span = document.getElementById("citySpan");
        span.innerHTML = "";
    }
}
function checkReligion(value){  // For Religion
    if(value == "") {
        var span = document.getElementById("religionSpan");
        span.innerHTML = "Please select atleast one";
    }
    else{
        span = document.getElementById("religionSpan");
        span.innerHTML = "";
    }
}
function checkState(value){  // For State
    if(value == "") {
        var span = document.getElementById("stateSpan");
        span.innerHTML = "Please select atleast one";
    }
    else{
        span = document.getElementById("stateSpan");
        span.innerHTML = "";
    }
}

function checkCaste(value){  // For State
    if(value == "") {
        var span = document.getElementById("casteSpan");
        span.innerHTML = "Please select atleast one";
    }
    else{
        span = document.getElementById("casteSpan");
        span.innerHTML = "";
    }
}
function checkMotherTongue(value){  // For Mother Tongue
    if(value == "") {
        var span = document.getElementById("motherTongueSpan");
        span.innerHTML = "Please select atleast one";
    }
    else{
        span = document.getElementById("motherTongueSpan");
        span.innerHTML = "";
    }
}

function checkHeight(value){  // For height
    if(value == "") {
        var span = document.getElementById("heightSpan");
        span.innerHTML = "Please select atleast one";
    }
    else{
        span = document.getElementById("heightSpan");
        span.innerHTML = "";
    }
}
function checkEducation(value){  // For Education
    if(value == "") {
        var span = document.getElementById("educationSpan");
        span.innerHTML = "Please select atleast one";
    }
    else{
        span = document.getElementById("educationSpan");
        span.innerHTML = "";
    }
}
function checkSalary(value){  // For Salary
    if(value == "") {
        var span = document.getElementById("salarySpan");
        span.innerHTML = "Please select atleast one";
    }
    else{
        span = document.getElementById("salarySpan");
        span.innerHTML = "";
    }
}
function checkProfession(value){  // For Mother Tongue
    if(value == "") {
        var span = document.getElementById("professionSpan");
        span.innerHTML = "Please select atleast one";
    }
    else{
        span = document.getElementById("professionSpan");
        span.innerHTML = "";
    }
}
function checkVerificationCode(value){  // For Mother Tongue
    if(value == "") {
        var span = document.getElementById("verificationCodeSpan");
        span.innerHTML = "This field cannot be left blank";
    }
    else{
        span = document.getElementById("verificationCodeSpan");
        span.innerHTML = "";
    }
}
function checkAboutUserWord1(value){  // For Mother Tongue
    if(value == "") {
        var span = document.getElementById("aboutUserWord1Span");
        span.innerHTML = "Please select atleast one";
    }
    else{
        span = document.getElementById("aboutUserWord1Span");
        span.innerHTML = "";
    }
}
function checkAboutUserWord2(value){  // For Mother Tongue
    if(value == "") {
        var span = document.getElementById("aboutUserWord2Span");
        span.innerHTML = "Please select atleast one";
    }
    else{
        span = document.getElementById("aboutUserWord2Span");
        span.innerHTML = "";
    }
}
function checkAboutUserWord3(value){  // For Mother Tongue
    if(value == "") {
        var span = document.getElementById("aboutUserWord3Span");
        span.innerHTML = "Please select atleast one";
    }
    else{
        span = document.getElementById("aboutUserWord3Span");
        span.innerHTML = "";
    }
}

