
//-- FORM VALIDATIONS
$(document).ready(function(){

	//--CHECK FORMS CONTENT
	//--subscribe form validation
	$("#sub1").validate({
		rules: {
			artist_pass: { 
				equalTo: "#first_pass" 
			},
			artist_login: { 
				remote: "req.php"
			},
			truc: {
				remote: {
					url: "req.php",
					type: "get",
					data: {
						as: function() {
							return $("#as").val();
						}
					}
				}
			}
		},
		messages: {
			artist_login: { 
				remote: "Username already exists"
			},
			truc: {
				remote: "Please check the code again."
			}
		}
	});
	
	//--check the project form
	$("#postProj").validate();
	
	//--check the login form
	$("#login_form").validate();
	
	//--check the profile form
	$("#profile").validate();
	
	
	//--AJAX SUBMISSION OF FORMS
	//--profile form
	$('#profile').submit(function() {
		var str = $(this).serialize();
		
		$.ajax({
			type: "POST",
			url: "req.php",
			data: str,
			success: function(msg){
				showMessage(msg);
			}
		});
		return false;
	});
	
	//--bio form
	$('#bio').submit(function() {
		var str = $(this).serialize();
		
		$.ajax({
			type: "POST",
			url: "req.php",
			data: str,
			success: function(msg){
				showMessage(msg);
			}
		});
		return false;
	});	


	//--show a message if there is text in it
	if ($("#message").text() != "") {
		var text = $("#message").text()
		showMessage(text);		
	}
});




//--toggle between login and subscription on first page
function toggleSub() {
	$("#loginForm").slideToggle(200);
	$("#subForm").slideToggle(200);
	$("#openSub").toggle();
	$("#openLogin").toggle();
}


//--toggle the add project form
function toggleAddProj() {
	$("#addProjectForms").slideToggle(200);
	$("#projAddBtn").toggle();
}

//--show / hide the fields needed by a project type
function toggleProjFields(fields) {
	$("#proj_elems").show();

	$("#proj_elems div").hide();
	$("#edit_proj div").hide();
		
	var arr = fields.split(",");

	$.ajax({
		type: "GET",
		url: "req.php",
		data: "techride="+arr[0],
		success: function(msg){
			$("#techride").html(msg);
		}
	});

	for(var i = 1; i < arr.length - 1; i++) {
		var id = "#"+arr[i];
		$(id).show();		
	}
	
	$("input[name='type']").val(arr[0]);

	$("#proj_submit").show();
}


//--show confirmation message
function showMessage(msg) {
	var txt = msg.substr(4);
	if (msg.startsWith('msg-')) {
		$("#message").removeClass('errormsg');
		$("#message").addClass('message');
	}
	else if (msg.startsWith('err-')) {
		$("#message").removeClass('message');
		$("#message").addClass('errormsg');
	}
	else txt = 'error: '+msg;
	$("#message").text(txt);
	$("#message").slideDown(200);
	
	setTimeout(function () { 
		$("#message").slideUp(200); 
		//window.location.href = "./";
	}, 4000);
}


//--show edit form
function showEdit(id) {
	$("#projEditFrame").attr("src", "edit.php?edit="+id);
	$("#projEdit").slideDown(200);
}

function closeEdit(msg) {
	if (confirm(msg)) {
		$('#projEdit').slideToggle(200);
		window.location.href = "./";
	}
}


//-tools
String.prototype.startsWith = function(str) {
	return (this.match("^"+str)==str)
}