$(document).ready(function(){

	$("div.questionnairePhoto > div").mouseover(function(){
		$("#" + $(this).attr("val")).css("backgroundImage", "url(/images/plus.png)");
	});
	$("div.questionnairePhoto > div").mouseout(function(){
		$("#" + $(this).attr("val")).css("backgroundImage", "url('')");
	});

	$("span.preview").click(function(){
        var objSpan = this.parentNode.parentNode.parentNode.getElementsByTagName("span");
        var objLink = $("#" + $(this).attr("val")).parent();

        $(objSpan).addClass("no_active");
        $(objSpan).removeClass("active");
        $(this).removeClass("no_active");
        $(this).addClass("active");
        $("#" + $(this).attr("val") + "_parent").css("backgroundImage", "url(" + $(this).attr("pic") + ")");
        $(objLink).attr("href", $(this).attr("big_src"));
	});


	$("div.clock").click(function(){
		location.href="/sort/new";
	});

	$("div.arrows").click(function(){
		location.href="/sort/random";
	});

	$("a.buttonPreview").click(function(){
		var form = document.contact;

		form.action.value = "new";
		form.submit();
	});

	$("a.buttonRefresh").click(function(){
		var form = document.contact;

		form.action.value = "update";
		form.submit();
	});

	$("a.buttonClear").click(function(){
		var form = document.contact;
		var agree=confirm("Вы уверены?");

	    if (agree) {
	    	form.action.value = "remove";
	    	form.submit();
	    };
	});

	$("a.buttonSend").click(function(){
		var form = document.contact;

		form.action.value = "send";
		form.submit();
	});

});




function p_turnoff(images,selected) {
	if(!images) return true;
	var imageElements = images.split(',');
	var i=0, countElements = imageElements.length;
	for(i=0;i<countElements;i++) {
		document.getElementById(imageElements[i]).style.backgroundImage='url(/images/des.number.bg.off.png)';
	}
	document.getElementById(selected).style.backgroundImage='url(/images/des.number.bg.on.png)';
}

function p_changeimage(id,image) {
 var elementName='picture'+id;
 document.getElementById(elementName).style.backgroundImage='url('+image+')';
}

function p_setstore(id,image) {
 var elementName='pstore'+id;
 document.getElementById(elementName).value=image;
}

function p_setcurlink(id,image)	{
	var elementName='photoselected'+id;
	document.getElementById(elementName).href=image;
}

function confirmSubmit() {
	var agree=confirm("Вы уверены?");
	if (agree) return true ; else return false;
}

function checkEmail(form) {
	var notcorrect='Не указан верный email';
	var notagreed='Вы обязаны принять наше соглашение';
	var a = $(".checkBox").attr('checked');
		if (!a) { alert(notagreed); $(".checkBox").focus(); return false; }
	//if(!form.elements["profile[terms]"].checked) { alert(notagreed); form.elements["profile[terms]"].focus(); return false; }
	if(document.getElementById("profile[email]").value=="") { alert(notcorrect); document.getElementById("profile[email]").focus(); return false; }
	//if(form.elements["profile[email]"].value=="") { alert(notcorrect); form.elements["profile[email]"].focus(); return false; }
	txt=document.getElementById("profile[email]").value;
	if (txt.indexOf(".") == -1) { alert(notcorrect); return(false) }
	if (txt.indexOf("@") == -1) { alert(notcorrect); return(false) }
	dog = txt.indexOf("@");
	if ((dog < 1) || (dog > txt.length - 5)) { alert(notcorrect); return(false) }
	if ((txt.charAt(dog - 1) == '.') || (txt.charAt(dog + 1) == '.')) { alert(notcorrect); return (false) }
}

/*function checkEmail(form) {
	var notcorrect='Не указан верный email';
	var notagreed='Вы обязаны принять наше соглашение';
	if(!form.elements["profile[terms]"].checked) { alert(notagreed); form.elements["profile[terms]"].focus(); return false; }
	if(form.elements["profile[email]"].value=="") { alert(notcorrect); form.elements["profile[email]"].focus(); return false; }
	txt=form.elements["profile[email]"].value;
	if (txt.indexOf(".") == -1) { alert(notcorrect); return(false) }
	if (txt.indexOf("@") == -1) { alert(notcorrect); return(false) }
	dog = txt.indexOf("@");
	if ((dog < 1) || (dog > txt.length - 5)) { alert(notcorrect); return(false) }
	if ((txt.charAt(dog - 1) == '.') || (txt.charAt(dog + 1) == '.')) { alert(notcorrect); return (false) }
}  */

