// Populate zones select #######################################################
function populate_zones(form_id, select_name_type, select_name_zones, selected_zone){
	
	var zones_select = $("form#" + form_id + " select[name='" + select_name_zones + "']");
	
	zones_select.empty();
	$("<option value=\"" + 0 + "\"></option>").html("Loading...").appendTo(zones_select);
	$(zones_select).attr("disabled", "disabled");
		
	var type_select = $("form#" + form_id + " select[name='" + select_name_type + "']");
	var type_selection = type_select.val();
	
	var URL = "";
	
	URL = do_link("site.remote", "request=zones&offer_type=" + type_selection);
	
	zones_select.load(URL, {}, function(){
		
		zones_select.removeAttr("disabled");
		
		if ((selected_zone != undefined) && (selected_zone != "") && (selected_zone != 0)) {
			
			try {
				jQuery(zones_select.find("option").removeAttr("selected").filter("[value='" + selected_zone + "']").get(0)).attr("selected", "selected");
			}
			catch(error){}
		}
		else {
			// jQuery(zones_select).find("option[value='0']").attr("selected", "selected");
			// zones_select.selectOptions("0", true);
		}
		
		if (typeof(return_zones) == "function") {
			return_zones();
		}
	});
}

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

// Form functions #######################################################

// Focus on input
function form_focus(form_id){
	$("form#" + form_id + " :input[value='']:not(select):enabled:visible:first").focus();
}

// Disable submit button
function jform_do_submit(form_id, button_name){
	
	var my_form = $("form#" + form_id);
	
	my_form.submit(function() {
		
		if (!form_is_submitted) {
			$("form#" + form_id + " :button[name='" + button_name + "'], form#" + form_id + " :submit[name='" + button_name + "']").attr("disabled", "disabled");
			
			form_is_submitted = 1;
			submitted_form = form_id;
			submission_button = button_name;
		}
	});
}

function toggle_location_panels(form_id, location, state){
	
	if (location == "choose") {
		$("#panel_predefined").show();
		$("#panel_define").hide();
		
		$("form#" + form_id + " :hidden[name='location']").attr({value: "choose"});
	}
	else if (location == "define") {

		populate_states("site_register", "fk_countries", "fk_states", "fk_cities", state);
		
		$("#panel_predefined").hide();
		$("#panel_define").show();
		
		$("form#" + form_id + " :hidden[name='location']").attr({value: "define"});
	}
	
}

function jform_set_select(sel_name, form_id, sel_value){
	$("form#" + form_id + " select[name='" + sel_name + "']:first").val(sel_value);
}

function jform_set_radio(radio_name, form_id, selected_value){
	// $("form#" + form_id + " :radio[name='" + radio_name + "'][value='" + selected_value + "']:first").attr("checked", "checked");
	
	var this_radio = jQuery("form#" + form_id + " :radio[name='" + radio_name + "'][value='" + selected_value + "']:first");
	
	if (jQuery(this_radio).length > 0) {
		jQuery(this_radio).click();
	}
}

function jform_set_checked(check_name, form_id, checked_mode){
	if (checked_mode != "" && checked_mode != 0 && checked_mode != "off") {
		$("form#" + form_id + " :checkbox[name='" + check_name + "']:first").attr("checked", "checked");
	}
}

function jform_check_all(form_id){
	$("form#" + form_id + " :checkbox").attr("checked", "checked");
}

function jform_uncheck_all(form_id){
	$("form#" + form_id + " :checkbox").removeAttr("checked");
}

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

function assign_toggle_tr() {
	jQuery(".toggle_tr").each(function(){
		if (jQuery("#" + jQuery(this).attr("rel")).find(">td:first").is(":visible")) {
			jQuery(this).find("> img").attr("src", "/img/icons/hide.gif");
		}
		else {
			jQuery(this).find("> img").attr("src", "/img/icons/expand.gif");
		}
	}).click(function(e){
		
		var rel_tr = jQuery("#" + jQuery(this).attr("rel"));
		
		rel_tr.toggle();
		
		if (rel_tr.find(">td:first").is(":visible")) {
			jQuery(this).find("> img").attr("src", "/img/icons/hide.gif");
		}
		else {
			jQuery(this).find("> img").attr("src", "/img/icons/expand.gif");
		}
		
		return false;
	});
}

function remote_action(options) {
	
	var URL = do_link("site.remote", jQuery.param(options));
	
	jQuery.getScript(URL, function(data){
		
	});
}

function remote_adm_action(options) {
	
	var URL = do_link("adm.remote", jQuery.param(options));
	
	jQuery.getScript(URL, function(data){
		
	});
}

//Removes duplicate values from array  
function array_unique(array){
	var key = '';
	var tmp_arr1 = new Array(); 
	var tmp_arr2 = new Array();
	var val = '';
	tmp_arr1 = array;
	    
	var __array_search = function (needle, haystack, strict) {
		var fkey = '';
		for (fkey in haystack) {
			if ((strict && haystack[fkey] === needle) || (!strict && haystack[fkey] == needle) ) {
				return fkey;
			}
		}
		return false;
	}    
		
	for (key in tmp_arr1) {
		val = tmp_arr1[key];
		if (false === __array_search(val, tmp_arr2)) {
			tmp_arr2[key] = val;
		}
		delete tmp_arr1[key];
	}
	return tmp_arr2;
}

function change_str_key(id){
	var status = jQuery("input#" + id).attr("readonly");
	if(!status){
		jQuery("input#" + id).attr("readonly", true);
		jQuery("a#link_build_str").hide();
	}
	else {
		jQuery("a#link_build_str").show();
		jQuery("input#" + id).removeAttr("readonly");
	}		
}

// Build string key
function build_string_key(modify_field, component){
	var input_val = "";
	var select_text = "";
	
	if(component.input != undefined){
		input_val = jQuery('input#' + component.input).val();
	} 
		
	var str = input_val;
	returnArray = str.toLowerCase().split(" ");
	str = array_unique(returnArray).toString();	
	str = encodeURIComponent(str);

	jQuery.ajax({
		type:"GET",
		url:do_link("adm.remote", "request=build_str_key"),
		data:"str=" + str,
		success: function(response){
			jQuery("input#" + modify_field).attr("value", response);
			check_uniq_field(modify_field, component.table, component.field_key, component.key);
		}
	});
}

function check_uniq_field(name_field, table, field_key, key){
	var str_key_val = encodeURIComponent(jQuery("input#" + name_field).val());
	jQuery.ajax({
		type:"GET",
		url:do_link("adm.remote", "request=check_uniq_field&table=" + table + "&name_field=" + name_field + "&obj_key=" + key + "&field_key=" + field_key),
		data:"str=" + str_key_val,
		success: function(response){
			var alert_mess = document.createElement("span");
			var _parent = jQuery("input#" + name_field).parent(); 
			_parent.append(alert_mess);
			alert_mess.setAttribute("id", "uniq_mess");
			
			if(response == 1){
				mess = "This string key already exists!";
				if(jQuery("span#uniq_mess").hasClass("is_unique")){
					jQuery("span#uniq_mess").removeClass("is_unique");
				}
				jQuery("span#uniq_mess").addClass("not_unique");
				jQuery("input#" + name_field).css("border", "1px solid #ff0000");
			} 
			else{
				if(jQuery("span#uniq_mess").hasClass("not_unique")){
					jQuery("span#uniq_mess").removeClass("not_unique");
				}
				mess = "This string key is valid!";
				jQuery("span#uniq_mess").addClass("is_unique");
				jQuery("input#" + name_field).css("border", "1px solid #b8b8b8");
			}
			jQuery("span#uniq_mess").text(mess);
		}
	});
}


function show_cropper(options){
	
	myBox = new Boxy("<iframe name=\"cropper\" src=\"" + options.link + "&rv=" + Math.floor(Math.random() * 10000000000)  + "\"  framespacing=\"0\" frameborder=\"no\" scrolling=\"no\" width=\"" + options.width + "\" height=\"" + options.height + "\"></iframe>", {
		title: "Cropper",
		closeText: "<span class='close_box'>[ <a href='#' class='close'>" + "close" + "</a> ] <a href='#' class='close'><img src='" + PATH + "/img/site/btn_close.gif' align='absmiddle'></a></span>",
		closeable: true,
		modal: true,
		unload: true,
		afterHide: function(){
			jQuery("img[src*=" + options.key + "]").each(function(i, v){
				var jThis = jQuery(this);
				
				var d = new Date;
				if (typeof(jThis.attr("src")) != "undefined") {
					var full_path = jThis.attr("src");
					var img_path = full_path.substr(0, (full_path.length - 4));
					
					var tmp = img_path.split(".v");
					if (tmp.length > 1) {
						delete tmp[tmp.length -1];
						img_path = tmp.join("");
					}
					
					var img_ext = full_path.substr(full_path.length - 4);
				}
				
				jThis.attr("src", img_path + ".v" + d.getTime() + img_ext);
				
				if (typeof(jThis.metadata().path_medium) != "undefined") {
					
					var full_path = jThis.metadata().path_medium;
					var img_path = full_path.substr(0, (full_path.length - 4));
					
					var tmp = img_path.split(".v");
					if (tmp.length > 1) {
						delete tmp[tmp.length -1];
						img_path = tmp.join("");
					}
					
					var img_ext = full_path.substr(full_path.length - 4);
					
					jThis.metadata().path_medium = img_path + ".v" + d.getTime() + img_ext;
				}
			});
		}
	});
}

function pic_edit(files_key) {
	
	if (editor_is_open == 0) {
		myBox = new Boxy("<iframe src=\"" + do_link("site.cropper", "files_key=" + files_key) + "?" + Math.floor(Math.random() * 10000000000) + "\"  framespacing=\"0\" frameborder=\"no\" scrolling=\"no\" width=\"900\" height=\"600\"></iframe>", {
			title: txt_edit,
			// closeText: "[" + link_close + "]",
			closeText: "<span class='close_box'>[ <a href='#' class='close'>close</a> ] <a href='#' class='close'><img src='" + PATH_STATIC + "/img/site/btn_close.gif' align='absmiddle'></a></span>",
			closeable: true,
			draggable: true
		});
	}
	
	editor_is_open = 1;
}

function on_pic_edit_close() {
	
	editor_is_open = 0;
	
	var big_path = jQuery("#big_pic").attr("src");
	var left_path = jQuery("#left_pic").attr("src");
	var top_path = jQuery("#top_pic").attr("src");
	
	if (big_path) {
		jQuery("#big_pic").attr("src", big_path.substr(0, big_path.indexOf("?") + 1) + Math.floor(Math.random() * 10000000000));
	}
	
	if (left_path) {
		jQuery("#left_pic").attr("src", left_path.substr(0, left_path.indexOf("?") + 1) + Math.floor(Math.random() * 10000000000));
	}
	
	if (top_path) {
		jQuery("#top_pic").attr("src", top_path.substr(0, top_path.indexOf("?") + 1) + Math.floor(Math.random() * 10000000000));
	}
	
	jQuery.each(jQuery("a.a_change img"), function(i, v) {
		var path = jQuery(v).attr("src");
		
		jQuery(v).attr("src", path.substr(0, path.indexOf("?") + 1) + Math.floor(Math.random() * 10000000000));
	});
}

function view_gallery(link, my_title){
	
	if (editor_is_open == 0) {
		
		myBox = new Boxy("<iframe src=\"" + link + "?" + Math.floor(Math.random() * 10000000000) + "\"  framespacing=\"0\" frameborder=\"no\" scrolling=\"no\" width=\"900\" height=\"600\"></iframe>", {
			title: my_title,
			// closeText: "[" + link_close + "]",
			closeText: "<span class='close_box'>[ <a href='#' class='close'>close</a> ] <a href='#' class='close'><img src='" + PATH_STATIC + "/img/site/btn_close.gif' align='absmiddle'></a></span>",
			closeable: true,
			draggable: true
		});
		
	}
	
	editor_is_open = 1;
}

function view_iframe(link, my_title, width, height, ele){
	
	ele = jQuery(ele);
	
	myBox = new Boxy("<iframe src=\"" + link + "?" + Math.floor(Math.random() * 10000000000) + "\"  framespacing=\"0\" frameborder=\"no\" scrolling=\"no\" width=\"" + width + "\" height=\"" + height + "\"></iframe>", {
		title: my_title,
		// closeText: "[" + link_close + "]",
		closeText: "<span class='close_box'>[ <a href='#' class='close'>Close</a> ] <a href='#' class='close'><img src='" + PATH_STATIC + "/img/site/btn_close.gif' align='absmiddle'></a></span>",
		closeable: true,
		draggable: true,
		modal: true
	});
	
	return myBox;
}
