/* some precaching */
pre_checkbox_on = new Image();
pre_checkbox_on.src = 'images/login/Checkbox_on.jpg';
pre_checkbox_off = new Image();
pre_checkbox_off.src = 'images/login/Checkbox_off.jpg';

$(document).ready(function() {
	joinAObj = new joinA();
	joinAObj.init();
	
	setInputPreview( 'pwd', 'Password', 'preview' );
	setInputPreview( 'username', 'Email', 'preview' );
	setInputPreview( 'resetPassEmail', 'Email', 'preview' );
	
	//Reset
	if($('#reg_agree').is(':checked')) {
		$('#reg_agree').parent().find('.jNiceCheckbox').click();
	}
	$('#reg_agree').change(function() {
		if(this.checked===true) {
			$('#reg_submit').attr({'src':pre_register_on.src, 'disabled': ''});
		} else {
			$('#reg_submit').attr({'src':pre_register_off.src, 'disabled': 'disabled'});
		}
	});
	
	$('#resetFrom').css( { 'display':'none'});
	
	$('#openResetForm').bind("click", function(e) {
		e.preventDefault();
		var email =  $('#username').val();
		$('#resetPassEmail').val(email);
		
		$('#loginForm').css( { 'display':'none'});
		$('#resetFrom').css( { 'display':''});
	});
	
	$('#openSignInForm').bind("click", function(e) {
		e.preventDefault();
		var email =  $('#resetPassEmail').val();
		$('#username').val(email);
		
		$('#loginForm').css( { 'display':''});
		$('#resetFrom').css( { 'display':'none'});
	});
	
	
	$('#resetFrom').bind("submit", function(e) {
		e.preventDefault();
		
		var email =  $('#resetPassEmail').val();
		if (!validateEmail(email)) {
			watchitoo_OnResetFailed('Email not valid');
			return false;
		}
		
		$('#resetLoader').show();
		var resetPassword = new resetPassAPI(email);
		resetPassword.resetPassword();
		$('#resetLoader').hide();
	});
	
	$('#loginForm').bind("submit", function(e) {
		e.preventDefault();
		
		var email =  $('#username').val();
		var pwd1 = $('#pwd').val();
		var options = {'email': email, 'password': pwd1 };
		
		if (!validateEmail(email)) {
			watchitoo_OnLoginFailed('error', 'Email not valid', options);
			return false;
		} else if (pwd1.length == 0 ) {
			watchitoo_OnLoginFailed('error', 'Passwords can\'t be empty', options);
			return false;
		} else if (pwd1.length < 6) {
			watchitoo_OnLoginFailed('error', 'Passwords too short', options);
				return false;
		} else {
			$('#loginLoader').show();
			var remember = $('#singin_remember').is(':checked');
	
			var isLogedin = login.login({'username':email, 'password':pwd1, 'remember':remember});
			$('#loginLoader').hide();
		}
	});

	$('#registerForm').bind("submit", function(e) {
		e.preventDefault();
		
		pwd1 = $('#reg_pwd').val();
		terms = $('#reg_agree').attr('checked');
		email = $('#reg_username').val();
		fname = $('#reg_fullname').val();
		
		sex = $('#reg_sex').val();
		if(sex=='Male') sex = 'm';
		else if(sex=='Female') sex = 'f';
		b_month = $('#reg_month').val();
		b_day = $('#reg_day').val();
		b_year = $('#reg_year').val();
		var options = {'email': email, 'fullname': fname, 'password': pwd1, 
						'sex':sex, 'b_month':b_month, 'b_day':b_day, 'b_year':b_year,
						'autoLogin': true};
		
		if (!validateEmail(email)) {
			watchitoo_OnRegisterError('error', 'Email not valid', options);
			return false;
		} else if (fname.length<2) {
			watchitoo_OnRegisterError('error', 'Full Name Required!', options);
			return false;
		} else if (sex=='Sex') {
			watchitoo_OnRegisterError('error', 'Sex Required!', options);
			return false;
		} else if (b_month=='Month') {
			watchitoo_OnRegisterError('error', 'Month Required!', options);
			return false;
		} else if (b_day=='Day') {
			watchitoo_OnRegisterError('error', 'Day Required!', options);
			return false;
		} else if (b_year=='Year') {
			watchitoo_OnRegisterError('error', 'Year Required!', options);
			return false;
		} else if (pwd1.length == 0 ) {
			watchitoo_OnRegisterError('error', 'Passwords can\'t be empty', options);
			return false;
		} else if ( !validateDate( b_day, b_month, b_year ) ) {
			watchitoo_OnRegisterError('error', 'Date is Invalid or Out of Range', options);
			return false;
		} else if (pwd1.length < 6) {
				watchitoo_OnRegisterError('error', 'Passwords too short', options);
				return false;
		} else if (!terms) {
			watchitoo_OnRegisterError('error', 'You must agree to Terms', options);
			return false;
		} else {
			$('#registerLoader').show();
			var register = new RegisterAPI(options);
			register.init();
			$('#registerLoader').hide();
		}
	});
});

function validateDate(day, month, year) {
	var d = new Date();
	var curr_year = d.getFullYear();
	
	if (year < 1900 || year > curr_year) 
		return false;
	
	if (month < 1 || month > 12)
		return false;
	
	var daysInMonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	if(month==2) {
		//February has 29 days in any year evenly divisible by four,
	    //EXCEPT for centurial years which are not also divisible by 400.
		daysInMonth = (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
	} else
		daysInMonth = daysInMonth[month-1];
	
	//var daysInMonth = 32 - new Date(year, month, 32).getDate();
	if (day < 1 || day > daysInMonth) 
		return false;

	return true;
}