var xmlHttp
function GetXmlHttpObject()
{ 
var objXMLHttp=null
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
}
function InArray(arr, key)
{
	for (var i=0; i<arr.length; i++)
	{
		if (arr[i] == key)	return i;
	}

	return -1;

}//EOF InArray(arr, key)
function copytolist2(obj_from, obj_to)

{

	var flag		= false; // To check whether value is selected..

	var remove_flag = false; // To check whether value is selected..

	s=0;

	for (i=0;i<obj_from.options.length;i++)

	{

	var curr_option = obj_from.options[i];

		if(curr_option.selected )

		{

			if(obj_from == document.frm_main.elements['castearray[]'])

			{

				remove_flag = true;				

			}

			if(remove_flag == false)

			{

				//Here we are stroing the values which are present in LHS box and RHS box in one array, while populating RHS box we are not populating the values which are already in RHS box and user is again selecting the same value along with new value.

				to_array = new Array(); 

				for(j=0;j<obj_to.options.length;j++)

				{			

				if((curr_option.text) == (obj_to.options[j].text))

					{

						to_array[j] = curr_option.text;

						s=1;

					}

				}

			}

			if(remove_flag == true)

				{



						var optlen=obj_from.options.length;



						for(var j=0; j<optlen;j++)

						{

								to_value = document.frm_main.elements['castearray[]'].options[j].value;		

								s=2;								

							}

						

	if (curr_option.value!="")	
     {
		  var tstw=0;
	var cstghj=document.getElementById('castearray[]').length;

		 if(parseInt(cstghj)==1)
			{
			var tstw=55;
			}	
			xmlHttp=GetXmlHttpObject()

			if (xmlHttp==null)

			{

			alert ("Browser does not support HTTP Request")

			return

			} 

			var url="caaa_s.php?cast="+curr_option.value;

		//window.location=url;

			xmlHttp.onreadystatechange=function()

			{	
			if (tstw!=55)
				{
				document.getElementById('castearray[]').length=0;
				}
				document.getElementById('subcast[]').length=0;
				document.getElementById('subcast_t[]').length=0;
				if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
				{ 
					mearray=xmlHttp.responseText.split("[]");
					//alert(mearray);
					for (i=0;i<mearray.length-1;i++)
					{
					if(mearray[i]!="")
					{
							valcat=mearray[i].split("{}");

							oOption = document.createElement("Option");
							opOption = document.createElement("Option");
							oppOption = document.createElement("Option");
							oOption.text =valcat[3];
							oOption.value = valcat[0];
							
							opOption.text = valcat[2];
							opOption.value = valcat[4];
							stat = valcat[5];
							oppOption.text = valcat[1];
							oppOption.value = valcat[0];
							if(oOption.text!="")
							{
							document.getElementById('subcast[]').options.add(oOption);
							}
							if(stat=='Y' && opOption.text!="")
							{
							document.getElementById('subcast[]').options.add(opOption);
							document.getElementById('castearray[]').options.add(opOption);
							}
							if(oppOption.text!="")
							{
							document.getElementById('subcast_t[]').options.add(oppOption);
							}
						}						
					}
				}
			} 
			xmlHttp.open("GET",url,true)
			xmlHttp.send(null)
		}			
}



			flag = true;

			var new_option	   = curr_option.text;

			var new_option_val = curr_option.value;

			if(new_option != ""

			 && new_option != "----Select Mother Tongue----"

			 && new_option != "---------Select Caste---------"

			 && new_option_val != "")

			{

				// when user selects anything frm left when doesnt matter at right side, overwriting the doesnt matter value.







				// countryofresidence condition is added bcoz.. we can have "" value as 0th option in countryofresidencearray.. so it resets the whole list..



				if(obj_to.options[0] && (obj_to.options[0].text == "Doesn't Matter" || obj_to.options[0].text == "") && obj_from != document.frm_main.elements['caste_fromarray[]'] )
				{
				obj_to.options.length = 0;
				}
				if(new_option != "Doesn't Matter")
				{
					if(remove_flag == false)
					{
						if((new_option && InArray(to_array, new_option) == -1))
						{
						obj_to.options[obj_to.length] = new Option(new_option, new_option_val);
						}
					}
				}
				if(remove_flag){ // Uncomment if u dont want to remove the val frm left list.
				obj_from.options[i] = null;	//we can use obj_from.remove(i);
				i--; // Used for multiple selection..
				}
			}
		if(curr_option.value!="" && s==0)
		{
			xmlHttp=GetXmlHttpObject()
			if (xmlHttp==null)
			{
			alert ("Browser does not support HTTP Request")
			return
			} 
			var url="subc_s.php?subcast="+curr_option.value+"&dd=0";
			//window.location=url;
			xmlHttp.onreadystatechange=function()
			{
				document.getElementById('subcast[]').length=0;
				document.getElementById('castearray[]').length=0;
				if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
			{ 
					mearray=xmlHttp.responseText.split("[]");
					//alert(mearray);
					for (i=0;i<mearray.length-1;i++)
					{
						if(mearray[i]!="")
						{
							valcat=mearray[i].split("{}");
							oOption = document.createElement("Option");
							opOption = document.createElement("Option");
							opp = document.createElement("Option");
							oOption.text =valcat[1];
							oOption.value = valcat[0];	
							//alert(oOption.value);
							opOption.text = valcat[2];
							opOption.value = valcat[3];
							stat = valcat[4];
							
							opp.text = valcat[2];
							opp.value =0;
							
							if(stat=='Y' && opOption.text!="")
							{													
							document.getElementById('castearray[]').options.add(opOption);
							}
							//if(stat=='Y' || oOption.text !="")
								//{
								//document.getElementById('subcast[]').options.add(opp);	
							//	document.getElementById('subcast[]').options.add(oOption);
							//	}														
							if(oOption.text !="")
							{	
							document.getElementById('subcast[]').options.add(oOption);
							}								

						}
					}
				}
			} 
			xmlHttp.open("GET",url,true)
			xmlHttp.send(null)
		}
	} // EO if(curr_option.selected)
	} // EO for (i=0;i<obj_from.options.length;i++)
	if(obj_from.options.length == 0 && remove_flag)
	{	
		if(document.frm_main.elements['communityarray[]'].options.length == 0)
		{
			document.frm_main.elements['caste_fromarray[]'].options.length = 0;
		}	

		if(obj_from == document.frm_main.elements['communityarray[]']
		|| obj_from == document.frm_main.elements['castearray[]'])
		{
			var value = " ";
		}
		else
		{
			var value = "";
		}
		if(tstw==55)
		{
		obj_from.options[0] = new Option("Doesn't Matter", value);
		}		
	}
	if(obj_from.options.length > 0 && obj_from.options.length <= 2)
	{
			if(obj_from.options[0].text == "---------Select Caste---------" && obj_from.options.length == 2)
			{
				if(obj_from.options[1].text =="")
				obj_from.options[1] = null;
			}
			else if(obj_from.options.length == 1 && obj_from.options[0].text == "")
			{
				obj_from.options[0] = null;
			}
	}
	if (!flag)
	{
		alert ('Please select the Options');
		return false;
	}
	
var cp_option = obj_to.options[0];
				if(obj_to.options.length==1);	
				{	
					if(cp_option.text=="Doesn't Matter")
					{
    				obj_to.remove(0);
					}
				}	
	return true;

} // EO function copytolist1(obj_from,obj_to)


