function register_check_all(){
	this.country = false;
	this.member = false;
	this.password = false;
	this.c_password = false;
	this.email = false;
	this.c_email = false;
	this.first_name = false;
	this.last_name = false;
	this.company_name = false;
	this.address = false;
	this.state = false;
	this.tel = false;
	this.fax = true;
	this.mobile = true;
	this.position = true;
	this.city = true;
	this.information = false;
}
var bln_check = new register_check_all();

function change_country(){
	check_country();
	var country_id = $('country').value;
	$('info_state').style.display='none';
	if( country_id == 49 ){
		$('select_state_1').style.display = 'none';
		$('select_state_2').style.display = 'block';
	}else{
		$('select_state_1').style.display = 'block';
		$('select_state_2').style.display = 'none';
	}

	/*设置国家区号*/
	var param  = 'str_action=country&';
		param += 'country_id=' + country_id;
	var url = '/common/language.php';
	var myAjax = new Ajax.Request(
				url,
				{method: 'post', parameters: param, onComplete: showResponse}
				);

	/*ajax返回信息*/
	function showResponse(request){
		if (request.responseText == -1) {
			$('tel_1').value = "country code";
			$('fax_1').value = "country code";
		}
		else {	
			$('tel_1').value = request.responseText;
			$('fax_1').value = request.responseText;
		}	
    }
}

function check_country(){
	var country_id = $('country').value;
	if (country_id!=''){
		bln_check.country = true;
		$('info_country').style.display='none';
	}
	else {
		bln_check.country = false;
		$('info_country').style.display='block';
	}
}

function check_member(){
	var member_id = $('member_id').value;

	new Ajax.Request('/register/register.php',{
		method: 'post',
		parameters:'str_action=check_user&member_id='+member_id ,
		onSuccess: function(response) {
			if ((response.responseText!=0) || (!/[x00-xff]+/.test( member_id )) ){
				bln_check.member = false;
				$('info_member').innerHTML = response.responseText;
				$('info_member').style.display='block';
			}else{
				bln_check.member = true;
				$('info_member').style.display='none';
			}
		}
	});
}

function check_password(){
	var password = $('password').value;

	var flag = false;
	if( password.length < 6 || password.length > 15 || password.indexOf(' ') != -1 ){
		$('info_passwd').style.display = 'block';
	}else{
		flag = true;
		$('info_passwd').style.display = 'none';
	}
	var num = 0
	if( /[A-Za-z]+/.test( password ) ){
		num ++;
	}
	if( /[0-9]+/.test( password ) ){
		num ++;
	}
	if( /[^\w]+/.test( password ) ){
		num = num + 2;
	}

	if( num == 1 && flag == true ){
		$('passwd_bar').innerHTML = error_info.lower;
		$('passwd_bar').style.width = '33%';
		bln_check.password = true;
	}else if( num == 2 && flag == true ){
		$('passwd_bar').innerHTML = error_info.normal;
		$('passwd_bar').style.width = '66%';
		bln_check.password = true;
	}else if( num >= 3 && flag == true ){
		$('passwd_bar').innerHTML = error_info.high;
		$('passwd_bar').style.width = '100%';
		bln_check.password = true;
	}else{
		$('passwd_bar').innerHTML = '';
		$('passwd_bar').style.width = '0%';
		bln_check.password = false;
	}
}

function check_c_password(){
	var password = $('password').value;
	var c_password = $('c_password').value;
	if( password != c_password || password.length < 6 || password.length > 15 ){
		$('info_c_passwd').style.display = 'block';
		bln_check.c_password = false;
	}else{
		$('info_c_passwd').style.display = 'none';
		bln_check.c_password = true;
	}
}

function check_email(){
	var email = $('email').value;

	new Ajax.Request('/register/register.php',{
		method: 'post',
		parameters:'str_action=check_email&email='+ email ,
		onSuccess: function(response) {
			if (response.responseText!=0){
				bln_check.email = false;
				$('info_email').innerHTML = response.responseText;
				$('info_email').style.display='block';
			}else{
				bln_check.email = true;
				$('info_email').style.display='none';
			}
		}
	});
}

function check_c_email(){
	var email = $('email').value;
	var c_email = $('c_email').value;
	if( email != c_email ){
		$('info_c_email').style.display = 'block';
		bln_check.c_email = false;
	}else{
		$('info_c_email').style.display = 'none';
		bln_check.c_email = true;
	}
}

function check_first_name(){
	var first_name = $('first_name').value;
	if( (!/[a-zA-Z\.\_\s0-9]+$/.test( first_name )) || (!/[x00-xff]+/.test( first_name )) ){
		bln_check.first_name = false;
		$('info_first_name').style.display = 'block';
	}else{
		bln_check.first_name = true;
		$('info_first_name').style.display = 'none';
	}
}

function check_last_name(){
	var last_name = $('last_name').value;
	if( (!/[a-zA-Z\.\_\s0-9]+$/.test( last_name )) || (!/[x00-xff]+/.test( last_name )) ){
		bln_check.last_name = false;
		$('info_last_name').style.display = 'block';
	}else{
		bln_check.last_name = true;
		$('info_last_name').style.display = 'none';
	}
}

function check_company_name(){
	var company_name = $('company_name').value;

	new Ajax.Request('/register/register.php',{
		method: 'post',
		parameters:'str_action=check_company&company_name='+ encodeURIComponent( company_name ),
		onSuccess: function(response) {
			if ((response.responseText!=0) || (!/[x00-xff]+/.test( company_name )) ){
				bln_check.company_name = false;
				$('info_company').innerHTML = response.responseText;
				$('info_company').style.display='block';
			}else{
				bln_check.company_name = true;
				$('info_company').style.display='none';
			}
		}
	});
}

function check_position(){
	var position = $('position').value.trim();

	if( position.length == 0){
		bln_check.position = true;
		$('info_position').style.display = "none";
	}else if( !/[x00-xff]+/.test( position ) ){
		bln_check.position = false;
		$('info_position').style.display = "block";
	}
}


function check_city(){
	var city = $('city').value.trim();

	if( city.length == 0){
		bln_check.city = true;
		$('info_city').style.display = "none";
	}else if( !/[x00-xff]+/.test( city ) ){
		bln_check.city = false;
		$('info_city').style.display = "block";
	}
}

function check_address(){
	var address = $('company_address').value.trim();
	if( ( address.length <= 0) || (!/[x00-xff]+/.test( address )) ){
		bln_check.address = false;
		$('info_address').style.display='block';
	}else{
		bln_check.address = true;
		$('info_address').style.display='none';
	}
}

function check_state(){
	var state = $('country').value == 49 ? $('cn_state').value : $('state').value;
	if( (state.trim().length <= 0) || (!/[x00-xff]+/.test( state )) ){
		bln_check.state = false;
		$('info_state').style.display='block';
	}else{
		bln_check.state = true;
		$('info_state').style.display='none';
	}
}

function check_tel() {
	/*处理"0"操作*/
	if ($('tel_2').value != '') {
		if (/^[0][1-9]{0,3}$/.test($('tel_2').value)) {
			var tel_2 = $('tel_2').value.substr(1, $('tel_2').value.length);
		}
	}
	tel_2 = tel_2 ? tel_2 : "";

	var str_tel = String.format( "{0}-{1}-{2}" , $('tel_1').value.trim() , tel_2.trim() , $('tel_3').value.trim() );

	if( str_tel == "--" ){
		bln_check.tel = false;
		$('info_tel').style.display='block';
	}else if( /[0-9]{1,4}\-[0-9]{0,4}\-[0-9\-\*\s\#]{6,20}$/.test( str_tel ) ) {
		$('info_tel').style.display = 'none';
		bln_check.tel = true;
	}else{
		bln_check.tel = false;
		$('info_tel').style.display='block';
	}
}

function check_fax() {
	/*处理"0"操作*/
	if ($('fax_2').value != '') {
		if (/0[1-9]{0,3}$/.test($('fax_2').value)) {
			var fax_2 = $('fax_2').value.substr(1, $('fax_2').value.length);
		}
	}

	fax_2 = fax_2 ? fax_2 : "";

	var str_fax = String.format( "{0}-{1}-{2}" , $('fax_1').value.trim() , fax_2.trim() , $('fax_3').value.trim() );

	if( str_fax == "--" || /[0-9]{1,4}\-\-$/.test( str_fax ) ){
		bln_check.tel = true;
		$('info_fax').style.display='none';
	}else if( /[0-9]{1,4}\-[0-9]{0,4}\-[0-9\-\*\s\#]{6,20}$/.test( str_fax ) ) {
		$('info_fax').style.display = 'none';
		bln_check.tel = true;
	}else{
		bln_check.tel = false;
		$('info_fax').style.display='block';
	}
}

function check_mobile() {
	var mobile = $('mobile').value.trim();

	if( mobile.length == 0 ){
		bln_check.mobile = true;
		$('info_mobile').style.display='none';
	}else if( /[0-9]+$/.test( mobile ) ) {
		$('info_mobile').style.display = 'none';
		bln_check.mobile = true;
	}else{
		bln_check.mobile = false;
		$('info_mobile').style.display='block';
	}
}

function check_company_info() {
	var information = $('company_infor').value.trim();

	if( (information.length < 100) || (!/[x00-xff]+/.test( information )) ){
		bln_check.information = false;
		$('info_company_infor').style.display='block';
	}else{
		bln_check.information = true;
		$('info_company_infor').style.display='none';
	}
}

function check_focus(id) {
	var obj = $(id);
	if (obj.value == "country code") {
		obj.focus();
		obj.select();
	}
}

function check_cat(){
	var cat_id=$("hdn_catagory_id").value;
	if(cat_id==0 || cat_id==""){
		$("err_msg_company_catagory").style.display="";
		//$("hdn_catagory_id").focus();
		return false;
	}
	$("err_msg_company_catagory").style.display="none";
	return true;
}

function check_backup_email(){
	var txt_backup_email=$("backup_email").value.trim();
	if(txt_backup_email.length>0 && !txt_backup_email.isEmail()){
		if(("http://"+txt_backup_email).isEmail()){
			$("backup_email").value="http://"+$("backup_email").value.trim();
			$("backup_email_error").style.display="none";
			return;
		}
		$("backup_email_error").style.display="";
		return;
	}
	$("backup_email_error").style.display="none";
}

function on_submit(){
	var forum_registered = $('forum_registered').value;
	if ( forum_registered ){
		bln_check.member = true ;
		bln_check.password = true ;
		bln_check.c_password = true ;
		bln_check.email = true ;
		bln_check.c_email = true ;
	}
	
	check_cat();

	if( bln_check.member == true && bln_check.password == true && bln_check.c_password == true && bln_check.email == true && bln_check.c_email == true && bln_check.first_name == true && bln_check.last_name == true && bln_check.company_name == true  && bln_check.position == true && bln_check.address == true && bln_check.state == true && bln_check.tel == true && bln_check.fax == true && bln_check.mobile == true && bln_check.information == true && bln_check.city == true && bln_check.country == true ){
		var country_id = encodeURIComponent( $('country').value.trim() );
		var member_id = encodeURIComponent( $('member_id').value.trim() );
		var password = encodeURIComponent( $('password').value.trim() );
		var email = encodeURIComponent( $('email').value.trim() );
		var first_name = encodeURIComponent( $('first_name').value.trim() );
		var last_name = encodeURIComponent( $('last_name').value.trim() );
		var gender = encodeURIComponent( $('gender').value.trim() );
		var position = encodeURIComponent( $('position').value.trim() );
		var company_name = encodeURIComponent( $('company_name').value.trim() );
		var company_address = encodeURIComponent( $('company_address').value.trim() );
		var state = $('country').value == 49 ? $('cn_state').value : $('state').value;
			state = encodeURIComponent( state + "::" + $('city').value.trim() );
		var zipcode = encodeURIComponent( $('zipcode').value.trim() );
		var tel = encodeURIComponent( String.format( "{0}-{1}-{2}" , $('tel_1').value.trim() , $('tel_2').value.trim() , $('tel_3').value.trim() ) );
		var fax = encodeURIComponent( String.format( "{0}-{1}-{2}" , $('fax_1').value.trim() , $('fax_2').value.trim() , $('fax_3').value.trim() ) );
		var mobile = encodeURIComponent( $('mobile').value.trim() );
		var company_infor = encodeURIComponent( $('company_infor').value.trim() );
		var ver_code = encodeURIComponent( $('ver_code').value.trim() );
		var hdn_catagory_id=$('hdn_catagory_id').value;
		var user_standby_email=encodeURIComponent($("backup_email").value.trim());

		$('info_ver_code').style.display = 'none';
		$('info_submit').style.display = 'none';

		var param = "str_action=dopost&country_id="+ country_id +"&member_id=" + member_id + "&password=" + password;
		param += "&email=" + email + "&first_name=" + first_name +"&last_name=" + last_name +"&gender=" + gender;
		param += "&position=" + position + "&company_name=" + company_name + "&company_address=" + company_address;
		param += "&state=" + state + "&zipcode=" + zipcode + "&tel=" + tel + "&fax=" + fax + "&mobile=" + mobile;
		param += "&company_infor=" + company_infor + "&ver_code=" + ver_code + "&forum_registered=" + forum_registered;
		param += "&hdn_catagory_id="+hdn_catagory_id;
		param += "&user_standby_email="+user_standby_email;
		param += "&global_supplier="+$("is_supplier").value;
		param += "&ref="+$('ref').value;
		new Ajax.Request('/register/register.php',{
			method: 'post',
			parameters: param ,
			onSuccess: function(response) {
				if(response.responseText.substring(0,1) == "["){
					if( $("is_supplier").value == "true" ){
						var supplier = document.getElementsByName('supplier_type');
						location.href = "/services/global_supplier_services.html?action=buy&ptype="+( supplier[0].checked == true ? supplier[0].value : supplier[1].value);
					}else{
						var result = eval(response.responseText);
						func = function(){
							location.href = result[0].url;
						}
						alert(result[0].info);
					}
				}else if(response.responseText== 0){
					$('info_ver_code').style.display = 'block';
					$('verification').src='/common/verification.php?'+new Date().getTime();
				}else if(response.responseText==-1){
					$('info_submit').style.display = 'block';
					location.href('#list');
				}else{
					if( $("is_supplier").value == "true" ){
						var supplier = document.getElementsByName('supplier_type');
						location.href = "/services/global_supplier_services.html?action=buy&ptype="+( supplier[0].checked == true ? supplier[0].value : supplier[1].value);
					}else{
						func = function () {
							location.href = '/index.html';
						}
						alert(response.responseText);
					}
				}
			}
		});
	}else{
		check_country();
		$('info_submit').style.display = 'block';
		location.href='#list';
	}
}

function choose_catagory(){
	 cfg_catagory_dialog.idAction='hdn_catagory_id';
	 cfg_catagory_dialog.txtAction='txt_catagory_path';
	 cfg_catagory_dialog.nodeSelectable=false;
	 cfg_catagory_dialog.levelLimit=2;
	 open_catagory_dialog();
}