// ************************************************************************************************
// ************************************* Validation ***********************************************
// ************************************************************************************************

function Check_offshore(txt1,txt2,iMaxLength,event,any)
{
	
	var txtLength  = "";
	var txt2Length = "";
	var nextField ="";

	if((txt1!=null)&&(txt2!=null))
	{
		if((txt1.type == "text")&&(txt2.type == "text"))
		{
			txtLength = txt1.value.length;
			txt2Length= txt2.value.length;
            
			alert("txt1.form.length::"+txt1.form.length);
			nextField = txt1.form[(getIndex(txt1)+1) % txt1.form.length];
			alert("nextField::"+nextField);
			if(any==null || any==''){
				alert("nextField.type:"+nextField.type);
				if(nextField.type == "select-one")		// if next field is a dropdown list.
						nextField.focus();
				else if(nextField.type == "text")		// if next field is a textbox
						nextField.select();
			}
		
			if((txtLength >= iMaxLength)&&(txt2!=null))
			{
				
				if(txt2Length>0){
				txt2.select();
				}else{
				txt2.focus();
				}
				
			}
		}else if((txt1.type == "text")&&!(txt2.type == "text"))
		{
			txtLength = txt1.value.length;
			if((txtLength >= iMaxLength)&&(txt2!=null))
			{
				txt2.focus();
				
				
			}

		}

	}
}



function checkTab(evt,txt1,txt2,iMaxLength)
{
	var eventCode = evt.keyCode;
	if(txt2!=null && txt2.disabled==true)
	{
	return false;
	}
	if((txt1!=null)&&(txt2!=null))
	{
	
		if(eventCode!=9)
		{
			
			Check(txt1,txt2,iMaxLength,evt);
			
		}
		else{
				txt1.select();
		}
	}
}


//**********************************************************************************************************
//	This function enables the autotab feature of the forms. When called by the onkeyup event handler for a
//	form control, this function transfers the control to the next control, similar to pressing TAB key.
//	e.g. onkeyup="autoTab(this,9,event,null)"
//	input 	- 	form control object (pass as: this)
//	length	-	length at which autotab will occur
//	e		-	event object (pass as: event)
//	any		-	any other object that you want to pass control to, instead of next. Otherwise pass as: null
//**********************************************************************************************************

	function Check(input,txt2,length,evt) {
		
	  var eventCode = evt.keyCode;

	  if(eventCode!=9)
	  {

		var isNN = (navigator.appName.indexOf("Netscape")!=-1);
		var keyCode = (isNN) ? evt.which : evt.keyCode;  // get the key codes for Netscape and other browsers.
		
		
		var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,35,36,37,38,39,40,46]; // avoid processing for keys with these ASCII codes
		var nextField;
		var txtLength  = "";
	    var txt2Length = "";

		
		if( length == 9 && input.value.indexOf("-") > - 1)
		{
		   length = 11;
		}

		if(input.value.length >= length && !containsElement(filter,keyCode)) {
		//if(input.value.length >= length ) {


			input.value = input.value.slice(0, length);
			nextField = input.form[(getIndex(input)+1) % input.form.length];
            
			
			if(txt2==null || txt2=='' || txt2.disabled==true){
				
                 var i = findElementIndex(input);
				                 
				if( nextField.type != "select-one" || nextField.type != "text")
                {
					
					var found = false;
					while(!found)
                    {	
						
				      nextField = input.form[(i+1) % input.form.length];
					  if( i>input.form.length)
						  return true;
					  if( nextField.type == "select-one" || nextField.type == "text")
					  {
					  	if(isNN || nextField.disabled==false)
					  	{
						  found = true;
						 }
						 else
						 {
						  	i++;
						 }
					  }
					  else
					  {
						  i++;
					  }
                    } 
				}
                
				if(nextField.type == "select-one")		// if next field is a dropdown list.
					nextField.focus();
				else if(nextField.type == "text")		// if next field is a textbox
					nextField.select();
               
			}// if txt2 null
			else{

				/**if(input.form[getIndex(any)].type == "select-one")		// if next field is a dropdown list.
					input.form[getIndex(any)].focus();
				else if(input.form[getIndex(any)].type == "text")		// if next field is a textbox
					input.form[getIndex(any)].select(); **/
            

				if((input.type == "text")&&(txt2.type == "text"))
				{

					txtLength = input.value.length;
					txt2Length= txt2.value.length;

					if((txtLength >= length)&&(txt2!=null))
					{
						if(txt2Length>0){
						txt2.select();
						}else{
						txt2.focus();
						}
					}
				}
				else if((input.type == "text")&&!(txt2.type == "text"))
				{
					txtLength = input.value.length;
					if((txtLength >= length)&&(txt2!=null))
						txt2.focus();
					
				}
				
			}
		}

      }// for eventCode
		return true;
	}


	function containsElement(arr, ele) {
			var found = false, index = 0;
			while(!found && index < arr.length)
		    { 
			   if(arr[index] == ele)
			      found = true;
			   else
			     index++;
			}
		return found;
	}

	function getIndex(input) {
			var index = -1, i = 0, found = false;
			while (i < input.form.length && index == -1)
		    {
			  if (input.form[i] == input)index = i;
			  else i++;
			  return index;
			}
		return true;
	}

	


	function findElementIndex(input) {
		
	   for (i = 0; i < input.form.elements.length; i++) {
		   if (input.form.elements[i] == input) {
			   return i;
		   break;
				 }
			  }
		 }



function checkRaceCode(input,txt2,length,event,txt)
{	
	if(!txt2.disabled)
	 Check(input,txt2,length,event);
	else
	{
		Check(input,txt,length,event);
	}
		
}




