
function createXMLHTTP() {
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];

        for (var i = 0; i < prefixes.length; i++) {
            try {
                return new ActiveXObject(prefixes[i] + ".XMLHTTP");
            } catch (e) { }
        }
    }
}


//Função para abertura de pop-up

var popUpWin = 0;

function popUpWindow(URLStr, left, top, width, height) {

    if (popUpWin) {

        if (!popUpWin.closed) popUpWin.close();

    }

    popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=yes,width=' + width + ',height=' + height + ',left=' + left + ', top=' + top + ',screenX=' + left + ',screenY=' + top + '');

}

//Menu Central de Atendimento:

function centralAtendimento(param) {
    document.getElementById("linksCentral").style.visibility = param;
}

String.prototype.trim = function() {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function formatar(src, mask) {
    var i = src.value.length;
    var saida = mask.substring(0, 1);
    var texto = mask.substring(i)
    if (texto.substring(0, 1) != saida) {
        src.value += texto.substring(0, 1);
    }
}

function limpa_string(S) {
    var Digitos = "0123456789";
    var temp = "";
    var digito = "";
    for (var i = 0; i < S.length; i++) {
        digito = S.charAt(i);
        if (Digitos.indexOf(digito) >= 0) { temp = temp + digito }
    }
    return temp
}

function is_num(e, name_field) {
    var tecla = event.keyCode;
    if (tecla > 47 && tecla < 58)// numeros de 0 a 9
        return true;
    else {
        document.getElementById(name_field + "Msg").innerHTML = 'Permitido somente números.';
        if (tecla != 8) // backspace
            event.keyCode = 0;
        else
            return true;
    }
}

function limpa_NomeBusca(limpar) {
    if (document.getElementById("campoBuscarNew").value == 'O que voc&ecirc; procura?') {
        document.getElementById("campoBuscarNew").value = '';
    } else if (limpar == 1 && document.getElementById("campoBuscarNew").value == '') {
        document.getElementById("campoBuscarNew").value = 'O que voc&ecirc; procura?';
    }
}

function limpa_NomeCadNews(limpar) {
    if (document.getElementById("campoCadNewsNome").value == 'nome') {
        document.getElementById("campoCadNewsNome").value = '';
    } else if (limpar == 1 && document.getElementById("campoCadNewsNome").value == '') {
        document.getElementById("campoCadNewsNome").value = 'nome';
    }
}

function limpa_EmailCadNews(limpar) {
    if (document.getElementById("campoCadNewsEmail").value == 'e-mail') {
        document.getElementById("campoCadNewsEmail").value = '';
    } else if (limpar == 1 && document.getElementById("campoCadNewsEmail").value == '') {
        document.getElementById("campoCadNewsEmail").value = 'e-mail';
    }
}

function limpa_NomeCadNewsEsgotado(limpar) {
    if (document.getElementById("campoCadNewsNomeEsgotado").value == 'nome') {
        document.getElementById("campoCadNewsNomeEsgotado").value = '';
    } else if (limpar == 1 && document.getElementById("campoCadNewsNomeEsgotado").value == '') {
        document.getElementById("campoCadNewsNomeEsgotado").value = 'nome';
    }
}

function limpa_EmailCadNewsEsgotado(limpar) {
    if (document.getElementById("campoCadNewsEmailEsgotado").value == 'e-mail') {
        document.getElementById("campoCadNewsEmailEsgotado").value = '';
    } else if (limpar == 1 && document.getElementById("campoCadNewsEmailEsgotado").value == '') {
        document.getElementById("campoCadNewsEmailEsgotado").value = 'e-mail';
    }
}

function limpa_cadLateralNewsNome(limpar) {
    if (document.getElementById("cadLateralNewsNome").value == 'nome') {
        document.getElementById("cadLateralNewsNome").value = '';
    } else if (limpar == 1 && document.getElementById("cadLateralNewsNome").value == '') {
        document.getElementById("cadLateralNewsNome").value = 'nome';
    }
}

function limpa_cadLateralNewsEmail(limpar) {
    if (document.getElementById("cadLateralNewsEmail").value == 'e-mail') {
        document.getElementById("cadLateralNewsEmail").value = '';
    } else if (limpar == 1 && document.getElementById("cadLateralNewsEmail").value == '') {
        document.getElementById("cadLateralNewsEmail").value = 'e-mail';
    }
}

function AbreCadastroNews(param, url) {
    document.getElementById("frameCadastroNews").src = url;

    if (param == 1) {
        document.getElementById("divCadastroNews").style.display = "block";

    }
    else {
        document.getElementById("divCadastroNews").style.display = "none";
    }
}

function valida_news(codLoja, valor_nome, valor_email, id_produto, news, campanha, ip, reffer) {
    if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor_email))) {
        alert(valor_email + " não é um e-mail válido.");
        return false;
    }
    var oHTTPRequest = createXMLHTTP();
    oHTTPRequest.open("post", "../Scripts/script_news.asp", true);
    oHTTPRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    //oHTTPRequest.setRequestHeader("encoding", "UTF-8");
    oHTTPRequest.onreadystatechange = function() {
        if (oHTTPRequest.readyState == 4) {
            var r = oHTTPRequest.responseXML;
            //alert(r.getElementsByTagName('alert_news')[0].childNodes[0].nodeValue);
            window.location.href = r.getElementsByTagName('alert_news')[0].childNodes[0].nodeValue;
            //popUpWindow("FormularioCadastroNews.aspx?" + r.getElementsByTagName('alert_news')[0].childNodes[0].nodeValue,  200, 150, 450, 465);
            //window.open("FormularioCadastroNews.aspx?" + r.getElementsByTagName('alert_news')[0].childNodes[0].nodeValue, "_blank");
            //AbreCadastroNews(1,r.getElementsByTagName('alert_news')[0].childNodes[0].nodeValue);
        }
    }
    limpa_NomeCadNews(1);
    limpa_EmailCadNews(1);
    oHTTPRequest.send("nome=" + valor_nome.replace(/ /gi, '%20') + "&email=" + valor_email + "&id_loja=" + codLoja + "&id_produto=" + id_produto + "&news=" + news + "&camp=" + campanha + "&ip=" + ip + "&reffer=" + reffer);

}

function siteSeguro(dominio, page) {
    window.location.href = 'http://' + dominio + '/' + page;
}

function Busca(dominio) {
    document.getElementById("buscaNews").action = 'http://' + dominio + '/resultado_busca.asp?q=' + document.getElementById('campoBuscar').value;
    document.getElementById("buscaNews").submit();
}

function BuscaNew(dominio) {
    document.getElementById("buscaNewsNovo").action = 'http://' + dominio + '/resultado_busca.asp?q=' + document.getElementById('campoBuscarNew').value;
    document.getElementById("buscaNewsNovo").submit();
}
