//round the edges of the container in IE
/* IE only - firefox/webkit rounded by css*/
DD_roundies.addRule('#container', '10px');

function makeDialog(urlselector, title, width, height, callback){
	//The selected URL's contents will open in a jqui dialog. Requires a div id dialog_contents already on the page.
	if(!width){
		width='auto';
	}
	if(!height){
	    height='auto';
	}
	if(!title){
		title="Confirm";
	}
    $(urlselector).click(function(){
        var href = this.href;
        // destroy previous incarnation to make sure that title/width/etc. don't "stick"
        $("#dialog_contents").dialog('destroy');
        // prepare the form when the DOM is ready 
        var dialog = $("#dialog_contents").dialog({
            title: title,
            modal: false,
            autoOpen: true,
            width: width,
            height: height
        });
        $("#dialog_contents").html('Loading...').load(href, callback).dialog('open');
        return false; 
    });
    return true;
}

function attachCalc(fromField, toField, rate, allowedchars){
    //Make toField set to fromField * rate when there's a keyup in fromField.
    //fromField & toField are jquery selectors.
	//Also prevents non-numeric inputs using the AlphaNumeric plugin (must be loaded first).
	if (!allowedchars){
	    allowedchars=".";
	}
      $(fromField).numeric({allow:allowedchars}).keyup(function(){
        markedUp = (parseFloat(this.value) * rate).toFixed(2);
        if (isNaN(markedUp)){markedUp="";}
        $(toField).val(markedUp);
      });
  }

function makeTransferSelect(selectedBox, unselectedBox, addButton, removeButton){
    //Start with a selectedBox with all choices, the ones we want selected
    //unselected box can be hidden to start for nice degradation
    //Move not selected items to the unselectedBox and show it
    //bind addButton and removeButton to the box and show them
    
    $(unselectedBox+' option').remove();
    $(selectedBox+' option:not(:selected)').remove().appendTo(unselectedBox);
    
    $(addButton+','+removeButton+','+unselectedBox).show();
    
    $(addButton).click(function() {  
        return !$(unselectedBox+' option:selected').remove().appendTo(selectedBox);  
    });  
    $(removeButton).click(function() {  
        return !$(selectedBox+' option:selected').remove().appendTo(unselectedBox);  
    });
    //on submit, select the items in the second box
    $('form').submit(function() {  
        $(toBox+' option').each(function(i) {  
            $(this).attr("selected", "selected");  
        });
    });
}

function suggestDaily(){
    //use the hourly rate to suggest a daily rate,
    //then trigger the onchange to recalculate commission
    $("#id_daily_rate").val( ($("#id_hourly_rate").val() * 6.50).toFixed(2)).keyup();
    return false;
}

function AjaxFunctions(form) {
    this.form_selector = form;
}
AjaxFunctions.prototype = {
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        alert("Sorry - we had a problem with your request: "+textStatus+' '+errorThrown);
    },
    success: function(data) {
        //did we actually succeed?
        if (data.status == 'success'){
            //reload the page. Django should have set a usermessage to report success to the user.
            window.location.href=window.location.href;
        } else { //some kind of form validation error
            //make an errormsg div
            $(this.form_selector+' #errorMsg').remove();
            $(this.form_selector).prepend('<div id="errorMsg">' +
                '<h3>Error Processing Form</h3><ol></ol></div>');
            //load em all into theerror list
            $.each(data['errors'], function(){
                msg = this;
                $("#errorMsg ol").append('<li>'+msg+'</li>')
            });
        }
    },
    preprocess: function(formData, jqForm, options) { 
        var form = jqForm[0];
        if(form.fromjs)
            form.fromjs.value = 'true';
        //Do some validation on required fields here, return false if you don't want to actually post
    }
}

