$(document).ready(function(){
	$("select[name=position_id]").change(function () {
          var str = "";
          var url="/Ajax/get_position_ajax.php";
          $("#position_id option:selected").each(function () {
          	  $.post(url, {position_id: $(this).val()}, function(data){
		            if(data.length >0) {
		                $('#sub_position').html(data);
		            }
		        });
        })
	});
	$("select[name=location_id]").change(function () {
      var str = "";
      var url="/Ajax/get_position_ajax.php";
      $("select[name=location_id] option:selected").each(function () {
      	  $.post(url, {location_id: $(this).val()}, function(data){
	            if(data.length >0) {
	                $('#sub_location').html(data);
	            }
	        });
   		 })
	});
		
});

function login(name,cursel,n){
	for(var i=1;i<=n;i++){
		var menu=document.getElementById(name+i);
		var con=document.getElementById("login_"+name+"_"+i);
		menu.className=i==cursel?"hover":"";
		con.style.display=i==cursel?"block":"none";
	}
}
var registration  = {
	
	errors : null, 	
	select_plan : function (plan,zh_cn){
		var words = document.getElementById('next_step_words');
		var holder = document.getElementById('proceed_holder');
		
		var plan_name_e = document.getElementById('plan_name');
		plan_name_e.innerHTML = zh_cn;
		plan_name_e.removeAttribute('id');

		//clear the holder
		holder.innerHTML = '';
		//insert the new thing
		var new_e = holder.appendChild(words.cloneNode(true));
		new_e.removeAttribute('style');
		new_e.removeAttribute('id');
		//reset the ID so we can get it again
		plan_name_e.setAttribute('id','plan_name');		
	},
	
	init : function (){
		this.initForm1();
	},
	
	initForm1 : function (){
		var e_form = document.getElementById('reg_form_1');
		for(var i = 0 ; i < e_form.elements.length; i ++){
			e_form.elements[i].onblur = function (evt) { registration.validateForm1Input(gt.getTargetElement(evt))	};
		}
		/*
		e_form.agree.onclick = function (evt) { registration.validateForm1Input(gt.getTargetElement(evt)) };
		if(e_form.agree.checked){
			e_form.submit.disabled = '';
		}
		*/
		if(e_tmp = document.getElementById('existing_email')){
			this.existing_email = e_tmp.innerHTML;
			e_form.email1.onkeyup = function (evt) { registration.reCheckExistingEmail(gt.getTargetElement(evt)) };
			e_form.email2.onkeyup = function (evt) { registration.reCheckExistingEmail(gt.getTargetElement(evt)) };
		}
		if(e_tmp = document.getElementById('existing_unverified_email')){
			this.existing_unverified_email = e_tmp.innerHTML;
			e_form.email1.onkeyup = function (evt) { registration.reCheckExistingEmail(gt.getTargetElement(evt)) };
			e_form.email2.onkeyup = function (evt) { registration.reCheckExistingEmail(gt.getTargetElement(evt)) };
		}
		
	},
	
	showError : function (err_type){
		var errors_div = document.getElementById('errors_div');
		gt.getElements(errors_div, 'SPAN', err_type)[0].style.display = 'block';
	},
	
	hideError : function (err_type){
		var errors_div = document.getElementById('errors_div');
		gt.getElements(errors_div, 'SPAN', err_type)[0].style.display = 'none';		
	},
	
	clearErrors : function (err_type){
		var errors_div = document.getElementById('errors_div');
		var error_spans = gt.getElements(errors_div, 'SPAN');
		for(var i = 0; i < error_spans.length; i ++){
			error_spans[i].style.display = 'none';
		}
	},
	
	existing_email : null,
	existing_unverified_email : null,
	
	reCheckExistingEmail : function (e_input_email){
		var e_form = document.getElementById('reg_form_1');
		
		if(e_form.email1.value == e_form.email2.value && e_form.email2.value == this.existing_email){
			this.makeInputEvil(e_input_email);
			this.showError('email_already_registered');
		}else if (e_form.email1.value == e_form.email2.value && e_form.email2.value  == this.existing_unverified_email){
			this.makeInputEvil(e_input_email);
			this.showError('email_already_but_unverified');
		}else{
			this.makeInputGood(e_form.email1);
			this.makeInputGood(e_form.email2);
			if(this.existing_email)
				this.hideError('email_already_registered')
			if(this.existing_unverified_email){
				this.hideError('email_already_but_unverified')
			}
		}
		
	},
	
	validateForm1Input : function (input){
		if (input.name != 'submit')// && input.name != 'agree')
			this.makeInputGood(input);			
		var error = false;	
		switch (input.name)
		{
			case "name":
				if(!this.checkBlankInput(input)){
					this.showError('blank_name');
//					dout('blank input');
					error = true;
				}else
					this.hideError('blank_name');
				break;
			case "email1":
				if(!this.validateEmail(input.value)){
					this.showError('invalid_email_syntax');
//					dout('invalid email syntax');
					error = true;
				}else if(input.form.email2.value && input.form.email2.value != input.value){
					this.hideError('invalid_email_syntax');					
					this.showError('emails_differ');
//					dout('emails do not match :(');
					error = true;
				}else{
					this.hideError('emails_differ');
					this.hideError('invalid_email_syntax');
					this.makeInputGood(input.form.email2);
				}
//				else
//					this.hideError('invalid_email_syntax');
				break;
			case "email2":
				if(!this.validateEmail(input.value)){
					this.showError('invalid_email_syntax');
//					dout('invalid email syntax');
					error = true;
				}else if(input.form.email1.value != input.value){
					this.hideError('invalid_email_syntax');					
					this.showError('emails_differ');
//					dout('emails do not match :(');
					error = true;
				}else{
					this.hideError('emails_differ');
					this.hideError('invalid_email_syntax');
					this.makeInputGood(input.form.email1);					
				}
				break;
			case "password1":
				if(!this.validatePassword(input.value)){
					this.showError('invalid_password');
//					dout('password invalid');
					error = true;
				}else if(input.form.password2.value && input.form.password2.value != input.value){
					this.hideError('invalid_password');
					this.showError('passwords_differ');
//					dout('passwords do not match :(');
					error = true;
				}else{
					this.hideError('invalid_password');
					this.hideError('passwords_differ');
					this.makeInputGood(input.form.password2);
				}
				break;
			case "password2":
				if(!this.validatePassword(input.value)){
					this.showError('invalid_password');
//					dout('password invalid');
					error = true;
				}
				else if(input.form.password1.value != input.value){
					this.hideError('invalid_password');
					this.showError('passwords_differ');
//					dout('passwords do not match :(');
					error = true;
				}else{
					this.hideError('invalid_password');
					this.hideError('passwords_differ');
					this.makeInputGood(input.form.password1);					
				}
				break;
			case "agree":
				if(!input.checked){
					this.showError('eula_not_checked');
//					dout('eula not checked');
					error = true;
//					document.getElementById('submit').disabled= 'disabled';
				}else{
					this.hideError('eula_not_checked');	
//					document.getElementById('submit').disabled= '';				
				}
				break;
			case "submit":
//				dout('left the submit button');
				break;
			//default: alert("how the heck did you get here? input name = " + input.name); //G changed hell to heck 2006-8-11
		}
		
		if(error){
			this.makeInputEvil(input);
			return false;
		}else{
			this.hideError('server_found_errors');
			return true;
		}
	},
	
	checkBlankInput : function (e_form_element){
		if(gt.trim(e_form_element.value)){
			return true;
		}else{
			return false;
		}
	},
	
	validateEmail : function (addr){
		/*var email = /^[^@ ]+@[^@. ]+\.[^@ ]*\w\w$/  ;
		
		if (email.test(addr)) {
			return true;
		}else{
			return false;
		}*/
		var str = addr;
		str = str.replace(/^\s+|\s+$/g,''); //去两边空格
		if(str.length<6)return false;//长度小于6
		if(str.indexOf('@')==-1)return false;//没有@
		var error_array = new Array(' ', '""', '..', ',', ';', '.@','!@','#@','$@','%@','&@','*@','+@','-@','/@','=@','?@','^@','_@','`@','{@','|@','}@','~@');
		for(var i=0; i<error_array.length; i++){
			if(str.indexOf(error_array[i]) >-1)return false;
		}
		if(str.split('"').length%2==0)return false;
	
		var regInvalid = /^[0-9a-z\"][_.0-9a-z\"!#$%&'*+-/=?^_`{|}~]{0,63}@([0-9a-z][0-9a-z_-]{0,255}[0-9a-z]\.){1,3}[a-z]{2,5}$/;
		if(regInvalid.test(str)){
			return true;
		}else{
			return false;
		}
	},
	validatePassword : function (passwd){
		if (passwd.length > 5) {
			return true;
		}else{
			return false;
		}
	},
	makeInputEvil : function (input){
		if(input.type != 'checkbox')
			input.style.border = '1px solid red';
	},	
	makeInputGood : function (input){
		if(input.type != 'checkbox')
			input.style.border = '1px solid #C9D5B9';		
	},
	
	validateForm1 : function(e_form){
		this.errors = new Array();
//		if(document.getElementById('disable_js_validation').checked){
//			//don't do the checking.
//		}
//		else{
			var blank_inputs_exist = false;
			var input_errors_exist = false;
			
			try {
				for(var i = 0; i < e_form.elements.length; i ++){
					if(!this.checkBlankInput(e_form.elements[i])){
//						this.blanks[e_form.elements[i].name] = true;
						this.makeInputEvil(e_form.elements[i]);
						blank_inputs_exist = true;
					}
					else if(!this.validateForm1Input(e_form.elements[i])){
						this.makeInputEvil(e_form.elements[i]);
						input_errors_exist = true;
					}
				}
				
				if(blank_inputs_exist){
					
					if(language=='en'){
					alert('You must fill in all fields');
					}else{
						alert('信息填写不完整，请检查!');	
					}
					return false;
				}
				else if (input_errors_exist){
					if(language=='en'){
						alert('There are some errors in your inputs');
					}else{
						alert('您的输入有误，请检查！');	
					}
					return false;
				}
				else{
					return true;
				}
		
			}catch(e){
				for(var i in e){
					alert('caught an exception : ' + e[i]);
				}
				return false;
			}
//		}
	}	
}
