﻿function flexToggle(el) {
    if (el.className == 'flexOpen') {
        el.className = 'flexShut';
    } else {
        el.className = 'flexOpen';
    }
}

function ge() {
    var ea;
    for (var i = 0; i < arguments.length; i++) {
        var e = arguments[i];
        if (typeof e == 'string')
            e = document.getElementById(e);
        if (arguments.length == 1)
            return e;
        if (!ea)
            ea = new Array();
        ea[ea.length] = e;
    }
    return ea;
}

function show() {
    for (var i = 0; i < arguments.length; i++) {
        var element = ge(arguments[i]);
        if (element && element.style) element.style.display = 'block';
    }
}

function show2() {
    for (var i = 0; i < arguments.length; i++) {
        var element = ge(arguments[i]);
        if (element && element.style) element.style.display = "inline";
    }
    return false;
}

function hide() {
    for (var i = 0; i < arguments.length; i++) {
        var element = ge(arguments[i]);
        if (element && element.style) element.style.display = 'none';
    }
}

function shown(el) {
    el = ge(el);
    return (el.style.display != 'none');
}

function shide(el) { if (shown(el)) { hide(el); } else { show(el); } }

function textLimit(ta, count) {
    var text = ge(ta);
    if (text.value.length > count) {
        text.value = text.value.substring(0, count);
        if (arguments.length > 2) { // id of an error block is defined
            ge(arguments[2]).style.display = 'block';
        }
    }
}

function isIE() {
    return (navigator.userAgent.toLowerCase().indexOf("msie") != -1);
}

function placeholderSetup(id) {
    var el = ge(id);
    if (!el) return;
    if (el.type != 'text') return;
    if (el.type != 'text') return;

    var ph = el.getAttribute("placeholder");
    if (ph && ph != "") {
        el.value = ph;
        el.style.color = '#777';
        el.is_focused = 0;
        el.onfocus = placeholderFocus;
        el.onblur = placeholderBlur;
    }
}

function placeholderFocus() {
    if (!this.is_focused) {
        this.is_focused = 1;
        this.value = '';
        this.style.color = '#000';

        var rs = this.getAttribute("radioselect");
        if (rs && rs != "") {
            var re = document.getElementById(rs);
            if (!re) { return; }
            if (re.type != 'radio') return;

            re.checked = true;
        }
    }
}

function placeholderBlur() {
    var ph = this.getAttribute("placeholder")
    if (this.is_focused && ph && this.value == "") {
        this.is_focused = 0;
        this.value = ph;
        this.style.color = '#777';
    }
}

function AddOptionToList(listToFill, text, value) {
    if (document.createElement) {
        var newlistToFillOption = document.createElement("OPTION");
        newlistToFillOption.value = value;
        newlistToFillOption.text = text;
        (listToFill.options.add) ? listToFill.options.add(newlistToFillOption) :
			listToFill.add(newlistToFillOption, null);
    }
    else {
        listToFill.options[i] = new Option(text, value, false, false);
    }
}

function ApproveMembershipRequest(userId, groupId) {
    if (window.location.href.indexOf('?') == -1) {
        window.location = location.href + "?ApproveMembershipRequest=" + userId + ":" + groupId;
    }
    else {
        window.location = location.href + "&ApproveMembershipRequest=" + userId + ":" + groupId;
    }

    return;

    //ajax version
    var btnApproveRequestMembership = $("btnAMR" + userId);

    var url = rootUrl + "/Misc/UserGroup/ApproveMembershipRequest.ashx?groupId=" + groupId + "&userId=" + userId;

    new Ajax.Request(url, {
        method: 'get',
        onSuccess: function(transport) {
            btnApproveRequestMembership.hide();
        }
    });
}

function RejectMembershipRequest(userId, groupId) {
    if (confirm('Вы уверены, что хотите отказать данному пользователю?')) {
        if (window.location.href.indexOf('?') == -1) {
            window.location = location.href + "?RejectMembershipRequest=" + userId + ":" + groupId;
        }
        else {
            window.location = location.href + "&RejectMembershipRequest=" + userId + ":" + groupId;
        }
    }

    return;
}

function InviteUserToGroup(userId, groupId) {
    var btnInvite = jQuery("#aInvite" + userId);
    var doneImage = jQuery("#iInvite" + userId);
    var preloader = jQuery("#iInvitePreloader" + userId);

    var url = "/Misc/UserGroup/UserGroupInvite.ashx?groupId=" + groupId + "&userId=" + userId;

    btnInvite.hide();
    preloader.show();

    jQuery.get(url, function () {
    	preloader.hide();
    	doneImage.show();

    	jQuery("#aInvite" + userId).parents("table").first().fadeOut("slow");
    });   
  
 }

function InviteUserToPlace(userId, placeID) {	
	var btnInvite = jQuery("#aInvite" + userId);
	var doneImage = jQuery("#iInvite" + userId);
	var preloader = jQuery("#iInvitePreloader" + userId);

	var url = "/Misc/Places/PlaceInvite.ashx?placeID=" + placeID + "&userId=" + userId;

	btnInvite.hide();
	preloader.show();

	jQuery.get(url, function () {
		preloader.hide();
		doneImage.show();

		jQuery("#aInvite" + userId).parents("table").first().fadeOut("slow");
	});   
}

function InviteUserToGroup2(userId, groupId) {
    var url = "/Misc/UserGroup/UserGroupInvite.ashx?groupId=" + groupId + "&userId=" + userId;

    jQuery.get(url, function (data) {
    	if (data.toArray()[0] == '3') {
    		alert(data.split('!')[1]);
    	}
    	else {
    		window.location.reload(true);
    	}
    }); 	

    return false;
}

function UserGroupDeleteMember(userId, groupId) {
    if (confirm('Вы уверены, что хотите исключить данного пользователя?')) {
        if (window.location.href.indexOf('?') == -1) {
            window.location = location.href + "?RejectMembershipRequest=" + userId + ":" + groupId;
        }
        else {
            window.location = location.href + "&RejectMembershipRequest=" + userId + ":" + groupId;
        }
    }

    return;
}

function LeaveGroup(userId, groupId) {

}

function JoinGroup(userId, groupId) {

}

function LeaveMeeting(userId, groupId) {
    LeaveGroup(userId, groupId);
}

function JoinMeeting(userId, groupId) {
    JoinGroup(userId, groupId);
}

// Отправляем запрос, о том, что юзверь нажал на банер.
function SetBanerClick(banerId, redirectUrl) {

    var url = rootUrl + '/Misc/Adv/SetBanerClick.ashx?banerId=' + banerId;
    // notice the use of a proxy to circumvent the Same Origin Policy.
    new Ajax.Request(url, {
        method: 'get',
        onSuccess: window.location.href = redirectUrl
    });
}
