var $j = jQuery.noConflict();
$j(document).ready(function()
{
    $j('#error_message').hide();
    $j('#wrapper').show();

    $j('#pin').hide();
    $j('input[name="pin"]').click(function()
    {
	if($j(this).val() == 'Code Confidentiel') $j(this).val('');
    });
    $j('input[name="pin"]').blur(function()
    {
	if($j(this).val() == '') $j(this).val('Code Confidentiel');
    });
    $j('.formElement_error').hide();
    function pinFormUpdate(ret, status)
    {
	if(status == 'success')
	{
	    if(ret.ok)
	    {
		top.location = './'+ret.location;
		return;
	    }
	    $j('.input_error').removeClass('input_error');

            $j.each(ret.error,function(i,val)
            {
                if(val) $j("[name='"+i+"']").addClass('input_error');
            });
            if(ret.output)
            {
                if(ret.output.error.length)
                {
                    $j('.formElement_error').empty();
                    $j.each(ret.output.error,function(i,val)
                    {
                        $j('.formElement_error').append(val+' ');
                    });
                    $j('.formElement_error').show();
                }
            }
            else $j('.formElement_error').hide();	    
	}
    }
    $j("#pincheck").submit(function()
    {
	$j('.formElement_error').html('Checking ...');
	var options = 
	{ 
	    success:		pinFormUpdate,
	    url:		'pincheck.php',
	    type:		'post',
	    dataType:		'json'
	};
	$j(this).ajaxSubmit(options);
	return false;
    });
    
    function formUpdate(ret, status)
    {
	if(status == 'success')
	{
	    $j("#scroll").empty();
	    //alert(ret.length);
	    if(ret.error)
	    {
		$j('.input_error').removeClass('input_error');
		$j.each(ret.error,function(i,val)
		{
		    if(val) $j("[name='"+i+"']").addClass('input_error');
		});
		if(ret.output)
		{
		    if(ret.output.error.length)
		    {
			$j('<div id="errorMessage"></div>').appendTo('#scroll');
			$j.each(ret.output.error,function(i,val)
			{
			    $j('#errorMessage').append(val+'<br />');
			});
            	    }
		}
            }
	    else
	    {
		$j('<p>En allant sur le site d’un des partenaires de notre offre « Special K soin offert », vous quittez le site Special K. Kellogg’s n’est pas responsable du contenu du site auquel vous allez accéder. </p>').appendTo($j('#scroll'));
		$j.each(ret,function(i,val)
		{
		    $j('<div id="venue-'+i+'" class="display"></div>').appendTo("#scroll");
		    $j.each(val,function(j,v)
		    {
		    //alert(v);
		    if(v != '')
		    {
			$j('<div id="'+j+'-'+i+'" class="venue_element">'+v+'</div>').appendTo("#venue-"+i);
			if(j == 'website')
			{
			    var matched = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/.exec(v);
			    if(matched) $j("#"+j+'-'+i).wrap('<a href="'+v+'" target="_blank"></a>');
			    else $j("#"+j+'-'+i).wrap('<a href="http://'+v+'" target="_blank"></a>');
			}
			else $j("#"+j+'-'+i).addClass("venue_"+j);
		    }
		    //alert(j);
		    });
		});
		$j("#scroll").show();
	    }
	}
    }
    
    function deptUpdate(ret,status)
    {
	if(status == 'success')
	{
	    $j('#select_county').empty();
	    $j('<option value="">Votre departement</option>').appendTo($j('#select_county'));
	    $j.each(ret,function(k,v)
	    {
		$j('<option value="'+v+'">'+v+'</option>').appendTo($j('#select_county'));
	    });
	}
    }
    
    $j("#region").submit(function()
    {
	var options = 
	{ 
	    success:		deptUpdate,
	    url:		'application.php',
	    type:		'post',
	    dataType:		'json'
	}; 
	$j(this).ajaxSubmit(options);
	return false;
    });
    
    $j('#pin').submit(function()
    {
	var options = 
	{ 
	    success:		formUpdate,
	    url:		'application.php',
	    type:		'post',
	    dataType:		'json'
	}; 
	pageTracker._trackPageview('/pincode/'+$j('input[name="pin"]').val());
	$j(this).ajaxSubmit(options);
	return false;
    });
    
    $j("#select_country").change(function()
    {
	if($j(this).val() != '')
	{
	    $j('#pin_country').remove();
	    $j('#pin_county').remove();
	    $j("#scroll").empty();
	    $j('<input type="hidden" name="country" value="'+$j(this).val()+'" id="pin_country" />').appendTo('#pin');
	    $j('#pin').hide();
	    $j("#region").submit();
	}
	else
	{
	    $j('#pin_country').remove();
	    $j('#pin_county').remove();
	    $j('#select_county').empty();
	    $j("#scroll").empty();
	    $j('<option value="">Votre departement</option>').appendTo($j('#select_county'));
	    $j('#pin').hide();
	}
    });    
    $j("#select_county").change(function()
    {
	if($j(this).val() != '')
	{
	    $j('#pin_county').remove();
	    $j("#scroll").empty();
	    $j('<input type="hidden" name="county" value="'+$j(this).val()+'" id="pin_county" />').appendTo('#pin');
	    $j('#pin').show();
	}
	else
	{
	    $j('#pin_county').remove();
	    $j("#scroll").empty();
	    $j('#pin').hide();
	}
    });    
});
