// eGameTimes.COM Utility Java Function Script V1.0
//     Copyright (C) eGameTimes.COM 1997-2004
//*************************************************

//==============================================================================
// String functions 
//==============================================================================

//------------------------------------------------------------------------------
// Trim a string
//------------------------------------------------------------------------------
function trim_string() 
{
	var ichar, icount;
	var strValue = this;
	
	ichar = strValue.length - 1;
	icount = -1;
	
	while (strValue.charAt(ichar)==' ' && ichar > icount)
		--ichar;
		
	if (ichar!=(strValue.length-1))
		strValue = strValue.slice(0,ichar+1);
		
	ichar = 0;
	icount = strValue.length - 1;
	
	while (strValue.charAt(ichar)==' ' && ichar < icount)
		++ichar;
		
	if (ichar!=0)
		strValue = strValue.slice(ichar,strValue.length);
		
	return strValue;
}

// Extend the string object to include the trim function
// String.prototype.Trim = trim_string;

function trim(str) 
{
	var ichar, icount;
	var strValue = str;
	
	ichar = strValue.length - 1;
	icount = -1;
	
	while (strValue.charAt(ichar)==' ' && ichar > icount)
		--ichar;
		
	if (ichar!=(strValue.length-1))
		strValue = strValue.slice(0,ichar+1);
		
	ichar = 0;
	icount = strValue.length - 1;
	
	while (strValue.charAt(ichar)==' ' && ichar < icount)
		++ichar;
		
	if (ichar!=0)
		strValue = strValue.slice(ichar,strValue.length);
		
	return strValue;
}

//==============================================================================
// Control functions 
//==============================================================================

//------------------------------------------------------------------------------
// Ensure that a control is non-blank
//------------------------------------------------------------------------------
function NonBlank( sFormName, sFieldName, sName ) 
{	
  // line 18
  var value;
  var n;

  var item = eval('document.'+sFormName+'.'+sFieldName);

  if (item.type) {
    if (item.type == 'text') {
    	item.value=trim(item.value);
    }
    else {
      if (item.type == 'select-one')
      {
        if (item.selectedIndex == 0) {
      		//alert("You must select a value from the " + sName + " field");
      		alert("您必须从 " + sName + " 种选择一个值。");
      		item.focus();
      		return false;
        }
        return true;
      }
    }
        
    if (item.value.length==0 || item.value == 'Type here') 
  	{
  		//alert("You must enter a value in the " + sName + " field");
  		alert("您必须在 " + sName + " 中输入数据。");
  		item.focus();
  		return false;
  	}
  }
  else {
    // checklist
    var bAny = false;

    for (n=0; n < item.length; n++) {
      if (item[n].type == "checkbox") {
        if (item[n].checked) {
          bAny = true;
          break;
        }
      }
    }

    if (bAny) {
  		//alert("You must select at least one item from the " + sName + " list");
  		alert("您必须在 " + sName + " 中选择至少一项。");
  		item[0].focus();
  		return false;
    }
  }

	return true;
}

//------------------------------------------------------------------------------
// Ensure that a control contains an email address
//------------------------------------------------------------------------------
function IsEmail( sFormName,sFieldName, sName ) 
{
	var re;

	if (!NonBlank( sFormName, sFieldName, sName ))
	  return false;
	  
  var item = eval('document.'+sFormName+'.'+sFieldName);
  
	re = /^([\w-_]+\.)*[\w-_]+\@([\w-_]+\.)+[a-zA-Z]{2,3}$/i;
	
	if (item.value.search(re) < 0) 
	{
		item.focus();
		//alert(sName + " must be a valid email address");
		alert(sName + " 中要求填入一个有效的 E-mail 地址。");
		return false;
	}

	return true;
}

//------------------------------------------------------------------------------
// Check value length
//------------------------------------------------------------------------------
function CheckLength( sFormName, sFieldName, sName, nMin, nMax ) 
{
  var item = eval('document.'+sFormName+'.'+sFieldName);

	item.value=trim(item.value);

	if (item.value.length < nMin || item.value.length > nMax) 
	{
		item.focus();
		//alert(sName + " must be between " + nMin + " and " + nMax + " characters");
		alert(sName + " 中的值必须界于 " + nMin + " 到 " + nMax + " 个字符。");
		return false;
	}

	return true;
}

//------------------------------------------------------------------------------
// Ensure that a control contains exactly 6 digits
//------------------------------------------------------------------------------
function Is6Digits( sFormName, sFieldName, sName ) 
{
	var re;

  var item = eval('document.'+sFormName+'.'+sFieldName);
  if (item.value == '')
    return true;
  
	re = /^\d\d\d\d\d\d$/i;
	
	if (item.value.search(re) < 0) 
	{
		item.focus();
		//alert(sName + " must be exactly 6 digits");
		alert(sName + " 必须是6位数字。");
		return false;
	}
	if (item.value == "000000") 
	{
		item.focus();
		//alert("000000 is an invalid registration no.");
		alert("000000 是无效的注册号。");
		return false;
	}

	return true;
}

//------------------------------------------------------------------------------
// Ensure that a control contains exactly 6 digits
//------------------------------------------------------------------------------
function Is6DigitsRequired( sFormName, sFieldName, sName ) 
{
	var re;

	if (!NonBlank( sFormName, sFieldName, sName ))
	  return false;
	  
  var item = eval('document.'+sFormName+'.'+sFieldName);
  
	re = /^\d\d\d\d\d\d$/i;
	
	if (item.value.search(re) < 0) 
	{
		item.focus();
		//alert(sName + " must be exactly 6 digits");
		alert(sName + " 必须是6位数字。");
		return false;
	}

	if (item.value == "000000") 
	{
		item.focus();
		//alert("000000 is an invalid registration no.");
		alert("000000 是无效的注册号。");
		return false;
	}
  
	return true;
}

//------------------------------------------------------------------------------
// Ensure that a control contains exactly 7 digits
//------------------------------------------------------------------------------
function Is7Digits( sFormName, sFieldName, sName ) 
{
	var re;

  var item = eval('document.'+sFormName+'.'+sFieldName);
  if (item.value == '')
    return true;
  
	re = /^\d\d\d\d\d\d\d$/i;
	
	if (item.value.search(re) < 0) 
	{
		item.focus();
		//alert(sName + " must be exactly 7 digits");
		alert(sName + " 必须是7位数字。");
		return false;
	}
	if (item.value == "0000000") 
	{
		item.focus();
		//alert("0000000 is an invalid build no.");
		alert("0000000 是无效的版本号。");
		return false;
	}

	return true;
}

//------------------------------------------------------------------------------
// Ensure that a control contains exactly 6 digits
//------------------------------------------------------------------------------
function Is7DigitsRequired( sFormName, sFieldName, sName ) 
{
	var re;

	if (!NonBlank( sFormName, sFieldName, sName ))
	  return false;
	  
  var item = eval('document.'+sFormName+'.'+sFieldName);
  
	re = /^\d\d\d\d\d\d\d$/i;
	
	if (item.value.search(re) < 0) 
	{
		item.focus();
		//alert(sName + " must be exactly 7 digits");
		alert(sName + " 必须是7位数字。");
		return false;
	}

	if (item.value == "0000000") 
	{
		item.focus();
		//alert("0000000 is an invalid build no.");
		alert("0000000 是无效的版本号。");
		return false;
	}
  
	return true;
}