﻿function i2m_Utiles() {

    this.modal = false;
    this.objSelectToList = function(select) {
        var selected = "";
        var html = "";

        var i = 0;
        var j = select.options.length;
        while (i < j) {
            html += '<li><span class="ComboBoxId">' + i + "</span><span>" + select.options[i].innerHTML + "</span></li>";

            ++i;
        }

        if (j > 0) {
            html = '<div id="ComboBox_' + select.id + '" class="i2m_ComboBox">' +
        "<h2>" + select.options[select.selectedIndex].innerHTML + "</h2>" +
        "<ul>" + html + "</ul>" +
        "</div>";
        }


        return html;
    };

    this.getLeft = function(elem) {
        xPos = elem.offsetLeft;

        tempEl = elem.offsetParent;
        while (tempEl != null) {
            xPos += tempEl.offsetLeft;
            tempEl = tempEl.offsetParent;
        }


        return xPos;
    };

    this.getTop = function(elem) {
        yPos = elem.offsetTop;

        tempEl = elem.offsetParent;
        while (tempEl != null) {
            yPos += tempEl.offsetTop;
            tempEl = tempEl.offsetParent;
        }


        return yPos;
    };

    this.loadJS = function(file, exec) {
        var loaded = 0;

        if ((exec != undefined) && (exec != "")) {
            try {
                if (exec != false) {
                    eval(exec)();
                }

                loaded = 1;
            } catch (e) {

            }
        }

        if (loaded == 0) {
            $$("head")[0].appendChild(new Element("script", {
                type: "text/javascript",
                src: file
            }));
        }
    };

    this.loadCSS = function(file, id) {
        var loaded = 0;
        if ((id != undefined) && (id != "") && ($(id) != undefined) && ($(id) != null)) {
            loaded = 1;
        }

        if (loaded == 0) {
            var elem = new Element("link", {
                href: file,
                rel: "stylesheet",
                type: "text/css"
            });

            if ((id != undefined) && (id != "")) {
                elem.id = id;
            }

            $$("head")[0].appendChild(elem);
        }
    };

    this.isNumber = function(elem) {
        var str = elem.value;
        var re = /^[-]?\d*\.?\d*$/;
        str = str.toString();
        if (!str.match(re)) {
            return false;
        }
        return true;
    }
    this.validEmail = function(elem) {
        var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
        return elem.value.match(re);
    }

    this.imprimir = function(id) {
        var width = 400;
        var height = 300;
        var left = parseInt((screen.availWidth / 2) - (width / 2));
        var top = parseInt((screen.availHeight / 2) - (height / 2));
        var windowFeatures = "width=" + width + ",height=" + height + ",status=0,resizable=0,left=" + left + ",top=" + top +
            ",screenX=" + left + ",screenY=" + top + ",scrollbars=yes";
        var winnueva = window.open("./impirmir.aspx?idNota=" + id, "winnueva", windowFeatures);
        winnueva.focus();
    }

    this.getVideo = function() {
        return $('ctl00_cuerpo_videoURL').value;
    }
    this.getBanne = function() {
        return $('ctl00_videoURL').value;
    }
    
    this.Compartir=function(){
        var url=document.location.href;
        
        new Ajax.Request("./RecomendarConte.aspx", {
         /* parameters: {
            action: "getComentario",
            idComen:id
          },*/
          evalScripts: true,
          onSuccess: function(r) {
            this.modal = new i2m_Modal_Window("Comentario", '&nbsp;', r.responseText);
            this.modal.onClose = function() {
            i2m_util.modal = false;
            };
          }
        });
    }

    this.showAlert = function(type) {
        new Ajax.Request("./alerta.aspx", {
            evalScripts: true,
            onSuccess: function(r) {
                this.modal = new i2m_Modal_Window("Alerta", '&nbsp;', r.responseText, "_Alert");
                this.modal.onClose = function() {
                    i2m_util.modal = false;
                };
            }
        });
    }

    this.showLogin = function(type) {
        new Ajax.Request("./loginForm.aspx", {
            evalScripts: true,
            onSuccess: function(r) {
                this.modal = new i2m_Modal_Window("Ingresar", '&nbsp;', r.responseText, "_Alert");
                this.modal.onClose = function() {
                    i2m_util.modal = false;
                };
            }
        });
    }    

    this.desCargas = function(elemento) {
        var valor = this.DevolverValorChqueado(elemento);
        window.open(valor, "descarga");
    }

    this.DevolverValorChqueado=function(Elemento){
	    var f = document.forms[0];
		var a = f.elements;
	    for(i=0;i<a.length;i++)
	    {		
		    if(a[i].name == Elemento)
		    {
			    if(a[i].checked){				
				    return a[i].value;				
			    }
		    }
	    }
	}

	this.validateGroup = function(nombre, email, Error, required) {
	    var ret = true;
	    var something = nombre.value || email.value;
	    if (required || something) {
	        if (!nombre.value) {
	            Error.innerHTML = "Debés ingresar el nombre de tu amiga.";
	            nombre.focus();
	            ret = false;
	        } else if (!email.value) {
	            Error.innerHTML = "Debés ingresar el email de tu amiga.";
	            email.focus();
	            ret = false;
	        } else if (!this.validEmail(email)) {
	            Error.innerHTML = "Debés ingresar un email válido";
	            email.focus();
	            ret = false;
	        }
	    }
	    return ret;
	}

	this.SendCompartir = function() {
	    var url = document.location.href;
	    var Error = $('valSum');
	    Error.style.display = "block";
	    var Nombre = $('Nombre');
	    var Email = $('Email');
	    var amigaNombre = $('amigaNombre');
	    var amigaEmail = $('amigaEmail');
	    var Comentario = $('Comentario');
	    if (!Nombre.value) {
	        Error.innerHTML = "Debés ingresar tu nombre.";
	        Nombre.focus();
	    } else if (!Email.value) {
	        Error.innerHTML = "Debés ingresar tu email.";
	        Email.focus();
	    } else if (!this.validEmail(Email)) {
	        Error.innerHTML = "Debés ingresar un email válido.";
	        Email.focus();
	    } else {
	        for (var i = 1; i <= 5; i++) {
	            if (!this.validateGroup($('amigaNombre' + i), $('amigaEmail' + i), Error, i == 1)) {
	                return;
	            }
	        }
	        if (!Comentario.value) {
	            Error.innerHTML = "Debés ingresar un comentario.";
	            Comentario.focus();
	        } else {
	            new Ajax.Request("./RecomendarConte.aspx", {
	                parameters: {
	                    action: "SenEmail",
	                    url: url,
	                    Nombre: Nombre.value,
	                    Email: Email.value,
	                    amigaNombre1: $('amigaNombre1').value, amigaEmail1: $('amigaEmail1').value,
	                    amigaNombre2: $('amigaNombre2').value, amigaEmail2: $('amigaEmail2').value,
	                    amigaNombre3: $('amigaNombre3').value, amigaEmail3: $('amigaEmail3').value,
	                    amigaNombre4: $('amigaNombre4').value, amigaEmail4: $('amigaEmail4').value,
	                    amigaNombre5: $('amigaNombre5').value, amigaEmail5: $('amigaEmail5').value,
	                    Comentario: Comentario.value
	                },
	                evalScripts: true,
	                onSuccess: function(r) {
	                    this.modal.close();
	                }
	            });
	        }

	    }
	}
}

var i2m_util = new i2m_Utiles();

function updateChat(r) {
    if (r.value) {
        var h = '<div class="global-chat-box"><span class="global-chat-talk">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><a class="global-chat-link" href="Chat.aspx?w=' + r.value + '">Nuevo<br/>mensaje de chat</a></div>';
        document.getElementById('chatGlobal').innerHTML = h;
    } else {
        document.getElementById('chatGlobal').innerHTML = "";
    }
}

function initChat() {
    if (document.location.pathname.indexOf("Chat.aspx") < 0) {
        var f = function() {ChatEngine.getLastMessage( function(r){updateChat(r);})} ;
        f.call(null);
        setInterval(f, 15000);
    }
}

function setCookie(name, value) {
    document.cookie = name + "=" + escape(value);
}

function stopSound() {
    setCookie("SOUND", "OFF");
}

function playSound() {
    setCookie("SOUND", "ON");
}



function isSoundOn() {
    if (window.flashSoundOn != undefined) return window.flashSoundOn;
    var name = "SOUND";
    if (document.cookie.length > 0) {
        var start = document.cookie.indexOf(name + "=");
        if (start != -1) {
            start = start + name.length + 1;
            var end = document.cookie.indexOf(";", start);
            if (end == -1) end = document.cookie.length;
            return unescape(document.cookie.substring(start, end)) == "ON";
        }
    }
    return true;
}

function pauseFlashSound() {
    window.flashSoundOn = false;
}

function changeImage(img, url) {
    if (document.all) {
        var v = parseFloat(navigator.appVersion.split("MSIE")[1]);
        if (v < 7 && img.className == "pngfix" && img.filters && img.filters.length > 0) {
            img.filters(0).src = url;
        } else img.src = url;
    } else img.src = url;
}


function showHide(id) {
    var e = document.getElementById(id);
    if (e) {
        if (e.style) {
            if (e.style.display == "none") e.style.display = "block";
            else e.style.display = "none";
        }
    }
}