﻿
function AsyncAddUser(name,username,email,pass,address,state,city,phone,cellphone,newsletter, urlRedirect,sIdError, sIdDivLoading)
{
     
            
    obLoading = document.getElementById(sIdDivLoading);
    obError = document.getElementById(sIdError);
    if (obLoading!=null) obLoading.style.display ='block';

jQuery.ajax({ 
            url:  "/async.ashx?mod=register&struct=" + encodeURIComponent(name) + "|" + encodeURIComponent(username) + "|" + encodeURIComponent(email) + "|" + encodeURIComponent(pass) +  "|" + encodeURIComponent(address) + "|" + encodeURIComponent(state) + "|" + encodeURIComponent(city) + "|" + encodeURIComponent(phone) +"|" + encodeURIComponent(cellphone) + "|" + encodeURIComponent(newsletter) + "|" + encodeURIComponent(urlRedirect), 
            type: "GET",
            cache: false, 
            success: function(results) 
            {
                switch(results.split('|')[0])
                {
                    case 'error':
                        if (obLoading!=null)obLoading.style.display ='none';
                        if (obError!=null)obError.style.display ='block';
                        SetValueInput(sIdError, results.split('|')[1]);
                    break;
                    case 'registerok':
                         if (obLoading!=null)obLoading.style.display ='none';                         
                         window.location = urlRedirect;
                    break;
                    default:
                        if (obLoading!=null)obLoading.style.display ='none';
                        if (obError!=null)obError.style.display ='block';
                        SetValueInput(sIdError, "Se produjo un error, No se puede realizar esta operación");
                    break;                                    
                }            
            }
            }) 
}


function AsyncRegisterClient(dni,clientid,zipcode,urlRedirect, sIdError, sIdDivLoading)
{
     
            
    obLoading = document.getElementById(sIdDivLoading);
    obError = document.getElementById(sIdError);
    if (obLoading!=null) obLoading.style.display ='block';

jQuery.ajax({ 
            url:  "/async.ashx?mod=register_client&struct=" + encodeURIComponent(dni) + "|" + encodeURIComponent(clientid) + "|" + encodeURIComponent(zipcode)  +"|" + encodeURIComponent(urlRedirect), 
            type: "GET",
            cache: false, 
            success: function(results) 
            {
                switch(results.split('|')[0])
                {
                    case 'error':
                        if (obLoading!=null)obLoading.style.display ='none';
                        if (obError!=null)obError.style.display ='block';
                        //SetValueInput(sIdError, results.split('|')[1]);
                    break;
                    case 'registerok':
                         if (obLoading!=null)obLoading.style.display ='none';
                         window.location = results.split('|')[1];
                    break;
                    default:
                        if (obLoading!=null)obLoading.style.display ='none';
                        if (obError!=null)obError.style.display ='block';
                        //SetValueInput(sIdError, "Se produjo un error, No se puede realizar esta operación");
                    break;                                    
                }            
            }
            }) 
}



function AsyncLogin(u, p, remember,urlRedirect, sIdError, sIdDivLoading)
{
    obLoading = document.getElementById(sIdDivLoading);
    obError = document.getElementById(sIdError);
    if (obLoading!=null) obLoading.style.display='block';
    if (obError!=null) obError.innerHTML='';
    
    var sData = "mod=login&struct=" + encodeURIComponent(u) + "|" + encodeURIComponent(p) + "|" + encodeURIComponent(remember);
    jQuery.ajax({ 
        url:  "/async.ashx", 
        type: "POST",
        data: sData,
        cache: false, 
        success: function(results) 
        {
            if (obLoading!=null) obLoading.style.display='none';
            switch (results.split('|')[0])
            {
                case "loginok":
                    window.location = urlRedirect;
                    break;
                case "loginok-showclientid":
                    window.location = '/Default.aspx?jsfunction=MessageBoxRegisterClient';
                    break;
                case "error":
                    obError.innerHTML = results.split('|')[1];
                break;
            }
        }
        })     

}

function AsyncPasswordRecover(u,urlRedirect, sIdError, sIdDivLoading)
{
    obLoading = document.getElementById(sIdDivLoading);
    obError = document.getElementById(sIdError);
    if (obLoading!=null) obLoading.style.display='block';
    if (obError!=null) obError.innerHTML='';
    
    var sData = "mod=passwordrecover&struct=" + encodeURIComponent(u);
    jQuery.ajax({ 
        url:  "/async.ashx", 
        type: "POST",
        data: sData,
        cache: false, 
        success: function(results) 
        {
            if (obLoading!=null) obLoading.style.display='none';
            switch (results.split('|')[0])
            {
                case "ok":
                    window.location = urlRedirect;
                    break;
                case "error":
                    obError.innerHTML = results.split('|')[1];
                break;
            }
        }
        })     
}



function AsyncAddNewsletter(iIdLang, sEmail)
{
    jQuery.ajax({ 
            url:  "/Async.ashx?mod=addnewsletter&lId="+ iIdLang + "&email=" + sEmail,
            type: "GET",
            cache: false, 
            success: function(results) {
            var value = results.split('|')[0];
            var message = results.split('|')[1];
            if (value == 0)
                MessageBoxOk(message,'');
            else
                MessageBoxError(message,'');
       } 
    })

}    
    
function AsyncGetCards(sType, iPage)
{
    jQuery.ajax({ 
            url:  "/Async.ashx?mod=getcards&lId="+ iIdLang + "&email=" + sEmail,
            type: "GET",
            cache: false, 
            success: function(results) {
            var value = results.split('|')[0];
            var message = results.split('|')[1];
            if (value == 0)
                MessageBoxOk(message,'');
            else
                MessageBoxError(message,'');
       } 
    })
 }   
    
 
 function LoadDepartmentJQuery(idState,sIdObjectRender,sIdObLoading)
{
    ob = document.getElementById(sIdObjectRender);
    obLoading = document.getElementById(sIdObLoading);
    if (obLoading!=null) obLoading.style.display='block';
    if (ob!=null)
    {
        ob.innerHTML = "";
        jQuery.ajax({ 
            url:  "/Async.ashx?mod=department&idState=" + idState, 
            type: "GET",
            cache: false, 
            success: function(results) 
            {
                $('#'+sIdObjectRender).html(results)
                if (obLoading!=null) obLoading.style.display='none';
            }
            })     
    }
}
