﻿    var xmlObjLogin;
    var useNewLoginMethod = 0;
    var authenticationDebug = 0;
    var language = getCookie("idioma");
    
    function getCookie(Name) 
    {
        var search = Name + "=" ;
	    document.cookie.setDomain="batanga.com"
	    //HttpCookie languageCookie = HttpContext.Current.Request.Cookies[Name];
	    if (document.cookie.length > 0) 
	    { // if there are any cookies
		    offset = document.cookie.indexOf(search)
		    if (offset != -1 && offset != null) { // if cookie exists
			    offset += search.length;
			    // set index of beginning of value
			    end = document.cookie.indexOf(";", offset);
			    // set index of end of cookie value
			    if (end == -1)
				    end = document.cookie.length;
			    return unescape(document.cookie.substring(offset, end));
		    }
    		
	    }
	    else
	    {
		    return null;
	    }
    }
    
    function getTagValue(data, tag)
    {
        var startTag = "<" + tag + ">";
        var endTag = "</" + tag + ">";
        var returnValue = null;
        
        var startPos = data.indexOf(startTag);
        if(startPos > 0)
        {
            returnValue = data.substr(startPos + startTag.length);
            returnValue = returnValue.substr(0, returnValue.indexOf(endTag));               
        }
        
        return returnValue;
    }

    function setSessionCookie(name, value, duration) 
    {
	    var cookiestring;
    	
	    if(duration != null)
	        cookiestring = name+"="+escape(value)+";EXPIRES="+getexpirydate(duration) + ";path=/;domain=batanga.com;";
        else	
	        cookiestring = name+"="+escape(value)+";path=/;domain=batanga.com;";
    	
	    document.cookie = cookiestring;
    }
    
    function Logout()
    {
        setSessionCookie("batangakey", "", -1);
        document.location.reload();
    }

    function getexpirydate(nodays)
    {
        var UTCstring;
        Today = new Date();
        nomilli=Date.parse(Today);
        Today.setTime(nomilli+nodays*24*60*60*1000);
        UTCstring = Today.toUTCString();
        return UTCstring;
    }

    function setupCookie(key, cookieName, data)
    {
        var value = getTagValue(data, key);      
        if(cookieName == null)
            cookieName = key;
        if(authenticationDebug == 1)
            alert("setupCookie: cookieName=" + cookieName + ", value=" + value);               
            
        setSessionCookie(cookieName, value, null);        
    }  

    function showStatusMessage(message)
    {
        var StatusMessage = document.getElementById("StatusMessage");
        StatusMessage.innerHTML = message;
        StatusMessage.style.display = "";
    }
    
    function RetrieveAuthenticateData() 
    {
        var outputText;
                
        if(authenticationDebug == 1)
        {
            alert(xmlObjLogin.responseText);           
        }

        if ((xmlObjLogin != null) && (xmlObjLogin.readyState == 1))         
        {
            var authenticationText;
            if(language == "EN")            
                authenticationText = "Authentication in progress...";
            else
                authenticationText = "Autenticaci&oacute;n en progreso...";
            
            var message = "<font color=black>" + authenticationText + "</font>";                         
            showStatusMessage(message);             
        }

        if ((xmlObjLogin != null) && (xmlObjLogin.readyState == 4) && (xmlObjLogin.status == 200))
        { 
            var loginButton = document.getElementById("loginbutton");
            loginButton.disabled = false;
        
            outputText = xmlObjLogin.responseText;  		//get the reesponse from the web service
            if(authenticationDebug == 1)
            	alert("outputText = " + outputText);
            
            var status = getTagValue(outputText, "status");
            if(authenticationDebug == 1)
                alert("Status = " + status);
            
            if(status == "1")
            {
                setupCookie("batangakey", "batangakey", outputText);
                setupCookie("displayname", "DJScreenName", outputText);
                setupCookie("username", "batangausername", outputText);
                setupCookie("userid", "batangauserid", outputText);
                
                var email = document.getElementById("email").value;                
                setSessionCookie("batangacom", email, null);                       
            }
            else
            {
                if(authenticationDebug == 1)                
                    alert("Authentication failed");
            }            
            xmlObjLogin = null;       
            
            if(status == "1")
            {
                closeLoginPanel();
                
                //authenticated successfully! so just reload the same page now...
                document.location.reload();
            }
            else            
            {        
                var reason = getTagValue(outputText, "errormessage");        
                if(authenticationDebug == 1)       
                    alert("actual failed reason = " + reason);
                
                if(language == "EN")
                    reason = "Email and Password do not match";
                else
                    reason = "Email y Contrase&ntilde;a no coinciden";
                    

                    
                var message = '<font color=red>Error: ' + reason + '</font>';                
                showStatusMessage(message);                             
                
                if(document.getElementById("password") != null)
                {
                    document.getElementById("password").focus();
                    document.getElementById("password").select();
                }
                
            }
        } // if  
    } 


	function doLogin()
	{
				
		if (window.ActiveXObject)   // ActiveX version
		{
		       xmlObjLogin  = new ActiveXObject("Microsoft.XMLHTTP");    // Internet Explorer 
		} 
		else if (window.XMLHttpRequest)     // Object of the current windows
		{ 
		        xmlObjLogin  = new XMLHttpRequest();     // Firefox, Safari, ...
		        xmlObjLogin.overrideMimeType('text/xml');
		} 
		else
		{
		    alert('no idea what browser!');
		    return;
        }
    
        var loginButton = document.getElementById("loginbutton");
        loginButton.disabled = true;
        
        var email = document.getElementById("email").value;
        var password = document.getElementById("password").value;
    
    	var now = new Date();	
    	//pass the time stamp now, or else the AJAX call seems to be cached if the URL is the same as in previous call!    	    
        var url = "/authenticate.asp?email=" + email + "&password=" + password + "&dt=" + now;
        if(authenticationDebug == 1)
		    alert("url = " + url);
        
	    xmlObjLogin.open("GET", url, true);        		
		
		xmlObjLogin.setRequestHeader("Content-Type", "text/xml; charset=utf-8");				
		xmlObjLogin.onreadystatechange = RetrieveAuthenticateData;				    
		xmlObjLogin.send(null);		
	}
	
	function resetStatusMessage()
	{
	    var statusMessage = document.getElementById("StatusMessage");
	    
	    if(statusMessage != null)
            statusMessage.style.display = "none";	
	}
	
	/*
	function closeLoginPanel()
	{
	    var panelcontainer = document.getElementById("panel-container");
        panelcontainer.style.display = "none";
        
		var loginpanel = document.getElementById("loginpanel");
        loginpanel.style.display = "none";	
	}
	*/
	
		function closeLoginPanel()
	{
	    if(useNewLoginMethod == 0)
	        closeLoginPanel_1();
	    else
	        closeLoginPanel_2();
	}
	
	function closeLoginPanel_1()
	{	    
	    var panelcontainer = document.getElementById("panel-container");
        panelcontainer.style.display = "none";
        
		var loginpanel = document.getElementById("loginpanel");
        loginpanel.style.display = "none";	
	}
	
	function closeLoginPanel_2()
	{
	    
	    var divLogin = document.getElementById("loginBox");
        divLogin.style.display = "none";
	    
	    var modalBackground = document.getElementById("modalBackground");
        modalBackground.style.display = "none";	 
	}
	
    function showLoginPanel()
	{
	    if(useNewLoginMethod == 0)
	        showLoginPanel_1();
	    else
	        showLoginPanel_2();
	}
	
    function showLoginPanel_1()
    {
        var text = '';
        var email = '';
        var password = '';
        
        if(document.getElementById("email") != null)
            email = document.getElementById("email").value;
            
        if(document.getElementById("password") != null)       
            password = document.getElementById("password").value;
                 
        
        var joinForFreeText;
        var loginText;
        var forgotPasswordText;
        var infoText;
        var closePanelText;
        var passwordText;
        
        
        if(language == "EN")
        {
            joinForFreeText = "Join for Free";
            loginText = "Log in";
            forgotPasswordText = "Forgot Password?";
            infoText = "Enter your Email and Password to log in:";
            closePanelText = "Close Panel";            
            passwordText = "Password";
        }
        else
        {
            joinForFreeText = "Reg&iacute;strate Gratis";
            loginText = "Entrar";
            forgotPasswordText = "&iquest;Olvidaste tu Contrase&ntilde;a?";            
            infoText = "Ingresa tu  Email y Contrase&ntilde;a  para entrar:";	    
            closePanelText = "Cerrar Panel";            
            passwordText = "Contrase&ntilde;a";
        }
                
        text = '<a style="float:right; font-size:10px" href="#" onclick="javascript:closeLoginPanel(); return false;">' + closePanelText + '</a><h3>' + infoText  + '</h3>';
        text = text + '<table align=center style="margin-top:6px;  font-size:12px;"><tr><td valign="top">Email&nbsp;&nbsp;<INPUT id=email tabIndex=100 maxLength=32 onkeypress="javascript:resetStatusMessage();" value="' + email + '" name=email >';
        text = text + '&nbsp;&nbsp;&nbsp;&nbsp;' + passwordText + '&nbsp;</td><td><INPUT id=password tabIndex=101 onkeypress="javascript:resetStatusMessage();" type=password maxLength=20 value="' + password + '" name=password>';
	    text = text + '<br><A style="font-size:10px" href="http://my.batanga.com/retrievepassword.php?networkval=batanga">' + forgotPasswordText +'</A></td>';
        text = text + '<td valign="top">&nbsp;&nbsp;<INPUT id=loginbutton tabIndex=102 type=submit value="' + loginText + '" name=loginbutton onclick="javascript:doLogin();"></td>';
        text = text + '</tr></table>';
        text = text + '<a style="FONT-SIZE: 12px;" href="http://my.batanga.com/joinNetwork.php?network=batanga&sset=">' + joinForFreeText + '</a>';
        text = text + '<div id="StatusMessage" style="display:none;text-align:center"></div>';

        var panelcontainer = document.getElementById("panel-container");
        panelcontainer.style.display = "";
        
        var loginpanel = document.getElementById("loginpanel");
        loginpanel.innerHTML = text;
        loginpanel.style.display = "";        
        
        if(document.getElementById("email") != null)
        {
            document.getElementById("email").focus();
            document.getElementById("email").select();
        }
        
        //Drag.init(panelcontainer, panel);
    } 
    
    function showLoginPanel_2()
    {
        ShowLoginDialog(500, 250);
        
         if(document.getElementById("email") != null)
        {
            document.getElementById("email").focus();
            document.getElementById("email").select();
        }
        
    }
    
    var IE = document.all?true:false

    // If NS -- that is, !IE -- then set up for mouse capture
    if (!IE) document.captureEvents(Event.MOUSEMOVE)

    // Set-up to use getMouseXY function onMouseMove
    document.onmousemove = getMouseXY;

    var mouseX = 0;
    var mouseY = 0;

    function getMouseXY(e) 
    {
      if (IE) 
      { // grab the x-y pos.s if browser is IE
        mouseX = event.clientX + document.body.scrollLeft
        mouseY = event.clientY + document.body.scrollTop
      } 
      else 
      {  // grab the x-y pos.s if browser is NS
        mouseX = e.pageX
        mouseY = e.pageY
      }  
      // catch possible negative values in NS4
      if (mouseX < 0){mouseX = 0}
      if (mouseY < 0){mouseY = 0}  
      
      return true
    }


    function getMouseXY2(e) // works on IE6,FF,Moz,Opera7
    { 
      if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)

      if (e)
      { 
        if (e.pageX || e.pageY)
        { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
          mouseX = e.pageX;
          mouseY = e.pageY;
          algor = '[e.pageX]';
          if (e.clientX || e.clientY) algor += ' [e.clientX] '
        }
        else if (e.clientX || e.clientY)
        { // works on IE6,FF,Moz,Opera7
          mouseX = e.clientX + document.body.scrollLeft;
          mouseY = e.clientY + document.body.scrollTop;
          algor = '[e.clientX]';
          if (e.pageX || e.pageY) algor += ' [e.pageX] '
        }  
      }
    }

    var winW, winH;
    function SetWinWidthHeightValues()
    {
        if (parseInt(navigator.appVersion)>3) 
        {
     	    if (navigator.appName=="Netscape") 
     	    {
      		    winW = window.innerWidth;
      		    winH = window.innerHeight;
     	    }
     	    if (navigator.appName.indexOf("Microsoft")!=-1) 
     	    {
      		    winW = document.body.offsetWidth;
      		    winH = document.body.offsetHeight;
     	    }
        }
    }

    function ShowLoginDialog(dlgWidth, dlgHeight)
    { 
	    //alert("ShowLoginDialog");
    	
        if (document.getElementById)
        {
    	    // Get the window width and height
            SetWinWidthHeightValues();
                            
            //var dlg = document.getElementById(DIVid);
            var dlg = document.getElementById("loginBox");
            var dlg_background  = document.getElementById("modalBackground");
            
            var yPositionValue = 0;
            var xPositionValue = 0;
            
            //alert("popup width=" + dlg.style.width + "   height=" + dlgHeight);            
            
            dlg_background.style.width = winW + "px";
            dlg_background.style.height = Math.max(document.documentElement.scrollHeight, document.body.scrollHeight) + "px";
            
            dlg.style.width = dlgWidth;
            dlg.style.height = dlgHeight;
            
            //getMouseXY(e);
            
            /////////////////////////////////
            //Determin the X position value
            if ( (mouseX + dlgWidth) > winW - 10 )
                { xPositionValue = winW - dlgWidth - 10; }
            else
                { xPositionValue = mouseX; }
            //alert("winW="+winW + "  dlg.width="+dlg.style.width+"  mouseX="+mouseX + "  positionValue="+xPositionValue);
            dlg.style.left = xPositionValue + "px";
            
            /////////////////////////////////
            //Determin the Y position value   
            if ( (mouseY + dlgHeight) > winH - 10)
                { yPositionValue = winH - dlgHeight - 50; }
            else 
                { yPositionValue = mouseY; }
            //alert("winH="+winH + "  dlg.height="+dlgHeight+"  mouseY="+mouseY + "  yPositionValue="+yPositionValue);
            dlg.style.top = yPositionValue + "px"; 

            /////////////////////////////////
            //Turn the dialog on or off        
            //alert(dlg.style.display);
            if (dlg.style.display == "none")
            { 
                dlg_background.style.display = "";
                dlg.style.display = "";
            } 
            else 
            { 
                dlg_background.style.display = "none";
                dlg.style.display = "none"; 
            }
        } 
    }  
    
     if(useNewLoginMethod == 1)
    {
        
        var text = '';
        var email = '';
        var password = '';
        
        if(document.getElementById("email") != null)
            email = document.getElementById("email").value;
            
        if(document.getElementById("password") != null)       
            password = document.getElementById("password").value;

        var joinForFreeText;
        var loginText;
        var forgotPasswordText;
        var infoText;
        var closePanelText;
        var passwordText;
        
        
        if(language == "EN")
        {
            joinForFreeText = "Join for Free";
            loginText = "Log in";
            forgotPasswordText = "Forgot Password?";
            infoText = "Enter your Email and Password to log in:";
            closePanelText = "Close Panel";            
            passwordText = "Password";
        }
        else
        {
            joinForFreeText = "Reg&iacute;strate Gratis";
            loginText = "Entrar";
            forgotPasswordText = "&iquest;Olvidaste tu Contrase&ntilde;a?";            
            infoText = "Ingresa tu  Email y Contrase&ntilde;a  para entrar:";	    
            closePanelText = "Cerrar Panel";            
            passwordText = "Contrase&ntilde;a";
        }
          

        text = "<div style=\"background-color:white;border: 2px solid #A1AE45; width:500px; \">";
        
        text = text + "<a style=\"float:right; font-size:10px;\" href=\"#\" onclick=\"javascript:closeLoginPanel(); return false;\">" + closePanelText + "</a><h3>" + infoText  + "</h3>";
        //text = text + "<a style=\"float:right; font-size:10px;\" href=\"#\" onclick=\"javascript:ShowLoginDialog(500, 250); return false;\">" + closePanelText + "</a><h3>" + infoText  + "</h3>";
        text = text + "<table align=center bgcolor=white style=\"margin-top:6px;  font-size:12px;\">";    
        text = text + "<tr><td valign=\"top\">Email&nbsp;&nbsp;<INPUT id=email tabIndex=100 maxLength=32 onkeypress=\"javascript:resetStatusMessage();\" value=\"" + email + "\" name=email >";
        text = text + "&nbsp;&nbsp;&nbsp;&nbsp;" + passwordText + "&nbsp;</td><td><INPUT id=password tabIndex=101 onkeypress=\"javascript:resetStatusMessage();\" type=password maxLength=20 value=\"" + password + "\" name=password>";
        text = text + "<br><A style=\"font-size:10px\" href=\"http://my.batanga.com/retrievepassword.php?networkval=batanga\">" + forgotPasswordText + "</A></td>";
        text = text + "<td valign=\"top\">&nbsp;&nbsp;<INPUT id=loginbutton tabIndex=102 type=submit value=\"" + loginText + "\" name=loginbutton onclick=\"javascript:doLogin();\"></td>";
        text = text + "</tr></table>";
        text = text + "<a style=\"FONT-SIZE: 12px;\" href=\"http://my.batanga.com/joinNetwork.php?network=batanga&sset=\">" + joinForFreeText + "</a>";    
        text = text + "<div id=\"StatusMessage\" style=\"display:none;text-align:center\"></div>";
        text = text + "</div>";

        
        //document.write("<DIV id=\"modalBackground\" class=\"modalBackground\" style=\"Z-INDEX: 50000; LEFT: 0px; POSITION: absolute; TOP: 0px;\"></DIV>");    
        document.write("<DIV id=\"modalBackground\" class=\"modalBackground\"></DIV>");    
        //document.write("<div id=\"loginBox\" style=\"display:none; position : absolute;  z-index: 50010; background-color: Transparent;\">");    
        document.write("<div id=\"loginBox\" class=\"loginBox\" style=\"display:none;\">");    
        document.write(text);	    
        document.write("</div>");
    }



    
   
    
