var datepickerSettings = {
    changeMonth: true,
    changeYear: true,
    duration: '',
    buttonImage: BASE_PATH + 'images/icons/calendar.gif',
    buttonImageOnly: true,
    showOn: 'both'
};

function checkEmailAddress(email) {
    // the following expression must be all on one line...
    var goodEmail = email.match(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/);
    if ( goodEmail ) {
        return true;
    } else {
        return false;
    }
}

function loadAnchorContent(id){
	parts = window.location.href.split("#");
	if(parts.length == 2){
		params = parts[1].split("/");
		params = params[1];
	}else{
		params = 'Biography';
	}

	loadContent(id, params);
}

function loadContent(id, obj){
	if (typeof obj == 'object'){
		p = $(obj).attr('href').split("/");
        $(".active-link").removeClass("active-link");
        $(obj).addClass("active-link");
		p = p[1];
	}else{
		p = obj;
        $("#l"+p).addClass("active-link");
	}

	$.ajax({
		url: BASE_PATH + 'attorneys/getInfo',
		data: {
			member_id: id,
			info: p
		},
		type: 'POST',
		dataType: 'json',
		success: function(response){
			$('#content_title').html(p);
			$('#content1').html(response.content);
		},
		beforeSend: function (){
			showLoadingPage();
		},
		complete: function(){
			hideLoading();
		}
	});

	return false;
}
/* Start loading indicator */
var OverlayTpl = '<div id="ajax-overlay"></div>';
var OverlayObj = null;
var LoadingTpl = '<div id="ajax-loading"><table><tr><td><img src="'+ BASE_PATH + 'images/loading.gif" width="16" height="16" alt="..." /></td><td>@text@</td></tr></table></div>';
var LoadingObj = null;
function showLoading(fromLeft, text, pObj1) {
	if (!text) {
		text = 'Please wait...';
	}
	var wObj = pObj1;
	pPos = pObj1.position();
	LoadingObj = $(LoadingTpl.replace('@text@', text)).remove().appendTo('body');
	LoadingObj.css({'top': ((wObj.height() - LoadingObj.outerHeight()) / 2 + pPos.top), 'left': (wObj.width() - LoadingObj.outerWidth() + fromLeft) / 2 + pPos.left});
}
function showLoadingPage(text) {
	if (typeof(text) != 'string') {
		text = 'Please wait...';
	}

	pObj = $('#content1');
	pPos = pObj.position();

	var sukaIE = pObj.height() + parseInt(pObj.css('paddingTop'));
	if (sukaIE > 0) {
		sukaIE--;
		sukaIE--;
	}

	var OverlayCss = {
		'width': pObj.width() - 2,
		'height': sukaIE,
		'top': pPos.top + 1,
		'left': pPos.left + 1,
		'opacity': 0.8
	};

	OverlayObj = $(OverlayTpl).remove().appendTo(pObj).css(OverlayCss);
	showLoading(0, text,pObj);
}
function hideLoading() {
	if (LoadingObj) {
		LoadingObj.remove();
		LoadingObj = null;
	}
	if (OverlayObj) {
		OverlayObj.remove();
		OverlayObj = null;
	}
}
/* End loading indicator */
function number_format(number, decimals, dec_point, thousands_sep) {
    var n = number, prec = decimals;
    n = !isFinite(+n) ? 0 : +n;
    prec = !isFinite(+prec) ? 0 : Math.abs(prec);
    var sep = (typeof thousands_sep == "undefined") ? ',' : thousands_sep;
    var dec = (typeof dec_point == "undefined") ? '.' : dec_point;

    var s = (prec > 0) ? n.toFixed(prec) : Math.round(n).toFixed(prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;

    var abs = Math.abs(n).toFixed(prec);
    var _, i;

    if (abs >= 1000) {
        _ = abs.split(/\D/);
        i = _[0].length % 3 || 3;

        _[0] = s.slice(0,i + (n < 0)) +
              _[0].slice(i).replace(/(\d{3})/g, sep+'$1');

        s = _.join(dec);
    } else {
        s = s.replace('.', dec);
    }

    if (s == 0 || s == '0') {
        s = '';
    }

    return s;
}
function fromMoney(val, retEmptyIfZero) {
    if (!val) {
        if (retEmptyIfZero) {
            return '';
        }
        return 0;
    }

    var value = val.toString().replace(/[^\d^.]*/g, '');
    if (retEmptyIfZero) {
        return value == 0 ? '' : value;
    }

    return value;
}
function toMoney(val, decimals) {
    if (!val) {
        return '';
    }
    var value = val.toString().replace(/,/g, '');

    if (!decimals && decimals != 0) {
        decimals = 2;
    }

    return number_format(value, decimals);
}

function redirect(url) {
    document.location.href = url;
}

function deleteItem(url, rowId) {
    if (confirm('Are you sure you want to delete this record?')) {
        $.post(url);
        afterDelete($('#'+rowId));
    }

    return false;
}

function afterDelete(rowObj) {
    rowObj.css('backgroundColor', '#ff7878');
    rowObj.fadeOut('slow', function(){
        rowObj.remove();
        var rTotalObj = $('#pager-rtotal');
        var totalRecords = parseInt(rTotalObj.text());
        rTotalObj.text('' + (totalRecords-1) + '');

        if (totalRecords == 1) {
            $('#grid-pager').remove();
        }

		$('.grid tr').removeClass('first-row');
		$('.grid tr:first-child').next().addClass('first-row');
    });
}

function DisplayStateSelect(idName) {
    if ($('#'+idName).val() == 'US') {
        $('#state-div').hide();
        $('#state-code-div').show();
    } else {
        $('#state-div').show();
        $('#state-code-div').hide();
    }
}

function deleteCompanyFile(url, id, category, company_id) {
    if (confirm('Are you sure you want to delete this file?')) {
        $.post(url, {id: id, company_id: company_id});

        var categoryObj = $('#cat-box-'+category);
        if ($('#cat-'+category+' > li').length == 1) {
            categoryObj.fadeOut(function(){
                categoryObj.remove();
            });
        }
        else {
            var idObj = $('#company-file-'+id);
            idObj.fadeOut(function(){
                idObj.remove();
            });
        }
    }

    return false;
}

/* need merge it in one function...*/

function deleteInvFile(url, id, inv_id) {
    if (confirm('Are you sure you want to delete this file?')) {
        $.post(url, {id: id, inv_id: inv_id});

            var idObj = $('#inv-file-'+id);
            idObj.fadeOut(function(){
                idObj.remove();
            });
    }

    return false;
}

function deleteMRFile(url, id) {
    if (confirm('Are you sure you want to delete this file?')) {
        $.post(url, {id: id});
            var idObj = $('#mr-file-'+id);
            idObj.fadeOut(function(){
                idObj.remove();
            });
    }

    return false;
}

function deleteSdocFile(url, id, sdoc_id) {
    if (confirm('Are you sure you want to delete this file?')) {
        $.post(url, {id: id, sdoc_id: sdoc_id});

            var idObj = $('#sdoc-file-'+id);
            idObj.fadeOut(function(){
                idObj.remove();
            });
    }
    return false;
}

function deleteSettFile(url, id, sett_id) {
    if (confirm('Are you sure you want to delete this file?')) {
        $.post(url, {id: id, sett_id: sett_id});

            var idObj = $('#sett-file-'+id);
            idObj.fadeOut(function(){
                idObj.remove();
            });
    }
    return false;
}
