function loginVIP() {
    
    $('.loader').css('display', 'inline');

   $.post('user/login', {
        username: $('#username').val(),
        password: $('#password').val()
   }, function(data) {
   $('.error').css('display', 'none');
   $('.loader').css('display','none')
    if(data.action == 'true') {
        document.location.href = 'chat/';
    }
    else if(data.action == 'admin') {
    		document.location.href = 'admin/'
    }
    else if(data.action == 'renewVip') { //Is vip but needs new account
    	  $('.error').css('display', 'inline');
        $('.error').html('<a href="#">' + language.renewVip + '</a>');
        $('.error a').unbind('click').click(function() {
        		$('#vipContainer').fadeOut();
        		showVipPayment(data.value.id, data.value.vip);
        		return false;
        });
    }
    else if(data.action == 'blocked') {
    	$('.error').css('display', 'inline');
    	$('.error').html(language.blocked);
    }
    else {
        $('.error').css('display', 'inline');
        $('.error').html(language.invalidLogin);
    }
   }, "json");

   return false;
}

function showVipPayment(uid, vip) {
	$('#vipPayLinkSingle').unbind('click').click(function() {
		mbetaal('id=' + vip.single + '&parameter[1]=' + uid);
		$('.overlay').fadeOut();
		$('#payVipContainer').fadeOut();
		return false;
	});
	$('#vipPayLinkWeekly').unbind('click').click(function() {
		mbetaal('id=' + vip.week + '&parameter[1]=' + uid);
		$('.overlay').fadeOut();
		$('#payVipContainer').fadeOut();
		return false;
	});
	$('#vipPayLinkMonthly').unbind('click').click(function() {
		mbetaal('id=' + vip.month + '&parameter[1]=' + uid);
		$('.overlay').fadeOut();
		$('#payVipContainer').fadeOut();
		return false;
	});
	$('#vipPayLinkYearly').unbind('click').click(function() {
		mbetaal('id=' + vip.year + '&parameter[1]=' + uid);
		$('.overlay').fadeOut();
		$('#payVipContainer').fadeOut();
		return false;
	});
	$('#payVipContainer').fadeIn();
}

function countDown(id) {
	if(document.getElementById('theTextarea').value.length > 200) {
		document.getElementById('theTextarea').value = document.getElementById('theTextarea').value.substr(0, 200);
	}
	$('#' + id + ' .counter').html(document.getElementById('theTextarea').value.length);
}

function countDown2(id) {
	if(document.getElementById('theTextarea2').value.length > 200) {
		document.getElementById('theTextarea2').value = document.getElementById('theTextarea2').value.substr(0, 200);
	}
	$('#' + id + ' .counter').html(document.getElementById('theTextarea2').value.length);
}
 
 function submitRegister() {
 	$.post('form/register', {
 		username: $('#registrerencontainer .rUsername').val(),
 		country: $('#registrerencontainer .rCountry').val(),
 		region: $('#registrerencontainer .rRegion').val(),
 		location: $('#registrerencontainer .rLocation').val(), 
 		description: $('#registrerencontainer .rDescription').val(),
 		profileName: $('#registrerencontainer .rProfileName').val(),
 		profileUrl: $('#registrerencontainer .rProfileUrl').val(),
 		photoUrl: $('#registrerencontainer .rPhotoFromUrl').val(),
 		password: $('#registrerencontainer .rPassword').val(),
 		password2: $('#registrerencontainer .rPassword2').val(),
 		photoName: uploadName 		
 	}, function(data) {
 			$('.error').html(''); //Clear old errors
 			if(typeof(data.errors) != 'undefined') {
	 			$.each(data.errors, function(index, value) {
	 				$('.' + index).html(value);
	 			});
 			}
 			else {
 				$('.error').html(''); //Clear old errors
 				//Everything a-ok-ay
 				$('#registrerencontainer').fadeOut();
 				//Set correct url for payment link
 				showVipPayment(data.uid, data.vip);
 			}
 	}, 'json');
}

function showRegister() {
	$('.overlay').fadeIn();
	$('#registrerencontainer').fadeIn();
	countDown('registrerencontainer');
	swapRegion();
}

function showUserLogin() {
	$('.overlay').fadeIn();
	$('#userlogincontainer').fadeIn();
	countDown('userlogincontainer');
	swapRegion(2);	
	swapLocation(2);		
}

function showForgot() {
	$('.overlay').fadeIn();
	$('.popup').fadeOut();
	$('#forgotPasswordContainer').fadeIn();
}

function swapRegion(id) {
	var country = '.rCountry';
	var region = '.rRegion';
	if(id == 2) {
		country = '.rCountry2';
		region = '.rRegion2';
	}
	$.post('form/fetchregion', {
		country: $(country).val()
	}, function(data) {
		var theList = '';
		$.each(data, function(index, value) {
			theList = theList + '<option value="' + value.id + '">' + value.name + '</option>';
		});
		$(region).html(theList);
		
		swapLocation(true); //Clear old location
		swapLocation(2);
	}, 'json');
}

function swapRegionCurrent() {
	country = '.rCountry2';
	region = '.rRegion2';
	$.post('form/fetchregion/current/yes/', {
	}, function(data) {
		var theList = '';
		$.each(data, function(index, value) {
		
			if(value.current == true) {
				var add = 'selected="selected"';
			}
			else {
				var add = '';
			}	
			
			theList = theList + '<option value="' + value.id + '"' + add + '>' + value.name + '</option>';
		});
		$(region).html(theList);
		
		swapLocation(true); //Clear old location
		
	}, 'json');
}
 
function swapLocation(empty) {
	if(empty == true)
		var theRegion = 1;
	else
		var region = '.rRegion';
		var theLocation = '.rLocation';
		if(empty == 2) {
			region = '.rRegion2';
			theLocation = '.rLocation2';
		}
		
		var theRegion = $(region).val();
	$.post('form/fetchlocation', {
		region: theRegion
	}, function(data) {
		var theList = '';
		$.each(data, function(index, value) {
			theList = theList + '<option value="' + value.id + '">' + value.name + '</option>';
		});
		$(theLocation).html(theList);
	}, 'json');
}

function swapLocationCurrent() {
	region = '.rRegion2';
	theLocation = '.rLocation2';
	$.post('form/fetchlocation/current/yes', {
	}, function(data) {	
		var theList = '';
		$.each(data, function(index, value) {
			if(value.current == true) {
				var add = 'selected="selected"';
			}
			else {
				var add = '';
			}	
			theList = theList + '<option value="' + value.id + '" ' + add + '>' + value.name + '</option>';
		});
		$(theLocation).html(theList);
	}, 'json');
}

function submitLogin() {
	
 	$.post('form/register', {
 		username: $('#userlogincontainer .rUsername').val(),
 		country: $('#userlogincontainer .rCountry2').val(),
 		region: $('#userlogincontainer .rRegion2').val(),
 		location: $('#userlogincontainer .rLocation2').val(), 
 		description: $('#userlogincontainer .rDescription').val(),
 		profileName: $('#userlogincontainer .rProfileName').val(),
 		profileUrl: $('#userlogincontainer .rProfileUrl').val(),
 		photoUrl: $('#userlogincontainer .rPhotoFromUrl').val(),
 		type: 'login',
 		photoName: uploadName
 	}, function(data) {
 			$('.error').html(''); //Clear old errors
 			if(typeof(data.errors) != 'undefined') {
	 			$.each(data.errors, function(index, value) {
	 				$('.' + index).html(value);
	 			});
 			}
 			else {
 				if(typeof(data.blocked) != 'undefined') {
 					$('.blocked').html(language.youAreBlocked);
 					return;
 				}
 				
 				$('.error').html(''); //Clear old errors
 				//Everything a-ok-ay
 				if(data.vip.user) { //Full
	 				$('#userlogincontainer').fadeOut();
	 				//Set correct url for payment link
	 				showUserPayment(data.uid, data.vip);
 				}
 				else {
 					document.location.href = 'chat/'; //okay is logged in
 				}
 				
 			}
 	}, 'json');
}


function submitEdit() {
	if(document.getElementById('showAds')) {
		if(document.getElementById('showAds').checked == true) {
			var showAds = '0';
			$('.ad a').css('display', 'none');
		}
		else {
			var showAds = '1';
			$('.ad a').css('display', 'block');	
		}
	}
	else {
		var showAds = '1';	
	}

	$('#update').html('');
	
 	$.post('form/edit', {
 		country: $('#profileEdit .rCountry2').val(),
 		region: $('#profileEdit .rRegion2').val(),
 		location: $('#profileEdit .rLocation2').val(), 
 		description: $('#profileEdit .rDescription').val(),
 		profileName: $('#profileEdit .rProfileName').val(),
 		profileUrl: $('#profileEdit .rProfileUrl').val(),
 		photoUrl: $('#profileEdit .rPhotoFromUrl').val(),
 		ads: showAds,
 		password: $('#profileEdit .password').val(),
		passwordY: $('#profileEdit .passwordY').val(),
		username: $('#profileEdit .username').val()
 	}, function(data) {
 			$('.error').html(''); //Clear old errors
 			if(typeof(data.errors) != 'undefined') {
	 			$.each(data.errors, function(index, value) {
	 				$('.' + index).html(value);
	 			});
 			}
 			else {
 				$('#update').html(language.updated);
 			}
 	}, 'json');
}

function showUserPayment(uid, vip) {
	$('#userPayLink').unbind('click').click(function() {
		mbetaal('id=' + vip.user + '&parameter[1]=' + uid);
		startChecking(1000, uid);
		return false;
	});
	$('#payUserContainer').fadeIn();
}

function startChecking(amount, uid) {
	$.post('user/normallogin', {
		uid: uid
	}, function(data) {
		if(data.action == 'true')
			document.location.href = 'chat/';
			
		setTimeout('startChecking(' + amount + ',' + uid + ')', amount);
	}, 'json');
	
}

$(document).ready(function() {
	var close = document.createElement('span');
	$(close).addClass('close');
	$('.popup').append(close);
	
	$('.overlay, .popup .close').click(function() {
		$('.overlay').fadeOut();
		$('.popup').fadeOut();
	});
});


function submitContact() {
	$('#contact .error').html('');
	$('#contact .inProgress').css('display', 'block');
	$('#contact .done').css('display', 'none');
	$.post('form/contact', {
		email: $('#contactEmail').val(),
		phone: $('#contactPhone').val(),
		message: $('#contactMessage').val(),
		subject: $('#contactSubject').val(),
		username: $('#contactUsername').val()
	}, function(data) {
		if(typeof(data.errors) != 'undefined') {
			$.each(data.errors, function(index, value) {
				$('#contact .' + index).html(value);
			});
		}
		else {
			if($('#contactUsername').attr('disabled') == true)
				var username = $('#contactUsername').val();
			else
				var username = '';
				
			$('#contact .done').css('display', 'block');
			$('#contact input, #contact textarea').val('');
			$('#contactUsername').val(username);
			$('#contact input[type=submit]').val(language.submit);
		}
		$('#contact .inProgress').css('display', 'none');
	}, 'json');
}

function submitPreLogin() {
	to = 'login';
	uploadPhoto();
}

function submitPreRegister() {
	to = 'register';
	uploadPhoto();
}
