var req;
var reqType;
var loadingImg = '<img src=./images/ajax-loader.gif height=15px width=200px />';

function detectClientBrowser()
{
    var appName = navigator.appName; 
    var objInfoMsg = document.getElementById("infoMessage");
    if(appName == 'Microsoft Internet Explorer'){
    }else if(appName != 'Microsoft Internet Explorer'){
        objInfoMsg.style.display = 'inline';
        objInfoMsg.innerHTML='<font face="Arial" color="#FF0000" style="font-size:15px;">'+
                                                         '<b> We recommend you to use Internet Explorer for better results</b></font>';
        setTimeout("doNothing()",10000);
    }    
}
function doNothing(){
        document.getElementById("infoMessage").style.display = 'none';
        document.getElementById("infoMessage").innerHTML = '';
}

function handleEnter() 
{ 
    if(event.keyCode == 13)
      validateLogin();
}  

function myAccount(obj)
{
    var userName = document.getElementById("validateUserNameNull").value;
    var pageId = document.getElementById("idLogin").value;
    if(userName == "NOTLOGGEDIN"){
       callTip('account',obj);
    }
    else{
        document.getElementById(pageId).action = "/PersonalInformation?Info=Info";
        document.getElementById(pageId).submit();
    }
}

//To handle user logout

function logOut(reqType)
{   
    document.getElementById("reqType").value = reqType;
    
    var url = "/Logout.jsp";
    
    if(window.XMLHttpRequest) { req = new XMLHttpRequest(); } 
    else if(window.ActiveXObject) { req = new ActiveXObject("Microsoft.XMLHTTP");  }
    
    req.open("POST",url,true);
    req.onreadystatechange = callbackLogout;
    
    //req.send(null);
     //Change By Avdhoot on 18/10/08
     // to deal with 411 status code, which comes with FF
     // so need to set content length
     if(window.ActiveXObject) req.send(null);
     else if(window.XMLHttpRequest) req.send(url.length);
}
function callbackLogout()
{
    if(req.readyState == 4){
        if(req.status == 200){ 
           var msg = req.responseText;
           //show logout msg to user
           document.getElementById("infoMessage").style.display = 'inline';
           document.getElementById("infoMessage").innerHTML='<font face="Arial" color="#FF0000" style="font-size:15px;">'+
                                                             '<b> Logged Out Successfully!</b></font>';
           setTimeout("doNothing()",10000);
           
           document.getElementById("validateUserNameNull").value = 'NOTLOGGEDIN';
           document.getElementById("validateUserIdNull").value = 'NOTLOGGEDIN';
           reqType = document.getElementById("reqType").value;
           //verify value of <input type="hidden" id="idRedirect" value="no"> variable
           //from each page
           //if value == no, do not redirect to user
           //if value == yes, redirect user to home
           //if other value, special requests
           
            var idRedirect = document.getElementById("idRedirect").value;
            var pageId = document.getElementById("idLogin").value;
            if(idRedirect == "no"){}
            else if(idRedirect == "yes")
            {   
                 document.getElementById(pageId).action = "/EnalHome.jsp"; 
                 document.getElementById(pageId).submit();
            }
            else if(idRedirect == "commentBox"){ 
              document.getElementById("userComment").style.display = 'none'; 
            }else if(idRedirect == "collaborate"){
               location.reload(true);
            } 
            
            if(reqType == 'normalLogout'){
                document.getElementById("tdLogin").innerHTML = '<a href=# class=footerFont onclick=callTip("login",this); style=cursor:pointer;>'+
                                                               '<font face=Verdana color=#FFFFFF style=font-size:11px;>'+
                                                               'Login</font></a>';
                document.getElementById("idJoinLogOutMyAct").innerHTML = '<font face=Latha size=2 color=#ffeaae >|</font>'+
                                                                         '<a href=EnalRegisterNow.jsp class=footerFont style=cursor:pointer;>'+
                                                                         '<font face=Verdana color=#FFFFFF style=font-size:11px;>Join Now</font>'+
                                                                         '</a><font face=Latha size=2 color=#ffeaae> |</font>'+
                                                                         '<a href=# class=footerFont onclick=myAccount(); style=cursor:pointer;>'+
                                                                         '<font face=Verdana color=#FFFFFF style=font-size:11px; margin-top:15px;>'+
                                                                         'My Account</font></a>';      
           }
        }
    }
}

/*==============================================================================================================*/
/*Login Validation Using ToolTip*/
function callTipCollaborate(obj)
{
    callTip('collabLoginDown',obj);
}
function callTip(reqType,obj)
{  
          var idRedirect = document.getElementById("idRedirect").value;
          if(idRedirect == 'collaborate' && reqType == 'login'){
              reqType = 'collabLoginUp';
          }else if(idRedirect == 'collaborate' && reqType == 'collabLoginDown'){
              reqType = 'collabLoginDown';
          }else if(idRedirect == 'registerNow' && reqType == 'registerNow'){
              reqType = 'registerNowDown';
          }else if(idRedirect == 'registerNow' && reqType == 'login'){
              reqType = 'registerNowUp';
          }else if(idRedirect == 'registerPartner' && reqType == 'login'){
              reqType = 'registerPartner';
          }
   
          var headerLoginStr = '<div align="left" style="width:250px;">'+
                                '<div align="left" style="width:250px;display:inline;" id="trsignin">'+
                                    '<div style="width:250px;height:30px;">'+
                                        '<div style="width:60px;float:left;padding-left:05px;padding-top:09px;"><font face=arial style="font-size:11px;">Email Id</div>'+
                                        '<div style="width:110px;float:left;padding-top:09px;"><input type="text" id="uname" name="uname" value="" style="width:100px;height:14px;" onkeypress="handleEnter();"></div>'+
                                        
                                        '<div style="float:right;width:10px;">'+
                                            '<a style="cursor:pointer;padding-top:00px;" onClick="UnTip();">X</a>' +
                                        '</div>'+
                                    '</div>'+
                                    '<div style="width:250px;height:25px;float:left;padding-left:05px;padding-top:05px;">'+
                                        '<div style="width:60px;float:left;"><font face=arial style="font-size:11px;">Password</div>'+
                                        '<div style="width:110px;float:left;"><input type="password" id="pwd" name="pwd" value="" style="width:100px;height:14px;" onKeyPress="handleEnter();"></div>'+
                                        '<div style="width:50px;float:left;padding-left:05px;padding-top:02px;">'+
                                            '<a class="link" onClick="validateLogin();" style="cursor:pointer;">'+
                                               'Sign In'+
                                              '</a>'+
                                        '</div>'+
                                      '</div>'+
                                     '<div style="width:80px;padding-left:75px;">'+
                                            '<a onClick="forgotPassword()"; style="cursor:pointer;"><font face="Arial" size="1" color="#FF0000" class="loginFont">Forgot Password?</font>'+
                                     '</a></div>'+
                                '</div>'+
                                '<div align="left" style="width:250px;display:none;" id="trfpassword">'+
                                    '<div style="width:60px;float:left;padding-left:05px;padding-top:09px;"><font face=arial style="font-size:11px;">Email Id</div>'+
                                    '<div style="width:110px;float:left;padding-top:09px;"><input type="text" id="emaiAdd" name="emaiAdd" value="" style="width:100px;height:14px;"></div>'+
                                    '<div style="width:60px;float:left;padding-top:12px;">'+
                                    '<a class="link" onClick="sendMePassword();" style="cursor:pointer;">'+
                                       'Send'+
                                      '</a>'+
                                    '</div>'+
                                    '<div style="float:right;width:10px;">'+
                                            '<a style="cursor:pointer;" onClick="UnTip();">X</a>' +
                                        '</div>'+
                                   '</div>'+
                                '<div id="info" style="float:left;width:230px; padding-left:15px;">&nbsp;</div>'+
                                '</div>';
              
              var appName = navigator.appName;                 
              if(reqType == 'login')
              {
                    document.getElementById("reqType").value = 'login';
                    if(appName == 'Microsoft Internet Explorer'){
                        Tip(headerLoginStr, FIX, [485,25], ABOVE, false); //width,height
                    }else if(appName != 'Microsoft Internet Explorer'){
                        Tip(headerLoginStr, FIX, [495,25], ABOVE, false);
                    }      
                    
              } 
              else if(reqType != 'login')
              {
                   if(reqType == 'account')
                   {
                        document.getElementById("reqType").value = reqType;
                        if(appName == 'Microsoft Internet Explorer'){
                            Tip(headerLoginStr, FIX, [600,25], ABOVE, false);
                        }else if(appName != 'Microsoft Internet Explorer'){
                            Tip(headerLoginStr, FIX, [610,25], ABOVE, false);
                        }  
                   }
                   else if(reqType == 'buy'){ 
                        document.getElementById("reqType").value = reqType;
                        Tip(headerLoginStr, FIX, [690,400], ABOVE, false);
                   }else if(reqType == 'rent'){ 
                        document.getElementById("reqType").value = reqType;
                        Tip(headerLoginStr, FIX, [690,450], ABOVE, false);
                   }else if(reqType == 'collabLoginDown'){ 
                        document.getElementById("reqType").value = reqType;
                        Tip(headerLoginStr, FIX, CalcFixXY(obj), ABOVE, false);
                   }else if(reqType == 'collabLoginUp'){ 
                        document.getElementById("reqType").value = reqType;
                        if(appName == 'Microsoft Internet Explorer'){
                            Tip(headerLoginStr, FIX, [485,25], ABOVE, false);
                        }else if(appName != 'Microsoft Internet Explorer'){
                            Tip(headerLoginStr, FIX, [495,25], ABOVE, false);
                        } 
                   }else if(reqType == 'registerNowUp'){
                        document.getElementById("reqType").value = reqType;
                        if(appName == 'Microsoft Internet Explorer'){
                            Tip(headerLoginStr, FIX, [485,25], ABOVE, false);
                        }else if(appName != 'Microsoft Internet Explorer'){
                            Tip(headerLoginStr, FIX, [495,25], ABOVE, false);
                        }
                  }else if(reqType == 'registerNowDown'){
                        document.getElementById("reqType").value = reqType;
                        Tip(headerLoginStr, FIX, CalcFixXY(obj), ABOVE, false);
                  }else if(reqType == 'registerPartner'){ 
                        document.getElementById("reqType").value = reqType;
                        if(appName == 'Microsoft Internet Explorer'){ 
                            Tip(headerLoginStr, FIX, [485,25], ABOVE, false);
                        }else if(appName != 'Microsoft Internet Explorer'){
                            Tip(headerLoginStr, FIX, [495,25], ABOVE, false);
                        }
                  }
                  else{
                        document.getElementById("reqType").value = reqType;
                        Tip(headerLoginStr, FIX, CalcFixXY(obj), ABOVE, false);
                  }  
              }
}
   
function CalcFixXY(obj) 
{
     var curleft = curtop = 0;
     if (obj.offsetParent) 
      {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
      }
      return [curleft,curtop];
}


function validateLogin()
{
    var userName="",url="",password="";
    
    var objUname = document.getElementById("uname");
    var objPwd = document.getElementById("pwd");
    
    userName = objUname.value; 
    password = objPwd.value; 
     
    if(objUname.value == "Email Id")
    {  alert('Please Enter User EmailId!'); objUname.focus(); return false; }
    else if(objUname.value == "")
    { alert('Please Enter User EmailId!'); objUname.focus(); return false; }
    else if(objUname.value != "")
    {  if(objUname.value.indexOf(" ") != -1) 
          { alert('No Blank Space Allowed');  objUname.focus();  return false; }
    }
   
    if(objPwd.value=="password")
    {  alert('Please Enter Password!');  objPwd.focus(); return false; }
    else if(objPwd.value=="")
    {  alert('Please Enter Password!');  objPwd.focus(); return false; }
    else if(objPwd.value != "")
    {  if(objPwd.value.indexOf(" ") != -1) 
          {  alert('No Blank Space Allowed');objPwd.value = ""; objPwd.focus();  return false; }
    }
    
     url = "/ValidateLogin?userName="+userName+"&password="+password;
     
     if(window.XMLHttpRequest) { req = new XMLHttpRequest(); }
     else if(window.ActiveXObject) { req = new ActiveXObject("Microsoft.XMLHTTP");  }
     
     req.open("POST",url,true);
     req.onreadystatechange=callbackValidateLogin;
     
     //req.send(null);
     //Change By Avdhoot on 18/10/08
     // to deal with 411 status code, which comes with FF
     // so need to set content length
     if(window.ActiveXObject) req.send(null);
     else if(window.XMLHttpRequest) req.send(url.length);
} 
 
function callbackValidateLogin()
{ var objInfo = document.getElementById("info");
  objInfo.style.display = 'inline';  
  
  if(req.readyState == 0){
        objInfo.innerHTML = loadingImg;
  }else if(req.readyState == 1){
        objInfo.innerHTML = loadingImg;
  }else if(req.readyState == 2){
        objInfo.innerHTML = loadingImg;
  }else if(req.readyState == 3){
        objInfo.innerHTML = loadingImg;
  }else if(req.readyState == 4){ 
        if(req.status==200)
         {   objInfo.style.display = 'none';   
             populateValidated(); }
  }
}

function populateValidated()
{
    var jsonData = req.responseText;
    var myJSONObject = eval('('+jsonData+')');
    var info = myJSONObject.UserData;
   
    if(info == "FAIL"){
         info = "";
         document.getElementById("info").style.display='inline';
         document.getElementById("info").innerHTML='<font face="Verdana" color="#0091D1" style="font-size:11px;">Wrong EmailId or Password!</font>';
         return false; 
    }else if(info == "WAIT"){ 
         info = "";
         document.getElementById("info").style.display='inline';
         document.getElementById("info").innerHTML='<font face="Verdana" color="#0091D1" style="font-size:11px;">Please Try After 1 Minute!</font>';
         return false; 
    }else if(info == "SUCCESS"){
         UnTip();
         info = "";
         var uname = myJSONObject.UserName;
         var ufname = myJSONObject.UserFirstName;
         var uid = myJSONObject.UserId;
         
         document.getElementById("validateUserNameNull").value = uname;
         document.getElementById("validateUserIdNull").value = uid;
         document.getElementById("tdLogin").innerHTML = '<font face=Latha style=font-size:12px; color=#ffeaae>'+
                                                        '<b> Welcome '+ufname+'</b></font>';
         document.getElementById("idJoinLogOutMyAct").innerHTML = '<font face=Latha size=2 color=#ffeaae>|</font>'+
                                                                 '&nbsp;<a href=# class=footerFont style=cursor:pointer; onclick=logOut("normalLogout");>'+
                                                                 '<font face=Verdana color=#FFFFFF style=font-size:11px; margin-top:10px;>Logout</font>'+
                                                                 '</a>&nbsp;<font face=Latha size=2 color=#ffeaae> |</font>'+
                                                                 '&nbsp;<a href=/PersonalInformation?Info=Info class=footerFont style=cursor:pointer;>'+
                                                                 '<font face=Verdana color=#FFFFFF style=font-size:11px; margin-top:15px;>My Account</font>'+
                                                                 '</a>';                                             
         var reqType = document.getElementById("reqType").value;
         
         if(reqType == 'login'){
         }else if(reqType == 'comment'){
         }else if(reqType == 'favourites'){ 
             addToFavouriteServlet();
         }else if(reqType == 'commentShow'){
             acceptComment('true',this);
         }else if(reqType == 'commentSubmit'){
             acceptComment('submit',this);
         }else if(reqType == 'buy'){
             var userName = document.getElementById("validateUserNameNull").value;
             if(userName == 'demouser@e-nalanda.com'){
                     Tip(demoUserInfo, FIX, [690,400], ABOVE, false);
             }else if(userName != 'demouser@e-nalanda.com'){
                 buyNrentaTopic('1',this);
             }
         }else if(reqType == 'rent'){
             var userName = document.getElementById("validateUserNameNull").value;
             if(userName == 'demouser@e-nalanda.com'){
                  Tip(demoUserInfo, FIX, [690,450], ABOVE, false);
             }else if(userName != 'demouser@e-nalanda.com'){
                  buyNrentaTopic('2',this);
             }
         }else if(reqType == 'account'){
            myAccount(this);
         }else if(reqType == 'collabLoginDown'){
           location.reload(true);
         }else if(reqType == 'collabLoginUp'){
           location.reload(true);
         }else if(reqType == 'registerNowDown' || reqType == 'registerNowUp' || reqType == 'registerPartner'){
           
           var pId = document.getElementById("idLogin").value;
           document.getElementById(pId).action = '/EnalHome.jsp';
           document.getElementById(pId).submit();
         }
    }
}    



