﻿// JScript File
function GetToken(Buffer,Index ,Delimiter ) {
    var ii, intLen 
    var intOffSet1, intOffSet2, intCount 
    var strNewBuffer
    
    intOffSet1 = 0
    intOffSet2 = 0
    intCount = 0

    if (Index <= 0) {
        return "";
    }
    strNewBuffer = Delimiter + Buffer + Delimiter;
    intLen = strNewBuffer.length;

	ii=0;
	for (ii=0; ii<intLen;ii++) {
        if (strNewBuffer.charAt(ii) == Delimiter) {
            intCount += 1
            if (intCount == Index) {
                intOffSet1 = ii;
            }
            if (intCount == (Index + 1)) {
                intOffSet2 = ii;
                break;
            }
        }
    }
    if (intOffSet2 == 0) {
        return "";
    }
    return strNewBuffer.substr(intOffSet1 + 1, intOffSet2 - (intOffSet1 + 1))
}


function RemoveAll(combobox)
{	
	while (combobox.length!=0)
	{
		combobox.options[0] = null;
		combobox.value[0] = null;
	}
}

function RemoveAllText(TextBox1,TextBox2,TextBox3)
{	
	TextBox1.value = "";
	TextBox2.value = "";
	TextBox3.value = "";
}

function FilterText(TextBox1,TextBox2,TextBox3,combobox1_value,cbSetting) {
	var ii

	var sOneToMany 
	var sValue1
	var sRecordSet2

	var sValueDesc2
	var sValue2;
	var sDesc2;
	var sDesc3;
	
	var sDlmtr1
	var sDlmtr2
	var sDlmtr3
	var sDlmtr4
	var sDlmtr5
	
	sDlmtr1 = "`";
	sDlmtr2 = "*";
	sDlmtr3 = "~";
	sDlmtr4 = "^";
	sDlmtr5 = "|";

	ii=0;
	while (1) {
		sOneToMany = GetToken(cbSetting,ii+1,sDlmtr1);
		if (sOneToMany == "")
			break;
		sValue1 = GetToken(sOneToMany,1,sDlmtr2);
		if (sValue1 == combobox1_value) {
			sRecordSet2 += GetToken(sOneToMany,2,sDlmtr2);
		}
		ii++;
	}

	RemoveAllText(TextBox1,TextBox2,TextBox3);

	if (sRecordSet2 == "") {
		return;
	}
	
	sRecordSet2 = sRecordSet2.replace("undefined","");
	
	sValueDesc2 = GetToken(sRecordSet2,1,sDlmtr5);

	sValue2 = GetToken(sValueDesc2,1,sDlmtr4);
	sDesc2 = GetToken(GetToken(sValueDesc2,1,sDlmtr3),2,sDlmtr4);						
	sDesc3 = GetToken(sValueDesc2,2,sDlmtr3);						
	TextBox1.value = sValue2;
	TextBox2.value = sDesc2;
	TextBox3.value = sDesc3;
}

function FilterComboBox(combobox1,combobox2,combobox1_value,cbSetting) {
	var ii

	var sOneToMany 
	var sValue1
	var sRecordSet2

	var sValueDesc2
	var sValue2;
	var sDesc2;
	
	var sDlmtr1
	var sDlmtr2
	var sDlmtr3
	
	sDlmtr1 = "`";
	sDlmtr2 = "*";
	sDlmtr3 = "~";
	sDlmtr4 = "^";

	ii=0;
	while (1) {
		sOneToMany = GetToken(cbSetting,ii+1,sDlmtr1);
		if (sOneToMany == "")
			break;
		sValue1 = GetToken(sOneToMany,1,sDlmtr2);
		if (sValue1 == combobox1_value) {
			sRecordSet2 += GetToken(sOneToMany,2,sDlmtr2);
			//break;
		}
		ii++;
	}

	RemoveAll(combobox2);

	if (sRecordSet2 == "") {
		return;
	}
	
	sRecordSet2 = sRecordSet2.replace("undefined","");
	
	ii=0;
	while(1) {
			if(ii==0){
				combobox2.options[ii] = new Option("", "");
				ii++;
				continue;
			}
			sValueDesc2 = GetToken(sRecordSet2,ii,sDlmtr3);

			if (sValueDesc2 == "")
				break;
			sValue2 = GetToken(sValueDesc2,1,sDlmtr4);
//			sDesc2 = sValue2 + " - " + GetToken(sValueDesc2,2,sDlmtr4);	
			sDesc2 = GetToken(sValueDesc2,2,sDlmtr4);						
			combobox2.options[ii] = new Option(sDesc2, sValue2);
			ii++;
	}
}

function LoadFirstCombo(combobox1,cbSetting) {
//	alert("FilterComboBox1");
	var ii

	var sOneToMany 
	var sValue1
	var sRecordSet2

	var sValueDesc2
		
	var sDlmtr1
	var sDlmtr2
	var sDlmtr3
	
	sDlmtr1 = "`";
	sDlmtr2 = "*";
	sDlmtr3 = "~";
	sDlmtr4 = "^";

	ii=0;
	while (1) {
		sOneToMany = GetToken(cbSetting,ii+1,sDlmtr1);
		if (sOneToMany == "")
			break;
		sValue1 = GetToken(sOneToMany,1,sDlmtr2);
			sRecordSet2 = GetToken(sOneToMany,2,sDlmtr4);
			sValueDesc2 = GetToken(sOneToMany,1,sDlmtr2);
//			alert(GetToken(sRecordSet2,1,sDlmtr3));
			if (sValueDesc2 == "")
			    break;

			addToList(combobox1,GetToken(sRecordSet2,1,sDlmtr3),sValueDesc2);
		ii++;
	}
	
}	

function FilterUsrCtrComboBox(combobox1,combobox2,combobox1_value,cbSetting) {
	alert(cbSetting);
	var ii
	var sOneToMany	 
	var sValue1
	var sRecordSet2

	var sValueDesc2
	var sValue2;
	var sDesc2;
	
	var sDlmtr1
	var sDlmtr2
	var sDlmtr3
	
	sDlmtr1 = "`";
	sDlmtr2 = "*";
	sDlmtr3 = "~";
	sDlmtr4 = "^";

	ii=0;
			
	while (1) {
		sOneToMany = GetToken(cbSetting,ii+1,sDlmtr1);
		if (sOneToMany == "")
			break;
		sValue1 = GetToken(sOneToMany,1,sDlmtr2);	
		if (sValue1 == combobox1_value) {			
			sRecordSet2 = GetToken(sOneToMany,2,sDlmtr2);
			break;
		}
		ii++;
	}
	
	removeFromList(combobox2);
  
	if (sRecordSet2 == "") {	   
		return;
	}
	
	ii=0;
	while(1) {
			sValueDesc2 = GetToken(sRecordSet2,ii+1,sDlmtr3);				
			if (sValueDesc2 == "")
				break;
			sValue2 = GetToken(sValueDesc2,1,sDlmtr4);
			sDesc2 = GetToken(sValueDesc2,2,sDlmtr4);	
			if (sValue2 != "" && sDesc2 != "" ){
//				sDesc2 = sValue2 + "  -  " + sDesc2;
				sDesc2 = sDesc2;
				addToList(combobox2,sDesc2,sValue2);
			}
			ii++;
	}
}

function addToList(listField, newText, newValue) {	
   if ( ( newValue == "" ) || ( newText == "" ) ) {
//      alert("You cannot add blank values!");
   } else {
      var len = listField.length++; // Increase the size of list and return the size
      listField.options[len].value = newValue;
      listField.options[len].text = newText;
      listField.selectedIndex = -1; // Highlight the one just entered (shows the user that it was entered)
   }
}

function removeFromList(listField)
{
   if ( listField.length == -1) // If the list is empty
   {  		
      //alert("There are no values which can be removed!");
   }
   else 
   {      
	listField.length = 0  
   }
}

