﻿function RemoveAddressJQuery(idAddress,imgLoading )
{
    obLoading = document.getElementById(imgLoading);
    if (obLoading!=null) obLoading.style.visibility = 'visible';
    
    jQuery.ajax({ 
        url:  "/Async.ashx?mod=removeaddress&struct=" + idAddress, 
        type: "GET",
        cache: false, 
        success: function(results) 
        {
            if (obLoading!=null) obLoading.style.visibility ='hidden';
            switch (results.split('|')[0])
            {
                case "Ok":
                    window.location = window.location;
                break;
                case "error":
                    obError.innerHTML = results.split('|')[1];
                break;
            }
        }
        })   
}
function LoadChangeModel(idProduct,levelGroup,filterList,imgLoading,idCode,idTitle,idTitle2)
{
    obCode = document.getElementById(idCode);
    obTitle = document.getElementById(idTitle);
    obTitle2 = document.getElementById(idTitle2);
    obLoading = document.getElementById(imgLoading);
    if (obLoading!=null) obLoading.style.visibility = 'visible';
jQuery.ajax({ 
            url:  "/functions.aspx?mod=changemodel&struct="+idProduct+"|"+(levelGroup+1)+"|"+unescape(filterList),
            type: "GET",
            cache: false, 
            success: function(results) 
            {
                if (obLoading!=null) obLoading.style.visibility ='hidden';
                switch(results.split('|')[0])
                {
                    case 'error':
                        LoadMessageFancyBox(0,'error');
                    break;
                    case "ok":
                    
                        SetIdProductCart(results.split('|')[1]);                    
                        if(obCode!=null) obCode.innerHTML = results.split('|')[2];
                        if(obTitle!=null) obTitle.innerHTML = results.split('|')[3];
                        if(obTitle2!=null) obTitle2.innerHTML = results.split('|')[3];
                        
                        for(i=levelGroup+1;i<6;i++)
                        {
                            if(document.getElementById('divGroup'+i)!=null)
                            {
                                $('#divGroup'+i).remove();
                            }
                        }
                        
                        
                        //$('#divGroup'+(levelGroup+1)).remove();
                        $('#divGroup'+levelGroup).after(results.split('|')[4]);                    
                    break;
                    default:
                    break;                                    
                }                    
            }
            }) 
}

function LoadStateJQuery(idCountry,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=state&IdCountry=" + idCountry, 
            type: "GET",
            cache: false, 
            success: function(results) 
            {
            //alert(results);
                //ob.innerHTML = results;
                $('#'+sIdObjectRender).html(results)
                if (obLoading!=null) obLoading.style.display='none';
            }
            })     
    }
}
function LoadMessageFancyBox(alert)
{
	$.fancybox({
		'titlePosition'		: 'inside',
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
	    'autoDimensions'	: true,
		'overlayColor'      : '#000',
		'overlayOpacity'	: 0.5,
		'href'              : '/modules/fancybox/Message.aspx?eId=0&alert='+alert
		});
}

function CheckLogin(u, p, 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);
    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 "error":
                    obError.innerHTML = results.split('|')[1];
                break;
            }
        }
        })     
}

function RegisterJQuery(user,name,lastname,email ,password ,password2 ,chkNewsLetter ,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=register&struct=" + encodeURIComponent(user) + "|"
                             + encodeURIComponent(name) + "|" 
                             + encodeURIComponent(lastname) + "|" 
                             + encodeURIComponent(email) + "|" 
                             + encodeURIComponent(password) + "|"
                             + encodeURIComponent(password2) + "|" 
                             + encodeURIComponent(chkNewsLetter);
    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 = '/Default.aspx' ;
                    //LoadMessageFancyBox('Gracias por Registrarte! La operación finalizó con éxito.');
                break;
                case "error":
                    obError.innerHTML = results.split('|')[1];
                break;
            }
        }
        })     
}

function SendMailContactAsyn(n,a ,m ,me,chk, urlRedirect,obError )
{
    obError = document.getElementById(sIdError);
    if (obError!=null) obError.innerHTML='';
    
    var sData = "mod=Contact&struct=" + encodeURIComponent(n) + "|" + encodeURIComponent(a) + "|" + encodeURIComponent(m) + "|" + encodeURIComponent(me) + "|" + encodeURIComponent(chk) ;
    jQuery.ajax({ 
        url:  "/Async.ashx", 
        type: "POST",
        data: sData,
        cache: false, 
        success: function(results) 
        {
            switch (results.split('|')[0])
            {
                case "Ok":
                    window.location = urlRedirect;
                break;
                case "error":
                    obError.innerHTML = results.split('|')[1];
                break;
            }
        }
        })   
}

function sendRecoverPasswordEmailJQuery(email, 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=RecoverPassword&struct=" + encodeURIComponent(email);
    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":
                   LoadMessageFancyBox('Se ha generado una nueva clave, chequea tu cuenta de correo.');
                break;
                case "error":
                    obError.innerHTML = results.split('|')[1];
                break;
            }
        }
        })   
}


function LoadLoginFancyBox(urlredirect)
{
	$.fancybox({
		'titlePosition'		: 'inside',
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
	    'autoDimensions'	: true,
		'overlayColor'      : '#000',
		'overlayOpacity'	: 0.5,
		'href'              : '/modules/fancybox/Login.aspx?urlredirect=' + urlredirect
		});
}

function LoadContactFancyBox(urlredirect)
{
	$.fancybox({
		'titlePosition'		: 'inside',
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
	    'autoDimensions'	: true,
		'overlayColor'      : '#000',
		'overlayOpacity'	: 0.5,
		'href'              : '/modules/fancybox/Contact.aspx?urlredirect=' + urlredirect
		});
}

function LoadRegisterFancyBox(urlredirect)
{
	$.fancybox({
		'titlePosition'		: 'inside',
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
	    'autoDimensions'	: true,
		'overlayColor'      : '#000',
		'overlayOpacity'	: 0.5,
		'href'              : '/modules/fancybox/Register.aspx?urlredirect=' + urlredirect
		});
}

function LoadPasswordRecoverFancyBox(urlredirect)
{
	$.fancybox({
		'titlePosition'		: 'inside',
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
	    'autoDimensions'	: true,
		'overlayColor'      : '#000',
		'overlayOpacity'	: 0.5,
		'href'              : '/modules/fancybox/PasswordRecover.aspx?urlredirect=' + urlredirect
		});
}

function LoadVideoFancyBox(linkvideo)
{
	/*$.fancybox({
		'titlePosition'		: 'inside',
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
	    'autoDimensions'	: true,
		'overlayColor'      : '#000',
		'overlayOpacity'	: 0.5,
		'href'              : '/modules/fancybox/video.aspx?id=1'
		});*/
	$.fancybox({
		'titlePosition'		: 'inside',
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
	    'autoDimensions'	: true,
	    'scrolling'         : 'no',
	    'showCloseButton'   : true ,
		'overlayColor'      : '#000',
		'overlayOpacity'	: 0.5,
		'href'              : '/modules/fancybox/video.aspx?alert='+linkvideo
		});	
}

function AddFavorite(iIdLang, iIdProduct, idCheckbox)
{
    var action;
    if (document.getElementById(idCheckbox).checked)
        action = 'addfavourite';
    else
        action = 'removefavourite';
        
    jQuery.ajax({ 
            url:  "/Async.ashx?mod=" + action + "&lId="+ iIdLang + "&pId=" + iIdProduct,
            type: "GET",
            cache: false, 
            success: function(results) {
             switch(results.split('|')[0])
            {
                case '0':
                    document.getElementById(idCheckbox).checked = false;
                    break;
                case '1':
                    document.getElementById(idCheckbox).checked = true;
                    break;
            }
            LoadMessageFancyBox(results.split('|')[1]);
            
       } 
    });
    
}


function AddNewsletter(iIdLang, sEmail, sIdDivLoading)
{
    obLoading = document.getElementById(sIdDivLoading);
    if (obLoading!=null) obLoading.style.display='block';
    
    jQuery.ajax({ 
            url:  "/Async.ashx?mod=addnewsletter&lId="+ iIdLang + "&email=" + sEmail,
            type: "GET",
            cache: false, 
            success: function(results) {
            if (obLoading!=null) obLoading.style.display='none';
            LoadMessageFancyBox(results.split('|')[1]);            
       } 
    });
    
}

 

function SetVote(iIdLang, iIdProduct, iVote,imgControl)
{
    jQuery.ajax({ 
            url:  "/Async.ashx?mod=setvote&lId="+ iIdLang + "&pId=" + iIdProduct + "&vote=" + iVote,
            type: "GET",
            cache: false, 
            success: function(results) {
            switch(results.split('|')[0])
            {
                default:
                    imgControl.src = '/img/ui/' + results[0] + 'stars.png';
                    alert(results.split('|')[1]);
                break;                                    
            
            }
       } 
    });

}
