// login.js

	///////////////added by x55720 on 10.Dec.2007///////////	
window.onload = function() {	
	var fpKey = getRandomKey();	
	
	//document.loginForm.password.focus();		
	
	try {
		initializeFinPrints(new Array(new Array("fingerPrint", 0)));
		var objAX = document.getElementById("fingerPrint");//alert(fpKey);
		objAX.SetKey(fpKey);
		objAX.SetNewScanMethod(true);
		
		if(!isFireFox()){
			if (objAX.GetDeviceType() == 2  || objAX.GetDeviceType() == 3){       //upek设备
				//可设置超时时间也可不设置，不设则为无限期等待				
	  			if (objAX.ScanFingerPrint("fingerPrint", "doAfterScaned", "scanedFinPrint") == 0){
	  				//objAX.ScanFingerPrint（objName,"doAfterScaned", scanedFinPrintName）==0
 					//扫描线程成功运行
					changeBtnStatus("scanBtn", true);
    			}
			} else {
				changeBtnStatus("scanBtn", false);
			}	
		} else {
			changeBtnStatus("scanBtn", false);
		}	
	} catch(e) {
		//alert("Failed to init fingerprint ActiveX Object. Please install it correctly first.");
	}
}

window.onunload = function() {
	var objAX = document.getElementById("fingerPrint");
	try {
		objAX.StopScan();   //FireFox不会像IE那样，在使用同步方式的ScanFinger接口时，因扫描未结束而屏蔽关闭按钮，调一下停止设备(没有扫描的时候调用该函数不会有什么影响)
			} catch(e) {
		//alert("Failed to init fingerprint ActiveX Object. Please install it correctly first.");
	}
}

// Initializes all the ActiveX objects.
function initializeFinPrints(finPrints) {

	if (finPrints != null) {
		for (var i=0; i<finPrints.length; i++) {
			setFinPrintType(finPrints[i][0], finPrints[i][1]);
		}	
		try {
			var userID=document.getElementById(finPrints[0][0]).GetUserName();
			if (userID != null && userID.length > 0) {
				document.getElementsByName("uid")[0].value = userID;
				document.getElementById(finPrints[0][0]).setCurrentUserName(userID);
				document.loginForm.password.focus();	
			} else {
			    document.getElementsByName("uid")[0].focus();	
			}
		} catch(e) {
		
		}
	} 
}
// Sets the type of the ActiveX object.
function setFinPrintType(objName, finType) {
	var fshot=document.getElementById(objName);
	try {
      fshot.CollectFingerFeature(finType);
    } catch(e) {
		//alert("Failed to init fingerprint ActiveX Object. Please install it correctly first.");
	}
    //fshot.style.display="block";
}

// Scans the finger print and output the result to the given object.
function scanFinPrint(objName, scanedFinPrintName) {

	var objAX = document.getElementById(objName); 
	
	//IE使用新扫描接口，页面加载即启动扫描
	if(!isFireFox()){	
		if(objAX.ScanFingerPrint(objName, "doAfterScaned", scanedFinPrintName) == 0){ 
			//扫描线程成功运行
			//btn.disabled = true; 
			if(objAX.GetDeviceType() == 2 || objAX.GetDeviceType() == 3) {
			  changeBtnStatus("scanBtn", true);
			}
		}
	} else {
		changeBtnStatus("scanBtn", true);	
		var finval = objAX.ScanFinger();
		doAfterScaned(objName, finval, scanedFinPrintName); //得到指纹后页面主动调用doAfterScaned	
	}

/*
 	var objAX = document.getElementById(objName); 
 	//the object name of the finger print scaned to be output
 	var objValue = document.getElementById(scanedFinPrintName);
 	//scans and gets the value
   	var scanedValue = objAX.ScanFinger(); 
   
   	//output the value of the finger print scaned if scan times reaches the given number
	if(objAX.GetCollectCount() == objAX.GetCollectTimes() && objValue != null) {
		objValue.value = scanedValue;
   	}
*/   	
}

//由控件在得到指纹特征码后回调，此处finPrintVal为得到的特征码，objId为上面
//的ScanedFinPrint（表单中的一个hidden控件ID）
function doAfterScaned(objAxId, finPrintval, objName) {

	if(finPrintval != null && finPrintval.length < 1){   
	    //扫描失败或超时了
		changeBtnStatus("scanBtn", false);
	} else {
		//var form=document.loginForm;
	   	//form.actionFlag.value="loginAuthenticate";
	   	//form.loginMethod.value="finger";
	   	var objAX = document.getElementById(objAxId);
		if(objAX.GetCollectCount() == objAX.GetCollectTimes()) {
			
			var objValue = document.getElementsByName(objName);
			//objValue[0].value = fpKey + finPrintval;	
			objValue[0].value = finPrintval;			
			//changeBtnStatus("scanBtn", false); 			
			//…其它提交前想要做的事			
			//form.submit(); 
			submitfpForm();
		}	 
   	}
}

function changeBtnStatus(btnName, status){

	var	btns = document.getElementsByName(btnName);
	if (btns != null && btns.length > 0) {
		for (var i=0; i<btns.length; i++) {
			if (!isFireFox()) {
				btns[i].disabled = status;
			}			
		}
	}
}

function isFireFox(){
	if (navigator.userAgent.toLowerCase().indexOf("firefox") != -1){
		return true;
	} else {
		return false;
	}
}

function getRandomKey(){
	var key = Math.random()*10000 + "1860";
	return key.substring(0,4);
}