//berote this js file , we need to print the array_cont.js.
var selectedArray=Array();
var CounterCheck=Array();
selCount=0;
function swapSublist(frm,sel,sublistname,parentArray,childArray,childArrayNames)
{
	sublist=frm.elements[sublistname];
	//sublist.options.length=0;// added by seeroo(riyas)to clear the sublist when the items are there in the sublist while uploading to edit any record and also we select any new item from parent list
	var found=false;
	//var count=0;
	if(typeof(sel.options)=="undefined"){
	 //alert('undefined');
	 return;
	}
	for (var j=0; j < sel.options.length;j++ )
	{
		found=false;	
		if(sel.options[j].selected==true)
		{
			//we have got a selected item from the list
			for(var k=0;k < selectedArray.length;k++)
			{
				// chek our select array
				if(sel.options[j].value==selectedArray[k])
				{
					//found 
					found=true;
					break;
				}
			}
			
			if(!found)
			{
				// a new item has been clicked
				//add corresponding items to the sub list
				//alert("yy:Not found in selected array.Add  as new");
				//var childArray=eval("arrLocation"+sel.options[j].value+"Ids");
				//var childArrayNames=eval("arrLocation"+sel.options[j].value+"Names");
				tcount=sublist.length;// by riyas
				for(var l=0;l<parentArray.length;l++)
				{
					//alert("yy:adding to option " +childArrayNames[l]);
					if(parentArray[l]==sel.options[j].value)
					{
						sublist.options[sublist.options.length] = new Option(childArrayNames[l],childArray[l]);
						CounterCheck[tcount]=childArray[l];//by riyas
						tcount++;//by riyas
					}
				}
				//alert("adding to selected array"+sel.options[j].value);
				selectedArray[selectedArray.length]=sel.options[j].value;
			}
		}
		else
		{
			found=false;
			//alert("xx:Checking  Non Selected" +sel.options[j].value );
			//alert(selectedArray.length);
			for(var k=0;k < selectedArray.length;k++)
			{
				
				//alert("xx:In non select k loop.comparing  with selected array " + sel.options[j].value + ":" + selectedArray[k]);
				if(sel.options[j].value==selectedArray[k])
				{
					//alert("Found in selected array.We should remove");
					//selectedArray.length=0;
					found=true;
					break;
				}	

			}
			if(found)
			{
				var removeIndexes=Array();
				var removeIndexCount=0;
				///alert(sublist.options.length);
				for(var l=0;l<sublist.options.length;l++)
				{
					///alert(sublist.options[l].text);
					//alert("xx:In removal.Iterating thorugh sublist loop");
					//var childArray=eval("arrLocation"+sel.options[j].value+"Ids");
					for(var m=0;m < childArray.length; m++)
					{
						//alert("In removal m loop.Comparing sublist option  with array" + sublist.options[l].value + ":" +childArray[m]);
						//alert(childArray[m] +":"+sublist.options[l].value + ":"+parentArray[m]  + ":" + sel.options[j].value)
						if(childArray[m]==sublist.options[l].value && parentArray[m] == sel.options[j].value)
						{
							//alert(sublist.options[l].text);
							//alert("xx: removing " +sublist.options[l].value);
							//sublist.options.remove(l);
							//removeFlag=true;
							//removeIndex=l;
							//sublist.options.remove(l);
							removeIndexes[removeIndexCount]=sublist.options[l].value;
							removeIndexCount++;
						}
					}
				}

				for(var l=0;l<sublist.options.length;l++)
				{
					for(var n=0; n < removeIndexes.length; n ++)
					{
						if(sublist.options[l].value==removeIndexes[n])
						{
							//alert("com remover"+sublist.options[l].value+"    "+removeIndexes[n]);
							//alert("removing" + sublist.options[l].text);
							//sublist.options.remove(l);//this was the line earlier, then we get error in firefox
							//alert("removing "+removeIndexes[n]);
							//this loop by riyas
							for(var q=0; q < CounterCheck.length; q ++)
							{
								if(removeIndexes[n]==CounterCheck[q])
								{
									//alert("pre length"+CounterCheck.length);
									CounterCheck.splice(q,1);
									//alert("pro length"+CounterCheck.length);
								}
								//alert(CounterCheck[q]);
							}
							//ends loop here - by riyas
							//alert(removeIndexes[n]);
							sublist.remove(l);//this is the new line to avoide error in firefox also(seeroo)
						}
					
					}
				}
				//alert("xx:spicing"+selectedArray[k]);
				selectedArray.splice(k,1);
			}
		}
		
	}
	//alert("sublist length  "+sublist.options.length);
//	alert("counter check   "+CounterCheck.length);
//this loop by riyas
	for(var outer=0; outer < sublist.options.length; outer++ )
	{
		
		found=false;
	//alert(CounterCheck.length);
		for(var q=0; q < CounterCheck.length; q ++)
		{
			var count=0;
			if(sublist.options[outer].value == CounterCheck[q])
			{
				found=true;
				count++;
				break;
				//alert(CounterCheck[q]);
			}
		}
		if(!found || count > 1)
		{
			//alert("element to remove       "+sublist.options[outer].text);
			sublist.remove(outer);
		}
	}
	//ends loop here - by riyas
}
