var initial="";
var array_EquipInit = new Array();
var array_EquipNumb = new Array();
var arrayIndex=0;


/**
 * This function validates the Equipment Ids
 */
function validateEntry(resultanatStr)
{

	if(resultanatStr.length<3 || resultanatStr.length==0 || resultanatStr=="")
	{
		alert("Please provide one or more valid Equipment ID's ");
		document.forms[0].txtMultiEquipmentId.focus();
		return false;
	}
	else if(!checkHyphen(resultanatStr))
	{
		alert("Please do not give spaces before/after Hyphen");
		document.forms[0].txtMultiEquipmentId.focus();
		return false;
	}
	else if(!checkForChar(resultanatStr))
	{
		alert("[] is not a valid initial (item"+(itemCount+1)+" in your list)\nMust be 2,3 or 4 bytes of Alpha Characters");
		document.forms[0].txtMultiEquipmentId.focus();
		return false;
	}
	else
	{

		return true;
	}
}

/**
 * This function validates the spaces while entering the hyphen
 */
function checkHyphen(strTest)
{
	var bool=true;
	idx=strTest.indexOf('-');
	if(strTest.charAt(idx+1)==' ' || strTest.charAt(idx-1)==' ')
		bool=false;
	return bool;
}

/**
 * This function checks the starting character for the Valid Initial
 */
function checkForChar(strTest)
{
	var bool = false;
    	var ecChars = '~^!@#$%*&ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';

	testChar=strTest.charAt(0);
	 for (var j=0; j<ecChars.length;j++)
	 {
		if (testChar == ecChars.charAt(j))
		{
			bool = true;
		}
	 }
	 return bool;
}

/**
 * This function replaces the delimiters into spaces
 */
function trimDelimiters(strTest)
{

	while(strTest.indexOf(',') != -1) strTest=strTest.replace(',',' ');
	while(strTest.indexOf(';') != -1) strTest=strTest.replace(';',' ');
	while(strTest.indexOf('\t') != -1) strTest=strTest.replace('\t',' ');
	while(strTest.indexOf('\n') != -1) strTest=strTest.replace('\n',' ');
	while(strTest.indexOf('\r') != -1) strTest=strTest.replace('\r',' ');

	return trim(strTest);
}


/**
 * This function trims the leading and trailing blank spaces
 */
function trim(strTest)
{
    // This will get rid of leading spaces
    while (strTest.substring(0,1) == ' ')
       strTest = strTest.substring(1, strTest.length);

    // This will get rid of trailing spaces
    while (strTest.substring(strTest.length-1,strTest.length) == ' ')
       strTest = strTest.substring(0, strTest.length-1);
     return strTest;
}

/**
 * This function parses the Equipment Ids and validates the Ids
 */
function checkValidId(resultantStr)
{
	while(resultantStr!="" || resultantStr.length!=0)
	{
		resultantStr=parseId(resultantStr);
	}
	if(endFlag)
		return true;
	if(errorFlag)
		return false;
}

/**
 * This function parses the Equipment Ids
 */
function parseId(resultantStr)
{
	if(checkForChar(resultantStr))
	{
		initial=getInitial(resultantStr);
		if(initial=="")
		{
			errorFlag=true;
			resultantStr="";
			return resultantStr;
		}
		else
		{
			resultantStr=trim(resultantStr.substring(initial.length,resultantStr.length));
			if(resultantStr!="" && checkForNumb(resultantStr))
			{
				numb=parseNumbers(resultantStr);
				if(numb!="" || endFlag)
				{
					resultantStr=trim(resultantStr.substring(numb.length,resultantStr.length));
					itemCount++;
					if(resultantStr=="")
						endFlag=true;

				}
				else
				{
					errorFlag=true;
					resultantStr="";
				}

			}
			else
			{
				alert("["+resultantStr+"] is not a valid Equipment Number Entry. (item"+(itemCount+1)+" in your list)\nMust be 1 to 10 bytes of numeric characters with an optional hyphenated range.");
				document.forms[0].txtMultiEquipmentId.focus();
				errorFlag=true;
				resultantStr="";

			}
			return resultantStr;
		}
	}
	else if(checkForNumb(resultantStr))
	{
		numb=parseNumbers(resultantStr);
		if(numb!="" || endFlag)
		{
			resultantStr=trim(resultantStr.substring(numb.length,resultantStr.length));
			itemCount++;
			if(resultantStr=="")
				endFlag=true;
		}
		else
		{
			errorFlag=true;
			resultantStr="";
		}
		return resultantStr;
	}
	else
	{
		alert("["+resultantStr +"] is not a valid initial (item"+(itemCount+1)+")\nMust be 2,3 or 4 bytes of alpha characters");
		document.forms[0].txtMultiEquipmentId.focus();
		resultantStr="";
		errorFlag=true;
		return resultantStr;
	}

}

/**
 * This function fetches the initial part from the input string
 */
function getInitial(strTest)
{
	var initial="";
	idx = strTest.indexOf(' ');
	if(idx!=-1)
		tempStr=strTest.substring(0,idx);
	else
		tempStr=strTest;


	 for(i=0;i<=tempStr.length;i++)
	 {
	 	tempChar = tempStr.charAt(i);
	 	if(checkForChar(tempChar))
	 	{
	 		initial = initial + tempChar;
	 	}
	 	else
	 	{
	 		break;
	 	}
	 }

	if(initial.length<2 || initial.length>4)
	{
		alert("["+initial +"] is not a valid initial (item"+(itemCount+1)+" in your list)\nMust be 2,3 or 4 bytes of Alpha Characters");
		document.forms[0].txtMultiEquipmentId.focus();
		initial="";
		return initial;
	}
	else
	{
		return initial;
	}
}

/**
 * This function checks the starting character for the Valid Number
 */
function checkForNumb(strTest)
{
	var bool =false;
	if(strTest.charAt(0)>='0' && strTest.charAt(0)<='9')
	{
		bool=true;
		return bool;
	}
	else
	{
		bool=false;
		return bool;
	}
}

/**
 * This function parses the Equipment Id numbers and validates the numbers
 */
function parseNumbers(strTest)
{
	if(strTest.indexOf(' ')>0)
	{
		substr=strTest.substring(0,strTest.indexOf(' '));
	}
	else{
		substr=strTest;
	}

	if(strTest.indexOf('-')>0 && strTest.indexOf('-')<substr.length)
	{
		idx=strTest.indexOf('-');

		if(idx!=substr.length-1)
		{
			start=substr.substring(0,idx);
			end=substr.substring((start.length+1),substr.length);
			if(checkForValidNumb(start) && checkForValidNumb(end))
			{
				start=getNumber(start);
				end=getNumber(end);
			}
			else
			{
				alert("["+substr +"] is not a valid Range (item"+(itemCount+1)+" in your list)\nMust be 1 to 10 bytes of numbers ");
				document.forms[0].txtMultiEquipmentId.focus();
				substr="";
				return substr;
			}


			range=parseInt(end)-parseInt(start) + 1;
			var range_status=new Boolean();
			range_status=true;
			range_status=getRangeNumbers(start,end);
			
			if(range_status && range<=300 && range>=0)
			{
				for(var i=start; i<=end; i++)
				{
					if(!checkDuplicate(initial,i))
					{
						fill_Array(initial,i);
					}
				}
				strTest=trim(strTest.substring(substr.length,strTest.length));
				if(strTest=="")
				{
					endFlag=true;
					strTest=substr;
				}

			}
			else
			{
				errorFlag=true;
				substr="";
			}
			return substr;
		}
		else
		{
			strTest="";
			alert("["+substr+"] is not a valid equipment number entry (item "+(itemCount+1)+" in your list)\nMust be 1 to 10 bytes of numeric characters with an optional hyphenated range.");
			document.forms[0].txtMultiEquipmentId.focus();
		}
		return strTest;
	}
	else
	{
		numb=getNumber(substr);
		if(numb!="")
		{
			if(!checkDuplicate(initial,numb))
			{
				fill_Array(initial,numb);
			}
			strTest=trim(strTest.substring(numb.length,strTest.length));
		}
		else
		{
			numb="";

		}
		return numb;
	}
}

/**
 * This function checks for valid number in Equipment Id
 */
function checkForValidNumb(strTest)
{
	var bool =false;
	var count=0;
	for(i =0;i<strTest.length;i++)
	{
		if(strTest.charAt(i)>='0' && strTest.charAt(i)<='9')
			count++;
	}
	if(count==strTest.length && strTest.length<=10 && strTest.length>0 && strTest!=0)
		bool=true;

	return bool;

}

/**
 * This function fetches the Number part from the input string
 */
function getNumber(strTest)
{

	if(!checkForValidNumb(strTest) || strTest.length>10 || strTest.length==0)
	{
		alert("["+strTest+"] is not a valid equipment number entry(item "+(itemCount+1)+" in your list)\nMust be 1 to 10 bytes of numeric characters with an optional hyphenated range.");
		document.forms[0].txtMultiEquipmentId.focus();
		strTest="";
	}
	return strTest;
}

/**
 * This function validates the numbers specified in the range
 */
function getRangeNumbers(start, end)
{
	if(parseInt(start)>parseInt(end))
	{
		alert("Second number in a range must be larger than first number (item "+(itemCount+1)+" in your list)");
		document.forms[0].txtMultiEquipmentId.focus();
		return false;
	}
	else
	{
		range = parseInt(end) - parseInt(start) + 1;
		if(range>300)
		{
			alert("ID Limit Exceeded...Please limit input to 300 ID's ");
			document.forms[0].txtMultiEquipmentId.focus();
			return false;
		}
		else if(end.length>10 || start.length>10)
		{
			alert("ID should only contain 1 to 10 bytes as Equipment Number");
			document.forms[0].txtMultiEquipmentId.focus();
			return false;
		}
		else
		{

			return true;
		}
	}
}

/**
 * This function fills the array with Initials and Number part
 */
function fill_Array(initial, numb)
{
	array_EquipInit[arrayIndex] = initial.toUpperCase();
	array_EquipNumb[arrayIndex] = parseInt(numb);
	arrayIndex++;
}

/**
 * This function checks for duplicate Equipment Ids
 */
function checkDuplicate(initial,numb)
{
	var bool = false;
	if(arrayIndex!=0)
	{
		for(i=0;i<arrayIndex;i++)
		{
			if(array_EquipInit[i]==(initial.toUpperCase()) && array_EquipNumb[i]==parseInt(numb))
			{
				bool = true;
				break;
			}
		}
	}
	return bool;

}
