var info = {
	openedId: 0,
	
	show: function(id) {
		if (this.openedId != 0) {
			$('#info_' + id).slideUp();
		}
		
		if (this.openedId != id) {
			$('#info_' + id).show('normal');
			this.openedId = id;
		}
		else {
			this.openedId = 0;
		}
	}
}

var register = {
	interest: new Array(),
	mobiles: new Array(),
	errList: new Array(),
	
	init: function() {
		ctrl.setCheckBoxGroup('radio_gender_male', {id: 'radio_gender_female'});
		ctrl.setCheckBoxGroup('radio_gender_female', {id: 'radio_gender_male'});
		
		ctrl.setCheckBoxGroup('interest_family_no_family', {id: 'interest_family_with_family'});
		ctrl.setCheckBoxGroup('interest_family_with_family', {id: 'interest_family_no_family'});
		
		ctrl.setCheckBoxCallBack(register.onCheckBoxPress);
		
		ctrl.init();
	},
	
	step1: function() {
		$('span.ajax').addClass('loading');
		
		this.resetErrors();
		var service = new newService('register', {cmd: 1,
									 			  username: $('#input_username').val(),
									 			  name: $('#input_first_name').val(),
												  last_name: $('#input_last_name').val(),
												  phone: $('#input_tel_num').val(),
												  postcode: $('#input_postcode').val(),
												  email: $('#input_email').val(),
												  //mobile: dd['mobile'].getVal(),
												  birthday: birthday.get(),
												  pass: $('#input_password').val(),
												  repass: $('#input_re_password').val(),
												  male: ctrl.isCheckBoxChecked('radio_gender_male') ? 1 : 0,
												  female: ctrl.isCheckBoxChecked('radio_gender_female') ? 1 : 0,
												  rights: ctrl.isCheckBoxChecked('radio_rights') ? 1 : 0,
												  terms: ctrl.isCheckBoxChecked('radio_terms') ? 1 : 0,
												  //scode: $('#scode').val()
									 });
		
		service.call( {
					   success: function(res, status) {
						   		$('span.ajax').removeClass('loading');
								if (status == 'success') {
										redirect("?step=3");
									}
									else {
										register.showErrors(res);
									}
								}
					} );
	},
	
	step2: function() {
		$('span.ajax').addClass('loading');
		
		var service = new newService('register', {cmd: 2,
									 			  phone1: $('#phone1').val(),
												  phone2: $('#phone2').val(),
												  phone3: $('#phone3').val(),
												  phone4: $('#phone4').val(),
												  phone5: $('#phone5').val(),
												  phone6: $('#phone6').val()
									 });
		service.call( {
					   success: function(res, status) {
						   				$('span.ajax').removeClass('loading');
										if (status == 'success') {
											redirect("?step=3");
										}
										else {
											redirect('home.php');
										}
								}
					} );
	},
	
	step3: function() {
		$('span.ajax').addClass('loading');
		
		var service = new newService('register', {cmd: 3, interest: this.getInterestList()});
		service.call( {
					   success: function(res, status) {
						   				$('span.ajax').removeClass('loading');
										
										if (status == 'success') {
											redirect("myaccount.php");
										}
								}
					} );
	},
	
	update: function() {
		$('span.ajax').addClass('loading');

		this.resetErrors();
		var service = new newService('register', {cmd: 4,
									 			  //username: $('#input_username').val(),
									 			  name: $('#input_first_name').val(),
												  last_name: $('#input_last_name').val(),
												  phone: $('#input_tel_num').val(),
												  postcode: $('#input_postcode').val(),
												  email: $('#input_email').val(),
												 // mobile: dd['mobile'].getVal(),
												  birthday: birthday.get(),
												  pass: $('#input_password').val(),
												  repass: $('#input_re_password').val(),
												  male: ctrl.isCheckBoxChecked('radio_gender_male') ? 1 : 0,
												  female: ctrl.isCheckBoxChecked('radio_gender_female') ? 1 : 0,
												  rights: ctrl.isCheckBoxChecked('radio_rights') ? 1 : 0,
												  terms: ctrl.isCheckBoxChecked('radio_terms') ? 1 : 0,
												  interest: this.getInterestList()
									 });
		service.call( {success: function(res, status) {
									$('span.ajax').removeClass('loading');
									if (status == 'success') {
									}
									else {
										register.showErrors(res);
									}
								}
					} );
	},
	
	getInterestList: function() {
		var interest = '';
		for (id in this.interest) {
			if (this.interest[id] != false) {
				if (interest != '') interest += ';';
				interest += id;
			}
		}
		return interest;
	},
	
	onCheckBoxPress: function(id) {
		for (var indx in register.errList) {
			var elm = register.errList[indx][0];
			if (elm.attr('id') == id) {
				register.errList[indx] = [null, ""];
			}
		}
		
		if (id == 'interest_family_no_family') {
			register.interest['interest_family_with_family'] = false;
		}
		else if (id == 'interest_family_with_family') {
			register.interest['interest_family_no_family'] = false;
		}
		register.interest[id] = ctrl.isCheckBoxChecked(id);
	},
	
	showErrors: function(errList) {
		if (errList.indexOf('TEXT:') != -1) {
			$('#errors').html(errList.substr(5));
		}
		else {
			if (errList.indexOf('::') != -1) {
				var data = errList.split('::');
				errList = data[0];
				$('#errors').html(data[1]);
			}
			
			register.errList = new Array();
	
			var err = errList.split('\n');
			for (var indx in err) {
				var dat = err[indx].split('|');
				var elm = $('#' + dat[0]);
				if (elm.tagName() != "A") {
					elm = elm.parent();
				}
				register.errList[this.errList.length] = [elm, elm.css('background-image')];
				elm.css('background-image', 'url('+dat[1]+')');
			}
		}
	},
	
	resetErrors: function() {
		$('#errors').html('');
		
		for (var indx in this.errList) {
			var dat = this.errList[indx];
			if (dat[0] != null) {
				dat[0].css('background-image', dat[1]);
			}
		}
	}
}

function login() {
	$('#login').addClass('loading');
	
	register.resetErrors();
	var service = new newService('login', {user: $('#input_user').val(), pass: $('#input_pass').val()});
	service.call( {
				   success: function(res, status) {
					   	$('#login').removeClass('loading');
						
					    if (status == 'success') {
							redirect('myaccount.php');
						}
						else {
							register.showErrors(res);
						}
					}
				} );
}

function resetPass() {
	$('#resetInfo').remove();
	$('#reset').addClass('loading');
	register.resetErrors();
	
	var service = new newService('resetpass', {/*scode: $('#scode').val(), */username: $('#username').val()});
	service.call( {
				   success: function(res, status) {
					   	$('#reset').removeClass('loading');
						
						if (status != 'success') {
							register.showErrors(res);
						}
						else {
							alert(res);
						}
					}
				} );	
}

function sendUserName() {
	$('#fphonenum').addClass('loading');
	register.resetErrors();
	
	var service = new newService('sendusername', {/*scode: $('#scode2').val(), */phonenum: $('#phonenum').val()});
	service.call( {
				   success: function(res, status) {
					   	$('#fphonenum').removeClass('loading');
						
						if (status != 'success') {
							register.showErrors(res);
						}
						else {
							alert(res);
						}
					}
				} );	
}
	
function videoVote(id, vote) {
	var service = new newService('vote', {id: id, vote: vote});
	service.call( {success: function(res, status) { }} );
}

function closeAttentionDlg() {
	$('.overlay').remove(); 
	$('.attention-dlg').remove()
}

function reloadCaptcha(id, q) {
	$('#'+id).attr('src', '/captcha/captcha.php?'+ (new Date().getSeconds()) +(q ? '&' + q : ''));
}

function terms() {
	window.open('terms.php');
}