var _contactardiv1 = '';
var _contactardiv2 = '';
var _contactardiv3 = '';

function definirPosicionContactar(_envdiv, _envdiv2, _envdiv3)
{
    _contactardiv1 = _envdiv;
    _contactardiv12 = _envdiv2;
    _contactardiv13 = _envdiv3;    
}

function PosicionarContactar(_envdiv, _envdiv2, _envdiv3, _idversion, _diviframe, _pathResources, _tipocontactar, _altoFrame)
{
    definirPosicionContactar(_envdiv, _envdiv2, _envdiv3)

    var updateProgressDiv = document.getElementById(_contactardiv1);
    var updateProgressDiv2 = document.getElementById(_contactardiv12);
    var updateProgressDiv3 = document.getElementById(_contactardiv13);
    var iframeprogress = $get('iframecontactar');                
    
    if (updateProgressDiv == null) return;

    var _tipo = "2";
    switch (_tipocontactar) {
            case "Cotizacion":
            _tipo = "2";
        break;
            case "Oferta":
            _tipo = "3";
        break;
            case "PlanAhorro":
            _tipo = "4";
        break;
    }

    var divIFrame = document.getElementById(_diviframe);

    while (divIFrame.childNodes[0]) {
        divIFrame.removeChild(divIFrame.childNodes[0]);
    }

    var iframeContactar = document.createElement("iframe");
    iframeContactar.setAttribute("id", "iframeContactarPopUp");
    iframeContactar.setAttribute("height", _altoFrame);
    iframeContactar.setAttribute("width", 270);
    iframeContactar.frameBorder = 0;
    iframeContactar.setAttribute("scrolling", "no");
    iframeContactar.allowTransparency = "yes";
    iframeContactar.setAttribute("style", "border:none;");
    iframeContactar.setAttribute("src", _pathResources + "IFrame/Contactar/Default.aspx?id=" + _idversion + "&tipo=" + _tipo + "&popup=1");
    divIFrame.appendChild(iframeContactar);

    TraerContactar(_idversion, _tipocontactar, _pathResources)

    try {
        findTamVentanaPopUp(updateProgressDiv);
    } catch (e) {
    var algo = "NADA";
     }
    
    
    updateProgressDiv.style.display = '';
      
    if (updateProgressDiv2 != null) 
    {      
        updateProgressDiv2.style.display = '';
        PosScrollCentradoPopUp(updateProgressDiv3, true, 412, 325);        
    }

    var urlContactar = window.location.href;
    
    if (iframeprogress != null && (EsIE() || urlContactar.search("yahoo") != -1))
    {
        iframeprogress.style.display = '';
        iframeprogress.style.width = updateProgressDiv.style.width;
        iframeprogress.style.height = updateProgressDiv.style.height;
    }            
}

function SacarPosicionarContactar(_envdiv, _envdiv2, _envdiv3)
{
    var updateProgressDiv = document.getElementById(_contactardiv1);
    var updateProgressDiv2 = document.getElementById(_contactardiv12);
    var updateProgressDiv3 = document.getElementById(_contactardiv13);
    var iframeprogress = $get('iframecontactar');

    updateProgressDiv.style.display = 'none';
    updateProgressDiv2.style.display = 'none';
    iframeprogress.style.display = 'none';
}

function TraerContactar(_idversion, _tipocontactar, _pathResources) {
    makeRequestContactar(_idversion, _tipocontactar, _pathResources);
}

function makeRequestContactar(_idversion, _tipocontactar, _pathResources) {
    try {

        var url = _pathResources + "Webservices/Catalogo/Version.aspx?id=" + _idversion;

        if (_tipocontactar == "Oferta") url = _pathResources + "Webservices/Catalogo/Oferta.aspx?id=" + _idversion;
        if (_tipocontactar == "PlanAhorro") url = _pathResources + "Webservices/Catalogo/PlanAhorro.aspx?id=" + _idversion;

        http_request = false;
        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) { }
            }
        }

        if (!http_request) return false;

        http_request.open('GET', url, false);
        http_request.send(null);
        ArmarContactar(_idversion, _tipocontactar, http_request.responseXML);
    }
    catch (ex) {
        var error = ex;
    }

}

function ArmarContactar(_idversion, _tipocontactar, Xml) {
    var divImagen = document.getElementById('divImagenContactar');

    while (divImagen.childNodes[0]) {
        divImagen.removeChild(divImagen.childNodes[0]);
    }

    var _titulo = DarValorXML(Xml.getElementsByTagName("titulo")[0]);
    var _imagen = DarValorXML(Xml.getElementsByTagName("imagen")[0]);
    var _alto = DarValorXML(Xml.getElementsByTagName("altoimagen")[0]);
    var _ancho = DarValorXML(Xml.getElementsByTagName("anchoimagen")[0]);
    var _precio = DarValorXML(Xml.getElementsByTagName("precio")[0]);
    var _oferta = DarValorXML(Xml.getElementsByTagName("oferta")[0]);
    var _segmento = DarValorXML(Xml.getElementsByTagName("segmento")[0]);
    var _garantia = DarValorXML(Xml.getElementsByTagName("garantia")[0]);
    var _origen = DarValorXML(Xml.getElementsByTagName("origen")[0]);

    switch (_tipocontactar) {
        case "Cotizacion":
            document.getElementById('tituloAutoContactar').innerHTML = "<em>Pedido de Cotizaci&oacute;n:</em> " + _titulo;
            break;
        case "Oferta":
            document.getElementById('tituloAutoContactar').innerHTML = "<em>Oferta:</em> " + _titulo;
            break;
        case "PlanAhorro":
            document.getElementById('tituloAutoContactar').innerHTML = "<em>Plan de Ahorro:</em> " + _titulo;
            break;
    }

    if (_precio != '') {
        if (_tipocontactar == "Oferta")
            document.getElementById('precioAutoContactar').innerHTML = "<strong>Precio de Lista:</strong> " + _precio;
        else
            document.getElementById('precioAutoContactar').innerHTML = "<strong>Precio:</strong> " + _precio;
    }
    else
        document.getElementById('precioAutoContactar').style.display = 'none';
    
    if (_oferta != '')        
        if (_tipocontactar == "Oferta")
            document.getElementById('ofertaAutoContactar').innerHTML = "<strong>Precio de Oferta:</strong> " + _oferta;
        else
            document.getElementById('ofertaAutoContactar').innerHTML = "<strong>Oferta:</strong> " + _oferta;
    else
        document.getElementById('ofertaAutoContactar').style.display = 'none';

    if (_segmento != '')
        document.getElementById('segmentoAutoContactar').innerHTML = "<strong>Segmento:</strong> " + _segmento;
    else
        document.getElementById('segmentoAutoContactar').style.display = 'none';
    
    if (_garantia != '') 
        document.getElementById('garantiaAutoContactar').innerHTML = "<strong>Garant&iacute;a:</strong> " + _garantia;
    else
        document.getElementById('garantiaAutoContactar').style.display = 'none';
    
    if (_origen != '') 
        document.getElementById('origenAutoContactar').innerHTML = "<strong>Origen:</strong> " + _origen;
    else
        document.getElementById('origenAutoContactar').style.display = 'none';
    
    var imgPrincipal = document.createElement("IMG");
    imgPrincipal.id = "idPrincipalContactar";

    divImagen.appendChild(imgPrincipal);

    definirImagenContactar("idPrincipalContactar", _imagen, _alto, _ancho, 212, 283);
}

function definirImagenContactar(strImg, pIMG, altoImg, anchoImg, altoMax, anchoMax) {
    target = document.getElementById(strImg);
    if (target) {
        var alto = altoMax; /*468;*/
        var ancho = anchoMax; /*624;*/
        var nuevoWidth;
        var nuevoHeight;

        if (anchoImg / altoImg > ancho / alto) {
            nuevoWidth = ancho;
            nuevoHeight = Math.round((altoImg * ancho) / anchoImg);
        }
        else {
            nuevoHeight = alto;
            nuevoWidth = Math.round((anchoImg * alto) / altoImg);
        }
        target.onload = function() {
            this.style.width = "".concat(nuevoWidth, "px");
            this.style.height = "".concat(nuevoHeight, "px");
            //this.style.marginTop = "".concat((alto - this.height) / 2, "px");
        };
        target.src = pIMG;
    }
}