﻿function checkEmail (strng) {
	var error="";
	if (strng == "") {
	   error = "请输入您的电子邮箱地址！它将作为您登录我们系统的必要信息。\n";
	} else {
		var emailFilter=/^.+@.+\..{2,3}$/;
		if (!(emailFilter.test(strng))) { 
		   error = "请输入正确的电子邮箱地址。\n";
		}
		else {
		//test email for illegal characters
		   var illegalChars= /[\#\$\*\(\)\<\>\,\;\:\\\"\[\]]/
			 if (strng.match(illegalChars)) {
			  error = "您输入的电子邮箱包含不合法的字符。\n";
		   }
		}
	}
	return error;    
}


// phone number - strip out delimiters and check for 10 digits

function checkPhone (strng) {
	var error = "";
	if (strng == "") {
	   error = "您还没有填写必要的电话信息。\n";
	}

	var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "您填写的电话信息里包含除数字以外的其它字符";
  
    }
    if (!(stripped.length > 8 && stripped.length < 16)) {
	error = "您填写的电话信息的长度不对。\n";
    } 
	return error;
}

//allow the phone is empty
function checkPhoneAllowEmpty (strng) {
	var error = "";
	if (strng != "") {
		var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
		if (isNaN(parseInt(stripped))) {
		   error = "您填写的电话信息里包含除数字以外的其它字符";
	  
		}
		if (!(stripped.length > 8 && stripped.length < 16)) {
		error = "您填写的电话信息的长度不对。\n";
		} 
	}
	return error;
}
// password - between 6-8 chars, uppercase, lowercase, and numeral

function checkPassword (strng) {
	var error = "";
	if (strng == "") {
	   error = "请输入您的密码。\n";
	}

    var illegalChars = /[\W_]/; // allow only letters and numbers
    
    if ((strng.length < 4) || (strng.length > 12)) {
       error = "您的密码长度必须在4位和12位之间。\n";
    }
    else if (illegalChars.test(strng)) {
      error = "对不起，密码只允许包含数字和字符。\n";
    } 
    else if (!((strng.search(/(a-z)+/)) && (strng.search(/(A-Z)+/)) && (strng.search(/(0-9)+/)))) {
       //error = "对不起，密码必须包含至少一个大写字母和一个小写字母以及一个数字。\n";
    }  
	return error;    
}    


// username - 4-10 chars, uc, lc, and underscore only.


function checkUsername (strng) {
	var error = "";
	if (strng == "") {
	   error = "请输入用户名，它将作为您在TOTALCHINESE中的化名。\n";
	} else {
		var illegalChars = /\W/; // allow letters, numbers, and underscores
		if ((strng.length < 4) || (strng.length > 16)) {
		   error = "用户名的长度只能在4位和16位之间。\n";
		}
		else if (illegalChars.test(strng)) {
			error = "用户名只能包含字母，数字以及下划线。\n";
		} 
	}
	return error;
}       


// non-empty textbox

function isEmpty(strng) {
	var error = false;
  if (strng.length == 0) {
	 error = true;
  }
	return error;	  
}

// was textbox altered

function isDifferent(strng) {
var error = ""; 
  if (strng != "Can\'t touch this!") {
     error = "You altered the inviolate text area.\n";
  }
return error;
}

// exactly one radio button is chosen

function checkRadio(checkvalue) {
var error = "";
   if (!(checkvalue)) {
       error = "请填写所有必要信息。\n";
    }
return error;
}

// valid selector from dropdown list

function checkDropdown(choice) {
var error = "";
    if (choice == 0) {
    error = "请从下拉菜单中选择一项。\n";
    }    
return error;
}    

function checkUKPostCode (toCheck) {

  // Permitted letters depend upon their position in the postcode.
  var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
  var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
  var alpha3 = "[abcdefghjkstuw]";                                // Character 3
  var alpha4 = "[abehmnprvwxy]";                                  // Character 4
  var alpha5 = "[abdefghjlnpqrstuwxyz]";                          // Character 5
  

  // Array holds the regular expressions for the valid postcodes
  var pcexp = new Array ();

  // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Expression for postcodes: ANA NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

  // Expression for postcodes: AANA  NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 +"{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Exception for the special postcode GIR 0AA
  pcexp.push (/^(GIR)(\s*)(0AA)$/i);
  
  // Standard BFPO numbers
  pcexp.push (/^(bfpo)(\s*)([0-9]{1,4})$/i);
  
  // c/o BFPO numbers
  pcexp.push (/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);

  // Load up the string to check
  var postCode = toCheck;

  // Assume we're not going to find a valid postcode
  var valid = false;
  
  // Check the string against the types of post codes
  for ( var i=0; i<pcexp.length; i++) {
    if (pcexp[i].test(postCode)) {
    
      // The post code is valid - split the post code into component parts
      pcexp[i].exec(postCode);
      
      // Copy it back into the original string, converting it to uppercase and
      // inserting a space between the inward and outward codes
      postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
      
      // If it is a BFPO c/o type postcode, tidy up the "c/o" part
      postCode = postCode.replace (/C\/O\s*/,"c/o ");
      
      // Load new postcode back into the form element
      valid = true;
      
      // Remember that we have found that the code is valid and break from loop
      break;
    }
  }
  
  // Return with either the reformatted valid postcode or the original invalid 
  // postcode
  if (valid) {return postCode;} else return false;
}

