function centerPopup(){
	//request data for centering
	if (self.innerHeight) { // Everyone but IE
		var windowWidth = window.innerWidth;
		var windowHeight = window.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // IE6 Strict
		var windowWidth = document.documentElement.clientWidth;
		var windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // Other IE, such as IE7
		var windowWidth = document.body.clientWidth;
		var windowHeight = document.body.clientHeight;
	}
	
	if (window.innerHeight && window.scrollMaxY) {	
		var myScrollWidth = document.body.scrollWidth;
		var myScrollHeight = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight) { // All but Explorer Mac
		var myScrollWidth = document.body.scrollWidth;
		var myScrollHeight = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		myScrollWidth = document.body.offsetWidth;
		var myScrollHeight = document.body.offsetHeight;
	}

	
	var popupHeight = $("#reg").height();
	var popupWidth = $("#reg").width();
	//centering
	$("#reg").css({
		"position": "fixed",
		"top": (windowHeight/2-popupHeight/2)-15,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
}

function centerLoginPopup(){
	//request data for centering
	if (self.innerHeight) { // Everyone but IE
		var windowWidth = window.innerWidth;
		var windowHeight = window.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // IE6 Strict
		var windowWidth = document.documentElement.clientWidth;
		var windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // Other IE, such as IE7
		var windowWidth = document.body.clientWidth;
		var windowHeight = document.body.clientHeight;
	}
	
	if (window.innerHeight && window.scrollMaxY) {	
		var myScrollWidth = document.body.scrollWidth;
		var myScrollHeight = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight) { // All but Explorer Mac
		var myScrollWidth = document.body.scrollWidth;
		var myScrollHeight = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		myScrollWidth = document.body.offsetWidth;
		var myScrollHeight = document.body.offsetHeight;
	}

	
	var popupHeight = $("#login").height();
	var popupWidth = $("#login").width();
	//centering
	$("#login").css({
		"position": "fixed",
		"top": (windowHeight/2-popupHeight/2)-15,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
}

$(document).ready(function(){	
	centerPopup();
	centerLoginPopup();
	
	if ($.browser.msie) {
		if ($.browser.version <= 6) { window.location = base_url + 'ie6'; }
	}
	
	$('#open-reg').click(function(){
		$('.popup').fadeIn();
		$("#reg-name").select();
		$("#reg-name").focus();
		return false;
	});
	
	$('#close-reg').click(function(){
		$('.popup').fadeOut();
	});

	
	$('#open-login').click(function(){
		$('#bgblack, .popup2').fadeIn();
		$("#login_email").select();
		$("#login_email").focus();
		return false;
	});
	
	$('#close-login, #bgblack').click(function(){
		$('.popup, .popup2').fadeOut();
	});
	
	$('#reg-dob, #manual-dob').datepicker({
		dateFormat: 'yy/mm/dd',
		changeYear: true,
		changeMonth: true,
		yearRange: 'c-70',
		maxDate: new Date()
	});
	
	$('#reg-prov').change(function(){
		$('#cityloader').fadeIn();
		$('#reg-city').load(base_url + 'home/city/' + $(this).val(), function(){
			$('#cityloader').fadeOut();
		});
	});
	
	$('#manual-prov').change(function(){
		$('#manualcityloader').fadeIn();
		$('#manual-city').load(base_url + 'home/city/' + $(this).val(), function(){
			$('#manualcityloader').fadeOut();
		});
	});
	
	$('#reg_storecity').change(function(){
		$('#gerailoader').fadeIn();
		$('#reg-mostvisit').load(base_url + 'home/lokasi/' + $(this).val(), function(){
			$('#gerailoader').fadeOut();
		});
	});
	
	$('#manual_storecity').change(function(){
		$('#manualloader').fadeIn();
		$('#manual-mostvisit').load(base_url + 'home/lokasi/' + $(this).val(), function(){
			$('#manualloader').fadeOut();
		});
	});
	
	$('#reg-form').validate({
		messages: {
			'email' : { email : 'Format e-mail salah.', remote: 'E-mail sudah dipakai.' }
		},
		submitHandler: function(form)
		{
			$('#reg-submit-button').hide();
			$('#reg-submit-loading, #inputloader').fadeIn();
			
			$.ajax({
				type: 'POST',
				data: $(form).serialize(),
				url: base_url + 'home/register',
				success : function(){
					$('#reg div.wrapper').fadeOut();
					$('div.wrapper-success').fadeIn();
//					$('.popup').fadeOut();
				}
			});
		}
	});
	
	$('#login-form').validate({
		submitHandler: function(form)
		{
			$.ajax({
				type: 'POST',
				data : $(form).serialize(),
				url: base_url + 'home/login',
				success: function(html){
					if (html == 'success')
					{
						$('.popup, .popup2').fadeOut();
						document.location = base_url;
					}
					else if (html == 'fail')
					{
						document.location = base_url + 'member/login';
					}
				}
			});
		}
	});
});
