var which_add_button;

//Function FOR Advancse Search Country-State-City drop Down
//Author:Suvarna
////////////////////////////////////////////////////////
function csc_copytolist(obj_from, obj_to,from_button,from_csc)
{
	//PSL Phase 1.5 call to the validation function only when Add is click
	if(from_button=="Add")
	{	
		var return_val= csc_max_lenght_validation(from_csc);	
		if(return_val==false)
		{
			return false;
		}//EO if(return_val==false)
	}//EO if(from_button=="Add")

	which_add_button = from_csc;

	var flag		= false; // To check whether value is selected..
	var remove_flag = false; // To check whether value is selected..
	var call_ajax=1;  //to avoid the ajax call for preselected countries
	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['countryofresidencearray[]']
			|| obj_from == document.frm_main.elements['stateofresidencearray[]']
			|| obj_from == document.frm_main.elements['nearestcityarray[]'])
			{
				remove_flag = true;
			}//EO IF
							
			flag = true;
			
			var new_option	   = curr_option.text;
			var new_option_val = curr_option.value;
			var return_flag=true;
			// PSL EN 1.5 
			//<---To make fields readonly hence not to allow re-entry in to list.Start--->//
			if(obj_from == document.frm_main.elements['countryofresidence_fromarray[]']
			|| obj_from == document.frm_main.elements['cityofresidence_fromarray[]']
			|| obj_from == document.frm_main.elements['stateofresidence_fromarray[]'])
			{
				// PSL EN 1.5  added to allow values which are not on RHS
				to_array = new Array(); 
				for (j=0;j<obj_to.options.length;j++)
				{
					if((obj_from.options[i].value) == (obj_to.options[j].value))
					{
						to_array[j] = curr_option.text;
					}//EO if for compare the values
				}//EO for loop
			 }//EO if(obj_from == document.frm_main.elements['countryofresidence_fromarray[]'] || obj_from == document.frm_main.elements['cityofresidence_fromarray[]'] || obj_from == document.frm_main.elements['stateofresidence_fromarray[]'])
			
			// PSL EN 1.5 
			//<--------To make fields readonly hence not to allow reenry in to list.END -----------
			//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_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;
				}//EO 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[]'] )
				
				if(new_option != "Doesn't Matter")
				{
					//PSL EN 1.5 Readonly field if the value is already dropped.
					if(remove_flag == false)
					{
						if( InArray(to_array, new_option) == -1 && new_option)
						{
							obj_to.options[obj_to.length] = new Option(new_option, new_option_val);
							call_ajax=call_ajax+1;

						}
					}//for remove flag
				}//EO if(new_option != "Doesn't Matter")

				if(remove_flag==true)
				{ 
					if(obj_from==document.frm_main.elements['countryofresidencearray[]'])
					{
						csc_remove_state(obj_from.options[i].text);
						if(document.frm_main.elements['stateofresidence_fromarray[]'].options.length > 0)
						{
							csc_remove_city(obj_from.options[i].text);
						}//IF of state to call rmeove cities
					}//for country

					else if(obj_from==document.frm_main.elements['stateofresidencearray[]'])
					{
						//alert(obj_from.options[i].value);
						csc_remove_citiesforstate(obj_from.options[i].value);
					}//FOr state if selected remove cities
					obj_from.options[i] = null;	//we can use obj_from.remove(i);
					i--; //Used for multiple selection..
					return_flag=false;
				}//EO if(obj_to != document.frm_main.elements['countryofresidencearray[]'] && obj_to != document.frm_main.elements['nearestcityarray[]'])
			}//EO if(new_option != ""
		} // EO if(curr_option.selected)
	} // EO for (i=0;i<obj_from.options.length;i++)


	if(obj_from.options.length == 0 && remove_flag)
	{
		var value = "";
		obj_from.options[0] = new Option("Doesn't Matter", value);
	}//EO if(obj_from.options.length == 0 && remove_flag)

	// 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.length == 1 && obj_from.options[0].text == "")
		{
			obj_from.options[0] = null;
		}//EO else
	 }//EO if(obj_from.options.length > 0 && obj_from.options.length <= 2)

	if (!flag)
	{
		alert ('Please select the Options');
		return false;
	}//EO if (!flag)

	if(call_ajax==1)
	{
		return false;
	}
	if(return_flag == false)
	{
		return false;
	}
	return true;
} // EO function csc_copytolist(obj_from,obj_to)


// PSL Phase 1.5 Function for Country-State-city List in Smart Search
//This function calls the AJAX function depending on the countries selected. bcoz,
//only India and USA have the states currently..
function csc_get_selected_countries(selected_states, img_url, form_page)
{
	var selected_countries = "";
	var selected_states		= "";//PSL_EN_2
		
	for(var i=0;i<document.frm_main.elements['countryofresidencearray[]'].length;i++)
	{
		// Changed only for smart search////
		if( form_page == 'smart_search')
		{
			//alert(selected_countries);
			selected_countries += document.frm_main.elements['countryofresidencearray[]'].options[i].text + ",";
			if (document.frm_main.elements['countryofresidencearray[]'].options[i].text == "Doesn't Matter")
			 {
				document.getElementById('display1').style.display="none";
				document.getElementById('display2').style.display="none";
				
				//PSL Phase 1.5 bug to make the state and city list box empty
				document.frm_main.elements['stateofresidence_fromarray[]'].options.length = 0;
				document.frm_main.elements['stateofresidencearray[]'].options.length = 0;
				document.frm_main.elements['cityofresidence_fromarray[]'].options.length = 0;
				document.frm_main.elements['nearestcityarray[]'].options.length = 0;
			 }//EO if (document.frm_main.elements['countryofresidencearray[]'].options[i].text == "Doesn't Matter")
			 else
			 {
				document.getElementById('display1').style.display="";
			 }//else
		}//EO if( form_page == 'smart_search')
	}//EO for(var i=0;i<document.frm_main.elements['countryofresidencearray[]'].length;i++)

	if(!form_page || form_page == 'smart_search')
	{
		if(selected_countries || selected_states) // If India OR USA is selected..then popuplate the states list..
		{
			if(which_add_button == 'Country')
			{
				csc_display_state_dropdown(selected_countries, selected_states, '', document.frm_main.elements['stateofresidence_fromarray[]'], img_url, 'smart_search', '');
			}
			
			//PSL_EN_2 start
			for(var i=0;i<document.frm_main.elements['stateofresidencearray[]'].length;i++)
			{
				if(document.frm_main.elements['stateofresidencearray[]'].options[i].text != "Doesn't Matter")
				{
					///PSL_EN_1.5 document.frm_main.elements['stateofresidencearray[]'].options[i].text change to document.frm_main.elements['stateofresidencearray[]'].options[i].value
					var stateArray = document.frm_main.elements['stateofresidencearray[]'].options[i].value;			
					selected_states +=  stateArray + ",";
					document.getElementById('display2').style.display="";
				}//EO if(document.frm_main.elements['stateofresidencearray[]'].options[i].text != "Doesn't Matter")	 			
				else
				{
					document.getElementById('display2').style.display="none";

					//PSL Phase 1.5 bug to make the city list box empty
					document.frm_main.elements['cityofresidence_fromarray[]'].options.length = 0;
					document.frm_main.elements['nearestcityarray[]'].options.length = 0;
				}//EO else
			}//EO for(var i=0;i<document.frm_main.elements['stateofresidencearray[]'].length;i++)	
			
			if(form_page == 'smart_search'
			&& which_add_button == 'State')
			{
				csc_display_dropdown(selected_countries,selected_states,'',document.frm_main.elements['cityofresidence_fromarray[]'], img_url, 'smart_search','','');
			}//EO if(form_page == 'smart_search')
			
			
		}//EO if(selected_countries || selected_states )
		else
		{
			//Resetting the state of residence array..
			document.frm_main.elements['stateofresidencearray[]'].options.length = 0;
			//document.getElementById('show_hide_state').style.display = 'none';
		}
	}//EO if(!form_page || form_page == 'smart_search')
} // EO function csc_get_selected_countries()


//This function is for the validation of the csc size
//PSL Phase 1.5

function csc_max_lenght_validation(csc_value)
{
	var country_from_count=0;
	var state_from_count=0;
	var city_from_count=0;
	
			
	var country_to_count=document.frm_main.elements['countryofresidencearray[]'].length;
	var state_to_count=document.frm_main.elements['stateofresidencearray[]'].length;
	var city_to_count=document.frm_main.elements['nearestcityarray[]'].length;
		
	if(csc_value == "Country")
	{
		if(document.frm_main.elements['countryofresidencearray[]'].options[0].text=="Doesn't Matter")
		{
			country_to_count=0;
		}//EO if(document.frm_main.elements['countryofresidencearray[]'].options[0].text=="Doesn't Matter")
		
		// Validation for country

		for(var i=0;i<document.frm_main.elements['countryofresidence_fromarray[]'].options.length;i++)
		{
			if(document.frm_main.elements['countryofresidence_fromarray[]'].options[i].selected)
			{
				country_from_count++;
			}//EO if(document.frm_main.elements['countryofresidence_fromarray[]'].options[i].selected)
		}//EO for(var i=0;i<document.frm_main.elements['countryofresidence_fromarray[]'].options.length;i++) 
	
		var total_country_count=country_to_count + country_from_count ;
			
		if(total_country_count > 5)
		{
			alert("You can select only upto 5 countries.");
			return false;
		}//EO if(total_country_count > 5)
	}//EO if(csc_value == "Country")
	
	//validation for state
	
	if(csc_value == "State")
	{
		if(document.frm_main.elements['stateofresidencearray[]'].options[0].text=="Doesn't Matter")
		{
			state_to_count=0;
		}//EO if(document.frm_main.elements['stateofresidencearray[]'].options[0].text=="Doesn't Matter")

		for(var i=0;i<document.frm_main.elements['stateofresidence_fromarray[]'].options.length;i++)
		{
			if(document.frm_main.elements['stateofresidence_fromarray[]'].options[i].selected)
			{
				state_from_count++;
			}//EO if(document.frm_main.elements['stateofresidence_fromarray[]'].options[i].selected)
		}//EO for(var i=0;i<document.frm_main.elements['stateofresidence_fromarray[]'].options.length;i++)

		var total_state_count=state_to_count + state_from_count ;
		
		if(total_state_count > 10)
		{
			alert("You can select only upto 10 states.");
			return false;
		}//EO if(total_state_count > 10)
	}//EO if(csc_value == "State")
	
	//Validation for the City
	
	if(csc_value == "City")
	{
		for(var i=0;i<document.frm_main.elements['cityofresidence_fromarray[]'].options.length;i++)
		{
			if(document.frm_main.elements['cityofresidence_fromarray[]'].options[i].selected)
			{
				city_from_count++;
			}//EO if(document.frm_main.elements['cityofresidence_fromarray[]'].options[i].selected)
		}//EO for(var i=0;i<document.frm_main.elements['cityofresidence_fromarray[]'].options.length;i++)

		var total_city_count=city_to_count + city_from_count ;
		
		if(total_city_count > 50)
		{
			alert("You can select only upto 50 cities.");
			return false;
		}//EO if(total_city_count > 50)
	}//EO if(csc_value == "City")
}//EO function csc_max_lenght_validation()


/*PSL enhancement phase 1.5 
Function to remove the states of selected country from both LHS and RHS
*/
function csc_remove_state(selectedcountry)
{
	var country=selectedcountry;
	//For LHS side to remove the states for selected country 
	var obj_from=document.frm_main.elements['stateofresidence_fromarray[]'];
	var len_lhs_state=obj_from.options.length;

	for(var i=0;i<len_lhs_state;i++)
		{
			if(obj_from.options[i])
			{
				var lhs_text=obj_from.options[i].text;
				var lhs_value=obj_from.options[i].value;

				var cntry_state = new Array();
				cntry_state = lhs_value.split(":");
				if(cntry_state[0]==country || lhs_text==country)
				{
					obj_from.options[i] = null;	//we can use obj_from.remove(i);
					i--;//Used for multiple selection..
			    }//EO remove the values
			}//EO if for value present
		}//EO foreach loop state option
		

		var obj_rhs=document.frm_main.elements['stateofresidencearray[]'];
		var len_rhs_state=obj_rhs.options.length;
		
		if(len_rhs_state > 0 )
		{
			for(var i=0;i<len_rhs_state;i++)
			{
				if(obj_rhs.options[i])
				{
					var rhs_text=obj_rhs.options[i].text;
					var rhs_value=obj_rhs.options[i].value;
					var cntry_state = new Array();
					cntry_state = rhs_value.split(":");
					if(cntry_state[0]==country || rhs_text==country)
					{
						obj_rhs.options[i] = null;	//we can use obj_from.remove(i);
						i--;
					}//EO remove the values
				}//EO if for value present
			}//EO foreach loop state option
		}//EO else if

		//IF LHS have no value then hide the city and states
		var len_hide=obj_from.options.length;

		if(len_hide==0)
		{
			len_rhs_state=0;
			obj_rhs.options[0] = new Option("Doesn't Matter", "");
			document.getElementById('display1').style.display = 'none';
			document.getElementById('display2').style.display = 'none';
		}

		var len_default=obj_rhs.options.length;
		if(len_default==0)
		{
			obj_rhs.options[0] = new Option("Doesn't Matter", "");
		}

	return true;

}//Delete states from LHS AND RHS


//function to delete the cities from RHS and LHS for selected country or state

function csc_remove_city(selected_country)
{
	var country=selected_country;
	//For LHS side to remove the cities for selected country 
	var obj_lhs_city=document.frm_main.elements['cityofresidence_fromarray[]'];
	var len_lhs_city=obj_lhs_city.options.length;
	

	for(var i=0;i<len_lhs_city;i++)
		{
			if(obj_lhs_city.options[i])
			{
				var lhs_text=obj_lhs_city.options[i].text;
				var lhs_value=obj_lhs_city.options[i].value;


				var cntry_city = new Array();
				cntry_city = lhs_text.split("-");

				//var cntry_state = new Array();
				//cntry_state = lhs_value.split("-");

				var state_city = new Array();
				state_city = lhs_value.split(":");

				if(cntry_city[0]==country || state_city[0]==country)
				{
					
					obj_lhs_city.options[i] = null;	//we can use obj_from.remove(i);
					i--;//Used for multiple selection..
				}//EO remove the values
			}//EO if for value present
		}//EO foreach loop state option

			var obj_rhs_city=document.frm_main.elements['nearestcityarray[]'];
			var len_rhs_city=obj_rhs_city.length;

			var len_city=obj_lhs_city.options.length;
			if(len_city==0)
			{
				obj_rhs_city.length=0;
				obj_rhs_city.options[0] = new Option("Doesn't Matter", "");
				document.getElementById('display2').style.display = 'none';
			}//to hode the city lists
			else if (len_rhs_city > 0)
			{
				//alert(obj_rhs_city.length);
				for(var i=0;i<len_rhs_city;i++)
				{
					if(obj_rhs_city.options[i])
					{
						var rhs_text=obj_rhs_city.options[i].text;
						var rhs_value=obj_rhs_city.options[i].value;
						var cntry_state = new Array();
						cntry_state = rhs_value.split(":");

						var city_state = new Array();
						city_state = rhs_text.split("-");

						if(cntry_state[0]==country || city_state[0]==country)
						{
							obj_rhs_city.options[i] = null;	//we can use obj_from.remove(i);
							i--;
						}//EO remove the values
					}//EO if for value present
				}//EO foreach loop state option
			}//EO else if

		var len_default=obj_rhs_city.options.length;
		if(len_default==0)
		{
			obj_rhs_city.options[0] = new Option("Doesn't Matter", "");
		}
return true;
}//


function csc_remove_citiesforstate(selected_state)
{
	//For LHS side to remove the cities for selected state 
	var obj_lhs_city=document.frm_main.elements['cityofresidence_fromarray[]'];
	var len_lhs_city=obj_lhs_city.options.length;

	var cntry_state_arr= new Array();
	
	for(var i=0;i<len_lhs_city;i++)
		{
			if(obj_lhs_city.options[i])
			{
				var lhs_text=obj_lhs_city.options[i].text;
				var lhs_value=obj_lhs_city.options[i].value;
				lhs_text=lhs_text.replace(/-/, ":");

				cntry_state_arr=lhs_value.split(":");
				var state_city_txt=cntry_state_arr[0]+":"+cntry_state_arr[1];
				if(lhs_text==selected_state || state_city_txt==selected_state)
					{
						obj_lhs_city.options[i] = null;	//we can use obj_from.remove(i);
						i--;
					}//EO remove the values
			}//EO if for object present
		}//FOr each city in LHS

			//For LHS side to remove the cities for selected state from RHS 
		var obj_rhs_city=document.frm_main.elements['nearestcityarray[]'];
		var len_rhs_city=obj_rhs_city.options.length;
		for(var j=0;j<len_rhs_city;j++)
		{
			if(obj_rhs_city.options[j])
			{
				var rhs_text=obj_rhs_city.options[j].text;
				var rhs_value=obj_rhs_city.options[j].value;

				rhs_text=rhs_value.replace(/-/, ":");

				cntry_state_arr=rhs_value.split(":");
				var state_city_txt=cntry_state_arr[0]+":"+cntry_state_arr[1];
				if(rhs_text==selected_state || state_city_txt==selected_state)
					{
						obj_rhs_city.options[j] = null;	//we can use obj_from.remove(i);
						j--;
					}//EO remove the values
			}//EO object exist
		}//EO for each
	
		var len_default=obj_rhs_city.options.length;
		if(len_default==0)
		{
			obj_rhs_city.options[0] = new Option("Doesn't Matter", "");
		}

		var len_default=obj_lhs_city.options.length;
		if(len_default==0)
		{
			obj_lhs_city.options[0] = new Option("Doesn't Matter", "");
			document.getElementById('display2').style.display = 'none';
		}
	return true;
}//EO for remove cities for selected state
