//Add the item to the cookies:
var COOKIE_NAME = 'pro_products';
$(document).ready(function() {
	login = new loginAPI();
	
	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();
		
		
		$('#loginForm').css( { 'display':'none'});
		$('#resetFrom').css( { 'display':''});
	});
	
	$('#openSignInForm').bind("click", function(e) {
		e.preventDefault();
		
		
		$('#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();
		return false;
	});
	
	$('#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();
			return false;
		}
	});

	$('#submitRegistrationButton').bind("click", function(e) {
		e.preventDefault();
		$("#registerForm").submit();
	});
	$('#registerForm').bind("submit", function(e) {
		e.preventDefault();
		
		pwd1 = $('#reg_pwd').val();
		terms = $('#reg_agree').attr('checked');
		email = $('#reg_username').val();
		fname = $('#reg_fullname').val();
		code = 'Watchitoo';
		
		promoObj = $('#reg_promocode');
		if($('#reg_promocode').length) {
			code =$('#reg_promocode').val();
		}

		var options = {'email': email, 'fullname': fname, 'password': pwd1, 'code': code,
						'autoLogin': true};
		
		watchitoo_OnRegisterError('error', '', options);
		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 (pwd1.length == 0 ) {
			watchitoo_OnRegisterError('error', 'Passwords can\'t be empty', options);
			return false;
		} else if (pwd1.length < 6) {
			watchitoo_OnRegisterError('error', 'Your password must be at least 6 characters long. Please try another.', options);
			return false;
		} else if (pwd1.indexOf(' ') > -1) {
			watchitoo_OnRegisterError('error', 'Passwords cannot contain spaces', options);
			return false;
		} else if (code.length < 4) {
			watchitoo_OnRegisterError('error', 'Invalid Trial Code', options);
			return false;
		} else if (!terms) {
			watchitoo_OnRegisterError('error', 'You must agree to Terms', options);
			return false;
		} else {
			$('.submit').hide(); //Hide buttons
			$('#loadingButtons').show(); //Show loading
			var register = new RegisterAPI(options);
			register.init();
			$('.submit').show(); //Show buttons
			$('#loadingButtons').hide(); //Hide loading
			return false;
		}
	});
});

/* some precaching for the registration button */
pre_register_on = new Image();
pre_register_on.src = GetBaseURL() + '/img/home/register.jpg';
pre_register_off = new Image();
pre_register_off.src = GetBaseURL() + '/img/home/disregister.jpg';

function watchitoo_OnResetFailed(description) {
	$('#singInError').show().attr("innerHTML", description ); 
	//showError(description, 'resetPassError');
}
function watchitoo_OnResetSucess(description) {
	var email =  $('#resetPassEmail').val();
	$('#username').val(email);
	
	$('#loginForm').css( { 'display':''});
	$('#resetFrom').css( { 'display':'none'});
}

function watchitoo_OnStateChange(state) {
	
}

function watchitoo_OnError(description) {
	//showError(description, 'registerError');
	//alert(description);
}

function watchitoo_OnLogin(username, nickname, certificate, accountType) {
	if(accountType=='Member') {
		if(window.location['hash'].length) {
			//Check if the user try to see anything on #MyStuff - we should redirect him to #HomPage
			var reg = '^#MyStuff(.*)$';
			var MyStuffmatchs = window.location['hash'].match(reg);
		
			if(MyStuffmatchs && MyStuffmatchs[1] && MyStuffmatchs[1].length>0) {
				var pathname = window.location['pathname'];
				
				//Check if the users look on /X/index.php, if so, remove the index.php from the path
				var reg = '^(.*)index.php$';
				indexMatchs = pathname.match(reg);
				if( indexMatchs && indexMatchs[1] && indexMatchs[1].length>0) //set the new path without index.php
					pathname = indexMatchs[1];
			
				//var newUrl = window.location['protocol'] + '//' + window.location['hostname'] + pathname + 'member/#HomePage' + MyStuffmatchs[1];
				var newUrl = 'https://' + window.location['hostname'] + pathname + 'member/#HomePage' + MyStuffmatchs[1];
			
				window.location = newUrl;
			}
		} else {
			var domain = GetBaseURL();
			//domain = domain.replace('https://', 'http://');
			window.location=domain+ panel;
		}
	}
}
function watchitoo_OnLoginFailed(errorCode, description) {
	$('#singInError').show().attr("innerHTML", description ); 
}
function watchitoo_OnLogout(params) {
	//setPaymetnStep('stepOne');
	//window.location = GetBaseURL() + 'pro/products.php';
	window.location=GetBaseURL()+ domain;
}


function watchitoo_OnRegister(params) {
	//setPaymetnStep('stepTwo');
	//window.location=GetBaseURL()+"pro/newUser.php";
}

function watchitoo_OnRegisterError( type, description, options ) {
	$('#registerError').show().attr("innerHTML", description ); 
	//showError(description, 'registerError');
}

