function emptyCheck(elements){
	var flag=true;
	elements=elements.split(',');
	$(elements).each(function(){
		if($('#'+this).val()==''){
			$('#'+this).addClass('empty');
			$('#'+this+'Error').html("Please don't leave this empty.");
			flag=false;
			return false;
		}
	});
	return flag;
}

function serialize(values){
	values=values.split(',');
	var encodedValues={};
	$(values).each(function(){ encodedValues[this]=($('#'+this).val()); });
	return encodedValues;
}

// VALIDATE EMAIL:
// VALIDATE EMAIL:
// VALIDATE EMAIL:
function validEmail() {
	var flag = true;
	if(($('#email').val().length<4) || ($('#email').val().indexOf('@')==-1)) { flag = false; }
	if (!flag){
		$('#email').addClass('empty');
		$('#emailError').html("Please provide a valid email.");
	}
	return flag;
}

// REGISTRATION PROGRESS BAR:
// REGISTRATION PROGRESS BAR:
// REGISTRATION PROGRESS BAR:
var registration_fields=new Array('firstName','lastName','email','username','password','password2','country','year','income','residentialstatus','jobtitle','industry','companysize');
function registration_progess(){
	var total_fields=registration_fields.length+2;
	var total_width=parseInt(($('#registration-progress-bg').css('width')).substr(0,3));
	var empty_fields=0;
	$(registration_fields).each(function(){ if($('#'+this).val()=='') empty_fields++ });
	if($("input[@name=gender]:checked").size()==0) empty_fields++;
	if($("#tos:checked").size()==0) empty_fields++;
	var progress_width=Math.round((total_fields - empty_fields) *  (total_width / total_fields));	
	var progress_percent=100 - (Math.round(100 * empty_fields / total_fields));
	$('#registration-progress').animate({width:progress_width+'px'}, 'fast');
	$('#registration-progress-percent').html(progress_percent+'%');
}

// REGISTER:
// REGISTER:
// REGISTER:
function register(action){
	// SCRUB ERROR MESSAGES:
	$('#registration-form .empty').removeClass('empty');
	$('#registration-form .submitError').not('#countryError').not('#tosError').html('*');

	if (action == 'step1') {
		if(!emptyCheck('firstName,lastName,email,username,password,password2,country')) return false;
		if(!validEmail()) return false;
		// CHECK THAT PASSWORDS MATCH:
		if($('#password').val()!=$('#password2').val()){ $('#passwordError').html("The passwords don't match."); return false;}
		// CHECK THAT USERNAME IS VALID:
		var values = serialize('firstName,lastName,email,username,password,zip,phonenumber,country,timezone');
		// SUBMIT:
		$('#processing1').html('<img src="/images/indicator_small.gif"/>');
		$.post('login_handler?actionName=01h', values, function(response){ 
			var r = eval(response);
			r = r[0];
			$('#processing1').html('');
			
			var proceed = true;
			if (r.email == 'taken') {
				$('#emailError').html("Email already in use.");
				proceed = false;
			} 

			if (r.login == 'taken') {
				$('#usernameError').html("Username already in use.");
				proceed = false;
			} 
			
			if (proceed) {
				$('#registration-table-step1').animate({marginLeft:'-900', opacity:'0'}, 'slow', function(){
					$('#registration-table-step1').css('display', 'none');
					$('#registration-table-step2').css('display', 'block').animate({marginLeft:'0', opacity:'1'}, 'slow');
				});
			}
		});
	}	else if(action=='step1back'){
		$('#registration-table-step2').animate({marginLeft:'-900', opacity:'0'}, 'slow', function(){
			$('#registration-table-step2').css('display', 'none');
			$('#registration-table-step1').css('display', 'block').animate({marginLeft:'0', opacity:'1'}, 'slow');
		});
	}	else if(action=='step2'){
		if($("input[@name=gender]:checked").size()==0){ $('#genderError').html("Please don't leave this empty."); return false; }
		if(!emptyCheck('year,income,residentialstatus,jobtitle,industry,companysize')) return false;
		if($("#tos:checked").length == 0){ $('#tosError').html("You must agree to our Terms of Service."); return false; }
		// SUBMIT:
		$('#processing2').html('<img src="/images/indicator_small.gif"/>');
		var values = serialize('firstName,lastName,email,username,password,zip,phonenumber,country,timezone,year,income,residentialstatus,jobtitle,industry,companysize');
		values["gender"] = $("input[@name=gender]:checked").val();

		$.post('login_handler?actionName=01', values, function(response) {
			var r = eval(response);
			r = r[0];
			$('#processing2').html('');

			if (r.status == 'failure') {
				alert('Server failure..\b');
			} else {
				window.location.href = "account";
			}
		});
	}
}


// UPDATE ACCOUNT:
// UPDATE ACCOUNT:
function submitChanges(action){
	// SCRUB ERROR MESSAGES:
	$('.empty').removeClass('empty');
	$('.submitError').html('');
	//
	if(action=='update-profile'){
		if(!emptyCheck('firstName,lastName,email,country,year,income,residentialstatus,jobtitle,industry,companysize')) return false;
		if($("input[@name=gender]:checked").size()==0){ $('#genderError').html("Please don't leave this empty."); return false; }
	}
	else if(action=='change-password'){
		if(!emptyCheck('currentpassword,password,password2')) return false;
		if($('#password').val()!=$('#password2').val()){ $('#passwordError').html("The passwords don't much."); return false;}
	}
}

// SUBMIT FEEDBACK:
// SUBMIT FEEDBACK:
function submitFeedback(){
	$('.empty').removeClass('empty');
	$('#feedbackStatus').html('');
	//  CHECK THAT NOTHING IS EMPTY:
	if($("#feedback :input[value='']").not('.optionalField').not('button').size()>0){
		$("#feedbackStatus").html("Please don't leave the required fields empty.").attr({'className':'submitError'});
		$("#feedback :input[value='']").not('.optionalField').not('button').each(function(){ $(this).addClass('empty'); });
		return false;
	}
	// CHECK THAT PASSWORDS MATCH:
	var values=new Array('name', 'email', 'company', 'website', 'phone', 'subject', 'message');
	var encodedValues={};
	$(values).each(function(){ encodedValues[this]=encodeURIComponent($('#'+this).val()); });
	values=encodedValues;
	// SUBMIT:
	$.post('/contact.php', values, function(response){
		// we'll add stuff here later:
		alert(response);
	});
}