﻿var AOW = {
    showForm: function(id, scale) {
		this.hideall();
		if ($(id).style.display == "none") {
			if (id == "addInvoice") {this.createAutoComplete()}
			if (this.haveAnyOpen()) {
				setTimeout('Effect.BlindDown(\''+id+'\', {scaleTo:'+scale+'})', '882');
			} else {
				Effect.BlindDown(id, {scaleTo:scale});
			}
		}
	},
	
	hideall: function() {
		frms = document.getElementsByClassName('hideable');
		for (var i = 0; i < frms.length; i++) {
			if (frms[i].style.display != "none") Effect.BlindUp(frms[i]);
		}
	},
	showSingleForm: function(id, scale) {
		if ($(id).style.display == "none") {
			Effect.BlindDown(id, {scaleTo:scale});
		}
	},
	hideSingleForm: function(id) {
		Effect.BlindUp(id)
	},
	
	haveAnyOpen: function() {
		frms = document.getElementsByClassName('hideable');
		for (var i = 0; i < frms.length; i++) {
			if (frms[i].style.display != "none") return true;
		}
		return false;
	},
	
	createAutoComplete: function() {
		new Ajax.Autocompleter("autocomplete", 
			"autocomplete_companies", 
			"/core/getCompanies.aspx", 
			{ 
			minChars: 3,
			afterUpdateElement: this.getPk }
		);
	},
	getPk: function(field, listItem) {
        if (listItem.id){
	   	    $("AddInvoiceStart_iCompanyPk").value = listItem.id;
	   	}
	},
	doDeleteCompany: function(){
	    if (confirm("Are you sure you want to delete this company?")){
	        var options = {
		        parameters: "id="+ $("id").value,
		        onLoading: function() {
                    $('overlay').show();
                    center('busy');
                },
		        onComplete: function() {
	                $('overlay').hide();
                    Effect.Fade('busy',{duration:0.5,queue:'end'});
                    location.href='people.aspx';
			    }
	        };
	        new Ajax.Request("/core/deleteCompany.aspx", options);
	    }
	},
	doAddInvoice: function(){
        if ($("autocomplete").value.length == 0){return false}
        if ($("AddInvoiceStart_iCompanyPk").value.length != 0){
            if ($("todo")){
                if ($("todo").value == "duplicate"){
                    var options = {
				        parameters: "id="+ $("id").value + "&company=" + $("AddInvoiceStart_iCompanyPk").value,
				        onSuccess: function(r) {
					        location.href='invoice.aspx?id=' + r.responseText;
				        }
			        };
			        new Ajax.Request("/core/duplicateinvoice.aspx", options);
                }else{
                    location.href='addInvoice.aspx?id=' + $("AddInvoiceStart_iCompanyPk").value;
                }
            }
            else{
                location.href='addInvoice.aspx?id=' + $("AddInvoiceStart_iCompanyPk").value;
            }
        }
        else{
            if ($("todo").value != "duplicate"){
                location.href='addCompany.aspx?name=' + $("autocomplete").value;
            }else{
                alert('You need to choose a company to duplicate this invoice for')
                return false;
            }
            
        }
    },
    logout: function(){
        if (confirm('Are you sure you want to log out?')){location.href='logout.asp'}
    },
    RowClick: function(rowIndex){ 
        var selRow = this.GetCellByColumnUniqueName(this.Rows[rowIndex],"iInvoicePk");
        location.href = 'invoice.aspx?id=' +selRow.innerHTML;
    },
    deleteInvoice: function(id){
        if (confirm('Are you sure you want to delete this invoice?')){
            var options = {
				parameters: "id="+id,
				onSuccess: function() {
					//alert('Invoice Deleted');
					location.href='default.aspx';
				}
			};
			new Ajax.Request("/core/deleteinvoice.aspx", options);
        }
    },
    bustOut: function(){
        if (window != window.top)
            top.location.href = location.href;
    },
    doValidate: function(what){
        var valid = new Validation('Form1', {useTitles:true, immediate : true, onSubmit:false});
        var result = valid.validate();
        if (result){
            Sys.WebForms.PageRequestManager.getInstance()._doPostBack(what, '')
        }
    }
}


