var xhrOrigin,xhrDestin;
var flag_ajaxOriginCity=0,flag_ajaxDestinCity=0;
var buttonText;

Object.isArray = function(obj) {
	return (typeof obj == "object" && obj.constructor == Array);
}

function setHiddenCityFields(fieldName){
	//Set the hidden field in order to submit a field with name originCity or destinCity
	if (fieldName == "originCityCode")
		jQuery("#hiddenoriginCity").val(jQuery("#originCityCode :selected").text());
	if (fieldName == "destinCityCode") 
		jQuery("#hiddendestinCity").val(jQuery("#destinCityCode :selected").text());
}

function restoreCityTextBox(elementName,widthElement){
	var city = jQuery("#"+ elementName);
	
	if(!widthElement)
		widthElement = "textbox2";
	
	if(jQuery('#loadingImage' + elementName).length > 0){
		jQuery('#loadingImage' + elementName).remove();
		city.removeAttr('disabled').css('width',(city.width() + 20) + 'px');
	}
	
	if(!(jQuery("#"+ elementName  +"Code").length > 0) && !(jQuery('#loadingImage' + elementName).length > 0)){
		city.removeClass("addBorder");
	}
	
	if(jQuery("#"+ elementName  +"Code").length > 0){
		var divCityElement ="<input type='text' class='" + widthElement  +" blue' id='"+ elementName +"' name='" +elementName + "' value='" + localized_messages_CitySelect + "' />" ;			
		jQuery("#" + elementName +"Container").html(divCityElement);
	}
	
	jQuery("#" + elementName).bind('focus',(function(){
			var fieldvar = $(this).get(0);
			if (fieldvar.defaultValue == fieldvar.value) fieldvar.value = '';				
	}));
	
	 if(!(typeof doClearCityBlur == "undefined")){
		jQuery("#" + elementName).bind('blur',(function(){
			var fieldvar = $(this).get(0);
			doClearCityBlur(fieldvar);
		}));
	}
	
}

function populateSelectBoxes(country,elementName,presetValue){
	
	if(country.val() == "-" || country.val() == "0" )
		return;
	
	prepareAjaxRequestCity(elementName);
	disableSearchButton(elementName);
	
	if (presetValue)
		return makeAjaxRequest(country,elementName,presetValue);
	else
		return makeAjaxRequest(country,elementName);
}

function prepareAjaxRequestCity(elementName){
	var city = jQuery("#" + elementName);
	if (!(city.length > 0)){
		if(!(jQuery('#loadingImage' + elementName).length > 0)){ 
			city = jQuery("#" + elementName + "Code");
			city.parent().append(jQuery(document.createElement("img")).attr({
					id:  "loadingImage" + elementName,
					src: "/WisetripFrontEnd/images/ajax-loader.gif",
					alt: localized_messages_loading
			}));
			jQuery("span#select" + elementName + "Code").css('width','300px');
			city.css('width',(city.width() - 20) + 'px');
			city.attr('disabled','disabled');
			jQuery("#select" + elementName + "Code").text(localized_messages_loading);
		}
		return;
	}
		
	//To avoid reloading the same (loading) image when making another ajax request
	if(!(jQuery('#loadingImage' + elementName).length > 0)){ 	 
		city.css('width',(city.width() - 20) + 'px');
			city.parent().append(jQuery(document.createElement("img")).attr({
				id:  "loadingImage" + elementName,
				src: "/WisetripFrontEnd/images/ajax-loader.gif",
				alt: localized_messages_loading
			}));
		city.siblings(0).css('display','inline');
		city.attr('disabled','disabled');
		city.removeClass('addBorder');
		city.val(localized_messages_loading);
	}
}

function makeAjaxRequest(country,elementName,presetValue){
	
	var city =jQuery("#" + elementName);
	
	var xhr = jQuery.ajax({
	    url: 'AjaxRetrieveHops.action',
	    type: 'GET',
	    data: {country: country.val()},
	    timeout: 60000,
		dataType: 'json',
		error: function(){
	    	restoreCityTextBox(elementName);
	    	city.val(localized_messages_error_nocities).addClass('addBorder');
	    	enableSearchButton(elementName);	    	
			alert (localized_messages_error_nohops_tryagain);
	    },
	    success: function(resp){
			var responseTxt = resp.HopsWrapper.result;
			 
			if (responseTxt == "SUCCESS") {
			 	var jsonVar = resp.HopsWrapper.hops.intHops;
			 	var options = '',hopOptions='';
			 	options += '<option value="-">'+localized_messages_cities+'</option>';
			 	hopOptions += "<option value=\"-\">"+localized_messages_selectHop+"</option>";
			 	if (Object.isArray(jsonVar)) {
			 		for (var i = 0; i < jsonVar.length; i++) {
			 			options += '<option value="' + jsonVar[i].hopId + '">' + jsonVar[i].hopName + '</option>';
			 			hopOptions += '<option value="' + jsonVar[i].hopName + "##" + jsonVar[i].hopId + '">' + jsonVar[i].hopName + '</option>';

			 		}
			 	}
			 	else {
			 		options += '<option value="' + jsonVar.hopId + '">' + jsonVar.hopName + '</option>';
		 			hopOptions += '<option value="' + jsonVar.hopName + "##" + jsonVar.hopId + '">' + jsonVar.hopName + '</option>';
			 	}
			 	
		    	jQuery('#loadingImage' + elementName).remove();
		    	
		    	if(jQuery("#" + elementName + "Code").length > 0 )
		    		jQuery("#" + elementName +"Container").empty();
		    	
		    	/* Added for Advanced Search */
		    	if(jQuery("#originHop").length > 0 || jQuery("#destinHop").length > 0 ){
		    		hopElement = elementName.replace("City","Hop");
		    		jQuery("#" + hopElement).html(hopOptions);
		    	}
		    	
		    	createSelectElement(elementName, options, jQuery("#" + elementName +"Container"));
		    	if(presetValue){
		    		//jQuery("#" + elementName + "Code option[text=" + presetValue +"]").attr("selected","selected") ;
		    		jQuery("#" + elementName + "Code").val(jQuery("#" + elementName + "Code option:contains('"+ presetValue +"')").val());
		    		setHiddenCityFields(elementName + "Code");
		    	}
		    	enableSearchButton(elementName);
		    	
			 }
			 else{
				restoreCityTextBox(elementName);
				city.val(localized_messages_error_nocities).addClass('addBorder');
			    enableSearchButton(elementName);
			 	alert (localized_messages_error_nohops_tryagain);
			 }
	    }
	});
	
	return xhr;
		
}


