﻿/*-------------------------------------------------------------------         
        Description			    :	 EsComponents.js 
        Create on			    :    13-02-2009
        Initial Code Created    :    Sathish.M
--------------------------------------------------------------------*/
//Call EStore Handlers
function funCallHandler(HandlerName)
{   
    try
    {    
        new Ajax.Request(HandlerName,
        {
            method   : 'get',
            onSuccess: funOnSuccess,            
            onFailure: funOnFailure
        });
    }
    catch(e)
    {
        alert('@ funCallHandler');       
    }
}
//On Success     
function funOnSuccess(originalRequest)
{   
    arrResponse=new Array();
    var orgRegText='';
    try
    {  
        arrResponse=originalRequest.responseText.split('|');
        if(arrResponse.length>0)
        {
            $('dvResult').innerHTML='';
            $('dvResult').innerHTML=arrResponse[0];
            if($('hdnSiteMap')!=null)
            {
                $('lblSiteMap').innerHTML=$F('hdnSiteMap');
                $('lblCtgDesc').innerHTML=$F('hdn3rdLvlDesc');                
            } 
            if($('hdnPrdDesc')!=null)
                $('h1PrdName').innerHTML=$F('hdnPrdDesc')
                
            switch($('dvResult').innerHTML)
            {   
                case 'Processing Wish List...':
                    window.location=$F('hdnAppPath')+'wish-list.html';
                break;
                case 'Processing Add to Cart...':
                    switch(arrResponse[1])
                    {
                        case 'Gen':
                            if($('hdnUsrId').value!=0)
                               window.location=$F('hdnAppPath')+'ViewCart.aspx';
                            else
                               window.location=$F('hdnAppPath')+'UsrLogin.aspx?mode=nomsrmnt';            
                        break;
                        case 'SA':
                        if($('hdnUsrId').value!=0)
                            //Redirection occures automatically to ViewCart.aspx if all the
                            //measurement flags are set to 'N' else it will move to MsrMntPg.aspx  
                            window.location=$F('hdnAppPath')+'MsrMntPg.aspx?'+arrResponse[2];  
                        else if($('hdnUsrId').value==0)
                            window.location=$F('hdnAppPath')+'UsrLogin.aspx?mode=samsrmnt&url=' + arrResponse[3];
                        break;  
                        case 'SL':
                        case 'GH':
                        case 'KR':
                        case 'SH':
                        case 'SU':
                        if($('hdnUsrId').value!=0)
                            window.location=$F('hdnAppPath')+'MsrMntPg.aspx?'+arrResponse[2];  
                        else if($('hdnUsrId').value==0)
                            window.location=$F('hdnAppPath')+'UsrLogin.aspx?mode=othrmsrmnt&url=' + arrResponse[3];
                        break;   
                    }
                break;                   
            }      
        }
    }
    catch(e)
    {
        //alert('@ funOnSuccess');       
    }         
}
//On Failure 
function funOnFailure(originalRequest)
{
    try
    {
        alert(originalRequest.responseText);
    }
    catch(e)
    {
        alert('@ funOnFailure');            
    }
}

//Change cursor style as hand symbol on mouse over
function funCursorStyle(CtrlId)
{
    try
    {
        $(CtrlId).style.cursor='pointer';
    }
    catch(e)
    {
        alert('@ funCursorStyle');            
    }
}

//Below functions are used to Pop up
function getBrowserSize() 
{
    try
    {
        var myWidth=0, myHeight=0;
	    if( typeof( window.innerWidth ) == 'number' ) 
	    {
		    /*Non-IE*/
            myWidth = screen.width;
            myHeight = screen.height-170;
	    } 
	    else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
	    {
		    /*IE 6+ in 'standards compliant mode'*/
		    myWidth = document.documentElement.clientWidth;
		    myHeight = document.documentElement.clientHeight;
	    } 
	    else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
	    {
		    /*IE 4 compatible*/
		    myWidth = document.body.clientWidth;
		    myHeight = document.body.clientHeight;
	    }
	    return new Array(myWidth, myHeight);
    }
    catch(e)
    {
        alert('@ getBrowserSize');
    }		
}
function revealElement(elementID) 
{ 
    try
    {
        var element = document.getElementById(elementID);
	    if (element != null) 
	    {
		    element.style.display = "block";
		    browserSize = getBrowserSize();
		    element.style.position = "absolute";
		    var lft = (browserSize[0]/2) - (element.offsetWidth/2);
		    var tp = browserSize[1]/1 - element.offsetHeight/1;
		    element.style.left= lft+'px';
		    element.style.top= tp+'px';
	    }
    }
    catch(e)
    {
        alert('@ revealElement');
    }
}


/// Products Page User Login 


function funUsrLoginCallHandler(HandlerName)
    {   
        try
        { 
            new Ajax.Request(HandlerName,
            {
                method   : 'get',
                onSuccess: funUsrLoginOnSuccess,            
                onFailure: funUsrLoginOnFailure
            });
        }
        catch(e)
        {
            alert('@ funUsrLoginCallHandler');       
        }
    }
        
    function funUsrLoginOnSuccess(originalRequest)
    {   
        try
        {   
            if(originalRequest.responseText!='Y')
                alert(originalRequest.responseText);
            else
            {
                $('dvLogin').hide();  
                if($('hdnPageName')!=null && $F('hdnPageName')=='Products')
                    window.location=location.href;
                else if($('hdnPageName')==null)
                    window.location=location.href;   
            }
        }
        catch(e)
        {
            alert('@ funUsrLoginOnSuccess');       
        }         
    }

    function funUsrLoginOnFailure(originalRequest)
    {   
        var Err;
        try
        {
            Err=originalRequest.responseText;
        }
        catch(e)
        {
            alert('@ funUsrLoginOnFailure');            
        }
    }
    function funChkLogin()
    {
      try
      {      
      var ErrMsg='',qStr='';      
      if($('txtLogin').value=='')    
         ErrMsg='\n Login-ID Missing/Invalid';
      if($('txtPwd').value=='')
         ErrMsg=ErrMsg + '\n Password';    
      if(ErrMsg!='')
      {  
        alert('Please submit the below required details.\r\n-------------------------------------------------\r'+ErrMsg);                      
        return false;            
      }
      else
      {
           qStr='login|';  
           if($('chRememberMe').checked==true)
                qStr=qStr+'1|';
           else if($('chRememberMe').checked==false)
                qStr=qStr+'0|';
           qStr=qStr+$F('txtLogin')+'|';
           qStr=qStr+$F('txtPwd')+'|';
           funUsrLoginCallHandler($F('hdnAppPath')+'GenetricHandler/ProductDetails.ashx?qStr='+qStr);
           return true;         
      }
      }
      catch(e)
      {
        alert('@ funChkLogin');
      }
    } 
    
    function funRevwPrdCallHandler(HandlerName)
    {   
        try
        { 
            new Ajax.Request(HandlerName,
            {
                method   : 'get',
                onSuccess: funRevwPrdOnSuccess,            
                onFailure: funRevwPrdOnFailure
            });
        }
        catch(e)
        {
            alert('@ funRevwPrdCallHandler');       
        }
    }
        
    function funRevwPrdOnSuccess(originalRequest)
    {   
        try
        {  
            if(originalRequest.responseText=='No')
            {
                alert('We are sorry you cannot give ratings until you have made atleast one purchase with us.');
                $('dvVisitorsReview').hide();
            }
            else if(originalRequest.responseText=='Yes')
            {
                alert('Thanks for your reviews.');
                funResetVisitorsReview();
                $('dvVisitorsReview').hide();
            }
        }
        catch(e)
        {
            alert('@ funRevwPrdOnSuccess');       
        }         
    }

    function funRevwPrdOnFailure(originalRequest)
    {
        var Err;
        try
        {
            Err=originalRequest.responseText;
        }
        catch(e)
        {
            alert('@ funRevwPrdOnFailure');            
        }
    }

function funSubmitVisitorsReview()
{   
    var ErrMsg='';
    try
    {  
        if(rated=='')
            ErrMsg='\n Rating';    
        if($F('txtVisitorsComments')=='')        
            ErrMsg=ErrMsg+'\n Comment';    
        if(ErrMsg!='')
        {
            alert('Please submit the below required details.\r\n-------------------------------------------------\r'+ErrMsg);  
            return false;            
        }
        else
        {
           var qStr='';
           qStr='revwprd|';
           qStr=qStr+$F('hdnPrdId')+'|';
           switch($('hdnRating').value)
           {
                case 'Okay':
                    qStr=qStr+'1|';
                    break;
                case 'Good':
                    qStr=qStr+'2|';
                    break;
                case 'Great':
                    qStr=qStr+'3|';
                    break;
                case 'Excellent':
                    qStr=qStr+'4|';
                    break;
                case 'Wow':
                    qStr=qStr+'5|';
                    break;
                    
           }
           qStr=qStr+$F('txtVisitorsComments')+'|';
           funRevwPrdCallHandler($F('hdnAppPath')+'GenetricHandler/ProductDetails.ashx?qStr='+qStr);
           return true;        
        }
    }
    catch(e)
    {
        alert('@ funSubmitVisitorsReview');
    }
}

function funResetVisitorsReview()
{
    try
    {  
        rated='';
        $('_1').className = '';
        $('_2').className = '';
        $('_3').className = '';
        $('_4').className = '';
        $('_5').className = '';
        $('hdnRating').value='';
        $("spnRateStatus").innerHTML='';
        $('txtVisitorsComments').value='';
    }
    catch(e)
    {
        alert('@ funResetVisitorsReview');
    }    
} 



function funRefrAFrndCallHandler(HandlerName)
    {   
        try
        {    
            new Ajax.Request(HandlerName,
            {
                method   : 'get',
                onSuccess: funRefrAFrndOnSuccess,            
                onFailure: funRefrAFrndOnFailure
            });
        }
        catch(e)
        {
            alert('@ funRefrAFrndCallHandler');       
        }
    }
        
    function funRefrAFrndOnSuccess(originalRequest)
    {   
        try
        {   
            $('dvRefFrnd').hide();
        }
        catch(e)
        {
            alert('@ funRefrAFrndOnSuccess');       
        }         
    }

    function funRefrAFrndOnFailure(originalRequest)
    {
        var Err;
        try
        {
            Err=originalRequest.responseText;
        }
        catch(e)
        {
            alert('@ funRefrAFrndOnFailure');            
        }
    }

    function funReferAFrnd()
    { 
        var ErrMsg='';
        try
        {
            if($F('txtYourName')=='')        
                ErrMsg='\n Name';    
            if($F('txtYourEmailId')=='' || emailCheck($F('txtYourEmailId'))==false)    
                ErrMsg=ErrMsg + '\n EmailId is Missing/Invalid';   
            if($F('txtMessage')=='')    
                 ErrMsg=ErrMsg + '\n Message';    
            if($F('txtFrndName')=='')    
                ErrMsg=ErrMsg + '\n Friends Name';     
            if($F('txtFrndEmailId')=='' || emailCheck($F('txtFrndEmailId'))==false)    
                ErrMsg=ErrMsg + '\n Friends EmailId is Missing/Invalid';   
//            if(funInvalidSplSymbols($F('txtMessage')==false)
//                ErrMsg=ErrMsg + '\n Remove special characters in message'; 
            if(ErrMsg!='')
            {
                alert('Please submit the below required details.\r\n-------------------------------------------------\r'+ErrMsg);  
                return false;            
            }
            else
            {   
               alert('Your referral email has been sent.');
               var qStr='';
               qStr='refafrnd|';
               qStr=qStr+$F('txtYourName')+'|';
               qStr=qStr+$F('txtYourEmailId')+'|';
               qStr=qStr+$F('txtFrndName')+'|';
               qStr=qStr+$F('txtFrndEmailId')+'|';
               qStr=qStr+$F('txtMessage')+'|';
               if('<%=Session["EStoreId"] %>'==87)
                funRefrAFrndCallHandler('../EStoreUI/GenetricHandler/ProductDetails.ashx?qStr='+qStr);
               else
                funRefrAFrndCallHandler('../GenetricHandler/ProductDetails.ashx?qStr='+qStr);
                
               funResetReferAFrnd();
               $('dvRefFrnd').hide();
               return true;        
            }
        }
        catch(e)
        {
            alert('@ funReferAFrnd');
        }
    }
    function funResetReferAFrnd()
    {
        try
        {   
            $('txtYourName').value='';        
            $('txtYourEmailId').value='';
            $('txtMessage').value='';    
            $('txtFrndName').value='';    
            $('txtFrndEmailId').value='';
        }
        catch(e)
        {
            alert('@ funResetReferAFrnd');
        }            
    }
    
    function funInvalidSplSymbols(param)
    {
        try
        {
            flg=0;
            str='';
            spc=''
            arw='';
            for (var i=0;i< param.length;i++)
            {
                cmp="<>?;'%$&"
                tst=param.substring(i,i+1)
                if (cmp.indexOf(tst)>-1)
                {
                    flg++;
                    str+=' '+tst;
                    spc+=tst;
                    arw+='^';
                }
                else
                {
                    arw+=' ';
                }
            }
            if (flg!=0)
            {
                if (spc.indexOf(' ')>-1) 
                {
                    str+=' and a space';
                }
                return false;
            }
        }
        catch(e)
        {
            alert('@ funInvalidSplSymbols');
        }
     }