﻿function buildRecommendationMail(adNumber, group) {
    var vWin = window.open(
		'about:blank'
		, '_proposta'
		, 'top=60,left=440,width=386,height=472,menubar=no,resizable=no,scrollbars=no,toolbar=no'
	);
    if (vWin == null) {
        alert('Seu navegador est\u00E1 bloqueando popups. N\u00E3o \u00E9 poss\u00EDvel a exibi\u00E7\u00E3o dos detalhes do an\u00FAncio.');
    }
    else {
        var addr    = ''.concat(window.location);
        var baseAdd = /^(.+)\u002Fsection\u002F/i.test(addr) ? RegExp.$1 : '.';
        vWin.document.location = baseAdd.concat('/general/indicate/?id=').concat(adNumber)
            .concat('&group=').concat(group);
    }
}

function popupDetails(theURL, winName, features, myWidth, myHeight, isCenter) {
    if (window.screen) if (isCenter) if (isCenter == "true") {
        var myLeft = (screen.width - myWidth) / 2;
        var myTop = (screen.height - myHeight) / 2;
        features += (features != '') ? ',' : '';
        features += ',left=' + myLeft + ',top=' + myTop;
    }
    window.open(theURL, winName, features + ((features != '') ? ',' : '') + 'width=' + myWidth + ',height=' + myHeight);
}

function frameDetails(u) {
    var vP = document.getElementById('detailPreview');
    if (vP) { vP.style.display = 'block' }
    var vC1 = document.getElementById('detailsTitle');
    if (vC1) { vC1.style.display = 'block' }
    var vC2 = document.getElementById('dtPrevFrame');
    if (vC2) { vC2.style.display = 'block' }

    var vT = document.getElementById('framePreview');
    if (vT) {
        var w =
            document.body.offsetWidth ? parseInt(document.body.offsetWidth)
        :   document.body.clientWidth ? parseInt(document.body.clientWidth)
        :   document.body.width ? parseInt(document.body.width)
        :   0;
        var ww =
            vT.style.width ? parseInt(vT.style.width)
        :   vT.offsetWidth ? parseInt(vT.offsetWidth)
        :   vT.clientWidth ? parseInt(vT.clientWidth)
        :   vT.width       ? parseInt(vT.width)
        :   0;

        vT.style.left    = ''.concat((w - ww) / 2).concat('px');
        vT.style.top     = '20px';
        vT.style.display = 'block';
    }
    
    var vF = document.getElementById('dtPrevFrame');
    if (vF) {
        if (vF.src != 'about:blank') { vF.src = 'about:blank' }
        vF.src = u;
    }
}
function hideDetails() {
    var vP = document.getElementById('detailPreview');
    if (vP) { vP.style.display = 'none' }
    var vF = document.getElementById('framePreview');
    if (vF) { vF.style.display = 'none' }
    var vC1 = document.getElementById('detailsTitle');
    if (vC1) { vC1.style.display = 'none' }
    var vC2 = document.getElementById('dtPrevFrame');
    if (vC2) { vC2.style.display = 'none' }
}

// Abre os detalhes
function viewDetails(code, tipo) {
    if (code == "") {
        alert("Por favor, informe o código");
        return;
    }
    var addXp = /(.*)\/section/i;
    var url = addXp.test(window.location) ? RegExp.$1 : 'dtPrevFrame';
    switch(tipo)
    {
        case 'autos':
            url = url.concat('/section/autos/details/').concat(code);
            break;
        case 'imoveis':
            url = url.concat('/section/imoveis/details/').concat(code);
            break;
        case 'empregos':
            url = url.concat('/section/empregos/details/').concat(code);
            break;
        case 'negocios':
            url = url.concat('/section/negocios/details/').concat(code);
            break;
        default:
            url = url.concat('/section/details/').concat(code);
            break;
    }
    frameDetails(url);
    //popupDetails(url, '', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes', '622', '582', 'true');
}

function openForgotPasswordWindow(startPath) {
    var forgot_pass_window = window.open(startPath + 'forgotpassword', 'forgot', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=405, height=200');
    forgot_pass_window.focus();
}

function openContactsWindow(startPath) {

    var open_contacts_window = window.open(startPath + 'General/Contacts.aspx', 'contacts', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=400,height=450');
    open_contacts_window.focus();       
            
}


function openChangePasswordWindow(startPath) {
    var openWindow = window.open(startPath + 'customerarea/changepassword', 'changepassword', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=370, height=250');
    openWindow.focus();
}

function isFKey(k) {
    return ';8;9;35;36;37;38;39;40;46;'.indexOf(';'.concat(k).concat(';')) >= 0;
}
function format(e, c, f, m) {
    e.returnValue = 0;
    var t = c.value;
    var s = t.length;
    var k =
		('keyCode' in e) ? e.keyCode
	:   ('which' in e) ? e.which
	:   0;
    if (isFKey(k)) { e.returnValue = 1 }
    else {
        if (s >= m) { e.returnValue = 0 }
        else if ((k >= 48) && (k <= 57)) {
            e.returnValue = 1;
            switch (f) {
                case 0: // CPF
                    if ((s == 3) || (s == 7)) { c.value = t.concat('.') }
                    else if (s == 11) { c.value = t.concat('-') }
                    break;
                case 1: // CNPJ
                    if ((s == 2) || (s == 6)) { c.value = t.concat('.') }
                    else if (s == 10) { c.value = t.concat('/') }
                    else if (s == 15) { c.value = t.concat('-') }
                    break;
                case 2: // Telefone
                    if (s == 0) { c.value = t.concat('(0xx') }
                    else if (s == 6) { c.value = t.concat(')') }
                    else if (s == 11) { c.value = t.concat('-') }
                    break;
                case 3: // CEP
                    if (s == 2) { c.value = t.concat('.') }
                    else if (s == 6) { c.value = t.concat('-') }
                    break;
                case 4: // Data
                    if ((s == 2) || (s == 5)) { c.value = t.concat('/') }
                    break;
            }
        }
    }
    return e.returnValue
}
function filter(e, c, m) {
    e.returnValue = 0;
    var t = c.value;
    var s = t.length;
    var k =
		('keyCode' in e) ? e.keyCode
	:   ('which'   in e) ? e.which
	:                      0;
    if (isFKey(k) && (k != 46)) { e.returnValue = 1 }
    else {
        if ((m > 0) && (s >= m)) { e.returnValue = 0 }
        else { e.returnValue = (k >= 48) && (k <= 57) }
    }
    return e.returnValue
}
function filterFloat(e, c) {
    e.returnValue = 0;
    var t = c.value;
    var s = t.length;
    var k =
		('keyCode' in e) ? e.keyCode
	:   ('which'   in e) ? e.which
	:                      0;
    if (e.shiftKey) { e.returnValue = 0 }
    else if (isFKey(k) && (k != 46)) { e.returnValue = 1 }
    else { e.returnValue = ((k >= 48) && (k <= 57)) || (k == 44) }
    return e.returnValue
}

function AdCookieDetail(n,m) {
    this.cookieName = n;
    this.pbCtrlName = m;
    this.fVal = '';
    this.cXp = new RegExp('(;?)'.concat(n).concat('\=([^;]*);?'), '');

    AdCookieDetail.prototype.$get = function() {
        var t = document.cookie;
        var result = '';
        if ((t.length > 0) && this.cXp.test(t)) {
            result = RegExp.$2
        }
        return result
    }
    AdCookieDetail.prototype.$set = function(v) {
        var c = document.getElementById(this.pbCtrlName);
        if (c) { c.value = v; }
    }
    AdCookieDetail.prototype.$count = function() {
        var itm = this.fVal;
        var result = itm.replace(/[^\|]/g, '').length - 1;
        return (result >= 0) ? result : 0
    }
    AdCookieDetail.prototype.updateCount = function() {
        var c = document.getElementById('selectedCookieCount');
        if (c) {
            c.innerText = this.$count()
            testVal = this.$get();
            c.parentNode.style.color = (this.fVal == testVal) ? 'black' : 'red'
        }
    }
    AdCookieDetail.prototype.init   = function()   { this.fVal = this.$get(); this.updateCount() }
    AdCookieDetail.prototype.commit = function()   { this.$set(this.fVal); this.updateCount(); }
    AdCookieDetail.prototype.store  = function(an) {
        var val = an.toString().concat('|');
        var tVal = '|'.concat(val);
        if (this.fVal.indexOf(tVal) < 0) {
            if (this.fVal.length == 0) { this.fVal = '|' }
            this.fVal = this.fVal.concat(val)
        }
    }
	AdCookieDetail.prototype.rem    = function(an) {
		var val = '\\|'.concat(an).concat('\\|');
		var rXp = new RegExp( val, '');
		this.fVal = this.fVal.replace( rXp, '|' );
	}
	AdCookieDetail.prototype.clear = function() {
	    var nums = this.fVal.split(/\|/g);
	    var total = nums.length;
	    for (var i = 0; i < total; ++i) {
	        var c = document.getElementById('chkSel_'.concat(nums[i]));
	        if (c) { c.checked = 0 }
	    }
	    this.fVal = ''
	}
	AdCookieDetail.prototype.updateUI = function() {
	    this.$set(this.fVal);
	    this.updateCount();
	    var nums = this.fVal.split(/\|/g);
	    var total = nums.length;
	    for (var i = 0; i < total; ++i) {
	        var c = document.getElementById('chkSel_'.concat(nums[i]));
	        if (c) { c.checked = 1 }
	    }
	}
}

var detailMan = new AdCookieDetail('WebPublishing.SelectedAds', 'cAdCookieEdit');
detailMan.init();

function toogleAdNum(c, n) {
    c.checked ? detailMan.store(n) : detailMan.rem(n);
    detailMan.commit();
}
function clearAdCookie()    { detailMan.clear(); detailMan.commit() }
function updateAdCookieUI() { detailMan.updateUI() }
