register_form = {
	confirm: function()
	{
	}
};

school_searchbox = {
	id: 'school_searchbox',
	nameField: '',
	idField: '',
	district: '',
	current: 'P',
	clear: function(k){
		$("#school_name_" + k).val('');
		$("#school_id_" + k).val('');
	}, 
	
	search_school: function(){
		
		
		f = document.getElementById(this.formId);
		$("#"+this.id+" div.searchbox")
		.load
		(
			//"/member/search_school", 
			"/common/search_school", 
			{ school_name: document.getElementById(this.nameField).value, district: this.district, type: this.current }, 
			function(data){
			}
		);

		
	},
	
	change_district: function( xxx ){
		this.district = xxx;	
		this.search_school();
	},
	
	disable_school: function(){
	//alert(selectbox.className);
/*		if( selectbox.className == 'b'){
			document.getElementById('searchbox').style.display = "none";
			document.getElementById('districtbox').style.display = "none";
			//$("#searchbox").css('display','none');
			//$("#searchbox").css( {'display':'none','width': '300px'} );
		}
		*/
		//this.disable_div();
		
	},
	
	disable_div: function(){
		$("#"+this.id+" div.searchbox").css('display','none');
		$("#"+this.id+" div.districtbox").css('display','none');
	},
	
	enable_school: function( school_type, name_field, id_field ) {
		//alert(school_type);
		this.current = school_type;
		this.nameField = name_field;
		this.idField = id_field;
		this.change_district('');
		$("#"+this.id+" div.searchbox").css('display','block');
		$("#"+this.id+" div.districtbox").css('display','block');
	},
	
	confirm_school: function( school_id, school_name ){

		//this.current = document.getElementById('school_type_'+ school_id).value;
		document.getElementsByName( this.nameField )[0].value = school_name;
		document.getElementById( this.idField ).value = school_id;
		//document.getElementById('school_name_'+ this.current).value = $("#school_hide_"+ school_id).attr("value");
		this.disable_div();
	},
	
	districtbox_tab: function( tab, div )
	{
		$("#"+this.id+" div.districtbox div.tabContent div").css('display','none');
		$("#"+this.id+" div.districtbox div.tabContent div.dn").css('display','block');
		$("#"+this.id+" div.districtbox div.tabContent div."+tab).css('display','block');
	}
			
			
	/*
	submit_register: function(){
		f = document.getElementById('registerform');

		f.submit();
	}
	*/
};


function backStep(current, backTo) {
	$('#'+current).hide();
	$('#'+backTo).fadeIn(1000);
	
	changePart(current, backTo);
}

function changePart(current, to) {
	$('span.part_'+current).removeClass('current');
	$('span.part_'+to).addClass('current');
}

function register_step3(boxid){
	var valid = step3validation();
	if(valid){
		//$("#f1").hide();
		$("#f2").hide();
		$("#"+boxid).fadeIn(1000);
		
		changePart('f2', boxid);
	}	
}
function register_step2(boxid){
//document.boxid.style.display = "block";
   //document.getElementById(boxid).style.display="block";
//  if( alert(boxid);
//alert(  document.getElementById('email').value);
//alert(  document.getElementById('name').value);
//$().attr('value')
	valid = step2validation();
	
	//alert(valid);
	if (valid==true){
		//$("#f1").hide();
		$("#f1").hide();
		$("#"+boxid).fadeIn(1000);
		
		changePart('f1', boxid);		
	}   
}


function step3validation(){
	f=0;
	if (document.getElementsByName("first_name")[0].value == ''){ 
		$("input[name='first_name']").next().text(msg_this_is_required); f=1;
	}else{
		$("input[name='first_name']").next().text("");
	}
	
	if (document.getElementsByName("last_name")[0].value == ''){ 
		$("input[name='last_name']").next().text(msg_this_is_required); f=1;
	}else{
		$("input[name='last_name']").next().text("");
	}
	/*
	if (document.getElementsByName("description")[0].value == ''){ 
		$("input[name='description']").next().text(msg_this_is_required); f=1;
	}else{
		$("input[name='description']").next().text("");
	}
	*/
	
	//checkbox
	var chk = document.getElementById("privacy_options");
	if (!chk.checked){ 
		$("input[name='privacy_options']").next().text(msg_must_agree); f=1;
	}else{
		$("input[name='privacy_options']").next().text("");
	}
	
	if (f==1) return false;
	return true;
}
function step2validation(){
	
	f=0;
	//Login
	if (document.getElementsByName("login")[0].value == ''){ 
		$("input[name='login']").next().next().text(msg_this_is_required); f=1;
	}
	else if (document.getElementsByName("login_chk")[0].value != 'OK'){ 
		$("input[name='login']").next().next().text("請檢查"); f=1;
	}
	else{
		$("input[name='login']").next().next().text("");
	}
	
	
	
	//Name
	if (document.getElementsByName("nickname")[0].value == ''){ 
		$("input[name='nickname']").next().text(msg_this_is_required); f=1;
	}else{
		$("input[name='nickname']").next().text("");
	}
	//Password
	if ( (document.getElementsByName("password")[0].value == '') || (document.getElementsByName("password2")[0].value == '') ) 
	{ 
		if ( document.getElementById("password").value != document.getElementById("password2") ){
			$("input[name='password']").next().text(msg_password_not_the_same);
		}else {
			$("input[name='password']").next().text(msg_this_is_required);
		}
		f=1;
	}else{
		$("input[name='password']").next().text("");
	}	
	
//	if (( document.getElementsByName("password")[0].value != document.getElementsByName("password2")[0].value   )&&(f==0) ) {
	
	//Email
	email = document.getElementById('email');
	valid_email = checkEmail( email ) ;
	if ( !valid_email ){ 
		$("input[name='email']").next().text(msg_email_invalid);f=1;
	}else{
		$("input[name='email']").next().text("");
	}
	
	
	//checkbox
	var chk = document.getElementById("terms_options");
	if (!chk.checked){ 
		$("input[name='terms_options']").next().text(msg_must_agree); f=1;
	}else{
		$("input[name='terms_options']").next().text("");
	}
	
	if (f==1) return false;
	return true;
	//Checkbox
	//if (document.getElementByName('terms_options')[0].checked==true)	alert('checked');

}

function checkEmail(email) {
	//var email = document.getElementById(’emailaddress’);
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email.value)) { //alert(’Please provide a valid email address’);
		email.focus
		return false;
	}
	return true;
}

function do_reg_submit(){
	if( $("#privacy_options").attr("checked") == false ){
		alert('您必須同意私隱條款才可登記喔');
	}
	else{
		pvideo.register_submit();
	}
}
function clear_ctrl(ref_id){
	var val = $("#"+ref_id).attr('value', '');
}

function isGoodUserId(string) {
	var r1 = (string.search(/^[A-Za-z0-9_\-]+$/) != -1);
	var r2 = (string.search(/^[_\-]+$/) == -1);
	return (r1 && r2 && (string.length>=3) && (string.length<=20) );
}


function check_login_name(ref_id, hidden_id){
	var val = $("#"+ref_id).attr('value');
	if(val == ''){
		alert(msg_this_is_required);
		$("#"+ref_id).focus();
	}
	else if (!isGoodUserId(val)){
		alert('用戶名稱只可以是英文字、數字、 _ 或 -');
		$("#"+ref_id).focus();
	}
	else
	{
		$.post(
			"/member/check_username", 
			{ login: val }, 
			function(data){
				if(data == 'OK'){
					alert('用戶名稱沒有問題');
					$("#"+hidden_id+"").attr('value', data);
				}
				else{
					alert('用戶名稱已經被使用了');
					$("#"+hidden_id+"").attr('value', '');
				}
				
			}
		);
	}
	
}
