$(document).ready(function() {
	$("div.sub_menu").each(function(){
	});

	$('#pics').cycle({ 
    fx:      'fade', 
    speed:    3000, 
    timeout:  10000 
	});

	$("div.sub").hover(function(){
		var el = $(this).children("div.sub_menu");
		el.show("fast");
	}, function(){
		$(this).children("div.sub_menu").slideUp("fast");
	});
	
	$("input.reg").change(function(){
		setSubmitButtonState();
	});

	$("input#ageCheck").click(function(){
		setSubmitButtonState();
	});

	$("input#ageCheck").change(function(){
		setSubmitButtonState();
	});

	$("input.help").hover(function(){
		$("div#regHelper").html($(this).attr("help"));
	}, function(){
		$("div#regHelper").html("Нужно заполнить все поля формы. Спасибо :)");
	});

	setSubmitButtonState();

	$("#iwantDiv a").attr("href", "javascript:showRegForm();");
	
	var ctr = 0;
	
	$("span#passwordHide").html(" (<a href=\"javascript:togglePassword();\">за мной подглядывают, скройте символы!</a>)");
	
	$("a.confirm").each(function(){
		$(this).attr("id", "confirm" + ctr);
		$(this).attr("hrefOriginal", $(this).attr("href"));
		$(this).attr("href", "javascript:confirmAction(" + ctr + ");");
		ctr++;
	});
});

function confirmAction(id){
	var el = $("a#confirm" + id);
	var answer = confirm(el.attr("message"));
	if(answer){
		window.location.href = el.attr("hrefOriginal");
	}
}

function togglePassword(){
	var val = $("input#password").attr("value");
	if($("input#password").attr("type") == "text"){
		$("span#passwordDiv").html("<input class=\"reg\" type=\"password\" id=\"password\" name=\"password\">");
		$("span#passwordHide").html(" (<a href=\"javascript:togglePassword();\">все ушли, покажите символы</a>)");
	} else {
		$("span#passwordDiv").html("<input class=\"reg\" type=\"text\" id=\"password\" name=\"password\">");
		$("span#passwordHide").html(" (<a href=\"javascript:togglePassword();\">за мной подглядывают, скройте символы!</a>)");
	}
	
	$("input#password").attr("value", val);
}

function showRegForm(){
//	$("#iwantDiv").css("display", "none");
//	$("#regDiv").css("display", "block");
	$("#iwantDiv").slideUp("slow");
	$("#regDiv").slideDown("slow");
}

function checkFormFilled(){
	var formFilled = true;
	
//check if all text fields are filled
	$("input.reg").each(function(){
		if(formFilled == true && $(this).attr("value") == ""){
			formFilled = false;
		}
	});

//e-mail validation
	if(formFilled == true && isValidEmailAddress($("#email").attr("value"))){
		formFilled = false;
	}

//check if age checkbox is checked (age > 21)
	if(formFilled == true && !$("#ageCheck").attr("checked")){
		formFilled = false;
	}
	
	return formFilled;
}

function setSubmitButtonState(){
//if form is filled enable submit button
	if(checkFormFilled()){
		$("input#regBt").removeAttr("disabled");
		$("div#regHelper").slideUp("slow");
	} else {
		$("input#regBt").attr("disabled", "true");
		$("div#regHelper").html("Нужно заполнить все поля формы. Спасибо :)");
		$("div#regHelper").slideDown("slow");
	}
}

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/);
	return !pattern.test(emailAddress);
}

