var isPinyinCityDefaultValueChanged = false;
var pinyinCityElementInnerHTML;
var standardCityElementInnerHTML;
var stateProvinceAutoSet = true;
var countryAutoSet = true;
var searchForm = new WebForm();

function getLocationSearchStatus() {
       if(searchFormType=="pinyin"){
         if(!checkPinyinCityDefaultValueChange())  {
            f.cityField.value="";
        }
     }
    cityEmpty = (f.cityField.value =="");

	stateProvinceEmpty = (f.stateProvince.value == "");
	countryEmpty = (f.country.value=="");
	countryUS = (f.country.value=="US");

	if ((!cityEmpty) && (!stateProvinceEmpty) && (!countryEmpty)) { return "COMPLETE_SEARCH"; }
	if ((!stateProvinceEmpty) && (!countryEmpty)) { return "STATE_SEARCH"; }
	if ((!cityEmpty) && (!countryUS) && (!countryEmpty)) { return "NON_US_CITY_SEARCH"; }
	if ((!countryEmpty) && (!countryUS) ) { return "NON_US_COUNTRY_SEARCH"; }
	if ((stateProvinceEmpty) && (countryUS) && (!cityEmpty) ){ return "MISSING_STATE_US_SEARCH"; }
	if ((stateProvinceEmpty) && (countryUS) ){ return "INVALID_US_SEARCH"; }
	if ((!cityEmpty) && (stateProvinceEmpty) && (countryEmpty)) { return "CITY_ONLY"; }
	if ((cityEmpty) && (stateProvinceEmpty) && (countryEmpty)) { return "BLANK"; }
	return "INCOMPLETE";
}
function getLocationStatus() {
  if(searchFormType=="pinyin"){
         if(!checkPinyinCityDefaultValueChange())  {
            f.cityField.value="";
        }
     }
    cityEmpty = (f.cityField.value=="");
	stateProvinceEmpty = (f.stateProvince.value == "");
	countryEmpty = (f.country.value=="");
	countryUS = (f.country.value=="US");

	if ((!cityEmpty) && (!stateProvinceEmpty) && (!countryEmpty)) { return "COMPLETE_SEARCH"; }
	if ((!stateProvinceEmpty) && (!countryEmpty)) { return "STATE_SEARCH"; }
	if ((!cityEmpty) && (!countryUS) && (!countryEmpty)) { return "NON_US_CITY_SEARCH"; }
	if ((!countryEmpty) && (!countryUS) ) { return "NON_US_COUNTRY_SEARCH"; }
	if ((stateProvinceEmpty) && (countryUS) ){ return "INVALID_US_SEARCH"; }
	if ((!cityEmpty) && (stateProvinceEmpty) && (countryEmpty)) { return "CITY_ONLY"; }
	if ((cityEmpty) && (stateProvinceEmpty) && (countryEmpty)) { return "BLANK"; }
	return "INCOMPLETE";
}

function address_searchForm_onSubmit() {

	searchForm.resetErrors();

//	city_onBlur();

	checkDates();

	zipCodeStatus = getZipCodeStatus();

	locationStatus = getLocationSearchStatus();

	if (zipCodeStatus == "INVALID") {

		searchForm.addError("invalidZipCodeError", "zipCodeLabel");

	} else if (zipCodeStatus == "BLANK") {

		if ((locationStatus =="BLANK")) {

			searchForm.addError("searchCriteriaRequiredError" );

		}

		if ((locationStatus =="INVALID_US_SEARCH") ) {

			searchForm.addError("toGeneralUSSearchError" );

		}

		if ((locationStatus =="MISSING_STATE_US_SEARCH") ) {

			searchForm.addError("stateProvinceRequiredError" );

		}

		if ((locationStatus =="CITY_ONLY") ) {

			searchForm.addError("countryRequiredForCitySearchError" );

		}

	}

	if (searchForm.hasErrors()) {

		searchForm.showErrors();

		return false;

	} else {

		return true;

	}

}
var searchFormType='pinyin';
function updateStateWithArgs(stateProvince, country){
    if(searchFormType == "pinyin"){
       stateProvince_onChange(stateProvince,country);
    }else{
        changedStateProv();
    }

}
function updateCountryWithArgs(stateProvince, country){
    if(searchFormType == "pinyin"){
       country_onChange(stateProvince,country);

    }else{
       changedCountry();
    }

}

function updateState(){
    if(searchFormType == "pinyin"){
       stateProvince_onChange();
    }else{
        changedStateProv();
    }
}

function updateCountry(){
    if(searchFormType == "pinyin"){
       country_onChange();
    }else{
        changedCountry();
    }
}

function showHide(whichDiv){
 if(whichDiv == 'pinyin'){
     if(document.specialOfferForm){
        document.specialOfferForm.reset();
         specOffSearchForm.resetErrors();
     } else if(document.tab1Form){ // condition for the /brand/search_results.html or /brand/go.search page
          resetPage('leftNav');
         document.getElementById('divCity').innerHTML="";
     } else{
         f.reset();
         searchForm.resetErrors();
    }
     swapContent('countryContent','cityContent');
     document.getElementById("pinyinCityElement").innerHTML = pinyinCityElementInnerHTML;
     document.getElementById("standardCityElement").innerHTML = "";
     searchFormType = whichDiv;
     document.getElementById('pinyin').style.display = 'inline';
     document.getElementById('standard').style.display = 'none';
     f.stateProvince.disabled=false;
     f.country.disabled=false;
    if(document.getElementById('searchOptionType')){
            document.getElementById('searchOptionType').value = whichDiv;
     }

 }else{
     if(document.specialOfferForm){
        document.specialOfferForm.reset();
        specOffSearchForm.resetErrors();
     } else if(document.tab1Form){ // condition for the /brand/search_results.html or /brand/go.search page
        resetPage('leftNav');
        document.getElementById('divCity').innerHTML="";
     } else{
       searchForm.resetErrors();
      }
     swapContent('countryContent','cityContent');
     document.getElementById("standardCityElement").innerHTML = standardCityElementInnerHTML;
     document.getElementById("pinyinCityElement").innerHTML = "";
     f.cityField.value = "";
     clearFields();
     searchFormType = whichDiv;
     document.getElementById('standard').style.display = 'inline';
     document.getElementById('pinyin').style.display = 'none';

     f.cityField.disabled=true;
     f.cityField.style.display='inline';

     f.stateProvince.disabled=true;
     f.stateProvince.style.display='inline';

     f.country.style.display='inline';
     if(document.getElementById('searchOptionType')){
            document.getElementById('searchOptionType').value = whichDiv;
     }

 }
}
function swapContent(elementOne,elementTwo){
     var temp=document.getElementById(elementOne).innerHTML;
     document.getElementById(elementOne).innerHTML = document.getElementById(elementTwo).innerHTML;
     document.getElementById(elementTwo).innerHTML=temp;
}
function clearFields() {
    f.stateProvince.selectedIndex = 0;
    f.stateProvince.value="";
    f.country.selectedIndex = 0;
    f.country.value="";

}
function city_onFocus() {
    isPinyinCityDefaultValueChanged = true;
    f.cityField.value = "";
    if (stateProvinceAutoSet) { f.stateProvince.selectedIndex = 0; }
	if (countryAutoSet) { f.country.selectedIndex = 0; }
}
function city_onBlur() {

	//Overwriting as pinyinCity field does not utilise this function

}

// Initialize form correctly
function initCities() {
   if(searchFormType=="standard"){
    var countryField = f.country;
	var stateProvince = f.stateProvince;
    var cityField = f.cityField;
	if ((countryField.value == "US") || (countryField.value == "CA")) {
		stateProvince.disabled = false;
		if (stateProvince.value != "") {
			constructXMLQuery();
		}
	} else {
		if (countryField.value != "") {
			constructXMLQuery();
		} else {
			stateProvince.value = "";
			stateProvince.disabled = false;
			cityField.value = "";
			cityField.disabled = true;

		}

	if ((countryField.value != "US") && (f.rateRange)) { f.rateRange.disabled = true; f.rateRange.value = ""; }
	else { if (f.rateRange) { f.rateRange.disabled = false; } }
         }
        }
}
function initializeSearchDiv(whichDiv){
    standardCityElementInnerHTML = document.getElementById("standardCityElement").innerHTML;
    pinyinCityElementInnerHTML = document.getElementById("pinyinCityElement").innerHTML;
    if(whichDiv == 'pinyin'){
     f.reset();
     searchFormType = whichDiv;
     document.getElementById('pinyin').style.display = 'inline';
     document.getElementById('standard').style.display = 'none';
      document.getElementById("standardCityElement").innerHTML = "";
     f.cityField.disabled=false;
     f.cityField.style.display='inline';

     f.stateProvince.disabled=false;
     document.getElementById('searchOptionType').value = whichDiv;
 }else{
     f.reset();
     var temp=document.getElementById('countryContent').innerHTML;
     document.getElementById('countryContent').innerHTML = document.getElementById('cityContent').innerHTML;
     document.getElementById('cityContent').innerHTML=temp;
     searchFormType = whichDiv;

     document.getElementById('standard').style.display = 'inline';
     document.getElementById('pinyin').style.display = 'none';
     document.getElementById("pinyinCityElement").innerHTML = "";

     f.cityField.style.display='inline';
     f.stateProvince.style.display='inline';
     f.country.style.display='inline';

     document.getElementById('searchOptionType').value = whichDiv;
 }
}

function checkPinyinCityDefaultValueChange(){
    return isPinyinCityDefaultValueChanged;
}
