// Alerts javascript

function set_proptype_alerts(val_arr, sel_opt){
	options = '<option value="">---------</option>'
	options += '<option value="">Any property type</option>'
	for(var x=0; x<val_arr.length; x++){
		if(sel_opt == val_arr[x].slugify()){
			options += '<option value="' + val_arr[x] +'" selected="selected">' + val_arr[x] + '</option>'
		}
		else{
			options += '<option value="' + val_arr[x] +'">' + val_arr[x] + '</option>'
		}
	}
    $("#id_property_type").html(options);
}

function set_province_alert(val_arr, sel_opt){
	options = '<option value="">---------</option>'
	for(var i=0; i<val_arr.length; i++){
		options += '<optgroup label="' + val_arr[i][0] +'">'
		opts = val_arr[i][1]
		for(var x=0; x<opts.length; x++){
			options += '<option value="' + opts[x][0] +'">' + opts[x][1] + '</option>'
		}
		options += '</optgroup>'
    }
    $("#id_region").html(options)
	$("#id_region").val(sel_opt)
}

function set_suburb_alert(value, ele, sel_opt){
    $.ajax({
		url: '/ajax/loc/sub/'+value+'/',
		async: false,
		success: function(data){
			$("#"+ele).html('');
            options = '<option value="">---------</option>'
            options += '<option value="">Any suburbs</option>'
            var opts = data.split("~");
            for (i=0; i<opts.length-1; i++) {
                val_text = opts[i].split('|')
                if(sel_opt == val_text[0]){
                    options += '<option value="' + val_text[0] +'" selected="selected">' + val_text[1] + '</option>'
                }
                else{
                    options += '<option value="' + val_text[0] +'">' + val_text[1] + '</option>'
                }
            }
            $("#"+ele).html(options);
		}
	});
}

function change_price(val, sela, selb){
	if((val == 'residential-to-let') || (val == 'residential-holiday-letting') || (val == 'commercial-to-let')){
		$('#id_rental').show();
		$('#id_sale').hide();
        $('#id_rent_from').val(selb[0]);
        $('#id_rent_to').val(selb[1]);
	}
	else{
		$('#id_sale').show();
		$('#id_rental').hide();
		$('#id_price_from').val(sela[0]);
        $('#id_price_to').val(sela[1]);
	}
}
function init_alerts(){
    $("#id_propster_form").validate({
		rules: {
			name:"required",
			number: {
                required: true,
				number: true
			},
            email: {
                required: true,
				email: true
			},
            division_type:"required",
            region:"required",
            price_from:{
				required: function(element){
					return $("#id_sale").is(':visible')
				}
			},
            price_to:{
				required: function(element){
					return $("#id_sale").is(':visible')
				}
			},
            rent_from:{
				required: function(element){
					return $("#id_rental").is(':visible')
				}
			},
            rent_to:{
				required: function(element){
					return $("#id_rental").is(':visible')
				}
			}
		}
    });
}
