﻿function openWindow(url,name,features){
	window.open(url,name,features);
}


function popAttach(frm,url,FormName,InputName)
{  // 
	//var param = frm.attachments.value;
	var param = 'url='+ url + '&FormName=' +FormName + '&InputName=' +InputName ; //代码;	
	w=window.open('attach.asp?' + param  +'', 'attach', 'toolbar=no,location=no,width=400,height=400');
	w.focus();
}


function PopWin(URL, winName, width, height)
{
	w=window.open(URL, winName, 'toolbar=no,scrollbars=yes,resizable=yes,width=' + width + ',height=' + height)
	w.focus();
}


function popChoice(frm,FormName,InputType,InputValue)
{  // 
	//var param = frm.attachments.value;
	var param = 'FormName=' +FormName + '&InputType=' +InputType + '&InputValue=' +InputValue ; //代码;	
	w=window.open('choice.asp?' + param  +'', 'attach', 'toolbar=no,location=no,scrollbars=yes,status=yes,width=500,height=400');
	w.focus();
}


function sizeLimit(e, frm, size) {
   if (e.innerText.length > size) {
      alert ('请勿输入超过' + size + '个字');
      e.innerText = e.innerText.slice(0,size);
   }
}





//常用检查函数 Start ===================================================
function CheckFormData(frm, CheckF, CheckFN, CheckFType, Lang)
{
	// 于此三行填入要检查的栏位,名称及类型
	/*
	var CheckF = new Array('');
	var CheckFN = new Array('');
	var CheckFType = new Array('');
	FType: text, textarea, num, idno, email, carno, mobile, sdt(yyyy/mm/dd), invoice
	*/
	
	var cf;
	var i, sel, chk;
	//if (arguments.length < 5) { Lang = 'TC' ;}
	
	switch(Lang)
	{
		case 'EN':
			ph1 = 'Please fill ';
			ph2 = ' is invalid!';
			ph3 = 'Please check one: ';
			ph4 = 'Please check one: ';
			ph5 = 'Please select: ';
			break;
		default:
			ph1 = '请填入 ';
			ph2 = ' 格式不正确!';
			ph3 = '请选择 ';
			ph4 = '请勾选 ';
			ph5 = '请选择 ';
			break;
	}

	for(i=0; i<CheckF.length; i++) {
		element = frm.elements[CheckF[i]];

		if(CheckFType[i] == "text"
				|| CheckFType[i] == "textarea"
				|| CheckFType[i] == "num"
				|| CheckFType[i] == "idno"
				|| CheckFType[i] == "email"
				|| CheckFType[i] == "carno"
				|| CheckFType[i] == "mobile"
				|| CheckFType[i] == "sdt"
				|| CheckFType[i] == "invoice"
				) {
			if(Trim(element.value) == "") {
				alert(ph1 + CheckFN[i] + " !");
				if(element.type!="hidden")element.focus();
				return false;
			}
			//检查数值型态
			if(CheckFType[i] == "num" && isNaN(element.value)) {
				alert(CheckFN[i] + ph2);
				if(element.type!="hidden")element.focus();
				return false;
			}
			//检查身份证字号
			if(CheckFType[i] == "idno" && !checkID(element.value)) {
				alert(CheckFN[i] + ph2);
				if(element.type!="hidden")element.focus();
				return false;
			}
			//检查email格式
			if(CheckFType[i] == "email" && !ValidEmail(element.value)) {
				alert(CheckFN[i] + ph2);
				if(element.type!="hidden") element.focus();
				return false;
			}
			//检查车牌格式
			if(CheckFType[i] == "carno" && !checkCarno(element.value,0)) {
				alert(CheckFN[i] + ph2);
				if(element.type!="hidden")element.focus();
				return false;
			}
			//检查手机格式
			if(CheckFType[i] == "mobile" && !checkMobile(element.value)) {
				alert(CheckFN[i] + ph2);
				if(element.type!="hidden")element.focus();
				return false;
			}
			//检查 Small Date Time 格式
			if(CheckFType[i] == "sdt" && !checkSmallDateTime(element.value)) {
				alert(CheckFN[i] + ph2);
				if(element.type!="hidden")element.focus();
				return false;
			}
			//检查统一编号格式
			if(CheckFType[i] == "invoice" && !checkInvoice(element.value)) {
				alert(CheckFN[i] + ph2);
				if(element.type!="hidden")element.focus();
				return false;
			}
		}
		else if(CheckFType[i] == "radio") {
			if(isNaN(element.length)){
				if(!element.checked){
					alert(ph3 + CheckFN[i] + " !");
					element.focus();
					return false;
				}
			}
			else{
				sel = false;
				for(j=0; j<element.length; j++) {
					sel |= element[j].checked;
				}
				if(! sel) {
					alert(ph3 + CheckFN[i] + " !");
					element[0].focus();
					return false;
				}
			}
		}
		else if(CheckFType[i] == "checkbox") {
			chk = false;
			FirstElement = 0;
			for(j=0; j<frm.elements.length; j++) {
				if(frm.elements[j].name == CheckF[i]) {
					if(FirstElement==0) FirstElement=j;
					chk |= frm.elements[j].checked;
				}
			}
			if(! chk) {
				alert(ph4 + CheckFN[i] + " !");
				frm.elements[FirstElement].focus();
				return false;
			}
		}
		else if (CheckFType[i] == "select") {
			if (element.selectedIndex<0 || element.options[element.selectedIndex].value=="") {
				alert(ph5 + CheckFN[i] + " !");
				element.focus();
				return false;
			}
		}
	}
	return true;
}


function Trim(str){
	// 将字串左右的空白清除
	
	str = str.replace(/^\s+/,"");
	str = str.replace(/\s+$/,"");
	return str;
}

function ValidEmail(emailtoCheck)
{
	// 规则: 1.只有一个 "@"
	//       2.网址中, 至少要有一个".", 且不能连续出现
	//       3.不能有空白

	var regExp = /^[^@^\s]+@[^\.@^\s]+(\.[^\.@^\s]+)+$/;
	if ( emailtoCheck.match(regExp) )
		return true;
	else
		return false;
}

function checkID(id)
{
	// 规则: 首为英文字母, 第二位为1或2,再接八位数字

	var regExp = /^[a-zA-Z][1,2]\d{8}$/;
	if ( id.match(regExp) )
		return true;
	else
		return false;
}

function checkCarno(carno,cartype)
{
	// 规则: 一般自小客车:2个字元+横线+4个数字
	// 规则: 一般自小客车:4个数字+横线+2个字元
	// 规则: 一般自小客车:1个字元+1个数字+横线+4个数字
	// 规则: 计程车:2个字元+横线+3个数字
	// 规则: 计程车:3个数字+横线+2个字元
	
	if(cartype==1){
		//检查一般自小客车
		var regExp1 = /^\w{2}-\d{4}$/;
		var regExp2 = /^\d{4}-\w{2}$/;
		var regExp3 = /^\w{1}d{1}-\d{4}$/;
		if ( carno.match(regExp1) || carno.match(regExp2) || carno.match(regExp3))
			return true;
		else
			return false;
	}
	else if(cartype==2){
		//检查计程车
		var regExp1 = /^\w{2}-\d{3}$/;
		var regExp2 = /^\d{3}-\w{2}$/;
		if ( carno.match(regExp1) || carno.match(regExp2))
			return true;
		else
			return false;
	}
	else if(cartype==0){
		//检查是否为一般自小车or计程车
		return (checkCarno(carno,1)|checkCarno(carno,2));
	}
	else{
		//错误
		return false;
	}
}

function checkMobile(mobile)
{
	// 规则: 10个数字
	
	var regExp = /^\d{10}$/;
	if ( mobile.match(regExp) )
		return true;
	else
		return false;
}

function checkSmallDateTime(sdt)
{
	// 规则: yyyy/mm/dd
	
	var regExp = /^\d{4}\/\d{1,2}\/\d{1,2}$/;
	if ( sdt.match(regExp) )
		return true;
	else
		return false;
}

function checkInvoice(invoice)
{
	// 规则: 8个数字
	
	var regExp = /^\d{8}$/;
	if ( invoice.match(regExp) )
		return true;
	else
		return false;
}

//常用检查函数 End ===================================================

