function populate_surgeon(form_id, select_profile, select_surgeon, selected_surgeon){
	
	var surgeon_select = $("form#" + form_id + " select[name='" + select_surgeon + "']");
	
	surgeon_select.empty();
	$("<option value=\"" + 0 + "\"></option>").html("Loading...").appendTo(surgeon_select);
	$(surgeon_select).attr("disabled", "disabled");
		
	var profile_select = $("form#" + form_id + " select[name='" + select_profile + "']");
	var profile_selection = profile_select.val();
	
	var URL = "";
	URL = do_link("site.remote", "request=surgeon&fk_profile=" + profile_selection);
	
	surgeon_select.load(URL, {}, function(){
		
		surgeon_select.removeAttr("disabled");
		
		if ((selected_surgeon != undefined) && (selected_surgeon != "") && (selected_surgeon != 0)) {
			
			try {
				jQuery(surgeon_select.find("option").removeAttr("selected").filter("[value='" + selected_surgeon + "']").get(0)).attr("selected", "selected");
			}
			catch(error){}
		}
		else {
			// jQuery(surgeon_select).find("option[value='0']").attr("selected", "selected");
			// surgeon_select.selectOptions("0", true);
		}
		
		if (typeof(return_surgeon) == "function") {
			return_surgeon();
		}
	});
}

function populate_states(form_name, select_name_country, select_name_states, select_name_cities){

	country_selection = eval("document." + form_name + "." + select_name_country + ".options[document." + form_name + "." + select_name_country + ".selectedIndex].value");
	
	var url = do_link("site.remote", "request=states&fk_countries=" + country_selection);
	
	remote_data_request(url, "", "populate_states_data('" + form_name + "', '" + select_name_country + "', '" + select_name_states + "')");
	
	document.forms[form_name].elements[select_name_states].options.length = 0;
	document.forms[form_name].elements[select_name_states].options[0] = new Option("Loading ...", 0);
	document.forms[form_name].elements[select_name_states].disabled = true;
	
	/*document.forms[form_name].elements[select_name_cities].options.length = 0;
	document.forms[form_name].elements[select_name_cities].options[0] = new Option("[ Select state ]", 0);
	document.forms[form_name].elements[select_name_cities].disabled = true;*/
}

function populate_states_data(form_name, select_name_country, select_name_states){
	
	document.forms[form_name].elements[select_name_states].disabled = false;
	document.forms[form_name].elements[select_name_states].options[0] = new Option("[ Select state ]", 0);
	
	idx = 0;
	for (var i in remote_res_states) {
		if ( (remote_res_states[i] != "") && !remote_res_states[i].toString().match("{") ) {
			idx ++;
			document.forms[form_name].elements[select_name_states].options[idx] = new Option(decodeURI(remote_res_states[i]), i);
		}
	}
	
	return_states();
}

function populate_cities(form_name, select_name_states, select_name_cities){
	
	state_selection = eval("document." + form_name + "." + select_name_states + ".options[document." + form_name + "." + select_name_states + ".selectedIndex].value");
	
	var url = do_link("site.remote", "request=cities&fk_states=" + state_selection);
		
	remote_data_request(url, "", "populate_cities_data('" + form_name + "', '" + select_name_states + "', '" + select_name_cities + "')");
		
	document.forms[form_name].elements[select_name_cities].options.length = 0;
	document.forms[form_name].elements[select_name_cities].options[0] = new Option("Loading ...", 0);
	document.forms[form_name].elements[select_name_cities].disabled = true;	
}

function populate_featured_cities(form_name, select_name_states, select_name_cities){
	
	state_selection = eval("document." + form_name + "." + select_name_states + ".options[document." + form_name + "." + select_name_states + ".selectedIndex].value");
	
	var url = do_link("site.remote", "request=cities&cities_featured=on&fk_states=" + state_selection);
		
	remote_data_request(url, "", "populate_cities_data('" + form_name + "', '" + select_name_states + "', '" + select_name_cities + "')");
		
	document.forms[form_name].elements[select_name_cities].options.length = 0;
	document.forms[form_name].elements[select_name_cities].options[0] = new Option("Loading ...", 0);
	document.forms[form_name].elements[select_name_cities].disabled = true;	
}

function populate_cities_data(form_name, select_name_states, select_name_cities){

	document.forms[form_name].elements[select_name_cities].disabled = false;
	document.forms[form_name].elements[select_name_cities].options[0] = new Option(" [ -- Select -- ] ", 0);
	
	idx = 0;
	for (var nr in remote_res_cities) {
		idx ++;
		for (var i in remote_res_cities[nr]) {
			if ( (remote_res_cities[nr][i] != "") && !remote_res_cities[nr][i].toString().match("{") ) {
				document.forms[form_name].elements[select_name_cities].options[idx] = new Option(decodeURI(remote_res_cities[nr][i]), i);
			}
		}
	}
	return_cities();
}
	 
function populate_cities_zip(form_name, select_name_cities, cities_zip_id, cities_zip_row, zipcode){
	
	city_selection = eval("document." + form_name + "." + select_name_cities + ".options[document." + form_name + "." + select_name_cities + ".selectedIndex].value");
	
	var url = do_link("site.remote", "request=cities_zip&fk_cities=" + city_selection);
	
	remote_data_request(url, "zip=" + zipcode, "populate_cities_zip_data('" + form_name + "', '" + select_name_cities + "', '" + cities_zip_id + "', '" + cities_zip_row + "')");
	
}

function populate_cities_zip_data(form_name, select_name_cities, cities_zip_id, cities_zip_row){
	
	city_selection = eval("document." + form_name + "." + select_name_cities + ".options[document." + form_name + "." + select_name_cities + ".selectedIndex].value");
	
	dom_get_element(cities_zip_id).innerHTML = remote_res_cities_zip;
	
	if (city_selection > 0) {
		dom_toggle_element(cities_zip_row, true);
	}
	else {
		dom_toggle_element(cities_zip_row, false);
	}
}

// ################################################################################

function FormatCurrency(NumToFormat){
	
	var Formatted;
	
	if (NumToFormat > 0) {
		
		Formatted = NumToFormat.replace(/(^|[^\w.])(\d{4,})/g, function($0, $1, $2){
		
			var Tmp;
			Tmp = $1 + $2.replace(/\d(?=(?:\d\d\d)+(?!\d))/g, "$&,");
			return Tmp;
		});
		
	}
	else {
		Formatted = NumToFormat;
	}
	
	return Formatted;
}

function CounterText(field, cntfield, maxlimit) {
	var obj_field = dom_get_element(field);
	var obj_cntfield = dom_get_element(cntfield);
	if (obj_field.value.length > maxlimit){
        		obj_field.value = obj_field.value.substring(0, maxlimit);
	}
   	else {
       		obj_cntfield.value = maxlimit - obj_field.value.length;
   	}
}

function CounterTextWords(field, cntfield_text, cntfield_words, maxlimit_text, maxlimit_words) {
	
	// Count chars
	var obj_field = dom_get_element(field);
	var obj_cntfield_text = dom_get_element(cntfield_text);
		
	if (obj_field.value.length > maxlimit_text){
        		obj_field.value = obj_field.value.substring(0, maxlimit_text);
        		obj_cntfield_text.value = 0;
	}
   	else {
       		obj_cntfield_text.value = maxlimit_text - obj_field.value.length;
   	}
   	
   	// Count words
   	var obj_cntfield_words = dom_get_element(cntfield_words);
	
	var fullStr = obj_field.value + " ";
	var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
	var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
	var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
	var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
	var splitString = cleanedStr.split(" ");
	var word_count = splitString.length -1;
	if (fullStr.length < 2) {
		word_count = 0;
	}

	var lastSpace = obj_field.value.lastIndexOf(' ');
	
   	if (word_count > maxlimit_words){
        		obj_field.value = obj_field.value.substring(0, lastSpace);
	}
   	else {
       		obj_cntfield_words.value = maxlimit_words - word_count;
   	}
   	   	
}

var myBox;

function start_ol(page, params, my_title, width, height){
	
	jQuery(myBox).unload();
	
	params += "&rv=" + Math.floor(Math.random() * 10000000000);
	
	myBox = new Boxy("<div id=\"iframe_content\" style=\"width: " + width + "px; height: " + height + "px;\"></div>", {
		title: my_title,
		// closeText: "[" + link_close + "]",
		closeText: "<span class='close_box'>[ <a href='#' class='close'>" + link_close + "</a> ] <a href='#' class='close'><img src='" + PATH + "/img/site/btn_close.gif' align='absmiddle'></a></span>",
		closeable: true,
		modal: true
	});
	
	
	jQuery("#iframe_content").append("<iframe name=\"overlay\" src=\"" + do_link(page, params)  + "\"  framespacing=\"0\" frameborder=\"no\" scrolling=\"no\" width=\"" + width + "\" height=\"" + height + "\"></iframe>");
}
