var pag = 1;
var total_pages = 0;




function mycarousel_initCallback(carousel) {
	
    jQuery('a.car_con').bind('click', function() {
		
		
		var pagi = jQuery.jcarousel.intval(jQuery(this).attr("pag"));
		var desc = jQuery(this).attr("desc");
		 
		pag = pagi;
        carousel.scroll(pagi);
		jQuery('a.car_con').
		each(
		function()
			{
			$(this).text($(this).text().replace("[","").replace("]",""));
			}
		);
		$(this).text("[" + $(this).text() + "]");
		
		$(".jcarousel-description").html(desc);
        return false;
    });



	$('.jcarousel-next').click(function() {
	if (pag > 0 && pag <total_pages)
	 {
		pag++;
		jQuery('a.car_con').
		each(
		function()
			{
			$(this).text($(this).text().replace("[","").replace("]",""));
			}
		);
		$("#pag" + pag).text("[" + $("#pag" + pag).text() + "]");
		var desc = $("#pag" + pag).attr("desc");
		$(".jcarousel-description").html(desc);
		
	}
	});	


$('.jcarousel-prev').click(function() {
	if (pag > 1 && pag <=total_pages) {
		pag--;
		jQuery('a.car_con').
		each(
		function()
			{
			$(this).text($(this).text().replace("[","").replace("]",""));
			}
		);
		$("#pag" + pag).text("[" + $("#pag" + pag).text() + "]");
		var desc = $("#pag" + pag).attr("desc");
		$(".jcarousel-description").html(desc);
	}	
});

};


function mycarousel_itemLoadCallback(carousel, state)
{
    // Check if the requested items already exist
    if (carousel.has(carousel.first, carousel.last)) {
        return;
    }

    jQuery.get(
        'http://www.foucault.nl/helper.php',
        {
            a:$("#album_ii").attr("aa"),
			l:$("#lang_ii").attr("ll"),
			first: carousel.first,
            last: carousel.last
        },
        function(xml) {
            mycarousel_itemAddCallback(carousel, carousel.first, carousel.last, xml);
        },
        'xml'
    );
};

function mycarousel_itemAddCallback(carousel, first, last, xml)
{
    // Set the size of the carousel
    carousel.size(parseInt(jQuery('total', xml).text()));

    jQuery('image', xml).each(function(i) {
		
		
        carousel.add(first + i, mycarousel_getItemHTML(jQuery(this).text(),jQuery('desc', xml).text()));
    });
	
};

/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(url,alt)
{
    return '<img src="' + url + '" alt="'+alt+'" />';
};

function isValidEmail(sValue)
{	
	var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
    if (!sValue.match(re))
        return false;
    return true;
}

function checkUserRegistration(oForm, errorMessage)
{
	var bRet = true, oElem;	
	
	var firstLetter = document.getElementById('new_first_letter');
	var lastName = document.getElementById('new_last_name');
	var address = document.getElementById('new_street');
	var zip = document.getElementById('new_zip');
	var city = document.getElementById('new_city');
	var phone = document.getElementById('new_phone');
	var email = document.getElementById('new_email');
	var password = document.getElementById('new_password');
	var confimPassword = document.getElementById('confirm_new_password');
	
	var bRet = true;
	
	if(firstLetter.value == '')
	{
		firstLetter.style.borderColor = 'red';
		bRet = false;
	}
	
	if(lastName.value == '')
	{
		lastName.style.borderColor = 'red';
		bRet = false;
	}
	
	if(address.value == '')
	{
		address.style.borderColor = 'red';
		bRet = false;
	}
	
	if(zip.value == '')
	{
		zip.style.borderColor = 'red';
		bRet = false;
	}
	
	if(city.value == '')
	{
		city.style.borderColor = 'red';
		bRet = false;
	}
	
	if(phone.value == '')
	{
		phone.style.borderColor = 'red';
		bRet = false;
	}
	
	if(email.value == '')
	{
		email.style.borderColor = 'red';
		bRet = false;
	}
	
	if(password.value == '')
	{
		password.style.borderColor = 'red';
		bRet = false;
	}
		
	if(confimPassword.value == '')
	{
		confimPassword.style.borderColor = 'red';
		bRet = false;
	}
	
	if(password.value != confimPassword.value)
	{
		document.getElementById('new_PasswordValidError').style.display = 'inline';
		bRet = false;
	}
	
	if(bRet == false)	
		document.getElementById('general_message_error').style.display = 'inline';	
		

	return bRet;
}


function checkUserLogin(oForm)
{
	var bRet = true, oElem;	
	
	document.getElementById('emailValidError').style.display = 'none';	
	document.getElementById('final_message').style.display = 'none';	
			
	for(var i=0; i<oForm.elements.length; i++)
	{
		oElem = oForm.elements[i];
		
		if(oElem.type != 'text' && oElem.type != 'password') continue;
		
		document.getElementById(oElem.name + 'Error').style.display = 'none';
			
		if(!oElem.value)
		{
			document.getElementById(oElem.name + 'Error').style.display = 'inline';		
			bRet = false;			
		}			
	}
	
	//email validation
	if (oForm.email.value && !isValidEmail(oForm.email.value))
	{
		document.getElementById('emailValidError').style.display = 'inline';
		bRet = false;	
	}
	
	if(bRet == false)
		document.getElementById('final_message').style.display = 'inline';	
	else
		document.getElementById('final_message').style.display = 'none';
	return bRet;
}


function checkUserForgot(oForm)
{
	var bRet = true, oElem;	
	
	document.getElementById('forgot_emailValidError').style.display = 'none';	
	document.getElementById('forgot_final_message').style.display = 'none';	
			
	for(var i=0; i<oForm.elements.length; i++)
	{
		oElem = oForm.elements[i];
		
		if(oElem.type != 'text') continue;
		
		document.getElementById(oElem.name + 'Error').style.display = 'none';
			
		if(!oElem.value)
		{
			document.getElementById(oElem.name + 'Error').style.display = 'inline';		
			bRet = false;			
		}			
	}
	
	//email validation
	if (oForm.forgot_email.value && !isValidEmail(oForm.forgot_email.value))
	{		
		document.getElementById('forgot_emailValidError').style.display = 'inline';
		bRet = false;	
	}
		
	if(bRet == false)
		document.getElementById('forgot_final_message').style.display = 'inline';	
	else
		document.getElementById('forgot_final_message').style.display = 'none';			
	return bRet;
}

function checkNewsletter(oForm, errorMessage)
{
	var firstLetter = document.getElementById('first_letter');
	var lastName = document.getElementById('last_name');
	var address = document.getElementById('street');
	var zip = document.getElementById('zip');
	var city = document.getElementById('city');
	var phone = document.getElementById('phone');
	var email = document.getElementById('email');
	
	document.getElementById('first_letter_error').style.display = 'none';
	document.getElementById('last_name_error').style.display = 'none';
	document.getElementById('street_error').style.display = 'none';
	document.getElementById('zip_error').style.display = 'none';
	document.getElementById('city_error').style.display = 'none';
	document.getElementById('phone_error').style.display = 'none';
	document.getElementById('email_error').style.display = 'none';
	document.getElementById('general_error').style.display = 'none';
	document.getElementById('email_not_valid_error').style.display = 'none';
	
	var bRet = true;
	
	if(firstLetter.value == '')
	{
		document.getElementById('first_letter_error').style.display = 'inline';
		bRet = false;
	}
	
	if(lastName.value == '')
	{
		document.getElementById('last_name_error').style.display = 'inline';
		bRet = false;
	}
	
	if(address.value == '')
	{
		document.getElementById('street_error').style.display = 'inline';
		bRet = false;
	}
	
	if(zip.value == '')
	{
		document.getElementById('zip_error').style.display = 'inline';
		bRet = false;
	}
	
	if(city.value == '')
	{
		document.getElementById('city_error').style.display = 'inline';
		bRet = false;
	}
	
	if(phone.value == '')
	{
		document.getElementById('phone_error').style.display = 'inline';
		bRet = false;
	}
	
	if(email.value == '')
	{
		document.getElementById('email_error').style.display = 'inline';
		bRet = false;
	}
	
	if(email.value != '' && !isValidEmail(email.value))
	{
		document.getElementById('email_not_valid_error').style.display = 'inline';
		bRet = false;
	}
	
	if(bRet == false)	
		document.getElementById('general_error').style.display = 'inline';	
		
	return bRet;
}

function checkChangePassword(oForm)
{
	var bRet = true, oElem;	
	
	document.getElementById('change_emailValidError').style.display = 'none';
	document.getElementById('change_confirm_new_passValidError').style.display = 'none';
			
	for(var i=0; i<oForm.elements.length; i++)
	{
		oElem = oForm.elements[i];
		
		if(oElem.type != 'text' && oElem.type != 'password') continue;
		
		document.getElementById(oElem.name + 'Error').style.display = 'none';
			
		if(!oElem.value)
		{
			document.getElementById(oElem.name + 'Error').style.display = 'inline';		
			bRet = false;			
		}			
	}
	
	//email validation
	if (oForm.change_email.value && !isValidEmail(oForm.change_email.value))
	{		
		document.getElementById('change_emailValidError').style.display = 'inline';
		bRet = false;	
	}
	
	//password validation
	if(oForm.change_new_pass.value && oForm.change_confirm_new_pass.value && (oForm.change_new_pass.value != oForm.change_confirm_new_pass.value))
	{
		document.getElementById('change_confirm_new_passValidError').style.display = 'inline';
		bRet = false;	
	}
	
	return bRet;
}

//jquery stuff
$(document).ready(function(){
	
	$(".producten_submenu li").hover(function()
								{
					$(this).css("padding-left","5px").children(".pic_no").fadeIn(100);
					
								},
						function(){
					$(this).css("padding-left","0pt").children(".pic_no").fadeOut(100);		
								}
	);
	
	/*$(".sitemap_ul li").hover(function()
								{
					$(this).children().animate({opacity: 1},200);
					
								},
						function(){
					$(this).children().animate({opacity: 0.7},200);	
								}
	);*/
	
	$(".new_user_btn").click(function(){
		
		$("#forgotSpan").hide().parent().css('padding-left','0pt');
		$("#registerSpan").show().parent().css('padding-left','25pt');
		
	});
	
	$(".forgot_btn").click(function(){
		
		$("#registerSpan").hide().parent().css('padding-left','0pt');
		$("#forgotSpan").show().parent().css('padding-left','25pt');
		
	});
});
