function stricmp (str1, str2)
{
	if (str1.toLowerCase () == str2.toLowerCase ())
		return (0);
	else if (str1.toLowerCase () > str2.toLowerCase ())
		return (1);
	else
		return (-1);
}

function numcmp (str1, str2)
{
	var num1 = parseInt (str1);
	var num2 = parseInt (str2);
	if (num1 == num2)
		return (0);
	else if (num1 > num2)
		return (1);
	else
		return (-1);
}

function GetOrderIndex (theArray, theAttribute, theValue, bNum)
{	
	var min = 0;
	var max = theArray.length;
	var mid = Math.floor ((max + min) / 2);
	var bExist = false;

	while (max > min) {
		var n = 0;
		if (!bNum)
			n = stricmp (eval ("theArray[mid]." + theAttribute), theValue);
		else
			n = numcmp (eval ("theArray[mid]." + theAttribute), theValue);
		if (n > 0)
			max = mid;
		if (n == 0) {
			bExist = true;
			break;
		}
		else if (n < 0)
			min = mid + 1;
		mid = Math.floor ((max + min) / 2);
	}
	return (new IndexObj (mid, bExist));
}

function GetOrderIndexOfGroup (theArray, theAttribute, theValue)
{	
	var min = 0;
	var max = theArray.length;
	var mid = Math.floor ((max + min) / 2);
	var bExist = false;

	while (max > min) {
		var n = 0;
		var szItem = eval ("theArray[mid]." + theAttribute);
		n = stricmp (szItem.substring(1, szItem.length-1), theValue.substring (1, theValue.length -1));
		if (n > 0)
			max = mid;
		if (n == 0) {
			bExist = true;
			break;
		}
		else if (n < 0)
			min = mid + 1;
		mid = Math.floor ((max + min) / 2);
	}
	return (new IndexObj (mid, bExist));
}

function IndexObj (nIndex, bExist)
{
	this.i = nIndex;
	this.bExist = bExist;
	return (this);
}

function InserIntoArray (theArray, nIndex, theValue)
{
	for (var j = theArray.length; j > nIndex; j--)
		theArray[j] = theArray[j - 1];
	theArray[nIndex] = theValue;
}

function InserIntoOptions (theOptions, nIndex, theValue)
{
	nLength = theOptions.length ;
	theOptions[nLength] = new Option ("", "") ;
	for (var j = (theOptions.length-1); j > nIndex; j--) {
		theOptions[j].text = theOptions[j - 1].text ;
		theOptions[j].value = theOptions[j - 1].value ;
	}
	theOptions[nIndex] = theValue;
}

function GetOrderIndex2Value (theArray, theFirstAttribute, theSecondAttribute, theFirstValue, theSecondValue, bNum)
{	
	var max = theArray.length;
	var mid = 0;
	var bExist = false;

	while (max > mid) {
		var n = 0;
		if (!bNum)
			n = stricmp (eval ("theArray[mid]." + theFirstAttribute), theFirstValue);
		else
			n = numcmp (eval ("theArray[mid]." + theFirstAttribute), theFirstValue);
		if (n > 0)
			break;
		if (n == 0) {
			var n = 0;
			if (!bNum)
				n = stricmp (eval ("theArray[mid]." + theSecondAttribute), theSecondValue);
			else
				n = numcmp (eval ("theArray[mid]." + theSecondAttribute), theSecondValue);
			if (n > 0)
				break;
			if (n == 0) {
				bExist = true;
				break;
			}
			else if (n < 0)
				mid++;
		}
		else if (n < 0)
			mid++;
	}
	return (new IndexObj (mid, bExist));
}
