// JavaScript Document
function isEnglish(name) //英文值检测
{
if(name.length == 0)
    return false;
for(i = 0; i < name.length; i++) {
    if(name.charCodeAt(i) > 128)
    return false;
}
return true;
}

function isMail(name) // E-mail值检测
{
if(! isEnglish(name))
    return false;
i = name.indexOf("@");
j = name.lastIndexOf("@");
if(i == -1)
    return false;
if(i != j)
    return false;
if(i == name.length)
    return false;
return true;
}

function isNumber(String) //检查电话号码是否合法
{ 
      var Letters = "1234567890-"; //可以自己增加可输入值
      var i;
      var c;
	  var start=String.charAt( 0 );
        if(start!='1'&&start!='0'&&start!='8')
             return false;
        if( String.charAt( String.length - 1 ) == '-' )
            return false;
	   if(String.length<10||String.length>20){
	        return false;
		}
		var flag= false;
       for( i = 0; i < String.length; i ++ )
       {   
            c = String.charAt( i );
			if(start!=c){
	           flag=true;
	        }
			start=c;
            if (Letters.indexOf( c ) < 0)
            return false;
        }
		if(flag==false){
		  return false;
		}
		
        return true;
}

function contain(str,charset)// 字符串包含测试函数
{
    var i;
    for(i=0;i<charset.length;i++)
    if(str.indexOf(charset.charAt(i))>=0)
    return true;
    return false;
}


function checkForm()
{
	document.getElementById("cname2").value = cjkEncode(document.getElementById("cname1").value);
	document.getElementById("cpname2").value = cjkEncode(document.getElementById("cpname1").value);
	if(document.form.email.value=="")
	{
		alert("email不能为空!");
		form.email.focus();
		return false ;
	}
	if(document.form.cl_name.value=="")
	{
		alert("姓名不能为空!");
		form.cl_name.focus();
		return false;
	}
	if(document.form.companyname.value=="")
	{
		alert("公司名称不能为空!");
		form.companyname.focus();
		return(false);
	}
	if(document.form.tel.value=="")
	{
		alert("联系电话不能为空!");
		form.tel.focus();
		return false;
	}
	if(! isMail(document.form.email.value)) 
	{
        alert("您的电子邮件不合法！");
        document.form.email.focus();
        return false;
     }
     if(! isNumber(document.form.tel.value)) 
	{
        alert("您的电话号码不合法！");
        document.form.tel.focus();
        return false;
    }
	if ((contain(document.form.cl_name.value, "%\(\)><")) || (contain(document.form.companyname.value, "%\(\)><")))
   {
        alert("输入了非法字符!");
        document.form.cl_name.focus();
        return false;
    }
    return true;
}

function cjkEncode(text) {
    if (text == null) {
        return "";
    }
 
    var newText = "";
    for (var i = 0; i < text.length; i++) {
        var code = text.charCodeAt(i);
        if (code >= 128 || code == 91 || code == 93) {//91 is "[", 93 is "]".
            newText += "[" + code.toString(16) + "]";
        } else {
            newText += text.charAt(i);
        }
    }

    return newText;
}

