<!--

// To get the HTTP object to call a method..
function getHTTPObject()
{
	var xmlhttp;

	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlhttp = new XMLHttpRequest();
			}
			catch (e)
			{
				xmlhttp = false;
			}
		}
	}

	return xmlhttp;

} // EO function getHTTPObject()



// Two objects are created for displaying state and city drop downs because one is overwriting another..
//################ Builds the options for State drop down..###################
/* Called from :

- profile -> Edit profile
- html-template -> Smart Search
- city-search -> City Search
- included_modify -> Registration page

*/

//Instatiate another object to call method..
var http_state = new getHTTPObject();

var check_selected_state;
var check_state_obj;
var check_state_from_page;

/*
selected_country : All selected county list - In case of error or preselection
selected_state   : All selected state list -------- " --------
selected_city	 : All selected city list -------- " --------
state_obj		 : Functions gets called from diff files so passing state object that needs to be filled in with the values.
img_url			 : Img server path
from_page		 : On search pages we hv multiple selection of countries, so using diff. script file for searches.
flag			 : Used to preselect the values while loading a page first time.
*/

function csc_display_state_dropdown(selected_country, selected_state, selected_city, state_obj, img_url, from_page, flag)
{ 

	// Using these 2 vars for city drop down as well, so assigning the values..
	check_state_obj = state_obj;
	check_state_from_page = from_page;
	var params =  null;
	// for Search page AJAX - Mulitple countries selected..
	if(from_page == "smart_search")
	{
		params = "country=" + escape(selected_country)+ "&from_page=" + escape(from_page);
		var myurl = "/ssi/csc-ajax-india-usa-states.php";
	}//EO if(from_page == "smart_search")
	else
	{
		return false;
	}
	if(selected_state == "-")
	{
		check_selected_state = "Other";
	}//EO if(selected_state == "-")
	else
	{
		check_selected_state = selected_state;
	}

	// required coz it doesnot preselect the values. it hides the form element by default..
	if(flag == "Y") 
	{
		//Resetting the state os residence array..
		state_obj.options.length = 0;
		
		// PSL Phase1.5 added if 
		if(document.getElementById("show_hide_state"))
		{
			document.getElementById("show_hide_state").style.display = "none";
		}//EO if(document.getElementById("show_hide_state"))
	}//EO if(flag == "Y")

	http_state.open("POST", myurl, true);

	//Send the proper header information along with the request
	http_state.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_state.setRequestHeader("Content-length", params.length);
	http_state.setRequestHeader("Connection", "close");

	http_state.onreadystatechange = useHttpResponse_india_usa_state;
	http_state.send(params);

	var state_from_obj = document.frm_main.elements['stateofresidence_fromarray[]'];
	state_from_obj.options.length = 0;
	state_from_obj.options[0] = new Option(" Loading . . . ", ""); 
} // EO function display_state_dropdown()


// To populate the multiple drop down with state values..

function useHttpResponse_india_usa_state()
{
	// This is used to preselect the castes in smart search..
	var state_to_obj = document.frm_main.elements['stateofresidencearray[]'];
	
	if (http_state.readyState == 4 && http_state.status == 200)
	{
		var textout = http_state.responseText;
		myString    = new String(textout);
		splitString = myString.split("|");
		
		check_state_obj.options.length = 0;

		// Getting all preselected states from the list box..
		var arr_to_states	= new Array();
		for (k=0;k<state_to_obj.options.length;k++)
		{
			arr_to_states[k] = state_to_obj.options[k].value;
		}//EO for (k=0;k<state_to_obj.options.length;k++)

		//Resetting the state of residence array..
		state_to_obj.options.length = 0;
		state_to_obj.options[0] = new Option("Doesn't Matter", ""); // On smart search to set it Doesn't Matter by default 

		var j = 0;
		var m = 0;
	
		for(var i=1; i<splitString.length; i++)
		{
			var curr_state = splitString[i].trim();
			
			if(curr_state.match("-----"))
			{
				curr_state_val = "";
			}//EO curr_state.match("-----")
			else
			{
				curr_state_val = curr_state;
			}
							
			//PSL PHASE 1.5 Bug Fixes Mozilla Issue
			curr_state_text_to = curr_state.split(":");	
			if(check_selected_state.match(curr_state))
			{
				if(check_state_from_page == "smart_search") // For preselected values, putting them in right list
				{
					state_to_obj[m] = new Option(curr_state_text_to[1], curr_state_val);
					m++;
				}//EO if(check_state_from_page == "smart_search")
				else 
				{
					check_state_obj[j] = new Option(curr_state, curr_state_val, false, true);
					check_state_obj[j].selected = true;
					j++;
				} 
			}//EO if(check_selected_state.match(curr_state))
			else
			{
				if(InArray(arr_to_states, curr_state) >= 0)
				{
					state_to_obj[m] = new Option(curr_state_text_to[1], curr_state_val);
					m++;
					
				}//EO if(InArray(arr_to_states, curr_state) >= 0)
			}//EO else
			
			//PSL EN 1.5 commented  to undone removing items from source list. START
			
			curr_state_text = curr_state.split(":");	
			curr_state_val_null='';
			var l=i-1;
			var curr_state_prev = splitString[l].trim();
			curr_state_prev_text = curr_state_prev.split(":");
				
			if(curr_state_text[0]!=curr_state_prev_text[0])
			{
				t=j++;
				check_state_obj[t] = new Option(curr_state_text[0], curr_state_val_null);							
				check_state_obj[t].style.background="#F2F2C3"; 
			}//EO if(curr_state_text[0]!=curr_state_prev_text[0])
				
			check_state_obj[j] = new Option(curr_state_text[1], curr_state_val);
			check_state_obj[j].style.background="#FFFFFF"; 
			j++;
					
			//PSL EN 1.5 commented  to undone removing items from source list. START
		}//EO for(var i=1; i<splitString.length; i++)
		
		if(document.getElementById("show_hide_state"))
		{
			document.getElementById("show_hide_state").style.display = "";
		}//EO if(document.getElementById("show_hide_state"))

	}//EO if (http_state.readyState == 4 && http_state.status == 200)
	
	else if(check_selected_state && check_state_from_page == "smart_search")
	{
		var splitString = check_selected_state.split("|");
		
		for(var i=0; i<splitString.length; i++)
		{
			var curr_state = splitString[i].trim();
			state_to_obj[i] = new Option(curr_state, curr_state);
		}//EO for(var i=0; i<splitString.length; i++)
	}//EO else if(check_selected_state && check_state_from_page == "smart_search")
}//EO function useHttpResponse_india_usa_state()

// To populate the drop down with state values..


//################################ END ####################################
//################ Builds the options for City drop down..###################

/* Called from :
- profile -> Edit profile
- city-search -> City Search
- included_modify -> Registration page

*/

//Instantiate an object to call method..
var http = new getHTTPObject();

// These vars coming frm state srop down function, 
//setting them as global so that we can use them in city drop down function..
var check_selected_city;
var check_city_obj;
var check_city_from_page;
var check_selected_state;
var check_selected_country;
/*
selected_country : All selected county list - In case of error or preselection
selected_state   : All selected state list -------- " --------
selected_city	 : All selected city list -------- " --------
city_obj		 : Functions gets called from diff files so passing city object that needs to be filled in with the values.
img_url			 : Img server path
from_page		 : On search pages we hv multiple selection of countries, so using diff. script file for searches.
flag			 : Used to preselect the values while loading a page first time.
state_option	 : In every case, city gets populated from state selected, but in case of city search, state drop down is optional. so to tackle this condition, passing this parameter.
*/

// PSL 1.5 Created New Function for Smart Search

function csc_display_dropdown(selected_country, selected_state, selected_city, city_obj, img_url, from_page, flag, state_option)
{
    check_selected_country=selected_country;
    check_selected_state=selected_state;
    check_city_obj = city_obj;
    check_city_from_page = from_page;

	var params;
	if(from_page == "smart_search" )//on the smart search page where cities populated for multiple states
	{
		params ="from_page="+ escape(from_page) + "&country=" + escape(selected_country) + "&state=" + escape(selected_state) + "&city=" + escape(selected_city) + "&state_option=" + escape(state_option) + "&from_page=" + escape(from_page);
		var myurl = "/ssi/csc-ajax-city-multiple.php";
	}//EO if(from_page == "smart_search" )
	
	if((selected_state == "" && state_option != "no_state_selected") || selected_country == "")
	{
		city_obj.options.length = 0;
		
		// For Multiple list of cities..	
		if(from_page == "city_search") 
		{
			city_obj.options.size = 1;
			city_obj[0] = new Option("---------No City---------", "");
		}//EO if(from_page == "city_search") 
		else if(from_page == "smart_search")// PSL_EN_2-start
		{
			city_obj[0] = new Option("", "");
		}// PSL_EN_2-end
		else
		{
			city_obj[0] = new Option("Select city", "");
		}

		return false;
	}//EO if((selected_state == "" && state_option != "no_state_selected") || selected_country == "")

	if(flag == "Y") // required coz it doesnot preselect the values. it hides the form element by default..
	{
		if(document.getElementById("show_hide_city"))
		{	
			document.getElementById("show_hide_city").style.display = "none";
		}//EO if(document.getElementById("show_hide_city"))
	}//EO if(flag == "Y")

	if(from_page != "smart_search")
	{
		if(document.getElementById("loading_city"))
		{
			document.getElementById("loading_city").innerHTML = "<img src=\"" + img_url + "/imgs/loading.gif\" hspace=\"16\" align=\"absmiddle\">";
		}//EO if(document.getElementById("loading_city"))
	}//EO if(from_page != "smart_search")

	if(selected_city == "-")
	{
		check_selected_city = "Other";
	}//EO if(selected_city == "-")
	else
	{
		check_selected_city = selected_city;
	}
	
	// PSL_EN_2-start
		http.open("POST", myurl, true);

	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");

	http.onreadystatechange = useHttpResponse_multiple_state;
	http.send(params);
	var city_from_obj = document.frm_main.elements['cityofresidence_fromarray[]'];
	city_from_obj.options.length = 0;
	city_from_obj.options[0] = new Option(" Loading . . . ", ""); 
} // EO function csc_display_dropdown()


// To populate the drop down with city values..
//################################ END ####################################
//PSL_EN_1.5-start
/**
 * populate the drop down with city values for multiple states - smart search.
 *Modified Function to populate city list with country-state label
 * @author Suvarna
 * @param
 * 
 * @usage  http.onreadystatechange = useHttpResponse_multiple_state;

*/

function useHttpResponse_multiple_state()
{
	// readyState = 0 being unintialised, 1 being loading, 2 being loaded, 3 being interactive and 4 being finished
	
	//PSL Phase 1.5 bug
	var city_to_obj = document.frm_main.elements['nearestcityarray[]'];

	if (http.readyState == 4 && http.status == 200)
	{
		var textout = http.responseText;

		//prompt("", textout);
		//PSL Phase 1.5 bug Getting all preselected states from the list box..
		var arr_to_city	= new Array();

		for (i=0;i<city_to_obj.options.length;i++)
		{
			arr_to_city[i] = city_to_obj.options[i].value;
		}//EO for (i=0;i<city_to_obj.options.length;i++)

		// PSL Phase 1.5 bug Resetting the state os residence array..
		// On smart search to set it Doesn't Matter by default 
		city_to_obj.options.length = 0;
		city_to_obj.options[0] = new Option("Doesn't Matter", ""); 

		myString    = new String(textout);
		splitString = myString.split("|");
		mystring_country=new String(splitString);
		splitString_country=mystring_country.split(":");
		
		check_city_obj.options.length = 0;

		var j=0; var l=1;m=0;
		for(var i=0; i<splitString.length; i++)
		{
			var curr_city_state_country=splitString[i].trim();
			mystring_country=new String(curr_city_state_country);			
			splitString_country=mystring_country.split(":");
			//PSL en 1.5 added for having colon as seprator between country and state name
			var curr_city = splitString_country[2];
			var curr_country_state=splitString_country[0]+"-"+splitString_country[1];
			var curr_city_val;
			
			//when getting 'Other' value then internally in database passing it as '-'..
			
			if(curr_city == "Other")
			{
				curr_city_val = "-";
			}//EO if(curr_city == "Other")
			else
			{
				curr_city_val = curr_city;
			}
		
			
			if(curr_city)
			{
				//PSL Phase 1.5 code bug To set the preselected city into the RHS
				// For preselected values, putting them in right list
				if(check_city_from_page == "smart_search") 
				{
					if(InArray(arr_to_city, curr_city_state_country) >= 0)
					{
						city_to_obj[m] = new Option(curr_city, curr_city_state_country);
						m++;
					}//EO if(InArray(arr_to_city, curr_city_state_country) >= 0)	
				}//EO if(check_city_from_page == "smart_search") 
				
				//Creating new options of city ..
				check_selected_city_spilt_string = check_selected_city.split('|');
				
				if (InArray(check_selected_city_spilt_string, curr_city) >= 0)
				{
					check_city_obj[i] = new Option(curr_city, curr_city_val, false, true);
					check_city_obj[i].selected = true;
				}//EO if (InArray(check_selected_city_spilt_string, curr_city) >= 0)
				else
				{
					//<--------------PS EN 1.5 Start ------------>		
					//---------Loop For Country-state combination in Smart Search
					if (i>0)
					{
						var l=i-1;
						curr_city_state_country_prev=splitString[l].trim();
						//	mystring_country_prev=new String(curr_city_state_country_prev);			
						splitString_country_prev = curr_city_state_country_prev.split(":");
						//	splitString_country_prev=mystring_country_prev.split(":");
						//PSL en 1.5 added for having hifen in between country and state name
						var curr_country_prev=splitString_country_prev[0]+"-"+splitString_country_prev[1];
					}//EO if (i>0) 

					if(curr_country_state!=curr_country_prev)
					{	
						curr_city_val_null="";
						l=j++;
						check_city_obj[l] = new Option(curr_country_state, curr_city_val_null);
						check_city_obj[l].style.background="#F2F2C3"; 
		 			}//EO if(curr_country_state!=curr_country_prev)

					check_city_obj[j] = new Option(curr_city, mystring_country);
					check_city_obj[j].style.background="#FFFFFF"; 
					j++;

					//<---------PS EN 1.5 End ----------------------->		
				}//EO else
			}//EO if(curr_city)
		}//EO for(var i=0; i<splitString.length; i++)		
	}//EO if (http.readyState == 4 && http.status == 200)
}//EO function useHttpResponse_multiple_state				
