﻿
var isAuth = false;
var timeoutID;
function StartSessionTimeoutCount() {
    if (isAuth) {
        var mseconds = (60*60-40) * 1000;
        timeoutID = window.setTimeout(function() { $("#dialog").dialog("open"); CountTimeout(); }, mseconds);
    }
}


var timeoutID2;
function CountTimeout() {
    var MaxSenconds = 30;
    var currentSencond = MaxSenconds;
    var elem = $("#KeepSessionTimeCounter");
    elem.text(currentSencond);
    timeoutID2 = setInterval(
    function() {
    if (currentSencond > 0) elem.text(--currentSencond);
        else {
            clearInterval(timeoutID2);
            currentSencond = MaxSenconds;
            window.location.href = $("#WebSiteURL").val() + "Probe.aspx?action=logout";
        }
    },
     1000);
}

function ResetSessionTimeoutCount() {
    if (timeoutID) clearTimeout(timeoutID);
    if (timeoutID2) clearInterval(timeoutID2);
    StartSessionTimeoutCount();
}
function SetAuth() {
    isAuth = true;
}



//  register for our events
Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(initializeRequest);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequest);
var postBackElementDisabled = false;
function initializeRequest(sender, args) {
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    if (prm.get_isInAsyncPostBack()) {
        alert("Still working on previous request,please wait!");
        args.set_cancel(true);
        return;
    }
    else {
        if (typeof (args.get_postBackElement().disabled) == "undefined") {
            args.get_postBackElement().href = "javascript:;";
            args.get_postBackElement().style.color = "Gray";
        }
        else {
            args.get_postBackElement().disabled = "disabled";
        }
    }
    /*if (postBackElementDisabled) {
    //$get(args.get_postBackElement().id).disabled = 'disabled';
    args.get_postBackElement.disabled = true;
    }*/


}

function endRequest(sender, args) {
    //$get(args.get_postBackElement().id).disabled = '';
    postBackElementDisabled = false;


    ResetSessionTimeoutCount();
}

function register() {
    postBackElementDisabled = true;
}



// author:Andy Ding
//check phone
function checkPhone(phone, number) {
    // regx to valid the phone whether is valid.          
    var regx = '^[\\d\\(\\)\\- ]{0,20}$';
    if (phone.toString().match(regx)) {
        // the reg to replace all the (,) - 
        var reg = new RegExp("[\\(\\)\\- ]{0,20}", "g");
        phone = phone.toString().replace(reg, '');

        // GET THE USER'S COUNTRY
        var country = '';
        var id = '';
        switch (number) {
            case 1: country = $get('ctl00_PrimaryContentPlaceHolder_hfPhoneNo').value;
                id = 'ctl00_PrimaryContentPlaceHolder_txtPhone';
                break;
            case 2: if (document.getElementById('ctl00_PrimaryContentPlaceHolder_EditProfilePanel_hfPhoneNo') != null) {
                    country = $get('ctl00_PrimaryContentPlaceHolder_EditProfilePanel_hfPhoneNo').value;
                    id = 'ctl00_PrimaryContentPlaceHolder_EditProfilePanel_txtPhone';
                } else if (document.getElementById('ctl00_PrimaryContentPlaceHolder_UserProfilePanelRegular_EditProfilePanel_hfPhoneNo') != null) {
                    country = $get('ctl00_PrimaryContentPlaceHolder_UserProfilePanelRegular_EditProfilePanel_hfPhoneNo').value;
                    id = 'ctl00_PrimaryContentPlaceHolder_UserProfilePanelRegular_EditProfilePanel_txtPhone';
                } else if (document.getElementById('ctl00_PrimaryContentPlaceHolder_UserProfilePanelAdmin_EditProfilePanel_hfPhoneNo') != null) {
                    country = $get('ctl00_PrimaryContentPlaceHolder_UserProfilePanelAdmin_EditProfilePanel_hfPhoneNo').value;
                    id = 'ctl00_PrimaryContentPlaceHolder_UserProfilePanelAdmin_EditProfilePanel_txtPhone';
                }
                break;
            default:
                country = $get('ctl00_PrimaryContentPlaceHolder_hfPhoneNo').value;
                id = 'ctl00_PrimaryContentPlaceHolder_txtPhone';
        }
        // if country is US/CANADA
        if (country == 'United States' || country == 'Canada') {
            if (phone.toString().length != 11 && phone.toString().length != 10) {
                alert('Please input a valid phone number!');
                return;
            }
            // the phone's length is 11
            if (phone.toString().length == 11 && phone.charAt(0) == '1') {
                phone = phone.toString().substring(1);
            } //the phone's length is 10
            else if (phone.toString().length == 10 && phone.charAt(0) != '0') {
                // do nothing
            }
            else {
                alert('Please input a valid phone number!');
                return;
            }

            var phone1 = phone.toString().replace(phone, '(' + phone.substring(0, 3) + ') ' + phone.substring(3, 6) + '-' + phone.substring(6, 10));

            $get(id).value = phone1;
        }
    }
    else {
        alert('Please input a valid phone number!');
    }
    return;
}

function IsPicture(url) {
    var re = /.jpg|.jpeg|.png|.gif/g;
    var r;
    r = url.toLowerCase().search(re);
    return (r>-1);
}
