//Initializing global variables.
var stateArray;
var cityArray;


// this function trims the value entered in the textbox.
function _trim( val ) {
	 while(val.charAt(0)==" ") {
		val = val.substring(1);
	}

	while(val.charAt(val.length-1)==" ") {
		val = val.substring(0,(val.length-1));
	}

	return val;
}



// This function will delete all the entries of city from the passed object.
function evacuateCities(obj) {
	var cityLen = 0;

	cityLen = obj.length;
	for(i=0;i<cityLen;i++) {
		obj.options[0]=null;
	}
}



// This function populates the cities dropdown based on the state selection.
function populateCities(stateObj, cityObj) {
	var counter = 0;
	var flag = false;
	// assigning the selected state value
	var selVal = stateObj.value;
	for( i=0;i<stateArray.length;i++ ) {
		if( stateArray[i]==selVal ) {
			if( !flag ) {
				cityObj.options[counter] = new Option("Select one","");
				counter ++;
				flag = true;
			}
			cityObj.options[counter] = new Option(cityArray[i],cityArray[i]);
			counter++;
		}
	}
}



//This is for making default selection of city and here i am checking cities length with one beacause the default for cities is 
// "Select a City" and its stored on the 0th index of Cities dropdown.
function defaultCity(obj, selCityVal) {
	if( obj && obj.length>1 ) {
		for( i=0;i<obj.length;i++ ) {
			if( obj.options[i].value==selCityVal ) {
				obj.options[i].text=obj.options[i].text;
				obj.options[i].selected=true;
				break;
			}
		}
	} else {
		obj.disabled = true;
	}
}



// This function will execute when any value is changed in dropdown of state.
function populateStateCities(stateObj, cityObj) {
	evacuateCities(cityObj);
	populateCities(stateObj, cityObj);
	
	if( cityObj.length==0 ) {
		cityObj.options[0] = new Option("Select one","");
		cityObj.options[0].text=cityObj.options[0].text;
		cityObj.options[0].selected = true;
		cityObj.disabled = true;
	}  else {
		cityObj.options[0].text=cityObj.options[0].text;
		cityObj.options[0].selected = true;
		cityObj.disabled = false;
	}
}



//Function to delete the value of form field Zip.
function clearZip(cityObj, zipObj) {
	if(cityObj.selectedIndex!=0)
		zipObj.value = "";
}



// This function is used for making default selections in state dropdowns.
function defaultStateSelection(stateObj, selState) {
	var flag = false;
	if( stateObj && stateObj.length>0 ) {
		for( i=0;i<stateObj.length;i++ ) {
			if( stateObj.options[i].value==selState ) {
				stateObj.options[i].text=stateObj.options[i].text;
				stateObj.options[i].selected=true;
				flag = true;
			}
		}
		// making default state selection if there is no state selected.
		if( !flag ) {
			stateObj.options[0].text=stateObj.options[0].text;
			stateObj.options[0].selected=true;
		}
	}
}



//Function for setting the default city for Insurance and Travel Search Box.
function defaultCitySelection(stateObj, cityObj, selCity) {
	if( ""==stateObj.value ) {
		// if there is no state selected then it will disable city and will select All in city as default selection.
		if( cityObj ) {
			evacuateCities(cityObj);
			cityObj.options[0] = new Option("Select One","");
			cityObj.options[0].text=cityObj.options[0].text;
			cityObj.options[0].selected = true;
			cityObj.disabled = true;
		}
	} else {
		// populate cities of selected state in AAR.
		populateCities(stateObj, cityObj);
		cityObj.disabled = false;
		defaultCity(cityObj, selCity);
	}
}



//Function to syncronize zip between Insurace and Travel Serch box.
function syncZip(obj, targetObj) {
	targetObj.value = obj.value;
}



//Function to syncronize state among all the search box.
function syncState(obj, targetStateObj, targetCityObj) {
	for(i=0;i<targetStateObj.length;i++) {
		if(targetStateObj.options[i].value==obj.value) {
			targetStateObj.options[i].text=targetStateObj.options[i].text;
			targetStateObj.options[i].selected=true;
			break;
		}
	}

	targetStateObj.value=obj.value;
	populateStateCities(targetStateObj, targetCityObj);
}



//Function to syncronize city between Insurance and Travel Agent.
function syncCity(obj, zipObj, targetCityObj, targetZipObj) {
	for(i=0;i<targetCityObj.length;i++) {
		if(targetCityObj.options[i].value==obj.value) {
			targetCityObj.options[i].text=targetCityObj.options[i].text;
			targetCityObj.value = obj.value;
			targetCityObj.options[i].selected=true;

			if( obj.selectedIndex!=0 ) {
				zipObj.value = "";
				targetZipObj.value = "";
			}
			break;
		}
	}
}



//Function to expand the Contact Center Box.
function expand() {
	var expanded = document.getElementById('ContactExpanded');
	var collapsed = document.getElementById('ContactCollapsed');

	expanded.style.display = "block";
	collapsed.style.display = "none";
}



//Function to collapse the contact center box.
function collapse() {
	var expanded = document.getElementById('ContactExpanded');
	var collapsed = document.getElementById('ContactCollapsed');

	expanded.style.display = "none";
	collapsed.style.display = "block";
}


