var validator;

function validate_by_registration_Form(){
  
  validator = new Validator_by_register("registration_Form");
  
    validator.required('email', 'The E-mail field is mandatory!');


	if($("#re_email").val()=='')
		validator.required('re_email', 'Re-enter email does not match in the confirmation entry!');
	else
		validator.eqaul('email', 're_email', 'Re-enter email does not match in the confirmation entry!');
	
	validator.required('nickname', 'The Username field is mandatory!');
	
	validator.required('password', 'The Password field is mandatory!');
	
	if($("#password").val()=='')
		validator.required('re_password', 'Re-enter password does not match in the confirmation entry!');
	else
		validator.eqaul('password', 're_password', 'Re-enter password does not match in the confirmation entry!');
		
		
	validator.required('surname', 'The Surname field is mandatory!');
	validator.required('other_name', 'The Other_name field is mandatory!');
	

	
	validator.required_radio('gender', 'The Gender field is mandatory!');
	
	/*
	if ($('input[@name=gender][@checked]').val()==undefined)
		validator.addErrorMsg('The Gender field is mandatory!');
*/

	
	var month = $("#month").val();
	var day = $("#day").val();
	var year = $("#year").val();
	
	validator.required_empty('day', 'brithday');
	validator.required_empty('month', 'brithday');
	validator.required_empty('year', 'brithday');
	
	if(month=='' || day=='' || year==''){
		validator.addErrorMsg('The Date of Birth field is mandatory!');
	}
	else{
		var strDate = $("#month").val() +'/'+ $("#day").val()+'/'+$("#year").val();
		//alert(strDate);
		
		if(!verifyDate(strDate))
			validator.addErrorMsg('Birthday format incorrect, please re-enter!');
	}
	
	
	/*
		validator.required_empty('entry_addr_floor', 'address');
		validator.required_empty('entry_addr_block', 'address');
		validator.required_empty('entry_addr_building', 'address');
		validator.required_empty('entry_street_address', 'address');
		validator.required_empty('entry_addr_district', 'address');
		*/
	//地址
	if( $("#entry_addr_flat").val()=='' && $("#entry_addr_floor").val()=='' && $("#entry_addr_block").val()=='' 
		&& $("#entry_addr_building").val()=='' && $("#entry_street_address").val()=='' && $("#entry_addr_district").val()=='' ){
		
		
		validator.red_word('address');
		
		//validator.required_empty('entry_addr_flat', 'address');
		
		validator.addErrorMsg('The Mailing Address field is mandatory!');
	}
	else{
		 validator.unRed_word('address');
	}
	
  
	validator.required('country', 'The Country/Region field is mandatory!');
	validator.required('inside_china', 'The Province/City/Municipality field is mandatory!');
	
	validator.required('phone', 'The Contact Number field is mandatory!');
	
	
	var child = $("#child").val();
	
	if(child==''){
		validator.required('child', 'The Number of Children field is mandatory!');
		//validator.addErrorMsg('The Number of Children field is mandatory!');
	}
		
	else{
	
		if(child =='1'){
			checkchild(1);
		}
		else if (child =='2'){
			checkchild(1);
			checkchild(2);
		}
		else if (child =='3'){
			checkchild(1);
			checkchild(2);
			checkchild(3);
			
		}
		else if (child =='M'){
			checkchild(1);
			checkchild(2);
			checkchild(3);
			checkchild(4);
		}

	}

	return validator.passed();
}


function checkchild(child){
	

			validator.required('child_surname'+child, 'The child’s Surname field is mandatory!');
			
			validator.required('child_Other_name'+child, 'The child’s Other Names field is mandatory!');
			
			
			validator.required_radio('child_Gender'+ child, 'The Gender field is mandatory!');
			
			/*
			if ($('input[@name=child_Gender'+ child+ '][@checked]').val()==undefined)
				validator.addErrorMsg('The child’s Gender field is mandatory!');
			*/
			
			
	
			validator.required_empty('day'+child, 'brithday'+child);
			validator.required_empty('month'+child, 'brithday'+child);
			validator.required_empty('year'+child, 'brithday'+child);
			if($("#month"+child).val()=='' || $("#day"+child).val()=='' || $("#year"+child).val()==''){
				validator.addErrorMsg('The child’s Birthday field is mandatory!');
			}
			else{
				var strDate = $("#month"+child).val() +'/'+ $("#day"+child).val()+'/'+$("#year"+child).val();
		
				if(!verifyDate(strDate))
					validator.addErrorMsg('The child’s Birthday format incorrect, please re-enter.!');
			}
		
		


}


/*
var form = "";
var submitted = false;
var error = false;
var error_message = "";
*/

function check_forms(form_name){
  error = false;
  
  form = document.getElementById("registration_Form");
  
  error_message = "Error in processing the form.\nPlease revise:\n\n";
  
  var email=form.email;
  var re_email=form.re_email;
  var Username=form.Username;
  var Password=form.Password;
  var re_password=form.re_password;
  var Surname=form.Surname;
  var Other_name=form.Other_name;
  var Male=form.Male;
  var Female=form.Female;
  
  var day=form.day;
  var month=form.month;
  var year=form.year;
  
  var birthday = month.value+day.value+year.value;
  var strDate=month.value+'/'+day.value+'/'+year.value;
  
  var myDate = new Date(); 
  var curr_year=myDate.getFullYear(); //YYYY 
  
  var address=form.address;
  var chi_country=form.country;
  
  check_input("email", 1, " The E-mail field is mandatory\n");
  check_input("re_email", 1, " The Re-enter E-mail field is mandatory\n");
  
  check_input("Username", 1, " The Username field is mandatory\n");
  check_input("Password", 1, " The Password field is mandatory\n");
  check_input("re_password", 1, " The Re-enter Password field is mandatory\n");
  check_input("Surname", 1, " The Surname field is mandatory\n");
  check_input("Other_name", 1, " The Other name field is mandatory\n");
  
  /*
  arr=new Array(email,re_email,Username,Password,re_password,Surname,Other_name);
  for(i=0;i<arr.length;i++){
		if(arr[i].value==''){
			document.getElementById(arr[i].id).style.cssText='background-color:#FEF1F1;';
			document.getElementById(arr[i].id+"_span").className='red';
		}else{
			document.getElementById(arr[i].id).style.cssText='';
			document.getElementById(arr[i].id+"_span").className='';
		}
  }
  
  if(Password.value!='' && re_password.value!=''){
  		if(Password.value.length<6 || re_password.value.length<6){
  			if(Password.value.length<6){
	  			document.getElementById("Password").style.cssText='background-color:#FEF1F1;';
				document.getElementById("Password_span").className='red';
	  			error=true;
		  		error_message+="*  Password must not be less than 6 digits\n\n";
	  		}
	  		if(re_password.value.length<6){
	  			document.getElementById("re_password").style.cssText='background-color:#FEF1F1;';
				document.getElementById("re_password_span").className='red';
	  			error=true;
		  		error_message+="*  Re-enter Password must not be less than 6 digits\n\n";
	  		}
  		}else{
  			if(Password.value!=re_password.value){
	  			document.getElementById("Password").style.cssText='background-color:#FEF1F1;';
				document.getElementById("Password_span").className='red';
	  			document.getElementById("re_password").style.cssText='background-color:#FEF1F1;';
				document.getElementById("re_password_span").className='red';
	  			error=true;
		  		error_message+="*  Re-enter password does not match in the confirmation entry.\n\n";
	  		}else{
	  			document.getElementById("Password").style.cssText='';
				document.getElementById("Password_span").className='';
	  			document.getElementById("re_password").style.cssText='';
				document.getElementById("re_password_span").className='';
	  		}
  		}
  }
  */
  
  if(email.value!='' && re_email.value!=''){
  	if(email.value!=re_email.value){
	  	document.getElementById('email').style.cssText='background-color:#FEF1F1;';
		document.getElementById("email_span").className='red';
		document.getElementById("re_email").style.cssText='background-color:#FEF1F1;';
		document.getElementById("re_email_span").className='red';
  	 	error=true;
	  	error_message+="*  Re-enter Email does not match in the confirmation entry.\n\n";
  	}else{
  		document.getElementById('email').style.cssText='';
		document.getElementById("email_span").className='';
		document.getElementById("re_email").style.cssText='';
		document.getElementById("re_email_span").className='';
  	}
  }
  
  	if(birthday!=''){
	 	if(!verifyDate(strDate)){
	 		document.getElementById('day').style.cssText='background-color:#FEF1F1;';
	 		document.getElementById('month').style.cssText='background-color:#FEF1F1;';
	 		document.getElementById('year').style.cssText='background-color:#FEF1F1;';
			document.getElementById("brithday_span").className='red';
			error_message+="*  Birthday format incorrect, please re-enter.\n";
			error=true;
	  	}else if(year.value>=curr_year){
	  		document.getElementById('day').style.cssText='background-color:#FEF1F1;';
	 		document.getElementById('month').style.cssText='background-color:#FEF1F1;';
	 		document.getElementById('year').style.cssText='background-color:#FEF1F1;';
			document.getElementById("brithday_span").className='red';
	  		error=true;
	  		error_message+="* The Birthday year must less than this year, please re-enter.\n\n";
	  	}else{
	  		document.getElementById('day').style.cssText='';
	 		document.getElementById('month').style.cssText='';
	 		document.getElementById('year').style.cssText='';
			document.getElementById("brithday_span").className='';
	  	}
 	}else{
 		document.getElementById('day').style.cssText='background-color:#FEF1F1;';
	 	document.getElementById('month').style.cssText='background-color:#FEF1F1;';
	 	document.getElementById('year').style.cssText='background-color:#FEF1F1;';
		document.getElementById("brithday_span").className='red';
 		error=true;
	  	error_message+="* The Date of Birth field is mandatory\n\n";
 	}
 	
	
 	if(address.value==''){
 		document.getElementById('address').style.cssText='background-color:#FEF1F1;';
		document.getElementById("address_span").className='red';
 		error=true;
	  	error_message+="* The Address field is mandatory\n\n";
 	}else{
 		document.getElementById('address').style.cssText='';
		document.getElementById("address_span").className='';
 	}
 	
 	if(!Male.checked && !Female.checked){
 		document.getElementById('Male').style.cssText='background-color:#FEF1F1;';
 		document.getElementById('Female').style.cssText='background-color:#FEF1F1;';
		document.getElementById("gender_span").className='red';
 		error=true;
	  	error_message+="* The Gender field is mandatory\n\n";
 	}else{
 		document.getElementById('Male').style.cssText='';
 		document.getElementById('Female').style.cssText='';
		document.getElementById("gender_span").className='';
 	}
 	if(chi_country.value==''){
 		document.getElementById('country').style.cssText='background-color:#FEF1F1;';
		document.getElementById("country_span").className='red';
 		error=true;
	  	error_message+="* The Country/Region field is mandatory\n\n";
 	}else{
 		document.getElementById('country').style.cssText='';
		document.getElementById("country_span").className='';
 	}
	
 	if(form.inside_china){
 		if(form.inside_china.value==''){
 			document.getElementById('inside_china').style.cssText='background-color:#FEF1F1;';
			document.getElementById("pr0").className='red';
 			error=true;
	  		error_message+="* The Province/City/Municipality field is mandatory\n\n";
 		}else{
 			document.getElementById('inside_china').style.cssText='';
			document.getElementById("pr0").className='';
 		}
 	}
 	
  if(error){
  	alert(error_message);
  	return false;
  }
  return true;
  
}


/*

format:mm/dd/yyyy
All by happywinds

*/

function verifyDate(text) {
    var str=text;

text = text.replace(/\s+/g,"");
    if(str.search(/^\d{1,2}([/])\d{1,2}([/])\d{4}$/) == 0){
    
  	 var y = parseInt(str.split("/")[2]*1);
     var m = parseInt(str.split("/")[0]*1);
     var d =parseInt(str.split("/")[1]*1);
  switch(m){
      
      case 1:
	  case 01:
      
      case 3:

      case 05:
	  case 5:
      
	  case 7:
      
	  case 8:
      case 10:

      case 12:

       if(d>31){

        return false;
    }else{

        return true;

    }

       break;

   case 02:
   case 2:

       if((y%4==0 && d>29) || ((y%4!=0 && d>28))){

        return false;

    }else{

        return true;

    }

       break;

      
	  case 4:
      
	  case 6:
     
	  case 9:
      case 11:

       if(d>30){

        return false;

    }else{

        return true;

    }

       break;

   default:

       return false;

  }

}else{

     return false;

}
}
