// largura da lista
var listWidth = 126;
// numero de listas
var listNumber = 6;

// largura das listas todas
var fullWidth = listWidth * listNumber - listWidth;

function resizeSlider()
{
	var width = $('slider').childElements().length * 126;
	listNumber = $('slider').childElements().length;
	$('slider').style.width = width + 'px';
	$('slider').style.left = '0px';
}

function goLeft()
{
	var width = $('slider').style.left;
	width = width.substring(0, width.length-2);
	if(width < 0 && width < listWidth && width%126!=0)
	{
		var navigate = parseInt(width)+listWidth;
		new Effect.Move('slider', {
		  x: -navigate, y: 0, mode: 'relative'
		});
	}
	else if(width < 0 && width >= -listWidth)
	{
		new Effect.Move('slider', {
		  x: listWidth, y: 0, mode: 'relative'
		});
		
	}
	else if(width < 0)
	{
		new Effect.Move('slider', {
		  x: listWidth, y: 0, mode: 'relative'
		});	
	}
}

function goRight()
{
	var width = $('slider').style.left;
	width = width.substring(0, width.length-2);
	var tempWidth = 5 * 126 - listNumber * 126;
	if(width > tempWidth && width < listWidth && width%126!=0)
	{
		var navigate = parseInt(width)+listWidth;
		new Effect.Move('slider', {
		  x: -navigate, y: 0, mode: 'relative'
		});
	}
	else if(width > tempWidth && width <= -listWidth)
	{
		new Effect.Move('slider', {
		  x: -listWidth, y: 0, mode: 'relative'
		});
		
	}
	else if(width > tempWidth)
	{
		new Effect.Move('slider', {
		  x: -listWidth, y: 0, mode: 'relative'
		});	
	}
}


function toggleLogos(toShow,totalPages)
{
    
	for(i=1;i<=totalPages;i++)
	{
		if(i == toShow)
		{
			$('logos'+i).show();
			//$('link'+i).addClassName('active');
			$('linkactive'+i).addClassName('active');
			
		}
		else
		{
			$('logos'+i).hide();
			//$('link'+i).removeClassName('active');
			$('linkactive'+i).removeClassName('active');
		}
	}
}


function validate_registry()
{
	var error = 0;
	if(!isNonblank($('v_nome').value))
		error++;
		
	if(!validate_email($('v_email')))
		error++;
	
	if($('v_tlm').value.length != 9 || !isNumber($('v_tlm').value))
		error++;
	
	if($('v_cp1').value.length != 4 || !isNumber($('v_cp1').value))
		error++;
	
	if($('v_cp2').value.length != 3 || !isNumber($('v_cp2').value))
		error++;
	
	if(!isNonblank($('v_localidade').value))
		error++;
		
	if(error != 0)
	{
		$('v_erro').style.display = 'block';
		return false;
	}
	$('registerForm').submit();
	return true;	
}

function validate_recomendation()
{
    
	var error = 0;
    
	if(!isNonblank($('v_nome').value))
		error++;
		
	if(!validate_email($('v_email')))
		error++;
		
	if(!isNonblank($('v_nomeAmigo').value))
		error++;
		
	if(!validate_email($('v_emailAmigo')))
		error++;
		
	if(error != 0)
	{
         $('v_erro').update('Por favor, preencha correctamente os campos do formul&aacute;rio.');
		$('v_erro').style.display = 'block';
		return false;
	}
	//$('recomendationForm').submit();
	return true;	
	
}

function validate_leasing()
{
	var error = 0;
	if(!isNonblank($('v_empresa').value))
		error++;
		
	if(!isNonblank($('v_nome')))
		error++;
		
	if(!isNumber($('v_tlm').value))
		error++;
		
	if(!validate_email($('v_email')))
		error++;
		
	if(error != 0)
	{
		$('v_erro').style.display = 'block';
		return false;
	}
	$('leasingForm').submit();
	return true;	
	
}

function validate_email(field)
{
	with (field)
	{
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2)
			return false;
		else
			return true;
	}
}



var isNonblank_re    = /\S/;
function isNonblank (s) {
   return String (s).search (isNonblank_re) != -1
}

var isInteger_re     = /^\s*(\+|-)?\d+\s*$/;
function isNumber(s) 
{   
	return String(s).search (isInteger_re) != -1
}

function popUp(URL) {
	window.open(URL, "myWindow", 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=920,height=670');
}

function popUpVideo(URL) {
	window.open(URL, "myWindow", 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=340,height=320');
}

function popUpAudio(URL) {
	window.open(URL, "myWindow", 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=300,height=320');
}


function toggleFoto(toShow)
{
	if(toShow == 1)
	{
		$('foto1').appear({ duration: 1.5, from: 0, to: 1 });
		$('link1').addClassName('active');
		
		$('foto2').fade({ duration: 1.5});
		$('link2').removeClassName('active');
		
	}
	else
	{
		$('foto2').appear({ duration: 1.5, from: 0, to: 1 });
		$('link2').addClassName('active');
		
		$('foto1').fade({ duration: 1.5 });
		$('link1').removeClassName('active');
		
	}
}

function startLoading() {
  $('botao').style.display   = 'none';
  $('loading').style.display = 'block';
  
}

function stopLoading() {
  $('botao').style.display   = 'block';
  $('loading').style.display = 'none';
}

//Submit Recomendation
function submitRecomendation(f){
    
    f = $(f);
    
   // if(validate_recomendation())
    //{
        $('v_erro').hide();
        $('v_erro').removeClassName('sucesso');
        $('v_erro').addClassName('erro');
        
        startLoading();
        f.request({
                onComplete: function(transport) {
                    
                                json = transport.responseJSON;
                                
                                $$('input.error').each(function(s) {
                                                          s.removeClassName('error');
                                                        });

                                
                                $('v_erro').update(json.msg);
                                $('v_erro').show();
                                stopLoading(); 
                                if(json.status == 1) {
                                    $('v_nome_amigo').value = '';   
                                    $('v_email_amigo').value = ''; 
                                    
                                    $('v_erro').removeClassName('erro');
                                    $('v_erro').addClassName('sucesso');
                                    
                                } else {
                                    for (var i = 0; i < json.errors.length; i++) {
                                        $(json.errors[i]).addClassName('error');
                                    }
                                }
                },
                
                onFailure: function(transport) {
                    
                }
            });
   // }
  return false;
}

//Submit Leasing
function submitLeasing(f){
    
    f = $(f);
    
        $('v_erro').hide();
        
        $('v_erro').removeClassName('sucesso');
        $('v_erro').addClassName('erro');
        
        startLoading();
        f.request({
                onComplete: function(transport) {
                    
                                json = transport.responseJSON;
                                
                                 $$('input.error').each(function(s) {
                                                          s.removeClassName('error');
                                                        });
                                 
                                 
                                $('v_erro').update(json.msg);
                                $('v_erro').show();
                                stopLoading(); 
                                if(json.status == 1) {
                                    $('v_empresa').value = '';   
                                    $('v_nome').value = ''; 
                                    $('v_tlm').value = '';   
                                    $('v_email').value = ''; 
                                    
                                    $('v_erro').removeClassName('erro');
                                    $('v_erro').addClassName('sucesso');
                                      
                                }else{
                                    for (var i = 0; i < json.errors.length; i++) {
                                        $(json.errors[i]).addClassName('error');
                                    }
                                }
                },
                
                onFailure: function(transport) {
                    
                }   
            });
  return false;
}


//Submit Registation
function submitRegistation(f){
    
    f = $(f);
    
        $('v_erro').hide();
        startLoading();
        f.request({
                onComplete: function(transport) {
                    
                                json = transport.responseJSON;
                                
                                $$('input.error').each(function(s) {
                                                          s.removeClassName('error');
                                                        });
                                
                                
                                $('v_erro').update(json.msg);
                                $('v_erro').show();
                                stopLoading(); 
                                if(json.status == 1) {
                                    /*$('v_nome').value = '';   
                                    $('v_email').value = ''; 
                                    $('v_tlm').value = '';   
                                    $('v_cp1').value = ''; 
                                    $('v_localidade').value = ''; 
                                    $('v_cp1').value = ''; 
                                    $('v_cp2').value = ''; 
                                    $('cMail').value = ''; 
                                    $('cMobile').value = '';*/
                                    $('botao').style.display   = 'none';
                                    $('fields').style.display = 'none';
                                    $('msg_nota').style.display = 'none';
                                    
                                    $('v_erro').removeClassName('error');
                                    $('v_erro').addClassName('sucesso');
                                    
                                     
                                } else { 
                                    for (var i = 0; i < json.errors.length; i++) {
                                        $(json.errors[i]).addClassName('error');
                                            
                                    }
                                }
                },
                
                onFailure: function(transport) {
                    
                }
            });
  return false;
}

//Submit Story
function submitStoryCategory(stoid){
    
    
    url = '/lojas/?id_story='+stoid+'&action=getcat';
    
    new Ajax.Request(url,{
            method: 'get',
            onComplete: function(transport) {
                
                            json = transport.responseJSON;
                            
                            if(json.status == 1) {
                                
                            }else{
                            }
            },
            
            onFailure: function(transport) {
                
            }   
        });
    return false;
};



function alpha(e) {
     var k;
     document.all ? k = e.keyCode : k = e.which;
     return ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8 || k == 32);
}

function toggleDirectory()
{
	if($('piso1').style.display == 'none')
	{
		$('nav1').style.display = 'block';
		$('piso1').style.display = 'block';
		$('nav0').style.display = 'none';
		$('piso0').style.display = 'none';
	} else {
		$('nav0').style.display = 'block';
		$('piso0').style.display = 'block';
		$('nav1').style.display = 'none';
		$('piso1').style.display = 'none';
	}
}

