
/**
 * This function perform the match of selected city or state with current city(as per state) and current state(as per country)
 * if match is found then it returns index else it returns -1
*/
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 display_dropdown_caste_new(from_page, selected_caste, gender)
{
	if(from_page == "smart_search")
	{
		var frm_obj				= document.frm_main;
		var community_obj		= frm_obj.elements['communityarray[]'];
		var mothertongue_obj	= frm_obj.elements['mothertonguearray[]'];
	}
	else if(from_page == "profile")
	{
		var frm_obj				= document.profile;
		var community_obj		= frm_obj.community;
		var mothertongue_obj	= frm_obj.mothertongue;
	}
	else
	{
		var frm_obj		  = document.quicksearch;
		var community_obj = frm_obj.community;
		var mothertongue_obj = frm_obj.elements['mothertonguearray[]'];


	}


	var community			= "";
	var mothertongue		= "";

	if(from_page == "smart_search")
	{

			for(var i=0; i < community_obj.options.length; i++)
			{

				var community_value = community_obj.options[i].value;

				if(!community.match(community_value) && community_obj.options[0].text != "Doesn't Matter" ) // Not to insert duplicate values..
				{
					community += new String(community_value) + "|"; // Taking only 1st element like 'Hindu'.
				}


			} // EO for(var i=0; i < community_obj.options.length; i++)





	}
	else
	{
		community = new String(community_obj.options[community_obj.selectedIndex].value);

	}


	//###### Passing mothertongue while building the caste options.. ######


	var arr_mothertongue = new Array;
	// This for loop is to pass multiple mothertongues to the ajax script..
	var j = 0;
	var bln_flag=false;
	for(var i=0; i < mothertongue_obj.options.length; i++)
	{
		var mothertongue_value = mothertongue_obj.options[i].value;

		if(from_page == "smart_search") // We are by default taking all added mothertongues
		{
			// Not to insert duplicate values..
			if(!mothertongue.match(mothertongue_value) && mothertongue_obj.options[0].text != "Doesn't Matter")
			{
				if(mothertongue_value)
				{
					arr_mothertongue[j] = mothertongue_value;
				}
			}
		}
		else // We are taking only selected mothertongues.. in other searches..
		{
			if(mothertongue_obj.options[i].selected == true)
			{
				if(mothertongue_value)
				{
					arr_mothertongue[j] = mothertongue_value;
				}
			}
		}

		j++;

	} // EO for(var i=0; i < mothertongue_obj.options.length; i++)

	if(arr_mothertongue)
	{
		mothertongue = arr_mothertongue.join("|");
	}

	if(from_page == "profile")
	{
		mothertongue = "";
	}

	if(((community && mothertongue) || (community)) && gender)
	{

		var status = AjaxRequest.get(
		{

			'url': "/ssi/ajax-com-mt-caste.php?community=" + escape(community) + "&mothertongue=" + escape(mothertongue)+ "&gender=" + escape(gender) + "&from_page=" + escape(from_page)
			,'onSuccess':function(req){

			if(from_page == "smart_search")
			{
				var caste_from_obj = frm_obj.elements['caste_fromarray[]'];
				var caste_to_obj = frm_obj.elements['castearray[]'];
				//frm_obj.action= serverpath+"/partner_search/matrimonial_search/searchresults.php";
			}
			else if(from_page == "profile")
			{
				caste_from_obj = frm_obj.caste;
			}
			else
			{
				var caste_from_obj = frm_obj.elements['castearray[]'];
			}
			//alert('respomse'+req.responseText);

			myString    = new String(req.responseText);

			if(myString != "")
			{
				splitString = myString.split("@");

				caste_from_obj.innerHTML = "";
				caste_from_obj.options.length = 0;
				//caste_from_obj[0]	= new Option("---------Select Caste---------", "");

				var m = 0;
				var k = 0;

				// create and element object for checking existing optgroup labels

				//var optGrps = frm_obj.castedp.getElementsByTagName("OPTGROUP");
				for(var i=0; i<splitString.length; i++)
				{
					myString1    = new String(splitString[i]);
					caste_txt_val = myString1.split("|");

					if((splitString[i].substr(0,1) != "|"))
					{

							// put Top caste and Remaining castes or put community heading value as blank

							arr_caste_label = Array();

							if(splitString[i].indexOf(":") > 0)
							{
								arr_caste_label = splitString[i].split(':');
								caste_display_label = arr_caste_label[0];
							}
							else
							{
								caste_display_label = splitString[i];
								if (splitString[i] == 'Spiritual - not religious')
								{
									caste_display_label = 'Spiritual';
								}

							}

							//optGroup = document.createElement('optgroup');
							//optGroup.label = caste_display_label;
							//optGroup.style.backgroundColor = '#FFFFE0';
							//caste_from_obj.appendChild(optGroup);

							/*
							caste_from_obj[m] = new Option(caste_display_label, '', false);
							caste_from_obj[m].style.backgroundColor = '#F2F2C3';
							//*/
							if (from_page=='new_home_page') {
								if (!bln_flag) {
									caste_from_obj[m] = new Option('Doesn\'t Matter', '', false);
									bln_flag=true;
								}
								else {
									caste_from_obj[m] = new Option('', '', false);
								}
							}
							else {
								caste_from_obj[m] = new Option(caste_display_label, '', false);
								caste_from_obj[m].style.backgroundColor = '#F2F2C3';
							}

							m++;

							//optGroup1 = document.createElement('optgroup');
							//optGroup1.label = '';
							//optGroup1.style.backgroundColor = '#FFFFFF';
							//caste_from_obj.appendChild(optGroup1);
							//
							//alert(optGroup);


					}

					// Splitting the db values n then checking if it matches for preselection..
					check_selected_caste_spilt_string = selected_caste.split('|');


					for(var j=1; j< caste_txt_val.length; j++)
					{
						var caste_txt  = caste_txt_val[j].trim();

						arr_caste_txt = Array();
						//Added  the below code b'coz we want to show Maya and not Hindu:Maya
						if(caste_txt.indexOf(":") > 0)
						{
							arr_caste_txt = caste_txt.split(':');
							caste_display_txt = arr_caste_txt[1];

							if(arr_caste_txt[0] == arr_caste_txt[1])
							{
								caste_txt = arr_caste_txt[0];
							}

						}
						else
						{
							caste_display_txt = caste_txt;
						}

						if(caste_display_txt && InArray(check_selected_caste_spilt_string, caste_display_txt) >= 0)
						{

							if(from_page == "smart_search")
							{
								caste_from_obj[m] = new Option(caste_display_txt, caste_txt, false);
								caste_from_obj[m].style.backgroundColor = '#FFFFFF';
								m++;
								caste_to_obj[k] = new Option(caste_display_txt, caste_txt);
								k++;
							}
							else
							{
								caste_from_obj[m] = new Option(caste_display_txt, caste_txt);
								m++;
							}


						} // EO if(caste_display_txt && InArray(check_selected_caste_spilt_string, caste_display_txt) >= 0)
						else
						{

							caste_from_obj[m] = new Option(caste_display_txt, caste_txt);
							caste_from_obj[m].style.backgroundColor = '#FFFFFF';
							m++;


						} // EO else of if(caste_display_txt && InArray(check_selected_caste_spilt_string, caste_display_txt) >= 0)

					} // EO for(var j=1; j< caste_txt_val.length; j++)

				} // EO for(var i=0; i<splitString.length; i++)

				//For removing the castes when we remove a particular mothertongue
				if(from_page == 'smart_search')
				{

					var caste_rhs = document.frm_main.elements['castearray[]'];
					var caste_lhs = document.frm_main.elements['caste_fromarray[]'];
					var optlen1	  = caste_rhs.options.length;

					for(var e=0; e < optlen1; e++)
					{
						remcst_flag=false;
						if(caste_rhs.options[e].value != "")
						{
							for(var s=0; s < caste_lhs.options.length; s++)
							{
								if(caste_rhs.options[e].value == caste_lhs.options[s].value)
								{
									remcst_flag = true;
									break;
								}
							}
						}

						if(remcst_flag==false)
						{
							caste_rhs.options[e] = null;
							e = -1;
							optlen1--;
						}
					}

					if(optlen1 == 0)
					{
						caste_rhs.options[0] = new Option("Doesn't Matter", "");
					}
				}

				// for city search saved search preselection .
				if(from_page == "city_search" || from_page == "keyword_search" || from_page == "spcases" || from_page == 'new_home_page')
				{
					for(var l=0; l<caste_from_obj.options.length; l++)
					{
						if(caste_from_obj.options[l].value !="" && InArray(check_selected_caste_spilt_string, caste_from_obj.options[l].value) >= 0)
						{
							caste_from_obj.options[l].selected = true;

						}
					}
				}


			} // EO if(myString != "")
			else
			{
				document.getElementById('show_hide_caste').style.display = 'none';

			} // EO else of if(myString != "")

			// for hidding the caste drop down when there is no value of caste
			// related to the selected community and mother tongue drop down
					if(myString.search(/no matches/i)== -1)
					{
						document.getElementById('show_hide_caste').style.display = 'inline';
						document.getElementById('castedp').style.display = 'inline';
						if(from_page!="smart_search")
						{
							document.getElementById("loading_caste").innerHTML = ""
						}
					}
					else
					{
						if (from_page=='new_home_page')
						{
							document.getElementById('castedp').style.display = 'inline';
							document.getElementById("loading_caste").innerHTML = ""
						}
						else
						{
							document.getElementById('show_hide_caste').style.display = 'none';
							document.getElementById('castedp').style.display = 'inline';
							if(from_page!="smart_search")
							{
								document.getElementById("loading_caste").innerHTML = ""
							}
						}
					}

			}
			,'onError':function(req){ alert('Error!\nStatusText='+req.statusText+'\nContents='+req.responseText);}
		 }
	 );

		return status;
		/*
		http_caste.open("GET", url, true);
		http_caste.onreadystatechange = useHttpResponse_caste;
		http_caste.send(null);*/
	}

}


function copytolist1(obj_from, obj_to)
{
	var flag		= false; // To check whether value is selected..
	var remove_flag = false; // To check whether value is selected..


	for (i=0;i<obj_from.options.length;i++)
	{
		var curr_option = obj_from.options[i];

		if(curr_option.selected )
		{


			// To display 'Doesn't Matter' if not added anything..
			if(obj_from == document.frm_main.elements['communityarray[]']
			|| obj_from == document.frm_main.elements['mothertonguearray[]']
			|| 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.value) == (obj_to.options[j].value))
					{
						to_array[j] = curr_option.value;
					}
				}
			}

				if(remove_flag == true)
				{

					if(obj_from == document.frm_main.elements['communityarray[]'])
					{
						if(obj_from.options.length == 1)
						{
							document.frm_main.elements['castearray[]'].options.length = 0;

						}
						else
						{
							var optlen = document.frm_main.elements['castearray[]'].options.length;
							for(var j=0; j<optlen;j++)
							{
								var to_value;
								to_value = document.frm_main.elements['castearray[]'].options[j].value;
								if(to_value.indexOf(curr_option.value) != -1)
								{
									document.frm_main.elements['castearray[]'].options[j] = null;
									j = -1;
									optlen--;
								}
							}
							if(optlen==0)
							{
								document.frm_main.elements['castearray[]'].options[0] = new Option("Doesn't Matter", "");
							}
						}
					}
				}

			flag = true;

			var new_option	   = curr_option.text;
			var new_option_val = curr_option.value;

			//Need to put select mothertongue n caste condition bcoz we hv static option files
			//for occupation n residency status.. which doesnt hv value attribute in option tag.
			//so we hv checked text of the option..
			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['countryofresidencearray[]'] )
				{
					obj_to.options.length = 0;

				}

				if(new_option != "Doesn't Matter")
				{
					if(remove_flag == false)
					{
						if((new_option && InArray(to_array, new_option_val) == -1))
						{
							obj_to.options[obj_to.length] = new Option(new_option, new_option_val);
						}
					}
				}

				//Removing the option frm the source list n
				//decrementing bcoz now we need to loop thr the new list count
				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..
				}
			}

		} // 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;
			document.getElementById('show_hide_caste').style.display = 'none';
		}

		// This is done bcoz, we need " " value for 'Doesn't Matter' option in case of community block only so that
		// we can display default mothertongue list..
		// For other elements we need "" value..
		if(obj_from == document.frm_main.elements['communityarray[]']
		|| obj_from == document.frm_main.elements['mothertonguearray[]']
		|| obj_from == document.frm_main.elements['castearray[]'])
		{
			var value = " ";
		}
		else
		{
			var value = "";
		}

		obj_from.options[0] = new Option("Doesn't Matter", value);
	}

	// This block is used to remove the blank separator between the options in the LHS box
	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;
	}

	return true;

} // EO function copytolist1(obj_from,obj_to)



function which_community(com,gen,fn)
{
	var wio_final_array = new Array();


	wio_final_array["Muslim"] = new Array('All','Bengali','Dawoodi Bohra','Shia','Sunni');
	wio_final_array["Christian"] = new Array('All','Born Again','Protestant','Roman Catholic');
	wio_final_array["Sikh"] = new Array('All','Gurusikh','Jat','Khatri','Ramgharia');
	wio_final_array["Jain"] = new Array('All','Digambar','Shwetamber','Vania');
	wio_final_array["Parsi"] = new Array("parsi");
	wio_final_array["Buddhist"] = new Array("Buddhist");
	wio_final_array["Jewish"] = new Array("Jewish");
	wio_final_array["NoReligion"] = new Array("No Religion");
	wio_final_array["SpiritualNoReligious"] = new Array("Spiritual - Not Religious");
	wio_final_array["Other"] = new Array("Other");

	if(com=="" || com==" ")
	{
		document.getElementById("show_hide_mothertongue").style.display='none';
		document.getElementById("show_hide_caste").style.display='none';
	}
	else
	{
		if(com=="Hindu")
		{
			document.getElementById("show_hide_mothertongue").style.display='';
			document.getElementById("show_hide_caste").style.display='none';
		}
		else
		{

			var d = eval("document."+fn);
			var caste_object = d.elements['castearray[]'];
			caste_object.options.length = 0;
			if(com.match("Spiritual"))
			{
				com = "SpiritualNoReligious";
			}
			else
			{
				com = com.replace(/\s/, '');
			}
			if(wio_final_array[com].length > 1)
			{
				caste_object[0] = new Option("Doesn't Matter"," ", false);
				for(var i=0; i <  wio_final_array[com].length; i++)
				{
					if(wio_final_array[com][i]=="All")
						caste_object[i+1] = new Option(wio_final_array[com][i],"", false);
					else
						caste_object[i+1] = new Option(wio_final_array[com][i],com+":"+wio_final_array[com][i], false);
				}

				document.getElementById("show_hide_mothertongue").style.display='none';
				document.getElementById("show_hide_caste").style.display='';
			}
			else
			{
				document.getElementById("show_hide_mothertongue").style.display='none';
				document.getElementById("show_hide_caste").style.display='none';
			}
		}
	}
}  // EO Which Community Function


function show_mt_caste(val,gen,actionpage,img_url,caste)
{
	if(val=="")
	{
		/*
		document.getElementById('show_hide_mothertongue').style.display = 'none';
		document.getElementById('show_hide_caste').style.display = 'none';
		//*/
		if (actionpage!="new_home_page") {
			document.getElementById('show_hide_mothertongue').style.display = 'none';
			document.getElementById('show_hide_caste').style.display = 'none';
		}
	}
	else if(val=="" || val=="doesn't_matter")
	{
		/*
		document.getElementById('show_hide_mothertongue').style.display = 'inline';
		document.getElementById('show_hide_caste').style.display = 'none';
		//*/
		if (actionpage=="new_home_page") {
			document.getElementById('show_hide_mothertongue').style.display = '';
			document.getElementById('show_hide_caste').style.display = '';
		}
		else {
			document.getElementById('show_hide_mothertongue').style.display = '';
			document.getElementById('show_hide_caste').style.display = 'none';
		}
	}
	else
	{
		/*
		//alert(img_url);
		if(document.getElementById("loading_caste"))
		{
			document.getElementById("loading_caste").innerHTML = "<img src=\"" + img_url + "/imgs/loading.gif\" align=\"absmiddle\">";

		}
		//*/

		if(document.getElementById("loading_caste"))
		{
			if (actionpage=='new_home_page') {
				document.getElementById("loading_caste").innerHTML = "<img src=\"http://img.shaadi.com/imgs/loading.gif\" align=\"absmiddle\">";
			}
			else {
				document.getElementById("loading_caste").innerHTML = "<img src=\"" + img_url + "/imgs/loading.gif\" align=\"absmiddle\">";
			}
		}

		document.getElementById('show_hide_mothertongue').style.display = '';
		document.getElementById('show_hide_caste').style.display = '';
		document.getElementById('castedp').style.display = 'none';

        if (typeof caste === 'undefined') {
            caste = '';
        }

        display_dropdown_caste_new(actionpage, caste, gen);
	}
}


