String.prototype.trim = function() {

    return this.replace(/^\s+/, '').replace(/\s+$/, '');

}

function stub() {

}



function commonGet(id) {

    return document.getElementById(id);

}



function commonValidId(id) {

    return (id && commonGet(id));

}



function commonShow(id) {

    if (commonValidId(id)) {

        commonGet(id).style.display = 'block';

    }

}



function commonHide(id) {

    if (commonValidId(id)) {

        commonGet(id).style.display = 'none';

    }

}



function commonProcessFieldError(fieldName, errorId) {

    for (var i = 0; i < 10; i++) {

        commonHide(fieldName + '_error_' + i);

    }

    commonShow(fieldName + '_' + errorId);

    return (errorId == null);

}



function commonValidateRequired(form, fieldName, errorCode) {

    if (!form[fieldName]) {

        return true;

    }

    if (form[fieldName].value.trim().length == 0) {

        return commonProcessFieldError(fieldName, errorCode);

    } else {

        return commonProcessFieldError(fieldName, null);

    }

}



function commonValidateMinLength(form, fieldName, length, errorCode) {

    if (!form[fieldName]) {

        return true;

    }

    if (form[fieldName].value.trim().length < length) {

        return commonProcessFieldError(fieldName, errorCode);

    } else {

        return commonProcessFieldError(fieldName, null);

    }

}



function commonValidatePasswords(form, fieldName1, fieldName2, errorCode) {

    if (!form[fieldName1] || !form[fieldName2]) {

        return true;

    }

    if (form[fieldName1].value != form[fieldName2].value) {

        return commonProcessFieldError(fieldName2, errorCode);

    } else {

        return commonProcessFieldError(fieldName2, null);

    }

}



function commonValidateEmail(form, fieldName, errorCode) {

    if (!form[fieldName]) {

        return true;

    }

    if (form[fieldName].value.trim().length == 0) {

        return commonProcessFieldError(fieldName, null);

    }

    var email = /^\w[\w\-]*(\.\w[\w\-]*)*@\w[\w\-]+(\.\w[\w\-]+)*\.(a[c-gil-oq-uwz]|b[a-bd-jm-or-tvwyz]|c[acdf-ik-orsuvx-z]|d[ejkmoz]|e[ceghr-u]|f[i-kmorx]|g[abd-ilmnp-uwy]|h[kmnrtu]|i[delm-oq-t]|j[emop]|k[eg-imnprwyz]|l[a-cikr-vy]|m[acdghk-z]|n[ace-giloprtuz]|om|p[ae-hk-nrtwy]|qa|r[eouw]|s[a-eg-ort-vyz]|t[cdf-hjkm-prtvwz]|u[agkmsyz]|v[aceginu]|w[fs]|y[etu]|z[admrw]|com|edu|net|org|mil|gov|biz|pro|aero|coop|info|name|int|museum)$/;

    if (!email.test(form[fieldName].value)) {

        return commonProcessFieldError(fieldName, errorCode);

    } else {

        return commonProcessFieldError(fieldName, null);

    }

}



function commonValidateFileExt(form, fieldName, ext, errorCode) {

    if (!form[fieldName]) {

        return true;

    }

    if (form[fieldName].value.trim().length == 0) {

        return commonProcessFieldError(fieldName, null);

    }

    if (!form[fieldName].value.match(new RegExp('[.]' + ext + '$', 'gi'))) {

        return commonProcessFieldError(fieldName, errorCode);

    } else {

        return commonProcessFieldError(fieldName, null);

    }

}

function commonGetAjaxParams() {

    return 'mode=async&rand=' + new Date().getTime();

}

function commonSendRequest(url, data, isPost, callback) {

    var req = null;

    if (window.XMLHttpRequest) {

        req = new XMLHttpRequest();

    } else if (window.ActiveXObject) {

        req = new ActiveXObject('Microsoft.XMLHTTP');

    }

    if (!req) {

        return null;

    }

    try {

        var postData = null;

        var method = null;

        if (isPost) {

            method = 'POST';

            postData = data;

        } else if (!data) {

            method = 'GET';

        } else if (data.length > 0) {

            method = 'GET';

            if (url.indexOf('?') >= 0) {

                url += '&' + data;

            } else {

                url += '?' + data;

            }

        }

        req.open(method, encodeURI(url), true);

        if (method == 'POST') {

            req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

        }

        req.onreadystatechange = function() {

            if (req.readyState == 4) {

                var xml = req.responseXML;

                if (xml && xml.documentElement) {

                    if (xml.documentElement.nodeName == 'success') {

                        callback(xml.documentElement, null);

                    } else if (xml.documentElement.nodeName == 'failure') {

                        var childs = xml.documentElement.childNodes;

                        var errors = [];

                        for (var i = 0; i < childs.length; i++) {

                            if (childs[i].nodeName == 'error') {

                                var content = null;

                                if (childs[i].textContent) {

                                    content = childs[i].textContent;

                                } else if (childs[i].innerText) {

                                    content = childs[i].innerText;

                                } else if (childs[i].text) {

                                    content = childs[i].text;

                                }

                                errors.push(content);

                            }

                        }

                        callback(null, errors);

                    }

                }

            }

        };

        req.send(postData);

    } catch (e) {

        return e;

    }

    return null;

}




// member profile



var memberProfileEditAdditionalVisible = false;



function memberProfileEditEnableFormProfile(params) {

    var form = params['form_id'];

    var addLink = params['additional_link_id'];

    var addBlock = params['additional_block_id'];



    if (commonValidId(addLink)) {

        commonGet(addLink).onclick = function() {

            if (memberProfileEditAdditionalVisible) {

                commonHide(addBlock);

            } else {

                commonShow(addBlock);

            }

            var st1 = (memberProfileEditAdditionalVisible ? 'collapse_link' : 'expand_link');

            var st2 = (!memberProfileEditAdditionalVisible ? 'collapse_link' : 'expand_link');

            memberProfileEditAdditionalVisible = !memberProfileEditAdditionalVisible;

            if (this.className.indexOf(st1) >= 0) {

                this.className = this.className.replace(st1, st2);

            }

            return false;

        };

    }



    if (commonValidId(form)) {

        commonGet(form).onsubmit = function() {

            var errorField = null;

            if (!commonValidateRequired(this, 'display_name', 'error_1')) {

                errorField = (errorField ? errorField : 'display_name');

            } else if (!commonValidateMinLength(this, 'display_name', 3, 'error_2')) {

                errorField = (errorField ? errorField : 'display_name');

            }

            if (!commonValidateFileExt(this, 'avatar', 'jpg', 'error_1')) {

                errorField = (errorField ? errorField : 'avatar');

            }

            var day = this['birth_date_Day'].selectedIndex;

            var month = this['birth_date_Month'].selectedIndex;

            var year = this['birth_date_Year'].selectedIndex;

            var sum = day + month + year;

            if (sum > 0) {

                if ((day == 0) || (month == 0) || (year == 0)) {

                    commonProcessFieldError('birth_date', 'error_1');

                    errorField = (errorField ? errorField : 'birth_date_Day');

                }

            }

            if (errorField) {

                this[errorField].focus();

            }

            return !errorField;

        };

    }

}



function memberProfileEditEnableFormPassword(params) {

    var form = params['form_id'];



    if (commonValidId(form)) {

        commonGet(form).onsubmit = function() {

            var errorField = null;

            if (!commonValidateRequired(this, 'old_pass', 'error_1')) {

                errorField = (errorField ? errorField : 'old_pass');

            }

            if (!commonValidateRequired(this, 'pass', 'error_1')) {

                errorField = (errorField ? errorField : 'pass');

            } else if (!commonValidateMinLength(this, 'pass', 5, 'error_2')) {

                errorField = (errorField ? errorField : 'pass');

            }

            if (!commonValidateRequired(this, 'pass2', 'error_1')) {

                errorField = (errorField ? errorField : 'pass2');

            } else if (!commonValidatePasswords(this, 'pass', 'pass2', 'error_2')) {

                errorField = (errorField ? errorField : 'pass2');

            }

            if (errorField) {

                this[errorField].focus();

            }

            return !errorField;

        };

    }

}



function memberProfileEditEnableFormEmail(params) {

    var form = params['form_id'];



    if (commonValidId(form)) {

        commonGet(form).onsubmit = function() {

            var errorField = null;

            if (!commonValidateRequired(this, 'email', 'error_1')) {

                errorField = (errorField ? errorField : 'email');

            } else if (!commonValidateEmail(this, 'email', 'error_2')) {

                errorField = (errorField ? errorField : 'email');

            }

            if (errorField) {

                this[errorField].focus();

            }

            return !errorField;

        };

    }

}

var memberProfileViewAddToFriendStorage = null;

var memberProfileViewSendMessageStorage = null;



function memberProfileViewTriggerBlock(storage, hide) {

    if (storage['visible'] || hide) {

        commonHide(storage['blockId']);

    } else {

        commonShow(storage['blockId']);

    }

    var st1 = (storage['visible'] ? 'collapse_link' : 'expand_link');

    var st2 = (!storage['visible'] && !hide ? 'collapse_link' : 'expand_link');

    if (hide) {

        storage['visible'] = false;

    } else {

        storage['visible'] = !storage['visible'];

    }

    var link = commonGet(storage['linkId']);

    if (link.className.indexOf(st1) >= 0) {

        link.className = link.className.replace(st1, st2);

    }

}



function memberProfileViewEnableAddToFriend(params) {

    var atfLink = params['link_id'];

    var atfBlock = params['block_id'];

    memberProfileViewAddToFriendStorage = {visible: false, linkId: atfLink, blockId: atfBlock};



    if (commonValidId(atfLink)) {

        commonGet(atfLink).onclick = function() {

            if (memberProfileViewSendMessageStorage) {

                memberProfileViewTriggerBlock(memberProfileViewSendMessageStorage, true);

            }

            if (memberProfileViewGrantPointsStorage) {

                memberProfileViewTriggerBlock(memberProfileViewGrantPointsStorage, true);

            }

            memberProfileViewTriggerBlock(memberProfileViewAddToFriendStorage, false);

            return false;

        };

    }

}



function memberProfileViewEnableSendMessage(params) {

    var form = params['form_id'];

    var smLink = params['link_id'];

    var smBlock = params['block_id'];

    memberProfileViewSendMessageStorage = {visible: false, linkId: smLink, blockId: smBlock};



    if (commonValidId(smLink)) {

        commonGet(smLink).onclick = function() {

            if (memberProfileViewAddToFriendStorage) {

                memberProfileViewTriggerBlock(memberProfileViewAddToFriendStorage, true);

            }

            if (memberProfileViewGrantPointsStorage) {

                memberProfileViewTriggerBlock(memberProfileViewGrantPointsStorage, true);

            }

            memberProfileViewTriggerBlock(memberProfileViewSendMessageStorage, false);

            return false;

        };

    }



    if (commonValidId(form)) {

        commonGet(form).onsubmit = function() {

            var errorField = null;

            if (!commonValidateRequired(this, 'message', 'error_1')) {

                errorField = (errorField ? errorField : 'message');

            }

            if (errorField) {

                this[errorField].focus();

            }

            return !errorField;

        };

    }

}



function memberProfileViewEnableGrantPoints(params) {

    var form = params['form_id'];

    var idLink = params['link_id'];

    var idBlock = params['block_id'];

    memberProfileViewGrantPointsStorage = {visible: false, linkId: idLink, blockId: idBlock};



    if (commonValidId(idLink)) {

        commonGet(idLink).onclick = function() {

            if (memberProfileViewAddToFriendStorage) {

                memberProfileViewTriggerBlock(memberProfileViewAddToFriendStorage, true);

            }

            if (memberProfileViewSendMessageStorage) {

                memberProfileViewTriggerBlock(memberProfileViewSendMessageStorage, true);

            }

            memberProfileViewTriggerBlock(memberProfileViewGrantPointsStorage, false);

            return false;

        };

    }



    if (commonValidId(form)) {

        commonGet(form).onsubmit = function() {

            var errorField = null;

            if (!commonValidateRequired(this, 'points_amount', 'error_1')) {

                errorField = (errorField ? errorField : 'points_amount');

            }

            if (errorField) {

                this[errorField].focus();

            }

            return !errorField;

        };

    }

}



function memberProfileViewEnableDeleteConfirm(params) {

    var form = params['form_id'];

    var confirmationText = params['confirmation_text'];

    if (commonValidId(form)) {

        commonGet(form).onsubmit = function() {

            if (confirmationText) {

                return confirm(confirmationText);

            }

        };

    }

}



// list videos



function listVideosEnableDeleteForm(params) {

    var form = params['form_id'];

    var confirmationText = params['delete_confirmation_text'];

    var noSelectedText = params['no_items_selected'];



    if (commonValidId(form)) {

        commonGet(form).onsubmit = function() {

            var selectedCount = 0;

            for (var i = 0; i < this.elements.length; i++) {

                var field = this.elements[i];

                if ((field.type == 'checkbox') && (field.checked)) {

                    selectedCount++;

                }

            }

            if (selectedCount == 0) {

                alert(noSelectedText);

                return false;

            } else if (confirmationText) {

                return confirm(confirmationText);

            }

            return true;

        };

    }

}



//  list photos



function listAlbumsEnableDeleteForm(params) {

    var form = params['form_id'];

    var confirmationText = params['delete_confirmation_text'];

    var noSelectedText = params['no_items_selected'];



    if (commonValidId(form)) {

        commonGet(form).onsubmit = function() {

            var selectedCount = 0;

            for (var i = 0; i < this.elements.length; i++) {

                var field = this.elements[i];

                if ((field.type == 'checkbox') && (field.checked)) {

                    selectedCount++;

                }

            }

            if (selectedCount == 0) {

                alert(noSelectedText);

                return false;

            } else if (confirmationText) {

                return confirm(confirmationText);

            }

            return true;

        };

    }

}



// list messages



function listMessagesEnableFriends(params) {

    var confirmButton = params['confirm_button_id'];

    var rejectButton = params['reject_button_id'];

    var messageFrom = params['message_from_user_id'];



    if (commonValidId(confirmButton)) {

        commonGet(confirmButton).onclick = function() {

            window.location = '?action=confirm_add_to_friends&message_from_user_id=' + messageFrom;

        };

    }

    if (commonValidId(rejectButton)) {

        commonGet(rejectButton).onclick = function() {

            window.location = '?action=reject_add_to_friends&message_from_user_id=' + messageFrom;

        };

    }

}



function listMessagesEnableDeleteForm(params) {

    var form = params['form_id'];

    var confirmationText = params['delete_confirmation_text'];

    var noSelectedText = params['no_items_selected'];



    if (commonValidId(form)) {

        commonGet(form).onsubmit = function() {

            var selectedCount = 0;

            for (var i = 0; i < this.elements.length; i++) {

                var field = this.elements[i];

                if ((field.type == 'checkbox') && (field.checked)) {

                    selectedCount++;

                }

            }

            if (selectedCount == 0) {

                alert(noSelectedText);

                return false;

            } else if (confirmationText) {

                return confirm(confirmationText);

            }

            return true;

        };

    }

}



// message details



function messageDetailsEnableSendMessage(params) {

    var form = params['form_id'];



    if (commonValidId(form)) {

        commonGet(form).onsubmit = function() {

            var errorField = null;

            if (!commonValidateRequired(this, 'message', 'error_1')) {

                errorField = (errorField ? errorField : 'message');

            }

            if (errorField) {

                this[errorField].focus();

            }

            return !errorField;

        };

    }

}



// members blog



function listMembersBlogEnableDeleteForm(params) {

    var form = params['form_id'];

    var confirmationText = params['delete_confirmation_text'];

    var noSelectedText = params['no_items_selected'];



    if (commonValidId(form)) {

        commonGet(form).onsubmit = function() {

            var selectedCount = 0;

            for (var i = 0; i < this.elements.length; i++) {

                var field = this.elements[i];

                if ((field.type == 'checkbox') && (field.checked)) {

                    selectedCount++;

                }

            }

            if (selectedCount == 0) {

                alert(noSelectedText);

                return false;

            } else if (confirmationText) {

                return confirm(confirmationText);

            }

            return true;

        };

    }

}


function listMembersBlogEnableAddEntry(params) {

    var form = params['form_id'];

    var acWait = params['wait_id'];

    var acLink = params['ac_link'];

    var ownerId = params['owner_id'];


    if (commonValidId(form)) {

        commonGet(form).onsubmit = function() {

            var errorField = null;

            if (!commonValidateRequired(this, 'entry', 'error_1')) {

                errorField = (errorField ? errorField : 'entry');

            }


            if (!errorField) {
                var form = this;

                commonSendRequest('?' + commonGetAjaxParams(), 'action=add&owner_id=' + ownerId + '&entry=' + encodeURIComponent(form['entry'].value), true, function(successNode, errorsList) {
                    if (successNode) {
                        form.reset();
                        if (commonValidId(acLink)) {
                            commonGet(acLink).onclick(commonGet(acLink));
                        } else {

                        }

		                $(document).ready(function(){
	
							var comments = $('#comments');

						    var action = 'showblog';

							var template = '<li>{mmm}</li>';
	
							function loadComments(){
		
								if(this != window){
									if($(this).html() == 'Loading..'){
										return false;
									}
								$(this).html('Loading..');
								}
		

								$.getJSON('/ajax.php',{'action': action, 'owner_id': ownerId},function(r){
			
								comments.find('li').show('slow', function() {

							    });
	
								$.each(r.mess,function(i,message){
									comments.append(templateReplace(template,{mmm:message}));
								});

								});
		
								return false;
							}
	
							function templateReplace(template,data){
								return template.replace(/{([^}]+)}/g,function(match,group){
								return data[group];
							});
							}

							loadComments();	
						});

                    } else if (errorsList) {
                        for (var i = 0; i < errorsList.length; i++) {
                            commonShow(errorsList[i]);
                        }
                    }
                });

            } else {
                this[errorField].focus();
            }
            return false;
        }
    }
}

