	dwr.engine._defaultPath = 'https://www.blackberry.com/express/dwr';   
	expressHandler._path = 'https://www.blackberry.com/express/dwr';     
	DWREngine.setRpcType(DWREngine.ScriptTag);


	var valid = false;
	var allValid = false;
	var multiPin = true;
	//var pinNumber = 1;
	var currentPin;
	var pins = new Array();
	
	RN=Math.floor(Math.random()*9999999999);
	while (String(RN).length < 10) {
 		RN='0'+RN;
	}	
	
  	function pinValidation(val,pinNum) {
			submitbtn.disabled = true;
			submitbtn.value = "Validating..."
			
			currentPin = pinNum;
			
			expressHandler.getPINUsed(val, {
				callback:function(dataFromServer) { 
					usedCallback(dataFromServer, val);
				}
			});	
			
			
		}
  	
  	function usedCallback(data, val) {
  		if (data == true) // PIN has been used
  		{	
			showError('PIN has already been used!',currentPin); 
			submitbtn.disabled = false;
			submitbtn.value = "Submit";
			valid=false;
			pins[currentPin] = 0;
		}
  		else {
  			expressHandler.checkPin(val, SAPCallback);
  		}
  	}
  	
  	function SAPCallback(data) {
  		//alert(currentPin);
		
		var currentPinNumber = document.getElementById('pin'+currentPin).value;
		var errorArea = document.getElementById('pin'+currentPin+'error');
		var nextPin = currentPin+1;
		var nextPinObj = document.getElementById('pin'+nextPin);
		
		submitbtn.disabled = false;
		submitbtn.value = "Submit"

		if (data) // everything is okay
  		{ 

			showError('&nbsp;',currentPin); 
				
			
			if(!pinUsed(currentPin,currentPinNumber)){ // Check if pin was used
				pins[currentPin] = currentPinNumber;
				
			/*	if(nextPinObj != null){
					nextPinObj.focus();
				}*/
				
				valid = true;
				
				expressHandler.storePinInfo(RN,currentPinNumber,'bps5');
				showMsg('Valid',currentPin); 
				
				
				if(allAreValid()){ // Check if all pins are valid
					allValid = true;
				}
				
				//pinNumber++;
			}
					
		}
  		else				// not okay 		
  		{ 
		  showError('Invalid Pin',currentPin); 
		  valid = false;
		  pins[currentPin] = 0;
		}
  	}
	
	/* Show specified error */
	function showError(errorString,pinNum){
		var errorArea = document.getElementById('pin'+pinNum+'error');
		var pinField = document.getElementById('pin'+pinNum);
		
		errorArea.className = "error";
		errorArea.innerHTML = errorString;
		
		pinField.focus();
		//pinField.select();		

	}
	
	/* Show specified error */
	function showMsg(errorString,pinNum){
		var errorArea = document.getElementById('pin'+pinNum+'error');
		var pinField = document.getElementById('pin'+pinNum);

		errorArea.className = "valid";
		errorArea.innerHTML = errorString;
		
		pinField.focus();
		//pinField.select();		

	}

	function regExCheck(formElement,pattern){
		var regEx = new RegExp(pattern);
		var value = formElement.value;
		
		return value.match(regEx);
	}
	
	var lengthFlag = false;
	
	/* Main function call to start the pin validation*/
	function checkLength(text,pinNum){
		
		submitbtn = document.getElementById('submitBtn');
		
		/* Only run the validation if the pin meets the desired specs */
		if(text.length > 8){
			alert(text);
			text = text.substring(1,8);
			alert(text);
		}
		
		if(regExCheck(text,'^[0-9a-fA-F]{8}$')){			
			pinValidation(text.value,pinNum);
			//lengthFlag = true;
		}
		else{
			if(text.value.length < 8){
				
				showError('&nbsp;',pinNum);
				//lengthFlag = false;
			}
			else{				
				//alert("invalid2");
				showError('Invalid Pin',pinNum); 
			}
			
			valid = false;
		}
	}
	
	/* Check for duplication */
	function pinUsed(pinNum,pin){
		var used = false;
		for (i=1; i<=5;i++){
			if(pin == pins[i] && i!=pinNum){
				used = true;
				showError('Duplicate PIN',pinNum);
				pins[pinNum] = 0;
			}
		}
		
		return used;
	}
	
	/* Check if all pins are valid */
	function allAreValid(){
		var val = pins[1];
		var validPins = 0;
		
		/* Check each pin or until 1 is incorrect */
		for(i=1;i<=currentPin;i++){
			val = pins[i];
			if(val!==undefined){
				if(val.length == 8)
					//alert(val);
					validPins++;
				
			}
		
		}
		
		if (validPins >= 3){
			return true;
			
		}
		return false;
	}
	
	/* function to get the first valid pin to send to ESD */
	function getValidPin(){
		var foundPin = false;
		var i=1;
		var pin;
		
		var pinField = document.getElementById('pin');
		
		while (!foundPin || i>=5){
			pin = pins[i];
			if(pin!==undefined){
				if(pin.length == 8){
					//alert(pinField);
					pinField.value = pin;
					//alert(pinField.value);
					foundPin = true;
				}
			}
			
			i++;
			
		}
		
		
	}
	
	
	/* Check for form submittion to ensure all pins are valid */
	function isValid(){
		
		if(!allValid){
			alert('Please enter 5 valid PINs');
			
			/*var pinField = document.getElementById('pin'+currentPin);
			pinField.focus();
			pinField.select();*/
			
			return false;
		}
	
		getValidPin();
	
		return true;
	}
