﻿$(document).ready(function () {

    $("a[rel=example_group]").fancybox({
        'transitionIn': 'none',
        'transitionOut': 'none',
        'titlePosition': 'over',
        'titleFormat': function (title, currentArray, currentIndex, currentOpts) {
            return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
        }
    });

    $("#feeds").load("feedreader.asp");

    /* Property Hover */
    $('#propertyOptions').hover(function () {
        $(this).find('ul').show();
    }, function () {
        $(this).find('ul').hide();
    })
    /* Advice Hover */
    $('#adviceOptions').hover(function () {
        $(this).find('ul').show();
    }, function () {
        $(this).find('ul').hide();
    })
    /* populate list of countries and source of the single error on the page */
    $.ajax({
        url: "countries.ashx",
        dataType: 'json',
        success: function (data) {
            for (var i = 0; i < data.length; i++) {
                $('#countrylist').append('<option value="' + data[i].code + '"> ' + data[i].name + ' </option>');
            }
        }
    });
    /* Login Functionality */
    $('#loginBtn').click(function () {
        if ($('#pword').css('display') != 'none') {
            if (echeck($('#email').val())) {
                $(this).attr('disabled', 'disabled');
                /* Test if Account Exists */
                $.ajax({
                    url: "register.ashx",
                    data: {
                        func: "login",
                        email: "" + $('#email').val() + "",
                        pword: "" + $('#pword input').val() + ""
                    },
                    success: function (data) {
                        if (data == "False") {
                            $('#loginBtn').removeAttr("disabled");
                            alert('Invalid Username or Password');
                        } else {
                            window.location.href = window.location.href; // Reload Page With New Session("LOGIN") Set
                        }
                    }
                });
            }
        } else {
            $('#pword').slideDown('slow');
        }
    });

    /* Registration  Functionality */
    $('#registerBtn').click(function () {
        if (echeck($('#email').val())) {
            $(this).attr('disabled', 'disabled');
            /* First Ensure that Email is not currently in use */
            $.ajax({
                url: "register.ashx",
                data: {
                    func: "testEmail",
                    email: "" + $('#email').val() + ""
                },
                success: function (data) {
                    if (data == "True") {
                        /* Reveal Form on Success */
                        $('#regforum').css('left', ($(window).width() - $('#regforum').width()) / 2)
                        $('#regforum span').html($('#email').val());
                        $('#regforum').show('fast');
                        $('#regforum input:button').click(function () {
                            switch ($(this).val()) {
                                /* Read Client Side Data */ 
                                case "Register Account":
                                    $(this).attr('disabled', 'disabled');
                                    if ($('#fname').val() == '' || $('#lname').val() == '' || $('#pass1').val() == '' || $('#countrylist').val() == 'no' || $('#inTel').val() == '') {
                                        alert('Please fill out blue required fields.');
                                        $(this).removeAttr("disabled");
                                    } else {
                                        if ($('#pass1').val() == $('#pass2').val()) {
                                            var output = new Array(); var i = 0;
                                            output[0] = $('#regforum span').html();
                                            $('#regforum th').each(function () {
                                                if ($(this).next().children().val() != "") {
                                                    if ($(this).next().children().val() == "M") {
                                                        output[++i] = $('#regforum input:radio:checked').val();
                                                    } else {
                                                        output[++i] = $(this).next().children().val();
                                                    }
                                                }
                                            });
                                            /* Post Data to Server */
                                            $.ajax({
                                                url: "register.ashx",
                                                data: {
                                                    func: "register",
                                                    email: "" + output[0] + "",
                                                    fname: "" + output[1] + "",
                                                    lname: "" + output[2] + "",
                                                    add1: "" + output[4] + "",
                                                    add2: "" + output[5] + "",
                                                    city: "" + output[6] + "",
                                                    country: "" + output[7] + "",
                                                    nationality: "" + output[8] + "",
                                                    sex: "" + output[9] + "",
                                                    budget: "" + output[10] + "",
                                                    pcode: "" + output[11] + "",
                                                    tel: "" + output[12] + "",
                                                    fax: "" + output[13] + "",
                                                    mob: "" + output[14] + "",
                                                    pobox: "" + output[15] + "",
                                                    pword: "" + $('#pass1').val() + ""
                                                },
                                                success: function (data) {
                                                    if (data == "False") {
                                                        alert("Error Entering Information Into Database.");
                                                    } else if (data == "True") {
                                                        alert("Registration Complete, Email Sent. Please Check Your Junk Mail");
                                                        $('#regforum').hide();/*
                                                        $.ajax({
                                                            url: "sendMail.ashx",
                                                            data: {
                                                                firstname: "" + output[1] + "",
                                                                email: "" + output[0] + ""

                                                            },
                                                            success: function (data) {
                                                                if (data == "true") {
                                                                    alert("Registration Complete, Email Sent. Please Check Your Junk Mail");
                                                                    $('#regforum').hide();
                                                                }
                                                                else {
                                                                    alert("Registration Complete, But there was an error in sending email.");
                                                                    $('#regforum').hide();
                                                                }
                                                            }
                                                        })*/

                                                    } else {
                                                        //alert("Failed To Send Confermationary Email.");
                                                        alert("Failed to Register. Please try again later.");
                                                    }
                                                }
                                            });
                                        } else { alert("Passwords Do Not Match"); $(this).removeAttr("disabled"); }
                                    }
                                    break;
                                case "Cancel Registration":
                                    $('#regforum').hide();
                                    $('#registerBtn').removeAttr("disabled");
                                    break;
                                default: break;
                            }
                        });
                    } else {
                        $('#registerBtn').removeAttr("disabled");
                        alert('Email Address already in-use.');
                    }
                }
            });
        }
    });

});

/* Email Validation */
function echeck(str) {

    var at = "@"
    var dot = "."
    var lat = str.indexOf(at)
    var lstr = str.length
    var ldot = str.indexOf(dot)
    if (str.indexOf(at) == -1) {
        alert("Invalid E-mail ID")
        return false
    }

    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
        alert("Invalid E-mail ID")
        return false
    }

    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
        alert("Invalid E-mail ID")
        return false
    }

    if (str.indexOf(at, (lat + 1)) != -1) {
        alert("Invalid E-mail ID")
        return false
    }

    if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
        alert("Invalid E-mail ID")
        return false
    }

    if (str.indexOf(dot, (lat + 2)) == -1) {
        alert("Invalid E-mail ID")
        return false
    }

    if (str.indexOf(" ") != -1) {
        alert("Invalid E-mail ID")
        return false
    }

    return true
}

function lockbutton(v) {
    v.disabled = true;
    v.value = 'Please wait...';
}
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, "");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/, "");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/, "");
}
function changeInputType(oldObject, oType) {
    var newObject = document.createElement('input');
    newObject.type = oType;
    if (oldObject.style) newObject.style = oldObject.style;
    if (oldObject.size) newObject.size = oldObject.size;
    if (oldObject.value) newObject.value = oldObject.value;
    if (oldObject.name) newObject.name = oldObject.name;
    if (oldObject.id) newObject.id = oldObject.id;
    if (oldObject.className) newObject.className = oldObject.className;
    newObject.style.background = 'red';
    oldObject.parentNode.replaceChild(newObject, oldObject);
    return newObject;
}

